-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatrix.py
466 lines (374 loc) · 12.7 KB
/
matrix.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
from math import log
from cmath import sqrt, sin, cos, exp, pi
from functools import reduce
import numpy as np
# from scipy.sparse import sparse as sps
from scipy.linalg import eigvals
from numba import njit, jit
from warnings import simplefilter
from numba.core.errors import NumbaPerformanceWarning
simplefilter('ignore', category=NumbaPerformanceWarning)
def haar(n):
z = np.random.randn(n, n) + 1j*np.random.randn(n, n) / np.sqrt(2)
q, r = np.linalg.qr(z)
d = np.diag(r)
Lambda = d / np.abs(d)
return np.multiply(q, Lambda, q)
ops = {
"H": 1.0 / sqrt(2.0) * (np.array([[1.0, 1.0], [1.0, -1.0]], dtype=complex)),
"h": np.array([[0.85355339+0.14644661j, 0.35355339-0.35355339j],
[0.35355339-0.35355339j, 0.14644661+0.85355339j]], dtype=complex),
"I": np.array([[1.0, 0.0], [0.0, 1.0]], dtype=complex),
"O": np.array([[0.0, 0.0], [0.0, 0.0]], dtype=complex),
"X": np.array([[0.0, 1.0], [1.0, 0.0]], dtype=complex),
"Y": np.array([[0.0, -1j], [1j, 0.0]], dtype=complex),
"Z": np.array([[1.0, 0.0], [0.0, -1.0]], dtype=complex),
"S": np.array([[1.0, 0.0], [0.0, 1j]], dtype=complex),
"T": np.array([[1.0, 0.0], [0.0, exp(1j * pi / 4.0)]], dtype=complex),
"0": np.array([[1.0, 0.0], [0.0, 0.0]], dtype=complex),
"1": np.array([[0.0, 0.0], [0.0, 1.0]], dtype=complex),
"D": 1.0 / sqrt(2) * np.array([[1.0, -1j], [-1j, 1.0]], dtype=complex),
"d": np.array([[ 9.23879533e-01, -3.82683432e-01j],
[ -3.82683432e-01j, 9.23879533e-01]], dtype=complex),
}
def op_at(opstrs, js, L, base=None, BC=None):
if type(js) == int:
js = [js]
if base is None:
base = ["I"]*L
for j, opstr in zip(js, opstrs):
if BC == "0":
j = j%L
base[j] = opstr
return listkron([ops[opstr] for opstr in base])
def op_on_state_inplace(op, js, state, ds=None):
if ds is None:
L = int(np.log2(len(state)))
ds = [2] * L
else:
L = len(ds)
dn = np.prod(np.array(ds).take(js))
dL = np.prod(ds)
rest = np.setdiff1d(np.arange(L), js)
ordering = list(rest) + list(js)
state = state.reshape(ds).transpose(ordering).reshape(np.int(dL / dn), dn)
state = (
state.dot(op, out=np.ascontiguousarray(state))
.reshape(ds)
.transpose(np.argsort(ordering))
.reshape(dL)
)
return state
def op_on_state(op, js, state, ds=None):
if ds is None:
L = int(np.log2(len(state)))
ds = [2] * L
else:
L = len(ds)
dn = np.prod(np.array(ds).take(js))
dL = np.prod(ds)
rest = np.setdiff1d(np.arange(L), js)
ordering = list(rest) + list(js)
state = (
state.reshape(ds)
.transpose(ordering)
.reshape(np.int(dL / dn), dn)
.dot(op)
.reshape(ds)
.transpose(np.argsort(ordering))
.reshape(dL)
)
return state
def op_on_state_2(local_op_list, js, state):
L = np.int(log(len(state), 2))
I_list = [np.eye(2.0, dtype=complex)] * L
for j, local_op in zip(js, local_op_list):
I_list[j] = local_op
big_op = listkron(I_list)
return big_op.dot(state)
def random_psi(L):
"Haar-random state of L qubits"
Re, Im = np.random.randn(2, 2**L)
psi = Re + 1j * Im
return psi / np.sqrt(psi @ np.conj(psi))
def ptranspose(state, js, outshape):
""" Partial transpose of a state vector or density matrix.
Parameters:
state (array): 1D interpreted as state vector, 2D interpreted as density matrix
js (array-like): Qubit indicies to be transposed in front of remaining indicies
outshape (tuple): Shape of the returned array
Returns:
array: state wth js transposed to be the first indicies
"""
js = np.array(js)
D = len(state.shape)
L = int(np.log2(len(state)))
ds = [2] * (L * D)
mask = np.ones(L, dtype=bool)
mask[js] = False
rest = np.arange(L)[mask]
if D == 1: # state vector
ordering = list(js) + list(rest)
elif D == 2: # density matrix
ordering = list(js) + list(js+L) + list(rest) + list(rest + L)
return state.reshape(ds).transpose(ordering).reshape(outshape)
@njit
def _rdms_njit(state):
""" Partial trace helper for numba implementation"""
djs, drest = state.shape
RDM = np.zeros((djs, djs), dtype=np.complex128)
for i in range(djs):
for j in range(i, djs):
Rij = 0
for k in range(drest):
Rij += state[i, k] * np.conj(state[j, k])
RDM[i, j] = Rij
if i != j:
RDM[j, i] = np.conj(Rij)
return RDM
def rdms_numba(state, js):
""" Partial trace (numba implementation)
Parameters:
state (array): 1D interpreted as state vector, 2D interpreted as density matrix
js (array-like): Qubit indicies to keep
Returns:
array: state wth js kept and the remaining qubits traced-out."""
L = int(np.log2(len(state)))
js = np.array(js)
dims =np.array([2]*L)
mask = np.ones(L, dtype=bool)
mask[js] = False
rest = np.arange(L)[mask]
ordering = np.concatenate((js, rest))
djs = np.prod(dims.take(js))
drest = np.prod(dims.take(rest))
state = ptranspose(state, js, outshape=(djs, drest))
return _rdms_njit(state)
def rdms_tensordot(state, js):
""" Partial trace (tensordot implementation)
Parameters:
state (array): 1D interpreted as state vector, 2D interpreted as density matrix
js (array-like): Qubit indicies to keep
Returns:
array: state wth js kept and the remaining qubits traced-out."""
L = int(np.log2(len(state)))
js = np.array(js)
dims = np.array([2]*L)
mask = np.ones(L, dtype=bool)
mask[js] = False
rest = np.arange(L)[mask]
state = state.reshape(dims)
djs = np.prod(dims.take(js))
return np.tensordot(state, state.conj(), (rest, rest)).reshape((djs, djs))
def rdms_einsum(state, js):
""" Partial trace (einsum implementation)
Parameters:
state (array): 1D interpreted as state vector, 2D interpreted as density matrix
js (array-like): Qubit indicies to keep
Returns:
array: state wth js kept and the remaining qubits traced-out."""
L = int(np.log2(len(state)))
js = np.array(js)
dims = np.array([2]*L)
djs = np.prod(dims.take(js))
idx1 = [i for i in range(L)]
idx2 = [L+i if i in js else i for i in np.arange(L)]
state = state.reshape(dims)
rho = np.einsum(state, idx1, state.conj(), idx2, optimize=True)
return rho.reshape(djs, djs)
def rdmr(rho, js):
js = np.array(js)
L = np.int(np.log2(len(rho)))
rest = np.setdiff1d(np.arange(L), js)
shape = [2] * 2 * L
d1 = 2**len(js)
d2 = 2**len(rest)
perm = list(js) + list(js+L) + list(rest) + list(rest + L)
block = ptranspose(rho, js, outshape=(d1, d1, d2**2))
mask = np.cumsum((d2 + 1) * np.ones(d2), dtype=np.int32) - (d2 + 1)
rho = np.sum(block[:, :, mask], axis=2)
return rho
def rdm(state, js):
if len(state.shape) == 1:
if len(js) <= 3:
return rdms_numba(state, js)
else:
return rdms_tensordot(state, js)
elif len(state.shape) == 2:
return rdmr(state, js)
@njit
def traceout_outer_njit(rho, dim, mask):
rho_out = np.zeros((dim, dim), dtype=rho.dtype)
for kk in range(dim):
for jj in range(dim):
Rij = 0
for ll in mask:
Rij += rho[kk, jj, ll]
rho_out[kk, jj] = Rij
return rho_out
def traceout_first(rho, ld=2):
L = int(np.log2(len(rho)))
dim = ld ** (L - 1)
rho = np.reshape(
np.transpose(np.reshape(rho, (ld, dim, ld, dim)), (1, 3, 0, 2)),
(dim, dim, ld ** 2),
)
mask = np.cumsum((ld + 1) * (np.ones(ld, dtype=np.int))) - (ld + 1)
return traceout_outer_njit(rho, dim, mask)
def traceout_last(rho, ld=2):
L = int(np.log2(len(rho)))
dim = ld ** (L - 1)
rho = np.reshape(
np.transpose(np.reshape(rho, (dim, ld, dim, ld)), (0, 2, 1, 3)),
(dim, dim, ld ** 2),
)
mask = np.cumsum((ld + 1) * (np.ones(ld, dtype=np.int))) - (ld + 1)
return traceout_outer_njit(rho, dim, mask)
def make_U2(V):
if type(V) == np.ndarray:
return V
Vs_angs = V.split("_")
if len(Vs_angs) == 2:
Vs, angs = Vs_angs
angs = angs.split("-")
elif len(Vs_angs) == 1:
Vs, angs = Vs_angs[0], []
else:
raise ValueError(
"improper V configuration {}: need one angle for every P, R, and p".format(
V
)
)
ang_inds = [i for i, v in enumerate(Vs) if v in ("P", "R", "p")]
if len(angs) != len(ang_inds):
raise ValueError(
"improper V configuration {}: need one angle for every P, R, and p".format(
V
)
)
ang_id = 0
Vmat = np.eye(2, dtype=complex)
for v in Vs:
if v == "P":
ang = angs[ang_id]
ang_in_rad = string_deg2float_rad(ang)
Pmat = make_Pmat(ang_in_rad)
Vmat = Vmat.dot(Pmat)
ang_id += 1
elif v == "R":
ang = angs[ang_id]
ang_in_rad = string_deg2float_rad(ang)
Rmat = make_Rmat(ang_in_rad)
Vmat = Vmat.dot(Rmat)
ang_id += 1
elif v == "p":
ang = angs[ang_id]
ang_in_rad = string_deg2float_rad(ang)
global_phase = make_global_phase(ang_in_rad)
Vmat = global_phase * Vmat
ang_id += 1
else:
try:
Vmat = Vmat.dot(ops[v])
except KeyError:
raise ValueError("string op {} not understood".format(v))
return Vmat
def make_Pmat(ang_in_rad):
return np.array([[1.0, 0.0], [0.0, exp(1j * ang_in_rad)]], dtype=complex)
def make_Rmat(ang_in_rad):
return np.array(
[
[cos(ang_in_rad / 2), -sin(ang_in_rad / 2)],
[sin(ang_in_rad / 2), cos(ang_in_rad / 2)],
],
dtype=complex,
)
def make_global_phase(ang_in_rad):
return exp(1j * ang_in_rad)
def string_deg2float_rad(string_deg):
float_rad = eval(string_deg) * pi / 180.0
return float_rad
def dagger(rho):
return np.transpose(np.conj(rho))
def commute(A, B):
return A.dot(B) - B.dot(A)
def isU(U):
m, n = U.shape
Ud = dagger(U)
UdU = np.dot(Ud, U)
UUd = np.dot(U, Ud)
I = np.eye(n, dtype=complex)
if np.allclose(UdU, I):
if np.allclose(UUd, I):
return True
else:
return False
def isherm(rho):
if np.allclose(rho, dagger(rho)):
return True
return False
def issymm(mat):
matT = mat.T
if np.allclose(mat, matT):
return True
return False
def istrace_one(mat):
tr = np.trace(mat)
if tr == 1.0:
return True
return False
def ispos(mat):
evals = eigvals(mat)
if np.all(evals >= 0.0):
return True
return False
def listkron(matlist):
return reduce(lambda A, B: np.kron(A, B), matlist)
# def spmatkron(matlist):
# return sps.csc_matrix(reduce(lambda A, B: sps.kron(A, B, 'csc'), matlist))
def listdot(matlist):
return reduce(lambda A, B: np.dot(A, B), matlist)
def concat_dicts(d1, d2):
d = d1.copy()
d.update(d2)
return d
def listdicts(dictlist):
return reduce(lambda d1, d2: concat_dicts(d1, d2), dictlist)
def dec_to_bin(n, count):
return [n >> y & 1 for y in range(count - 1, -1, -1)]
def bin_to_dec(n):
return int("".join(list(map(str, n))), 2)
def tensor(A, B):
a_nrows, a_ncols = A.shape
b_nrows, b_ncols = B.shape
m_nrows, m_ncols = a_nrows * b_nrows, a_ncols * b_ncols
b = list(zip(B.row, B.col, B.data))
a = list(zip(A.row, A.col, A.data))
M = np.zeros((m_nrows, m_ncols))
for a_row, a_col, a_val in a:
for b_row, b_col, b_val in b:
row = a_row * b_nrows + b_row
col = a_col * a_ncols + b_col
M[(row, col)] = a_val * b_val
return M
if __name__ == "__main__":
import states as ss
L = 7
IC = "f0-3"
js = [2, 0, 3]
op = listkron([ops["X"]] * (len(js) - 1) + [ops["H"]])
print()
print("op = XXH,", "js = ", str(js) + ", ", "IC = ", IC)
print()
init_state3 = ss.make_state(L, IC)
init_rj = [rdms(init_state3, [j]) for j in range(L)]
init_Z_exp = [np.trace(r.dot(ops["Z"]).real) for r in init_rj]
init_Y_exp = [np.trace(r.dot(ops["Y"]).real) for r in init_rj]
print("initial Z exp vals:", init_Z_exp)
print("initial Y exp vals:", init_Y_exp)
final_state = op_on_state(op, js, init_state3)
final_rj = [rdms(final_state, [j]) for j in range(L)]
final_Z_exp = [np.trace(r.dot(ops["Z"])).real for r in final_rj]
final_Y_exp = [np.trace(r.dot(ops["Y"])).real for r in final_rj]
print("final Z exp vals:", final_Z_exp)
print("final Y exp vals:", final_Y_exp)