-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathhhl.py
303 lines (232 loc) · 10.9 KB
/
hhl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# pylint: disable=wrong-or-nonexistent-copyright-notice
"""Demonstrates the algorithm for solving linear systems by Harrow, Hassidim, Lloyd (HHL).
The HHL algorithm solves a system of linear equations, specifically equations of the form Ax = b,
where A is a Hermitian matrix, b is a known vector, and x is the unknown vector. To solve on a
quantum system, b must be rescaled to have magnitude 1, and the equation becomes:
|x> = A**-1 |b> / || A**-1 |b> ||
The algorithm uses 3 sets of qubits: a single ancilla qubit, a register (to store eigenvalues of
A), and memory qubits (to store |b> and |x>). The following are performed in order:
1) Quantum phase estimation to extract eigenvalues of A
2) Controlled rotations of ancilla qubit
3) Uncomputation with inverse quantum phase estimation
For details about the algorithm, please refer to papers in the REFERENCE section below. The
following description uses variables defined in the HHL paper.
This example is an implementation of the HHL algorithm for arbitrary 2x2 Hermitian matrices. The
output of the algorithm are the expectation values of Pauli observables of |x>. Note that the
accuracy of the result depends on the following factors:
* Register size
* Choice of parameters C and t
The result is perfect if
* Each eigenvalue of the matrix is in the form
2π/t * k/N,
where 0≤k<N, and N=2^n, where n is the register size. In other words, k is a value that can be
represented exactly by the register.
* C ≤ 2π/t * 1/N, the smallest eigenvalue that can be stored in the circuit.
The result is good if the register size is large enough such that for every pair of eigenvalues,
the ratio can be approximated by a pair of possible register values. Let s be the scaling factor
from possible register values to eigenvalues. One way to set t is
t = 2π/(sN)
For arbitrary matrices, because properties of their eigenvalues are typically unknown, parameters C
and t are fine-tuned based on their condition number.
=== REFERENCE ===
Harrow, Aram W. et al. Quantum algorithm for solving linear systems of
equations (the HHL paper)
https://arxiv.org/abs/0811.3171
Coles, Eidenbenz et al. Quantum Algorithm Implementations for Beginners
https://arxiv.org/abs/1804.03719
=== CIRCUIT ===
Example of circuit with 2 register qubits.
(0, 0): ─────────────────────────Ry(θ₄)─Ry(θ₁)─Ry(θ₂)─Ry(θ₃)──────────────M──
┌──────┐ │ │ │ │ ┌───┐
(1, 0): ─H─@─────────│ │──X─@──────@────X─@──────@─│ │─────────@─H────
│ │QFT^-1│ │ │ │ │ │QFT│ │
(2, 0): ─H─┼─────@───│ │──X─@────X─@────X─@────X─@─│ │─@───────┼─H────
│ │ └──────┘ └───┘ │ │
(3, 0): ───e^iAt─e^2iAt───────────────────────────────────────e^-2iAt─e^-iAt─
Note: QFT in the above diagram omits swaps, which are included implicitly by
reversing qubit order for phase kickbacks.
"""
import math
import numpy as np
import sympy
import cirq
class PhaseEstimation(cirq.Gate):
"""A gate for Quantum Phase Estimation.
The last qubit stores the eigenvector; all other qubits store the estimated phase,
in big-endian.
Args:
num_qubits: The number of qubits of the unitary.
unitary: The unitary gate whose phases will be estimated.
"""
def __init__(self, num_qubits, unitary):
self._num_qubits = num_qubits
self.U = unitary
def num_qubits(self):
return self._num_qubits
def _decompose_(self, qubits):
qubits = list(qubits)
yield cirq.H.on_each(*qubits[:-1])
yield PhaseKickback(self.num_qubits(), self.U)(*qubits)
yield cirq.qft(*qubits[:-1], without_reverse=True) ** -1
class HamiltonianSimulation(cirq.EigenGate):
"""A gate that represents e^iAt.
This EigenGate + np.linalg.eigh() implementation is used here purely for demonstrative
purposes. If a large matrix is used, the circuit should implement actual Hamiltonian
simulation, by using the linear operators framework in Cirq, for example.
"""
def __init__(self, A, t, exponent=1.0):
cirq.EigenGate.__init__(self, exponent=exponent)
self.A = A
self.t = t
ws, vs = np.linalg.eigh(A)
self.eigen_components = []
for w, v in zip(ws, vs.T):
theta = w * t / math.pi
P = np.outer(v, np.conj(v))
self.eigen_components.append((theta, P))
def _num_qubits_(self) -> int:
return 1
def _with_exponent(self, exponent):
return HamiltonianSimulation(self.A, self.t, exponent)
def _eigen_components(self):
return self.eigen_components
class PhaseKickback(cirq.Gate):
"""A gate for the phase kickback stage of Quantum Phase Estimation.
It consists of a series of controlled e^iAt gates with the memory qubit as the target and
each register qubit as the control, raised to the power of 2 based on the qubit index.
unitary is the unitary gate whose phases will be estimated.
"""
def __init__(self, num_qubits, unitary):
super(PhaseKickback, self)
self._num_qubits = num_qubits
self.U = unitary
def num_qubits(self):
return self._num_qubits
def _decompose_(self, qubits):
qubits = list(qubits)
memory = qubits.pop()
for i, qubit in enumerate(qubits):
yield cirq.ControlledGate(self.U ** (2**i))(qubit, memory)
class EigenRotation(cirq.Gate):
"""Perform a rotation on an ancilla equivalent to division by eigenvalues of a matrix.
EigenRotation performs the set of rotation on the ancilla qubit equivalent to division on the
memory register by each eigenvalue of the matrix. The last qubit is the ancilla qubit; all
remaining qubits are the register, assumed to be big-endian.
It consists of a controlled ancilla qubit rotation for each possible value that can be
represented by the register. Each rotation is a Ry gate where the angle is calculated from
the eigenvalue corresponding to the register value, up to a normalization factor C.
"""
def __init__(self, num_qubits, C, t):
super(EigenRotation, self)
self._num_qubits = num_qubits
self.C = C
self.t = t
self.N = 2 ** (num_qubits - 1)
def num_qubits(self):
return self._num_qubits
def _decompose_(self, qubits):
for k in range(self.N):
kGate = self._ancilla_rotation(k)
# xor's 1 bits correspond to X gate positions.
xor = k ^ (k - 1)
for q in qubits[-2::-1]:
# Place X gates
if xor % 2 == 1:
yield cirq.X(q)
xor >>= 1
# Build controlled ancilla rotation
kGate = cirq.ControlledGate(kGate)
yield kGate(*qubits)
def _ancilla_rotation(self, k):
if k == 0:
k = self.N
theta = 2 * math.asin(self.C * self.N * self.t / (2 * math.pi * k))
return cirq.ry(theta)
def hhl_circuit(A, C, t, register_size, *input_prep_gates):
"""Constructs the HHL circuit.
Args:
A: The input Hermitian matrix.
C: Algorithm parameter, see above.
t: Algorithm parameter, see above.
register_size: The size of the eigenvalue register.
*input_prep_gates: A list of gates to be applied to |0> to generate the desired input
state |b>.
Returns:
The HHL circuit. The ancilla measurement has key 'a' and the memory measurement is in key
'm'. There are two parameters in the circuit, `exponent` and `phase_exponent` corresponding
to a possible rotation applied before the measurement on the memory with a
`cirq.PhasedXPowGate`.
"""
ancilla = cirq.LineQubit(0)
# to store eigenvalues of the matrix
register = [cirq.LineQubit(i + 1) for i in range(register_size)]
# to store input and output vectors
memory = cirq.LineQubit(register_size + 1)
c = cirq.Circuit()
hs = HamiltonianSimulation(A, t)
pe = PhaseEstimation(register_size + 1, hs)
c.append([gate(memory) for gate in input_prep_gates])
c.append(
[
pe(*(register + [memory])),
EigenRotation(register_size + 1, C, t)(*(register + [ancilla])),
pe(*(register + [memory])) ** -1,
cirq.measure(ancilla, key='a'),
]
)
c.append(
[
cirq.PhasedXPowGate(
exponent=sympy.Symbol('exponent'), phase_exponent=sympy.Symbol('phase_exponent')
)(memory),
cirq.measure(memory, key='m'),
]
)
return c
def simulate(circuit):
simulator = cirq.Simulator()
# Cases for measuring X, Y, and Z (respectively) on the memory qubit.
params = [
{'exponent': 0.5, 'phase_exponent': -0.5},
{'exponent': 0.5, 'phase_exponent': 0},
{'exponent': 0, 'phase_exponent': 0},
]
results = simulator.run_sweep(circuit, params, repetitions=5000)
for label, result in zip(('X', 'Y', 'Z'), list(results)):
# Only select cases where the ancilla is 1.
# TODO: optimize using amplitude amplification algorithm.
# Github issue: https://github.com/quantumlib/Cirq/issues/2216
expectation = 1 - 2 * np.mean(result.measurements['m'][result.measurements['a'] == 1])
print(f'{label} = {expectation}')
def main():
"""The main program loop.
Simulates HHL with matrix input, and outputs Pauli observables of the resulting qubit state |x>.
Expected observables are calculated from the expected solution |x>.
"""
# Eigendecomposition:
# (4.537, [-0.971555, -0.0578339+0.229643j])
# (0.349, [-0.236813, 0.237270-0.942137j])
# |b> = (0.64510-0.47848j, 0.35490-0.47848j)
# |x> = (-0.0662724-0.214548j, 0.784392-0.578192j)
A = np.array(
[
[4.30213466 - 6.01593490e-08j, 0.23531802 + 9.34386156e-01j],
[0.23531882 - 9.34388383e-01j, 0.58386534 + 6.01593489e-08j],
]
)
t = 0.358166 * math.pi
register_size = 4
input_prep_gates = [cirq.rx(1.276359), cirq.rz(1.276359)]
expected = (0.144130, 0.413217, -0.899154)
# Set C to be the smallest eigenvalue that can be represented by the
# circuit.
C = 2 * math.pi / (2**register_size * t)
# Simulate circuit.
print("Expected observable outputs:")
print("X =", expected[0])
print("Y =", expected[1])
print("Z =", expected[2])
print("Actual: ")
simulate(hhl_circuit(A, C, t, register_size, *input_prep_gates))
if __name__ == '__main__':
main()