-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy path_clifford_operations.py
More file actions
583 lines (476 loc) · 21.1 KB
/
Copy path_clifford_operations.py
File metadata and controls
583 lines (476 loc) · 21.1 KB
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
579
580
581
582
583
from functools import cache, reduce
import numpy as np
from scipy import sparse
from qibo.config import raise_error
name = "numpy"
def _get_rxz(symplectic_matrix, nqubits):
return (
symplectic_matrix[:, -1],
symplectic_matrix[:, :nqubits],
symplectic_matrix[:, nqubits:-1],
)
def I(symplectic_matrix, q, nqubits):
return symplectic_matrix
def H(symplectic_matrix, q, nqubits):
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (x[:, q] & z[:, q])
symplectic_matrix[:, [q, nqubits + q]] = symplectic_matrix[:, [nqubits + q, q]]
return symplectic_matrix
def CNOT(symplectic_matrix, control_q, target_q, nqubits):
ind_zt = nqubits + target_q
ind_zc = nqubits + control_q
r = symplectic_matrix[:, -1]
xcq = symplectic_matrix[:, control_q]
xtq = symplectic_matrix[:, target_q]
ztq = symplectic_matrix[:, ind_zt]
zcq = symplectic_matrix[:, ind_zc]
symplectic_matrix[:, -1] = r ^ (xcq & ztq) & (xtq ^ ~zcq)
symplectic_matrix[:, target_q] = xtq ^ xcq
symplectic_matrix[:, ind_zc] = zcq ^ ztq
return symplectic_matrix
def CZ(symplectic_matrix, control_q, target_q, nqubits):
"""Decomposition --> H-CNOT-H"""
ind_zt = nqubits + target_q
ind_zc = nqubits + control_q
r = symplectic_matrix[:, -1]
xcq = symplectic_matrix[:, control_q]
xtq = symplectic_matrix[:, target_q]
ztq = symplectic_matrix[:, ind_zt]
zcq = symplectic_matrix[:, ind_zc]
ztq_xor_xcq = ztq ^ xcq
symplectic_matrix[:, -1] = (
r ^ (xtq & ztq) ^ (xcq & xtq & (ztq ^ ~zcq)) ^ (xtq & ztq_xor_xcq)
)
z_control_q = xtq ^ zcq
z_target_q = ztq_xor_xcq
symplectic_matrix[:, ind_zc] = z_control_q
symplectic_matrix[:, ind_zt] = z_target_q
return symplectic_matrix
def S(symplectic_matrix, q, nqubits):
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (x[:, q] & z[:, q])
symplectic_matrix[:, q + nqubits] = z[:, q] ^ x[:, q]
return symplectic_matrix
def Z(symplectic_matrix, q, nqubits):
"""Decomposition --> S-S"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ ((x[:, q] & z[:, q]) ^ x[:, q] & (z[:, q] ^ x[:, q]))
return symplectic_matrix
def X(symplectic_matrix, q, nqubits):
"""Decomposition --> H-S-S-H"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (z[:, q] & (z[:, q] ^ x[:, q])) ^ (z[:, q] & x[:, q])
return symplectic_matrix
def Y(symplectic_matrix, q, nqubits):
"""Decomposition --> S-S-H-S-S-H"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = (
r ^ (z[:, q] & (z[:, q] ^ x[:, q])) ^ (x[:, q] & (z[:, q] ^ x[:, q]))
)
return symplectic_matrix
def SX(symplectic_matrix, q, nqubits):
"""Decomposition --> H-S-H"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (z[:, q] & (z[:, q] ^ x[:, q]))
symplectic_matrix[:, q] = z[:, q] ^ x[:, q]
return symplectic_matrix
def SDG(symplectic_matrix, q, nqubits):
"""Decomposition --> S-S-S"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (x[:, q] & (z[:, q] ^ x[:, q]))
symplectic_matrix[:, nqubits + q] = z[:, q] ^ x[:, q]
return symplectic_matrix
def SXDG(symplectic_matrix, q, nqubits):
"""Decomposition --> H-S-S-S-H"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (z[:, q] & x[:, q])
symplectic_matrix[:, q] = z[:, q] ^ x[:, q]
return symplectic_matrix
def RX(symplectic_matrix, q, nqubits, theta):
if theta % (2 * np.pi) == 0:
return I(symplectic_matrix, q, nqubits)
elif (theta / np.pi - 1) % 2 == 0:
return X(symplectic_matrix, q, nqubits)
elif (theta / (np.pi / 2) - 1) % 4 == 0:
return SX(symplectic_matrix, q, nqubits)
else: # theta == 3*pi/2 + 2*n*pi
return SXDG(symplectic_matrix, q, nqubits)
def RZ(symplectic_matrix, q, nqubits, theta):
if theta % (2 * np.pi) == 0:
return I(symplectic_matrix, q, nqubits)
elif (theta / np.pi - 1) % 2 == 0:
return Z(symplectic_matrix, q, nqubits)
elif (theta / (np.pi / 2) - 1) % 4 == 0:
return S(symplectic_matrix, q, nqubits)
else: # theta == 3*pi/2 + 2*n*pi
return SDG(symplectic_matrix, q, nqubits)
def RY_pi(symplectic_matrix, q, nqubits):
"""Decomposition --> H-S-S"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (x[:, q] & (z[:, q] ^ x[:, q]))
symplectic_matrix[:, [nqubits + q, q]] = symplectic_matrix[:, [q, nqubits + q]]
return symplectic_matrix
def RY_3pi_2(symplectic_matrix, q, nqubits):
"""Decomposition --> H-S-S-H-S-S-H-S-S"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = r ^ (z[:, q] & (z[:, q] ^ x[:, q]))
symplectic_matrix[:, [nqubits + q, q]] = symplectic_matrix[:, [q, nqubits + q]]
return symplectic_matrix
def RY(symplectic_matrix, q, nqubits, theta):
if theta % (2 * np.pi) == 0:
return I(symplectic_matrix, q, nqubits)
elif (theta / np.pi - 1) % 2 == 0:
return Y(symplectic_matrix, q, nqubits)
elif (theta / (np.pi / 2) - 1) % 4 == 0:
"""Decomposition --> H-S-S"""
return RY_pi(symplectic_matrix, q, nqubits)
else: # theta == 3*pi/2 + 2*n*pi
"""Decomposition --> H-S-S-H-S-S-H-S-S"""
return RY_3pi_2(symplectic_matrix, q, nqubits)
def GPI2(symplectic_matrix, q, nqubits, phi):
if phi % (2 * np.pi) == 0:
return RX(symplectic_matrix, q, nqubits, np.pi / 2)
if (phi / np.pi - 1) % 2 == 0:
return RX(symplectic_matrix, q, nqubits, -np.pi / 2)
if (phi / (np.pi / 2) - 1) % 4 == 0:
return RY(symplectic_matrix, q, nqubits, np.pi / 2)
# theta == 3*pi/2 + 2*n*pi
return RY(symplectic_matrix, q, nqubits, -np.pi / 2)
def SWAP(symplectic_matrix, control_q, target_q, nqubits):
"""Decomposition --> CNOT-CNOT-CNOT"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = (
r
^ (x[:, control_q] & z[:, target_q] & (x[:, target_q] ^ ~z[:, control_q]))
^ (
(x[:, target_q] ^ x[:, control_q])
& (z[:, target_q] ^ z[:, control_q])
& (z[:, target_q] ^ ~x[:, control_q])
)
^ (
x[:, target_q]
& z[:, control_q]
& (x[:, control_q] ^ x[:, target_q] ^ z[:, control_q] ^ ~z[:, target_q])
)
)
symplectic_matrix[
:, [control_q, target_q, nqubits + control_q, nqubits + target_q]
] = symplectic_matrix[
:, [target_q, control_q, nqubits + target_q, nqubits + control_q]
]
return symplectic_matrix
def iSWAP(symplectic_matrix, control_q, target_q, nqubits):
"""Decomposition --> H-CNOT-CNOT-H-S-S"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = (
r
^ (x[:, target_q] & z[:, target_q])
^ (x[:, control_q] & z[:, control_q])
^ (x[:, control_q] & (z[:, control_q] ^ x[:, control_q]))
^ (
(z[:, control_q] ^ x[:, control_q])
& (z[:, target_q] ^ x[:, target_q])
& (x[:, target_q] ^ ~x[:, control_q])
)
^ (
(x[:, target_q] ^ z[:, control_q] ^ x[:, control_q])
& (x[:, target_q] ^ z[:, target_q] ^ x[:, control_q])
& (x[:, target_q] ^ z[:, target_q] ^ x[:, control_q] ^ ~z[:, control_q])
)
^ (x[:, control_q] & (x[:, target_q] ^ x[:, control_q] ^ z[:, control_q]))
)
z_control_q = x[:, target_q] ^ z[:, target_q] ^ x[:, control_q]
z_target_q = x[:, target_q] ^ z[:, control_q] ^ x[:, control_q]
symplectic_matrix[:, nqubits + control_q] = z_control_q
symplectic_matrix[:, nqubits + target_q] = z_target_q
symplectic_matrix[:, [control_q, target_q]] = symplectic_matrix[
:, [target_q, control_q]
]
return symplectic_matrix
def FSWAP(symplectic_matrix, control_q, target_q, nqubits):
"""Decomposition --> X-CNOT-RY-CNOT-RY-CNOT-CNOT-X"""
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CNOT(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = RY(symplectic_matrix, control_q, nqubits, np.pi / 2)
symplectic_matrix = CNOT(symplectic_matrix, target_q, control_q, nqubits)
symplectic_matrix = RY(symplectic_matrix, control_q, nqubits, -np.pi / 2)
symplectic_matrix = CNOT(symplectic_matrix, target_q, control_q, nqubits)
symplectic_matrix = CNOT(symplectic_matrix, control_q, target_q, nqubits)
return X(symplectic_matrix, control_q, nqubits)
def CY(symplectic_matrix, control_q, target_q, nqubits):
"""Decomposition --> S-CNOT-SDG"""
r, x, z = _get_rxz(symplectic_matrix, nqubits)
symplectic_matrix[:, -1] = (
r
^ (x[:, target_q] & (z[:, target_q] ^ x[:, target_q]))
^ (
x[:, control_q]
& (x[:, target_q] ^ z[:, target_q])
& (z[:, control_q] ^ ~x[:, target_q])
)
^ ((x[:, target_q] ^ x[:, control_q]) & (z[:, target_q] ^ x[:, target_q]))
)
x_target_q = x[:, control_q] ^ x[:, target_q]
z_control_q = z[:, control_q] ^ z[:, target_q] ^ x[:, target_q]
z_target_q = z[:, target_q] ^ x[:, control_q]
symplectic_matrix[:, target_q] = x_target_q
symplectic_matrix[:, nqubits + control_q] = z_control_q
symplectic_matrix[:, nqubits + target_q] = z_target_q
return symplectic_matrix
def CRX(symplectic_matrix, control_q, target_q, nqubits, theta):
# theta = 4 * n * pi
if theta % (4 * np.pi) == 0:
return I(symplectic_matrix, target_q, nqubits)
# theta = pi + 4 * n * pi
elif (theta / np.pi - 1) % 4 == 0:
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CZ(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
return CY(symplectic_matrix, control_q, target_q, nqubits)
# theta = 2 * pi + 4 * n * pi
elif (theta / (2 * np.pi) - 1) % 2 == 0:
symplectic_matrix = CZ(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = Y(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CZ(symplectic_matrix, control_q, target_q, nqubits)
return Y(symplectic_matrix, target_q, nqubits)
# theta = 3 * pi + 4 * n * pi
elif (theta / np.pi - 3) % 4 == 0:
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CY(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
return CZ(symplectic_matrix, control_q, target_q, nqubits)
def CRZ(symplectic_matrix, control_q, target_q, nqubits, theta):
# theta = 4 * n * pi
if theta % (4 * np.pi) == 0:
return I(symplectic_matrix, target_q, nqubits)
# theta = pi + 4 * n * pi
elif (theta / np.pi - 1) % 4 == 0:
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CY(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
return CNOT(symplectic_matrix, control_q, target_q, nqubits)
# theta = 2 * pi + 4 * n * pi
elif (theta / (2 * np.pi) - 1) % 2 == 0:
symplectic_matrix = CZ(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CZ(symplectic_matrix, control_q, target_q, nqubits)
return X(symplectic_matrix, target_q, nqubits)
# theta = 3 * pi + 4 * n * pi
elif (theta / np.pi - 3) % 4 == 0:
symplectic_matrix = CNOT(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = X(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CY(symplectic_matrix, control_q, target_q, nqubits)
return X(symplectic_matrix, target_q, nqubits)
def CRY(symplectic_matrix, control_q, target_q, nqubits, theta):
# theta = 4 * n * pi
if theta % (4 * np.pi) == 0:
return I(symplectic_matrix, target_q, nqubits)
# theta = pi + 4 * n * pi
elif (theta / np.pi - 1) % 4 == 0:
symplectic_matrix = Z(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CNOT(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = Z(symplectic_matrix, target_q, nqubits)
return CZ(symplectic_matrix, control_q, target_q, nqubits)
# theta = 2 * pi + 4 * n * pi
elif (theta / (2 * np.pi) - 1) % 2 == 0:
return CRZ(symplectic_matrix, control_q, target_q, nqubits, theta)
# theta = 3 * pi + 4 * n * pi
elif (theta / np.pi - 3) % 4 == 0:
symplectic_matrix = CZ(symplectic_matrix, control_q, target_q, nqubits)
symplectic_matrix = Z(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CNOT(symplectic_matrix, control_q, target_q, nqubits)
return Z(symplectic_matrix, target_q, nqubits)
def ECR(symplectic_matrix, control_q, target_q, nqubits):
symplectic_matrix = S(symplectic_matrix, control_q, nqubits)
symplectic_matrix = SX(symplectic_matrix, target_q, nqubits)
symplectic_matrix = CNOT(symplectic_matrix, control_q, target_q, nqubits)
return X(symplectic_matrix, control_q, nqubits)
def _exponent(
x1: np.ndarray, z1: np.ndarray, x2: np.ndarray, z2: np.ndarray
) -> np.ndarray:
"""Helper function that computes the exponent to which i is raised for the product of the x and z paulis encoded in the symplectic matrix. This is used in _rowsum. The computation is performed parallely over the separated paulis x1[i], z1[i], x2[i] and z2[i].
Args:
x1 (np.array): Bits of the first x paulis.
z1 (np.array): Bits of the first z paulis.
x2 (np.array): Bits of the second x paulis.
z2 (np.array): Bits of the second z paulis.
Returns:
ndarray: The calculated exponents.
"""
# this cannot be performed in the packed representation for measurements (thus packed rows)
# because bitwise arithmetic difference and sum are needed, which cannot be done directly
# in the packed representation.
return 2 * (x1 * x2 * (z2 - z1) + z1 * z2 * (x1 - x2)) - x1 * z2 + x2 * z1
def _rowsum(symplectic_matrix, h, i, nqubits, determined=False):
"""Helper function that updates the symplectic matrix by setting the h-th generator equal to the (i+h)-th one. This is done to keep track of the phase of the h-th row of the symplectic matrix (r[h]). The function is applied parallely over all the rows h and i passed.
Args:
symplectic_matrix (np.array): Input symplectic matrix.
h (np.array): Indices of the rows encoding the generators to update.
i (np.array): Indices of the rows encoding the generators to use.
nqubits (int): Total number of qubits.
Returns:
ndarray: The updated symplectic matrix.
"""
# calculate the exponent in the unpacked representation
xi, zi = symplectic_matrix[i, :nqubits], symplectic_matrix[i, nqubits:-1]
xh, zh = symplectic_matrix[h, :nqubits], symplectic_matrix[h, nqubits:-1]
exponents = _exponent(xi, zi, xh, zh)
ind = (
2 * symplectic_matrix[h, -1]
+ 2 * symplectic_matrix[i, -1]
+ np.sum(exponents, axis=-1)
) % 4 == 0
r = np.ones(h.shape[0], dtype=np.uint8)
r[ind] = 0
# the rest can be done in the packed representation
symplectic_matrix = _pack_for_measurements(symplectic_matrix, nqubits)
packed_n = _packed_size(nqubits)
xi, zi = symplectic_matrix[i, :packed_n], symplectic_matrix[i, packed_n:-1]
xh, zh = symplectic_matrix[h, :packed_n], symplectic_matrix[h, packed_n:-1]
xi_xh = xi ^ xh
zi_zh = zi ^ zh
if determined:
r = reduce(np.logical_xor, r)
xi_xh = reduce(np.logical_xor, xi_xh)
zi_zh = reduce(np.logical_xor, zi_zh)
symplectic_matrix[h[0], -1] = r
symplectic_matrix[h[0], :packed_n] = xi_xh
symplectic_matrix[h[0], packed_n:-1] = zi_zh
else:
symplectic_matrix[h, -1] = r
symplectic_matrix[h, :packed_n] = xi_xh
symplectic_matrix[h, packed_n:-1] = zi_zh
return _unpack_for_measurements(symplectic_matrix, nqubits)
def _determined_outcome(state, q, nqubits):
"""Extracts the outcome for a measurement in case it is determined."""
state[-1, :] = 0
idx = (state[:nqubits, q].nonzero()[0] + nqubits).astype(np.uint)
if len(idx) == 0:
return state, state[-1, -1]
state = _rowsum(
state,
_dim_xz(nqubits) * np.ones(idx.shape, dtype=np.uint),
idx,
nqubits,
True,
)
return state, state[-1, -1]
def _random_outcome(state, p, q, nqubits, outcome):
"""Extracts the outcome for a measurement in case it is random."""
p = p[0] + nqubits
state[p, q] = 0
h = state[:-1, q].nonzero()[0]
state[p, q] = 1
if h.shape[0] > 0:
state = _rowsum(
state,
h.astype(np.uint),
np.uint(p) * np.ones(h.shape[0], dtype=np.uint),
nqubits,
False,
)
state[p - nqubits, :] = state[p, :]
# outcome = np.random.randint(2, size=1).item()
state[p, :] = 0
state[p, -1] = outcome
state[p, nqubits + q] = 1
return state, outcome
@cache
def _dim(nqubits):
"""Returns the dimension of the symplectic matrix for a given number of qubits."""
return _dim_xz(nqubits) + 1
@cache
def _dim_xz(nqubits):
"""Returns the dimension of the symplectic matrix (only the de/stabilizers generators part,
without the phases and scratch row) for a given number of qubits."""
return 2 * nqubits
@cache
def _packed_size(n):
"""Returns the size of an array of `n` booleans after packing."""
return np.ceil(n / 8).astype(int)
def _packbits(array, axis):
return np.packbits(array, axis=axis)
def _unpackbits(array, axis, count):
return np.unpackbits(array, axis=axis, count=count)
def _pack_for_measurements(state, nqubits):
"""Prepares the state for measurements by packing the rows of the X and Z sections of the symplectic matrix."""
r, x, z = _get_rxz(state, nqubits)
x = _packbits(x, axis=1)
z = _packbits(z, axis=1)
return np.hstack((x, z, r[:, None]))
def _unpack_for_measurements(state, nqubits):
"""Unpacks the symplectc matrix that was packed for measurements."""
x = _unpackbits(state[:, : _packed_size(nqubits)], axis=1, count=nqubits)
z = _unpackbits(state[:, _packed_size(nqubits) : -1], axis=1, count=nqubits)
return np.hstack((x, z, state[:, -1][:, None]))
def _init_state_for_measurements(state, nqubits, collapse):
if collapse:
return _unpackbits(state, axis=0, count=_dim(nqubits))
return state.copy()
def _sample_random_outcomes(n_samples):
return np.random.randint(2, size=n_samples).tolist()
def _M(state, qubits, nqubits, collapse=False):
sample = []
state = _init_state_for_measurements(state, nqubits, collapse)
possible_outcomes = []
for q in qubits:
p = state[nqubits:-1, q].nonzero()[0]
# random outcome, affects the state
if len(p) > 0:
state, _ = _random_outcome(state, p, q, nqubits, 0)
outcome = None
# determined outcome, state unchanged
else:
_, outcome = _determined_outcome(state, q, nqubits)
outcome = int(outcome)
sample.append(outcome)
if collapse:
state = _packbits(state, axis=0)
return sample
# valid for a standard basis measurement only
def M(state, qubits, nqubits, collapse=False, nshots=1):
if collapse and nshots != 1:
raise_error(
RuntimeError,
"Cannot generate multiple shots with a collapsing measurement.",
)
sample = _M(state, qubits, nqubits, collapse)
samples = [
(_sample_random_outcomes(nshots) if outcome is None else nshots * [outcome])
for outcome in sample
]
samples = list(zip(*samples))
return samples
def cast(x, dtype=None, copy: bool = False):
if dtype is None:
dtype = "complex128"
if isinstance(x, np.ndarray):
return x.astype(dtype, copy=copy)
elif sparse.issparse(x): # pragma: no cover
return x.astype(dtype, copy=copy)
return np.asarray(x, dtype=dtype, copy=copy if copy else None)
def _clifford_pre_execution_reshape(state):
"""Reshape and packing applied to the symplectic matrix before execution to prepare the state in the form needed by each engine.
Args:
state (np.array): Input state.
Returns:
(np.array) The packed and reshaped state.
"""
return _packbits(state, axis=0)
def _clifford_post_execution_reshape(state, nqubits: int):
"""Reshape and unpacking applied to the state after execution to retrieve the standard symplectic matrix form.
Args:
state (np.array): Input state.
nqubits (int): Number of qubits.
Returns:
(np.array) The unpacked and reshaped state.
"""
state = _unpackbits(state, axis=0, count=_dim(nqubits))[: _dim(nqubits)]
return state
def identity_density_matrix(nqubits, normalize: bool = True):
state = np.eye(2**nqubits, dtype="complex128")
if normalize is True: # pragma: no cover
state /= 2**nqubits
return state
def set_seed(seed):
np.random.seed(seed)