20
20
import numpy as np
21
21
22
22
23
- def acc_IQAE (qargs ,state_function , oracle_function , eps , alpha , kwargs_oracle = {}):
23
+ def IQAE (qargs ,state_function , oracle_function , eps , alpha , kwargs_oracle = {}):
24
24
r"""
25
- Accelerated Quantum Amplitude Estimation (IQAE). This function performs QAE with a fraction of the quantum resources of the well known QAE algorithm.
26
- See `Accelerated Quantum Amplitude Estimation without QFT <https://arxiv.org/abs/2407.16795>`_
25
+ Accelerated Quantum Amplitude Estimation (IQAE). This function performs :ref:`QAE <QAE>` with a fraction of the quantum resources of the well-known `QAE algorithm <https://arxiv.org/abs/quant-ph/0005055>`_.
26
+ See `Accelerated Quantum Amplitude Estimation without QFT <https://arxiv.org/abs/2407.16795>`_.
27
+
28
+ The problem of quantum amplitude estimation is described as follows:
29
+
30
+ * Given a unitary operator :math:`\mathcal{A}`, let :math:`\ket{\Psi}=\mathcal{A}\ket{0}`.
31
+ * Write :math:`\ket{\Psi}=\ket{\Psi_1}+\ket{\Psi_0}` as a superposition of the orthogonal good and bad components of :math:`\ket{\Psi}`.
32
+ * Find an estimate for :math:`a=\langle\Psi_1|\Psi_1\rangle`, the probability that a measurement of $\ket{\Psi}$ yields a good state.
27
33
28
34
Parameters
29
35
----------
@@ -38,48 +44,46 @@ def acc_IQAE(qargs,state_function, oracle_function, eps, alpha, kwargs_oracle =
38
44
A Python function tagging the good state :math:`\ket{\Psi_1}`.
39
45
This function will receive the variables in the list ``args`` as arguments in the
40
46
course of this algorithm.
41
- eps: Float
42
- Accuracy of the algorithm. Choose to be :math:`> 0`. See paper for explanation .
43
- alpha: Float
44
- Confidence level of the algorithm. Choose to be :math:`\ in (0,1)`. See paper for explanation .
47
+ eps : float
48
+ Accuracy $\epsilon>0$ of the algorithm .
49
+ alpha : float
50
+ Confidence level $\alpha\ in (0,1)$ of the algorithm .
45
51
kwargs_oracle : dict, optional
46
52
A dictionary containing keyword arguments for the oracle. The default is {}.
47
53
48
54
Returns
49
55
-------
50
- a_l, a_u : Float, Float
51
- Confidence bounds on the amplitude which is to be estimated.
56
+ a : float
57
+ An enstimate $\hat{a}$ of $a$ such that
58
+
59
+ .. math::
60
+
61
+ \mathbb P\{|\hat{a}-a|<\epsilon\}\geq 1-\alpha
52
62
53
63
Examples
54
64
--------
55
65
56
- We show the same QAE **Numerical integration** example which can also be found in the original QAE documentation.
66
+ We show the same **Numerical integration** example which can also be found in the :ref:` QAE documentation <QAE>` .
57
67
58
68
We wish to evaluate
59
69
60
70
.. math::
61
71
62
72
A=\int_0^1f(x)\mathrm dx.
63
73
64
- For this, we set up the corresponding ``state_function`` acting on the ``input_list``:
74
+ For this, we set up the corresponding ``state_function`` acting on the variables in ``input_list``:
65
75
66
76
::
67
77
68
- from qrisp import QuantumFloat, QuantumBool, control, z, h, ry, QAE
78
+ from qrisp import QuantumFloat, QuantumBool, control, z, h, ry, IQAE
69
79
import numpy as np
70
80
71
81
n = 6
72
82
inp = QuantumFloat(n,-n)
73
83
tar = QuantumBool()
74
84
input_list = [inp, tar]
75
-
76
- We define the oracle:
77
85
78
- def oracle_function(qb):
79
- z(qb)
80
-
81
-
82
- For the ``state_function`` we want to evaluate $f(x)=\sin^2(x)$:
86
+ For example, if $f(x)=\sin^2(x)$, the ``state_function`` can be implemented as follows:
83
87
84
88
::
85
89
@@ -91,24 +95,27 @@ def state_function(inp, tar):
91
95
with control(inp[k]):
92
96
ry(2**(k+1)/N,tar)
93
97
94
- Finally, we apply QAE and obtain an estimate $a$ for the value of the integral $A=0.27268$.
98
+ We define the ``oracle_function``:
99
+
100
+ ::
101
+
102
+ def oracle_function(inp, tar):
103
+ z(inp)
104
+
105
+ Finally, we apply IQAE and obtain an estimate $a$ for the value of the integral $A=0.27268$.
95
106
96
107
::
97
108
98
109
eps = 0.01
99
110
alpha = 0.01
100
111
101
- a_l, a_u = acc_QAE (input_list, state_function, oracle_function,eps=eps, alpha=alpha )
112
+ a = IQAE (input_list, state_function, oracle_function, eps=eps, alpha=alpha)
102
113
103
- >>> a_l
114
+ >>> a
104
115
0.26782038552705856
105
116
106
- >>> a_u
107
- 0.2741565776801965
108
-
109
117
"""
110
118
111
-
112
119
E = 1 / 2 * pow (np .sin (np .pi * 3 / 14 ), 2 ) - 1 / 2 * pow (np .sin (np .pi * 1 / 6 ), 2 )
113
120
F = 1 / 2 * np .arcsin (np .sqrt (2 * E ))
114
121
@@ -119,32 +126,30 @@ def state_function(inp, tar):
119
126
K_i = 1
120
127
m_i = 0
121
128
index_tot = 0
122
- while break_cond > 2 * eps :
129
+ while break_cond > 2 * eps :
123
130
index_tot += 1
124
131
125
132
alp_i = C * alpha * eps * K_i
126
133
N_i = int (np .ceil (1 / (2 * pow (E , 2 ) ) * np .log (2 / alp_i ) ) )
127
134
128
- # perform quantum-stuff
135
+ # Perform quantum step
129
136
qargs_dupl = [qarg .duplicate () for qarg in qargs ]
130
137
A_i = quantCirc ( int ((K_i - 1 )/ 2 ) , N_i , qargs_dupl , state_function ,
131
138
oracle_function , kwargs_oracle )
132
139
133
140
for qarg in qargs_dupl :
134
141
qarg .delete ()
135
142
136
-
137
143
#for qarg in qargs_dupl:
138
144
#qarg.delete()
139
145
140
- # compute new thetas
146
+ # Compute new thetas
141
147
theta_b , theta_sh = compute_thetas (m_i , K_i , A_i , E )
142
- #compute new Li
148
+ # Compute new Li
143
149
L_new , m_new = compute_Li (m_i , K_i , theta_b , theta_sh )
144
150
145
151
m_i = m_new
146
152
K_i = L_new * K_i
147
-
148
153
149
154
break_cond = abs ( theta_b - theta_sh )
150
155
@@ -153,19 +158,17 @@ def state_function(inp, tar):
153
158
return final_res
154
159
155
160
156
-
157
- def quantCirc (k ,N , qargs ,state_function ,
158
- oracle_function , kwargs_oracle ):
161
+ def quantCirc (k , N , qargs , state_function , oracle_function , kwargs_oracle ):
159
162
"""
160
163
Performs the quantum diffusion step, i.e. Quantum Amplitude Amplification,
161
164
in accordance to `Accelerated Quantum Amplitude Estimation without QFT <https://arxiv.org/abs/2407.16795>`_
162
165
163
166
Parameters
164
167
----------
165
168
k : int
166
- The amount of amplification steps, i.e. the power of :math:`\mathcal{Q}` in amplitude amplification.
169
+ The amount of amplification steps, i.e., the power of :math:`\mathcal{Q}` in amplitude amplification.
167
170
N : int
168
- The amount of shots, i.e. the amount of times the last qubit is measured after the amplitude amplification steps.
171
+ The amount of shots, i.e., the amount of times the last qubit is measured after the amplitude amplification steps.
169
172
state_function : function
170
173
A Python function preparing the state :math:`\ket{\Psi}`.
171
174
This function will receive the variables in the list ``args`` as arguments in the
@@ -192,15 +195,14 @@ def quantCirc(k,N, qargs,state_function,
192
195
res_dict = qargs [- 1 ].get_measurement (shots = N )
193
196
194
197
# case of single dict return, this should not occur but to be safe
195
- if True not in list (res_dict .keys ()):
198
+ if True not in list (res_dict .keys ()):
196
199
return 0
197
200
198
201
a_i = res_dict [True ]
199
202
200
203
return a_i
201
204
202
205
203
-
204
206
# theta arithmetics
205
207
def compute_thetas (m_i , K_i , A_i , E ):
206
208
"""
@@ -219,7 +221,6 @@ def compute_thetas(m_i, K_i, A_i, E):
219
221
:math:`\epsilon` limit
220
222
"""
221
223
222
-
223
224
b_max = max (A_i - E , 0 )
224
225
sh_min = min (A_i + E , 1 )
225
226
@@ -235,13 +236,12 @@ def compute_thetas(m_i, K_i, A_i, E):
235
236
return theta_b , sh_theta
236
237
237
238
238
-
239
239
def update_angle (old_angle , m_in ):
240
240
"""
241
- Subroutine to compute new angles
241
+ Subroutine to compute new angles#
242
+
242
243
Parameters
243
244
----------
244
-
245
245
old_angle : Float
246
246
Ond angle from last iteration.
247
247
m_in : Int
@@ -269,8 +269,6 @@ def update_angle(old_angle, m_in):
269
269
return final_intermed
270
270
271
271
272
-
273
-
274
272
def compute_Li (m_i , K_i , theta_b , theta_sh ):
275
273
"""
276
274
Helper function to perform statistical evaluation and compute further values for the next iteration.
0 commit comments