logo

Expression of type NamedExprs

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, ExprRange, Function, Lambda, NamedExprs, Variable, VertExprArray, e, l
from proveit.logic import And, Equals, InSet, Union
from proveit.numbers import Abs, Add, Exp, Interval, ModAbs, Sum, greater, one, two
from proveit.physics.quantum import NumKet, Z, ket_plus
from proveit.physics.quantum.QPE import ModAdd, QPE, _U, _b_floor, _ket_u, _neg_domain, _pos_domain, _rel_indexed_alpha, _s, _s_wire, _t, _two_pow_t
from proveit.physics.quantum.circuits import Gate, Input, Measure, MultiQubitElem, Output, Qcircuit
from proveit.statistics import ProbOfAll
In [2]:
# build up the expression from sub-expressions
sub_expr1 = Variable("_b", latex_format = r"{_{-}b}")
sub_expr2 = Variable("_a", latex_format = r"{_{-}a}")
sub_expr3 = [l]
sub_expr4 = Add(_t, one)
sub_expr5 = Add(_t, _s)
sub_expr6 = Interval(sub_expr4, sub_expr5)
sub_expr7 = MultiQubitElem(element = Gate(operation = QPE(_U, _t), part = sub_expr2), targets = Interval(one, sub_expr5))
sub_expr8 = Exp(Abs(_rel_indexed_alpha), two)
expr = NamedExprs(("lambda", Lambda(sub_expr1, Equals(ProbOfAll(instance_param_or_params = sub_expr3, instance_element = Function(sub_expr1, sub_expr3)), Add(Sum(index_or_indices = sub_expr3, summand = sub_expr8, domain = _neg_domain), Sum(index_or_indices = sub_expr3, summand = sub_expr8, domain = _pos_domain))).with_wrapping_at(2))), ("${_{-}b}$", Conditional(Qcircuit(vert_expr_array = VertExprArray([ExprRange(sub_expr2, Input(state = ket_plus), one, _t), ExprRange(sub_expr2, MultiQubitElem(element = Input(state = _ket_u, part = sub_expr2), targets = sub_expr6), one, _s)], [ExprRange(sub_expr2, sub_expr7, one, _t), ExprRange(sub_expr2, sub_expr7, sub_expr4, sub_expr5)], [ExprRange(sub_expr2, Measure(basis = Z), one, _t), _s_wire], [ExprRange(sub_expr2, MultiQubitElem(element = Output(state = NumKet(ModAdd(_b_floor, l), _t), part = sub_expr2), targets = Interval(one, _t)), one, _t), ExprRange(sub_expr2, MultiQubitElem(element = Output(state = _ket_u, part = sub_expr2), targets = sub_expr6), one, _s)])), And(InSet(l, Union(_neg_domain, _pos_domain)), greater(ModAbs(l, _two_pow_t), e)))))
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())
\left\{ \begin{array}{l}
{\rm lambda}: \left[{_{-}b} \mapsto \left(\begin{array}{c} \begin{array}{l} \left[\textrm{Prob}_{l}~{_{-}b}\left(l\right)\right] =  \\ \left(\left(\sum_{l = -2^{t - 1} + 1}^{-\left(e + 1\right)} \left|\alpha_{b_{\textit{f}} \oplus l}\right|^{2}\right) + \left(\sum_{l = e + 1}^{2^{t - 1}} \left|\alpha_{b_{\textit{f}} \oplus l}\right|^{2}\right)\right) \end{array} \end{array}\right)\right]\\
{_{-}b}: \left\{\begin{array}{c} \Qcircuit@C=1em @R=.7em{
\qin{\lvert + \rangle} & \multigate{4}{\textrm{QPE}\left(U, t\right)} & \meter & \multiqout{3}{\lvert b_{\textit{f}} \oplus l \rangle_{t}} \\
\qin{\lvert + \rangle} & \ghost{\textrm{QPE}\left(U, t\right)} & \meter & \ghostqout{\lvert b_{\textit{f}} \oplus l \rangle_{t}} \\
\qin{\begin{array}{c}:\\ \left(t - 3\right) \times \\:\end{array}} & \ghost{\textrm{QPE}\left(U, t\right)} & \measure{\begin{array}{c}:\\ \left(t - 3\right) \times \\:\end{array}} \qw & \ghostqout{\lvert b_{\textit{f}} \oplus l \rangle_{t}} \\
\qin{\lvert + \rangle} & \ghost{\textrm{QPE}\left(U, t\right)} & \meter & \ghostqout{\lvert b_{\textit{f}} \oplus l \rangle_{t}} \\
\qin{\lvert u \rangle} & \ghost{\textrm{QPE}\left(U, t\right)} & { /^{s} } \qw & \qout{\lvert u \rangle}
} \end{array} \textrm{ if } l \in \left(\{-2^{t - 1} + 1~\ldotp \ldotp~-\left(e + 1\right)\} \cup \{e + 1~\ldotp \ldotp~2^{t - 1}\}\right) ,  \left|l\right|_{\textup{mod}\thinspace 2^{t}} > e\right..\\
\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
0NamedExprslambda: 1
${_{-}b}$: 2
1Lambdaparameter: 63
body: 4
2Conditionalvalue: 5
condition: 6
3ExprTuple63
4Operationoperator: 7
operands: 8
5Operationoperator: 9
operands: 10
6Operationoperator: 11
operands: 12
7Literal
8ExprTuple13, 14
9Literal
10ExprTuple15, 16, 17, 18
11Literal
12ExprTuple19, 20
13Operationoperator: 21
operand: 35
14Operationoperator: 153
operands: 23
15ExprTuple24, 25
16ExprTuple26, 27
17ExprTuple28, 29
18ExprTuple30, 31
19Operationoperator: 106
operands: 32
20Operationoperator: 33
operands: 34
21Literal
22ExprTuple35
23ExprTuple36, 37
24ExprRangelambda_map: 38
start_index: 159
end_index: 155
25ExprRangelambda_map: 39
start_index: 159
end_index: 130
26ExprRangelambda_map: 40
start_index: 159
end_index: 155
27ExprRangelambda_map: 40
start_index: 113
end_index: 114
28ExprRangelambda_map: 41
start_index: 159
end_index: 155
29ExprRangelambda_map: 42
start_index: 159
end_index: 130
30ExprRangelambda_map: 43
start_index: 159
end_index: 155
31ExprRangelambda_map: 44
start_index: 159
end_index: 130
32ExprTuple148, 45
33Literal
34ExprTuple146, 46
35Lambdaparameter: 148
body: 47
36Operationoperator: 49
operand: 64
37Operationoperator: 49
operand: 65
38Lambdaparameter: 112
body: 51
39Lambdaparameter: 112
body: 52
40Lambdaparameter: 112
body: 53
41Lambdaparameter: 112
body: 54
42Lambdaparameter: 112
body: 55
43Lambdaparameter: 112
body: 56
44Lambdaparameter: 112
body: 58
45Operationoperator: 59
operands: 60
46Operationoperator: 61
operands: 62
47Operationoperator: 63
operand: 148
48ExprTuple64
49Literal
50ExprTuple65
51Operationoperator: 95
operands: 66
52Operationoperator: 73
operands: 67
53Operationoperator: 73
operands: 68
54Operationoperator: 69
operands: 70
55Operationoperator: 96
operands: 71
56Operationoperator: 73
operands: 72
57ExprTuple112
58Operationoperator: 73
operands: 74
59Literal
60ExprTuple115, 117
61Literal
62ExprTuple148, 75
63Variable
64Lambdaparameter: 148
body: 76
65Lambdaparameter: 148
body: 78
66NamedExprsstate: 79
67NamedExprselement: 80
targets: 88
68NamedExprselement: 81
targets: 82
69Literal
70NamedExprsbasis: 83
71NamedExprsoperation: 84
72NamedExprselement: 85
targets: 86
73Literal
74NamedExprselement: 87
targets: 88
75Operationoperator: 149
operands: 89
76Conditionalvalue: 91
condition: 90
77ExprTuple148
78Conditionalvalue: 91
condition: 92
79Operationoperator: 93
operand: 108
80Operationoperator: 95
operands: 102
81Operationoperator: 96
operands: 97
82Operationoperator: 127
operands: 98
83Literal
84Literal
85Operationoperator: 101
operands: 99
86Operationoperator: 127
operands: 100
87Operationoperator: 101
operands: 102
88Operationoperator: 127
operands: 103
89ExprTuple151, 155
90Operationoperator: 106
operands: 104
91Operationoperator: 149
operands: 105
92Operationoperator: 106
operands: 107
93Literal
94ExprTuple108
95Literal
96Literal
97NamedExprsoperation: 109
part: 112
98ExprTuple159, 114
99NamedExprsstate: 110
part: 112
100ExprTuple159, 155
101Literal
102NamedExprsstate: 111
part: 112
103ExprTuple113, 114
104ExprTuple148, 115
105ExprTuple116, 151
106Literal
107ExprTuple148, 117
108Literal
109Operationoperator: 118
operands: 119
110Operationoperator: 120
operands: 121
111Literal
112Variable
113Operationoperator: 153
operands: 122
114Operationoperator: 153
operands: 123
115Operationoperator: 127
operands: 124
116Operationoperator: 125
operand: 133
117Operationoperator: 127
operands: 128
118Literal
119ExprTuple129, 155
120Literal
121ExprTuple140, 155
122ExprTuple155, 159
123ExprTuple155, 130
124ExprTuple131, 132
125Literal
126ExprTuple133
127Literal
128ExprTuple139, 145
129Literal
130Literal
131Operationoperator: 153
operands: 134
132Operationoperator: 157
operand: 139
133Operationoperator: 136
operand: 140
134ExprTuple138, 159
135ExprTuple139
136Literal
137ExprTuple140
138Operationoperator: 157
operand: 145
139Operationoperator: 153
operands: 142
140Operationoperator: 143
operands: 144
141ExprTuple145
142ExprTuple146, 159
143Literal
144ExprTuple147, 148
145Operationoperator: 149
operands: 150
146Variable
147Literal
148Variable
149Literal
150ExprTuple151, 152
151Literal
152Operationoperator: 153
operands: 154
153Literal
154ExprTuple155, 156
155Literal
156Operationoperator: 157
operand: 159
157Literal
158ExprTuple159
159Literal