logo

Expression of type Qcircuit

from the theory of proveit.physics.quantum.QPE

In [1]:
import proveit
# Automation is not needed when building an expression:
proveit.defaults.automation = False # This will speed things up.
proveit.defaults.inline_pngs = False # Makes files smaller.
%load_expr # Load the stored expression as 'stored_expr'
# import Expression classes needed to build the expression
from proveit import Conditional, ConditionalSet, ExprRange, Variable, VertExprArray, t
from proveit.linear_algebra import MatrixExp, ScalarMult, VecAdd
from proveit.logic import Equals, NotEquals, Set
from proveit.numbers import Add, Exp, Interval, Mult, Neg, e, frac, i, one, pi, sqrt, two, zero
from proveit.physics.quantum import CONTROL, ket0, ket1, ket_plus
from proveit.physics.quantum.QPE import _U, _ket_u, _phase, _s
from proveit.physics.quantum.circuits import Gate, Igate, Input, MultiQubitElem, Output, Qcircuit
In [2]:
# build up the expression from sub-expressions
sub_expr1 = Variable("_a", latex_format = r"{_{-}a}")
sub_expr2 = Variable("_b", latex_format = r"{_{-}b}")
sub_expr3 = Add(t, one)
sub_expr4 = Add(sub_expr2, t)
sub_expr5 = Interval(sub_expr3, Add(t, _s))
sub_expr6 = Add(Neg(t), one)
expr = Qcircuit(vert_expr_array = VertExprArray([ExprRange(sub_expr1, Input(state = ket_plus), one, t), ExprRange(sub_expr1, MultiQubitElem(element = Input(state = _ket_u, part = sub_expr1), targets = sub_expr5), one, _s)], ExprRange(sub_expr2, [ExprRange(sub_expr1, ConditionalSet(Conditional(MultiQubitElem(element = CONTROL, targets = Set(sub_expr3)), Equals(sub_expr4, sub_expr1)), Conditional(Igate, NotEquals(sub_expr4, sub_expr1))), one, t).with_case_simplification(), ExprRange(sub_expr1, MultiQubitElem(element = Gate(operation = MatrixExp(_U, Exp(two, Neg(sub_expr2))), part = sub_expr1), targets = sub_expr5), one, _s)], sub_expr6, zero).with_decreasing_order(), [ExprRange(sub_expr1, Output(state = ScalarMult(frac(one, sqrt(two)), VecAdd(ket0, ScalarMult(Exp(e, Mult(two, pi, i, Exp(two, Neg(sub_expr1)), _phase)), ket1)))), sub_expr6, zero).with_decreasing_order(), ExprRange(sub_expr1, MultiQubitElem(element = Output(state = _ket_u, part = sub_expr1), targets = sub_expr5), one, _s)]))
expr:
In [3]:
# check that the built expression is the same as the stored expression
assert expr == stored_expr
assert expr._style_id == stored_expr._style_id
print("Passed sanity check: expr matches stored_expr")
Passed sanity check: expr matches stored_expr
In [4]:
# Show the LaTeX representation of the expression for convenience if you need it.
print(stored_expr.latex())
\begin{array}{c} \Qcircuit@C=1em @R=.7em{
\qin{\lvert + \rangle} & \control{} \qw \qwx[1] & \qw & \gate{\cdots} \qwx[1] & \qw & \qout{\frac{1}{\sqrt{2}} \cdot \left(\lvert 0 \rangle + \left(\mathsf{e}^{2 \cdot \pi \cdot \mathsf{i} \cdot 2^{t - 1} \cdot \varphi} \cdot \lvert 1 \rangle\right)\right)} \\
\qin{\lvert + \rangle} & \qw \qwx[1] & \control{} \qw \qwx[1] & \gate{\cdots} \qwx[1] & \qw & \qout{\frac{1}{\sqrt{2}} \cdot \left(\lvert 0 \rangle + \left(\mathsf{e}^{2 \cdot \pi \cdot \mathsf{i} \cdot 2^{t - 2} \cdot \varphi} \cdot \lvert 1 \rangle\right)\right)} \\
\qin{\begin{array}{c}:\\ \left(t - 3\right) \times \\:\end{array}} & \gate{\vdots} \qwx[1] & \gate{\vdots} \qwx[1] & \gate{\ddots} \qwx[1] & \gate{\vdots} & \qout{\vdots} \\
\qin{\lvert + \rangle} & \qw \qwx[1] & \qw \qwx[1] & \gate{\cdots} \qwx[1] & \control{} \qw \qwx[1] & \qout{\frac{1}{\sqrt{2}} \cdot \left(\lvert 0 \rangle + \left(\mathsf{e}^{2 \cdot \pi \cdot \mathsf{i} \cdot 2^{0} \cdot \varphi} \cdot \lvert 1 \rangle\right)\right)} \\
\qin{\lvert u \rangle} & \gate{U^{2^{t - 1}}} & \gate{U^{2^{t - 2}}} & \gate{\cdots} & \gate{U^{2^{0}}} & \qout{\lvert u \rangle}
} \end{array}
In [5]:
stored_expr.style_options()
namedescriptiondefaultcurrent valuerelated methods
spacingchange the spacing of a circuit using the format '@C=1em @R=.7em' where C is the column spacing and R is the row spacing@C=1em @R=.7em@C=1em @R=.7em
In [6]:
# display the expression information
stored_expr.expr_info()
 core typesub-expressionsexpression
0Operationoperator: 1
operands: 2
1Literal
2ExprTuple3, 4, 5
3ExprTuple6, 7
4ExprRangelambda_map: 8
start_index: 15
end_index: 94
5ExprTuple9, 10
6ExprRangelambda_map: 11
start_index: 110
end_index: 109
7ExprRangelambda_map: 12
start_index: 110
end_index: 92
8Lambdaparameter: 114
body: 13
9ExprRangelambda_map: 14
start_index: 15
end_index: 94
10ExprRangelambda_map: 16
start_index: 110
end_index: 92
11Lambdaparameter: 125
body: 17
12Lambdaparameter: 125
body: 18
13ExprTuple19, 20
14Lambdaparameter: 125
body: 21
15Operationoperator: 104
operands: 22
16Lambdaparameter: 125
body: 23
17Operationoperator: 39
operands: 24
18Operationoperator: 64
operands: 25
19ExprRangelambda_map: 26
start_index: 110
end_index: 109
20ExprRangelambda_map: 27
start_index: 110
end_index: 92
21Operationoperator: 44
operands: 28
22ExprTuple29, 110
23Operationoperator: 64
operands: 30
24NamedExprsstate: 31
25NamedExprselement: 32
targets: 50
26Lambdaparameter: 125
body: 33
27Lambdaparameter: 125
body: 34
28NamedExprsstate: 35
29Operationoperator: 123
operand: 109
30NamedExprselement: 37
targets: 50
31Operationoperator: 102
operand: 46
32Operationoperator: 39
operands: 45
33Operationoperator: 40
operands: 41
34Operationoperator: 64
operands: 42
35Operationoperator: 85
operands: 43
36ExprTuple109
37Operationoperator: 44
operands: 45
38ExprTuple46
39Literal
40Literal
41ExprTuple47, 48
42NamedExprselement: 49
targets: 50
43ExprTuple51, 52
44Literal
45NamedExprsstate: 53
part: 125
46Literal
47Conditionalvalue: 54
condition: 55
48Conditionalvalue: 56
condition: 57
49Operationoperator: 67
operands: 58
50Operationoperator: 59
operands: 60
51Operationoperator: 99
operands: 61
52Operationoperator: 62
operands: 63
53Literal
54Operationoperator: 64
operands: 65
55Operationoperator: 66
operands: 70
56Operationoperator: 67
operands: 68
57Operationoperator: 69
operands: 70
58NamedExprsoperation: 71
part: 125
59Literal
60ExprTuple97, 72
61ExprTuple110, 73
62Literal
63ExprTuple74, 75
64Literal
65NamedExprselement: 76
targets: 77
66Literal
67Literal
68NamedExprsoperation: 78
69Literal
70ExprTuple79, 125
71Operationoperator: 80
operands: 81
72Operationoperator: 104
operands: 82
73Operationoperator: 119
operands: 83
74Operationoperator: 102
operand: 94
75Operationoperator: 85
operands: 86
76Literal
77Operationoperator: 87
operand: 97
78Literal
79Operationoperator: 104
operands: 89
80Literal
81ExprTuple90, 91
82ExprTuple109, 92
83ExprTuple121, 93
84ExprTuple94
85Literal
86ExprTuple95, 96
87Literal
88ExprTuple97
89ExprTuple114, 109
90Literal
91Operationoperator: 119
operands: 98
92Literal
93Operationoperator: 99
operands: 100
94Literal
95Operationoperator: 119
operands: 101
96Operationoperator: 102
operand: 110
97Operationoperator: 104
operands: 105
98ExprTuple121, 106
99Literal
100ExprTuple110, 121
101ExprTuple107, 108
102Literal
103ExprTuple110
104Literal
105ExprTuple109, 110
106Operationoperator: 123
operand: 114
107Literal
108Operationoperator: 112
operands: 113
109Variable
110Literal
111ExprTuple114
112Literal
113ExprTuple121, 115, 116, 117, 118
114Variable
115Literal
116Literal
117Operationoperator: 119
operands: 120
118Literal
119Literal
120ExprTuple121, 122
121Literal
122Operationoperator: 123
operand: 125
123Literal
124ExprTuple125
125Variable