logo

Expression of type Prob

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
from proveit.statistics import Prob
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 = Prob(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())
\textrm{Pr}\left(\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}\right)
In [5]:
stored_expr.style_options()
no style options
In [6]:
# display the expression information
stored_expr.expr_info()
 core typesub-expressionsexpression
0Operationoperator: 1
operand: 3
1Literal
2ExprTuple3
3Operationoperator: 4
operands: 5
4Literal
5ExprTuple6, 7, 8
6ExprTuple9, 10
7ExprRangelambda_map: 11
start_index: 18
end_index: 97
8ExprTuple12, 13
9ExprRangelambda_map: 14
start_index: 113
end_index: 112
10ExprRangelambda_map: 15
start_index: 113
end_index: 95
11Lambdaparameter: 117
body: 16
12ExprRangelambda_map: 17
start_index: 18
end_index: 97
13ExprRangelambda_map: 19
start_index: 113
end_index: 95
14Lambdaparameter: 128
body: 20
15Lambdaparameter: 128
body: 21
16ExprTuple22, 23
17Lambdaparameter: 128
body: 24
18Operationoperator: 107
operands: 25
19Lambdaparameter: 128
body: 26
20Operationoperator: 42
operands: 27
21Operationoperator: 67
operands: 28
22ExprRangelambda_map: 29
start_index: 113
end_index: 112
23ExprRangelambda_map: 30
start_index: 113
end_index: 95
24Operationoperator: 47
operands: 31
25ExprTuple32, 113
26Operationoperator: 67
operands: 33
27NamedExprsstate: 34
28NamedExprselement: 35
targets: 53
29Lambdaparameter: 128
body: 36
30Lambdaparameter: 128
body: 37
31NamedExprsstate: 38
32Operationoperator: 126
operand: 112
33NamedExprselement: 40
targets: 53
34Operationoperator: 105
operand: 49
35Operationoperator: 42
operands: 48
36Operationoperator: 43
operands: 44
37Operationoperator: 67
operands: 45
38Operationoperator: 88
operands: 46
39ExprTuple112
40Operationoperator: 47
operands: 48
41ExprTuple49
42Literal
43Literal
44ExprTuple50, 51
45NamedExprselement: 52
targets: 53
46ExprTuple54, 55
47Literal
48NamedExprsstate: 56
part: 128
49Literal
50Conditionalvalue: 57
condition: 58
51Conditionalvalue: 59
condition: 60
52Operationoperator: 70
operands: 61
53Operationoperator: 62
operands: 63
54Operationoperator: 102
operands: 64
55Operationoperator: 65
operands: 66
56Literal
57Operationoperator: 67
operands: 68
58Operationoperator: 69
operands: 73
59Operationoperator: 70
operands: 71
60Operationoperator: 72
operands: 73
61NamedExprsoperation: 74
part: 128
62Literal
63ExprTuple100, 75
64ExprTuple113, 76
65Literal
66ExprTuple77, 78
67Literal
68NamedExprselement: 79
targets: 80
69Literal
70Literal
71NamedExprsoperation: 81
72Literal
73ExprTuple82, 128
74Operationoperator: 83
operands: 84
75Operationoperator: 107
operands: 85
76Operationoperator: 122
operands: 86
77Operationoperator: 105
operand: 97
78Operationoperator: 88
operands: 89
79Literal
80Operationoperator: 90
operand: 100
81Literal
82Operationoperator: 107
operands: 92
83Literal
84ExprTuple93, 94
85ExprTuple112, 95
86ExprTuple124, 96
87ExprTuple97
88Literal
89ExprTuple98, 99
90Literal
91ExprTuple100
92ExprTuple117, 112
93Literal
94Operationoperator: 122
operands: 101
95Literal
96Operationoperator: 102
operands: 103
97Literal
98Operationoperator: 122
operands: 104
99Operationoperator: 105
operand: 113
100Operationoperator: 107
operands: 108
101ExprTuple124, 109
102Literal
103ExprTuple113, 124
104ExprTuple110, 111
105Literal
106ExprTuple113
107Literal
108ExprTuple112, 113
109Operationoperator: 126
operand: 117
110Literal
111Operationoperator: 115
operands: 116
112Variable
113Literal
114ExprTuple117
115Literal
116ExprTuple124, 118, 119, 120, 121
117Variable
118Literal
119Literal
120Operationoperator: 122
operands: 123
121Literal
122Literal
123ExprTuple124, 125
124Literal
125Operationoperator: 126
operand: 128
126Literal
127ExprTuple128
128Variable