-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_encoding.py
578 lines (471 loc) · 18.9 KB
/
block_encoding.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# Copyright (C) 2024 Matthias Deiml, Daniel Peterseim - All rights reserved
from __future__ import annotations
import qiskit as qk
from qiskit_aer import Aer
import numpy as np
from qiskit.circuit.library import MCXGate, QFT
def _mcx_nonsymmetric(N, ancillas=True):
if N == 1:
qc = qk.QuantumCircuit(2, name="ns_mcx")
qc.cx(0, 1)
return qc
if N == 2:
qc = qk.QuantumCircuit(3, name="ns_mcx")
qc.rccx(0, 1, 2)
return qc
if N == 3:
qc = qk.QuantumCircuit(4 if not ancillas else 5, name="ns_mcx")
qc.rcccx(0, 1, 2, 3)
return qc
if ancillas:
x = qk.QuantumRegister(N, name="x")
b = qk.QuantumRegister(1, name="b")
a = qk.AncillaRegister(N-2)
qc = qk.QuantumCircuit(x, b, a, name="ns_mcx")
l = x[:]
anc = a[:]
while len(l) > 2:
k = len(l) // 2
for i in range(k):
qc.rccx(l[2*i], l[2*i+1], anc[i])
l = l[len(l)-len(l)%2:] + anc[:k]
anc = anc[k:]
qc.rccx(l[0], l[1], b)
return qc
else:
x = qk.QuantumRegister(N, name="x")
b = qk.QuantumRegister(1, name="b")
qc = qk.QuantumCircuit(x, b, name="ns_mcx")
qc.mcx(x, b)
return qc
def _run_circuit(qc, initial, backend=None):
if backend is None:
backend = Aer.get_backend("aer_simulator_statevector")
circ = qk.QuantumCircuit(qc.num_qubits)
circ.set_statevector(qk.quantum_info.Statevector(initial))
circ.append(qc, list(range(qc.num_qubits)))
circ.save_statevector()
circ = qk.transpile(circ, backend)
return backend.run(circ).result().get_statevector().data
class CPiNot:
def __init__(self, embedding_size, data: qk.QuantumCircuit | list = None, ctrl_state: list = None):
if isinstance(data, qk.QuantumCircuit):
assert data.num_qubits - data.num_ancillas == embedding_size + 1
assert data.num_clbits == 0
if isinstance(data, list):
assert all([b >= 0 and b <= embedding_size for b in data])
if ctrl_state is None:
ctrl_state = [0 for b in data]
else:
assert len(ctrl_state) == len(data)
else:
assert ctrl_state is None
self._embedding_size = embedding_size
self._data = data
self._ctrl_state = ctrl_state
self._avoid_ancillas = False
def __eq__(self, o):
return self._embedding_size == o._embedding_size and (
(self._data is None and o._data is None)
or
(
self._data == o._data and
self._ctrl_state == o._ctrl_state
)
)
def copy(self) -> CPiNot:
if self._data is None:
return CPiNot(self._embedding_size, None)
elif isinstance(self.data, qk.QuantumCircuit):
return CPiNot(self._embedding_size, self._data.copy())
else:
return CPiNot(self._embedding_size, self._data.copy(), self._ctrl_state.copy())
def num_ancillas(self) -> int:
if isinstance(self._data, qk.QuantumCircuit):
return self._data.num_ancillas
elif self._data is None or self._avoid_ancillas:
return 0
else:
return max(0, len(self._data) - 2)
def to_circuit(self, symmetric=False) -> qk.QuantumCircuit:
if isinstance(self._data, qk.QuantumCircuit):
return self._data
else:
x = qk.QuantumRegister(self._embedding_size, name="x")
b = qk.QuantumRegister(1, name="b")
a = qk.AncillaRegister(self.num_ancillas(), name="a")
qc = qk.QuantumCircuit(x, b, a, name="CX_Pi")
if self._data is None:
qc.x(b)
else:
bits = []
for (i, cs) in zip(self._data, self._ctrl_state):
bits.append(x[i])
for i in range(len(bits)):
if self._ctrl_state[i] == 0:
qc.x(bits[i])
if symmetric:
qc.append(MCXGate(len(bits)), bits[:] + b[:])
for i in range(len(bits)):
if self._ctrl_state[i] == 0:
qc.x(bits[i])
else:
qc.append(_mcx_nonsymmetric(len(bits), ancillas=not self._avoid_ancillas), bits[:] + b[:] + a[:])
return qc.decompose(["ns_mcx"])
def logical_and(self, other: CPiNot) -> CPiNot:
assert self._embedding_size == other._embedding_size
if self._data is None:
return other
if other._data is None:
return self
if isinstance(self._data, qk.QuantumCircuit) or isinstance(other._data, qk.QuantumCircuit):
x = qk.QuantumRegister(self._embedding_size, name="x")
a1 = qk.AncillaRegister(self.num_ancillas())
a2 = qk.AncillaRegister(other.num_ancillas())
a3 = qk.AncillaRegister(2)
b = qk.QuantumRegister(1, name="b")
qc = qk.QuantumCircuit(x, b, a1, a2, a3, name="CX_Pi")
qc.append(self.to_circuit(), x[:] + [a3[0]] + a1[:])
qc.append(other.to_circuit(), x[:] + [a3[1]] + a2[:])
qc.rccx(a3[0], a3[1], b)
return CPiNot(self._embedding_size, qc)
else:
bits = self._data + other._data
ctrl_state = self._ctrl_state + other._ctrl_state
return CPiNot(self._embedding_size, bits, ctrl_state)
def extend_embedding(self, n: int, front: bool) -> CPiNot:
if self._data is None:
return CPiNot(self._embedding_size + n, None)
elif isinstance(self._data, list):
if front:
return CPiNot(self._embedding_size + n, [i + n for i in self._data], self._ctrl_state)
else:
return CPiNot(self._embedding_size + n, self._data, self._ctrl_state)
else:
x = qk.QuantumRegister(self._embedding_size + n, name="x")
a = qk.AncillaRegister(self.num_ancillas(), name="a")
b = qk.QuantumRegister(1, name="b")
qc = qk.QuantumCircuit(x, b, a)
qc.append(
self._data,
(x[n:] if front else x[:-n]) +
b[:] +
a[:]
)
return CPiNot(self._embedding_size + n, qc)
def control(self, ctrl_state: int = 0) -> CPiNot:
return self.extend_embedding(1, False).logical_and(
CPiNot(
self._embedding_size + 1,
[self._embedding_size],
[ctrl_state]
)
)
def xor(self, other: CPiNot) -> CPiNot:
assert self._embedding_size == other._embedding_size
if self._data is None:
return other
if other._data is None:
return self
x = qk.QuantumRegister(self._embedding_size, name="x")
a = qk.QuantumRegister(max(self.num_ancillas(), other.num_ancillas()), name="a")
b = qk.QuantumRegister(1, name="b")
qc = qk.QuantumCircuit(x, b, a)
qc.append(
self.to_circuit(True),
x[:] + b[:] + a[:self.num_ancillas()]
)
qc.append(
other.to_circuit(),
x[:] + b[:] + a[:other.num_ancillas()]
)
return CPiNot(self._embedding_size, qc)
def projected_indices(self, backend=None):
x = qk.QuantumRegister(self._embedding_size)
a = qk.QuantumRegister(self.num_ancillas())
b = qk.QuantumRegister(1)
circ = qk.QuantumCircuit(x, a, b)
circ.append(self.to_circuit(), x[:] + b[:] + a[:])
N1 = 2**self._embedding_size
N2 = 2**(self._embedding_size + self.num_ancillas())
data = np.zeros(2 * N2)
indices = []
for i in range(N1):
data[i] = 1
out = _run_circuit(circ, data, backend)
data[i] = 0
if not np.all(np.isclose(out[N2:], 0)):
indices.append(i)
return np.array(indices, dtype=np.int32)
class BlockEncoding:
def __init__(
self,
U: qk.QuantumCircuit,
CX_dom: qk.CPiNot = None,
CX_img: qk.CPiNot = None,
normalization: float = 1,
) -> BlockEncoding:
self._embedding_size = U.num_qubits - U.num_ancillas
if CX_dom is None:
CX_dom = CPiNot(self._embedding_size, None)
if CX_img is None:
CX_img = CPiNot(self._embedding_size, None)
assert U.num_clbits == 0
self._U = U
self._CX_dom = CX_dom
self._CX_img = CX_img
assert normalization > 0
self._normalization = normalization
def identity(n: int) -> BlockEncoding:
qc = qk.QuantumCircuit(n)
return BlockEncoding(qc)
def copy(self) -> BlockEncoding:
return BlockEncoding(
self._U.copy(),
self._CX_dom.copy(),
self._CX_img.copy(),
self._normalization)
def transpose(self) -> BlockEncoding:
U = self._U.inverse()
CX_dom = self._CX_img
CX_img = self._CX_dom
U.name = self._U.name + "^T"
return BlockEncoding(U, CX_dom, CX_img, self._normalization)
def scale(self, factor: float) -> BlockEncoding:
res = self.copy()
res._normalization *= factor
return res
def add_subnormalization(self, factor: float) -> BlockEncoding:
assert factor <= 1 and factor > 0
x = qk.QuantumRegister(self._embedding_size + 1, name="x")
a = qk.AncillaRegister(self._U.num_ancillas, name="a")
U = qk.QuantumCircuit(
x, a,
name=self._U.name)
U.ry(np.arccos(factor) * 2, x[-1])
U.append(self._U, x[:-1] + a[:])
U.decompose()
CX_dom = self._CX_dom.control()
CX_img = self._CX_img.control()
normalization = self._normalization / factor
return BlockEncoding(U, CX_dom, CX_img, normalization)
def extend(self, n: int, front: bool = False) -> BlockEncoding:
x = qk.QuantumRegister(self._embedding_size + n, name="x")
a = qk.AncillaRegister(self._U.num_ancillas, name="a")
U = qk.QuantumCircuit(
x, a,
name=self._U.name)
if front:
U.append(self._U, x[n:] + a[:])
else:
U.append(self._U, x[:self._embedding_size] + a[:])
U = U.decompose()
if front:
CX_dom = self._CX_dom.extend_embedding(n, True).logical_and(
CPiNot(self._embedding_size + n, list(range(n)))
)
CX_img = self._CX_img.extend_embedding(n, True).logical_and(
CPiNot(self._embedding_size + n, list(range(n)))
)
else:
CX_dom = self._CX_dom.extend_embedding(n, False).logical_and(
CPiNot(self._embedding_size + n, list(range(self._embedding_size, self._embedding_size + n)))
)
CX_img = self._CX_img.extend_embedding(n, False).logical_and(
CPiNot(self._embedding_size + n, list(range(self._embedding_size, self._embedding_size + n)))
)
return BlockEncoding(U, CX_dom, CX_img, self._normalization)
def tensor(self, other) -> BlockEncoding:
ancillas = self._U.num_ancillas + other._U.num_ancillas
x = qk.QuantumRegister(self._embedding_size + other._embedding_size, name="x")
a = qk.AncillaRegister(ancillas, name="a")
U = qk.QuantumCircuit(
x, a,
name=self._U.name + "x" + other._U.name)
U.append(self._U, x[:self._embedding_size] + a[:self._U.num_ancillas])
U.append(
other._U,
x[self._embedding_size:] +
a[self._U.num_ancillas:ancillas])
U = U.decompose()
CX_dom = self._CX_dom.extend_embedding(
other._embedding_size,
False
).logical_and(
other._CX_dom.extend_embedding(self._embedding_size, True)
)
CX_img = self._CX_img.extend_embedding(
other._embedding_size,
False
).logical_and(
other._CX_img.extend_embedding(self._embedding_size, True)
)
normalization = self._normalization * other._normalization
return BlockEncoding(U, CX_dom, CX_img, normalization)
def multiply(self, other: BlockEncoding) -> BlockEncoding:
assert self._embedding_size == other._embedding_size
assert self._CX_dom == other._CX_img
U = None
CX_dom = None
CX_img = None
if self._CX_dom._data is None:
ancillas = max(
self._U.num_ancillas,
other._U.num_ancillas,
)
x = qk.QuantumRegister(
max(self._embedding_size, other._embedding_size), name="x")
a = qk.AncillaRegister(ancillas, name="a")
U = qk.QuantumCircuit(
x, a,
name=self._U.name+"*"+other._U.name)
U.append(
other._U,
x[:other._embedding_size] + a[:other._U.num_ancillas])
U.append(
self._U,
x[:self._embedding_size] + a[:self._U.num_ancillas])
CX_dom = other._CX_dom.copy()
CX_img = self._CX_img.copy()
else:
ancillas = max(
self._U.num_ancillas,
other._U.num_ancillas,
self._CX_dom.num_ancillas()
)
x = qk.QuantumRegister(
max(self._embedding_size, other._embedding_size) + 1, name="x")
a = qk.AncillaRegister(ancillas, name="a")
U = qk.QuantumCircuit(
x, a,
name=self._U.name+"*"+other._U.name)
U.append(
other._U,
x[:other._embedding_size] + a[:other._U.num_ancillas])
U.append(
self._CX_dom.to_circuit(True),
x[:self._embedding_size] +
[x[-1]] +
a[:self._CX_dom.num_ancillas]
)
U.x(x[-1])
U.append(
self._U,
x[:self._embedding_size] + a[:self._U.num_ancillas])
CX_dom = other._CX_dom.control()
CX_img = self._CX_img.control()
U = U.decompose()
normalization = self._normalization * other._normalization
return BlockEncoding(U, CX_dom, CX_img, normalization)
def _block_diagonal(
self,
other: BlockEncoding,
pre_h: bool,
post_h: bool
) -> BlockEncoding:
ratio = self._normalization / other._normalization
if not pre_h and not post_h:
assert np.isclose(ratio, 1)
if pre_h:
assert self._CX_dom == other._CX_dom
if post_h:
assert self._CX_img == other._CX_img
if pre_h and post_h:
ratio = np.sqrt(ratio)
angle = np.arctan(ratio)
ancillas = max(self._U.num_ancillas, other._U.num_ancillas)
x = qk.QuantumRegister(
max(self._embedding_size, other._embedding_size) + 1, name="x")
a = qk.AncillaRegister(
ancillas, name="a")
U = qk.QuantumCircuit(
x, a,
name="diag("+self._U.name+","+other._U.name+")")
if pre_h:
U.ry(2 * angle, x[-1])
U.append(
self._U.control(1, ctrl_state=0),
[x[-1]] + x[:self._embedding_size] + a[:self._U.num_ancillas])
U.append(
other._U.control(1, ctrl_state=1),
[x[-1]] + x[:other._embedding_size] + a[:other._U.num_ancillas])
if post_h:
U.ry(-2 * angle, x[-1])
U = U.decompose()
CX_dom = None
if pre_h:
CX_dom = self._CX_dom.control(0)
else:
CX_dom = self._CX_dom.control(0).xor(other._CX_dom.control(1))
CX_img = None
if post_h:
CX_img = self._CX_img.control(0)
else:
CX_img = self._CX_img.control(0).xor(other._CX_img.control(1))
return BlockEncoding(U, CX_dom, CX_img, self._normalization)
def block_diagonal(self, other: BlockEncoding) -> BlockEncoding:
return self._block_diagonal(other, False, False)
def block_vertical(self, other: BlockEncoding) -> BlockEncoding:
return self._block_diagonal(other, True, False)
def block_horizontal(self, other: BlockEncoding) -> BlockEncoding:
return self._block_diagonal(other, False, True)
def add(self, other: BlockEncoding) -> BlockEncoding:
return self._block_diagonal(other, True, True)
def simplify(self, barrier=False):
x = qk.QuantumRegister(self._embedding_size, name="x")
a = qk.AncillaRegister(max(self._U.num_ancillas, self._CX_dom.num_ancillas(), self._CX_img.num_ancillas()), name="a")
b = qk.QuantumRegister(1, name="b")
U = qk.QuantumCircuit(x, b, a)
U.append(self._CX_dom.to_circuit().inverse(), x[:] + b[:] + a[:self._CX_dom.num_ancillas()])
if barrier:
U.barrier()
U.append(self._U, x[:] + a[:self._U.num_ancillas])
if barrier:
U.barrier()
U.append(self._CX_img.to_circuit(), x[:] + b[:] + a[:self._CX_img.num_ancillas()])
U = U.decompose()
return BlockEncoding(
U,
CPiNot(self._embedding_size + 1, [self._embedding_size]),
CPiNot(self._embedding_size + 1, [self._embedding_size]),
normalization=self._normalization
)
def get_encoded_matrix(self, backend=None, return_projections=False, projection_hint=None):
# This assumes that the projection matrix only contains ones and zeros
if backend is None:
backend = Aer.get_backend("aer_simulator_statevector")
if projection_hint is None:
print("testing projection to domain")
P_dom = self._CX_dom.projected_indices(backend)
print(P_dom)
print("testing projection to image")
P_img = self._CX_img.projected_indices(backend)
print(P_img)
else:
P_dom = projection_hint[0]
P_img = projection_hint[1]
qc = self._U
flipped = False
if len(P_dom) > len(P_img):
qc = qc.inverse()
flipped = True
P_dom, P_img = P_img, P_dom
data = np.zeros(2 ** qc.num_qubits)
A = np.zeros((len(P_img), len(P_dom)), dtype=np.csingle)
for (i, index) in enumerate(P_dom):
data[index] = 1
A[:, i] = _run_circuit(qc, data, backend)[P_img]
data[index] = 0
if flipped:
A = A.T
P_dom, P_img = P_img, P_dom
if return_projections:
return A, P_dom, P_img
else:
return A
def output_stats(self, backend=None):
from qiskit.providers.fake_provider import FakePrague
if backend is None:
backend = FakePrague()
circ = qk.transpile(self._U, backend, optimization_level=3)
print(circ.count_ops())