-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoverlaps_classical.py
312 lines (232 loc) · 6.88 KB
/
overlaps_classical.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
import numpy as np
from scipy.linalg import eig
import scipy.sparse.linalg.eigen.arpack as arp
import cirq
from ncon import ncon
from circuits import StateAnsatzRepeatedXZ
def classically_find_env(U_params,method='Nelder-Mead', testing=False):
'''
Find env params classically
'''
from tracedistance import TraceDistanceAnalytic
from functools import partial
from scipy.optimize import minimize
Q = cirq.LineQubit.range(4)
initial_guess = U_params
cf = lambda x : TraceDistanceAnalytic(Th=U_params, Psi=x, Q=Q)
env_params = minimize(
cf,
method = method,
x0=initial_guess,
tol=1e-15,
options={
'ftol':1e-15
}
)
if env_params['success'] == False:
print('Failed to converge on env')
if testing:
return env_params
return env_params['x']
#############################################################################
# Find the exact overlap classically.
#############################################################################
# Note that tensors by default have the shape
# A.shape = (i, j, k)
#
# i -- A -- K
# |
# j
def unitary_to_tensor(U):
'''
Take a unitary U and make it a tensor A such that
j ---| |=== k
| U |
i ===| |---|0>
A.shape = (i, j, k)
i == A == k
|
j
'''
n = int(np.log2(U.shape[0]))
zero = np.array([1., 0.])
Ucontr = list(range(-1, -2*n, -1)) + [1] # contraction string for ncon
A = ncon([U.reshape(*2 * n * [2]), zero], [Ucontr, [1,]])
return A.reshape(2**(n-1), 2, 2**(n-1))
def unitary_to_tensor_v2(U):
'''
Take a unitary U and make it a tensor A such that
|0> k
| |
| | |
---U--- | direction of unitary
| | |
| | v
i j
A.shape = (i, j, k)
i == A == k
|
j
'''
n = int(np.log2(U.shape[0]))
zero = np.array([1., 0.])
Ucontr = list(range(-1, -n-1, -1)) + [1] + list(range(-n-1, -2*n, -1))
A = ncon([U.reshape(*2 * n *[2]), zero], [Ucontr, [1,]])
A = A.reshape(2**(n-1), 2, 2**(n-1))
return A
def map_AB(A, B):
'''
Combine A, B as follows
i -- A -- j , k -- B -- l
| |
=
i -- A -- j
|
k -- B -- l
where the shape of the output is (i*k, j*l)
'''
i, _, j = A.shape
k, _, l = B.shape
return np.einsum('inj, knl -> ikjl', A, B.conj()).reshape(i*k, j*l)
def right_fixed_point(E, all_evals=False):
'''
Calculate the right fixed point of a transfer matrix E
E.shape = (N, N)
'''
evals, evecs = eig(E)
sort = sorted(zip(evals, evecs), key=lambda x: np.linalg.norm(x[0]),
reverse=True)
# Look into `scipy.sparse.linalg.eigs, may be faster`
if all_evals:
mu, r = list(zip(*sort))
return np.array(mu), np.array(r)
mu, r = sort[0]
return mu, r
def state_params_to_unitary(θ):
'''
Convert state params for qubit to two qubit unitary.
'''
from circuits import StateAnsatz
Q = cirq.LineQubit.range(2)
return cirq.unitary(StateAnsatz(θ).on(*Q))
def exact_overlap(θ_A, θ_B, ab=True, all_evals=False, Ansatz=StateAnsatzRepeatedXZ):
'''
Calculate the overlap classically i.e. abs of max eigenvalue of transfer
matrix
'''
Q = cirq.LineQubit.range(2)
U = cirq.unitary(
Ansatz(θ_A).on(*Q) # 2 qubit circuit
)
U_prime = cirq.unitary(
Ansatz(θ_B).on(*Q)
)
A = unitary_to_tensor_v2(U)
B = unitary_to_tensor_v2(U_prime)
E = map_AB(A, B)
mu, r = right_fixed_point(E, all_evals=all_evals)
if ab:
return np.abs(mu)
return mu
def exact_overlap_sigmaZ(θ_A, θ_B, Ansatz=StateAnsatzRepeatedXZ):
Q = cirq.LineQubit.range(2)
U = cirq.unitary(
Ansatz(θ_A).on(*Q) # 2 qubit circuit
)
U_prime = cirq.unitary(
Ansatz(θ_B).on(*Q)
)
A = unitary_to_tensor_v2(U)
B = unitary_to_tensor_v2(U_prime)
'''
A.shape = (i, j, k)
i -- A -- k
|
j
'''
I = np.eye(2, 2)
sigma_z = np.array([[1., 0.],
[0., -1.]])
E = ncon([A, np.conj(A)], [[-1, 1, -3], [-2, 1, -4]])
Eprime = ncon([A, np.conj(A)], [[-2, 1, -4], [-1, 1, -3]])
E = E.reshape(4, 4)
Eprime = Eprime.reshape(4, 4)
Lambda, R = arp.eigs(E, k=1, which='LM')
R = R.reshape([2, 2])
trR = np.trace(R)
R = R / trR
sigz = ncon([I, A, sigma_z, np.conj(A), R], [[1, 4], [1, 2, 3], [2,5], [4, 5, 6], [6, 3]])
return sigz
def exact_overlap_sigmaX(θ_A, θ_B, ab=True, all_evals=False, Ansatz=StateAnsatzRepeatedXZ):
Q = cirq.LineQubit.range(2)
U = cirq.unitary(
Ansatz(θ_A).on(*Q) # 2 qubit circuit
)
U_prime = cirq.unitary(
Ansatz(θ_B).on(*Q)
)
A = unitary_to_tensor_v2(U)
B = unitary_to_tensor_v2(U_prime)
I = np.eye(2, 2)
sigma_x = np.array([[0., 1.],
[1., 0.]])
E = ncon([A, B], [[-1, 1, -3], [-2, 1, -4]])
E = E.reshape(4, 4)
Lambda, R = arp.eigs(E, k=1, which='LM')
R = R.reshape(2, 2)
trR = np.trace(R)
R = R / trR
sigx = ncon([I, A, sigma_x, np.conj(B), R], [[1, 4], [1, 2, 3], [2,5], [4, 5, 6], [3, 6]])
return sigx
def exact_overlap_sigmaY(θ_A, θ_B, ab=True, all_evals=False, Ansatz=StateAnsatzRepeatedXZ):
Q = cirq.LineQubit.range(2)
U = cirq.unitary(
Ansatz(θ_A).on(*Q) # 2 qubit circuit
)
U_prime = cirq.unitary(
Ansatz(θ_B).on(*Q)
)
A = unitary_to_tensor_v2(U)
B = unitary_to_tensor_v2(U_prime)
I = np.eye(2, 2)
sigma_y = np.array([[0., -1.j],
[1.j, 0.]])
E = ncon([A, B], [[-1, 1, -3], [-2, 1, -4]])
E = E.reshape(4, 4)
Lambda, R = arp.eigs(E, k=1, which='LM')
R = R.reshape(2, 2)
trR = np.trace(R)
R = R / trR
sigy = ncon([I, A, sigma_y, np.conj(B), R], [[1, 4], [1, 2, 3], [2,5], [4, 5, 6], [3, 6]])
return sigy
def merge(A, B): # TODO Write test for this
'''
Merge tensors A, B such that
-- A -- B --
| |
'''
ai, aj, ak = A.shape
bi, bj, bk = B.shape
return np.einsum('ijk, klm', A, B).reshape(ai, aj*bj, bk)
def exact_n_overlaps(θ_A, θ_B, n_range, ab=True, all_evals=False):
from circuits import StateAnsatz
Q = cirq.LineQubit.range(2)
U = cirq.unitary(
StateAnsatz(θ_A).on(Q[-1], Q[-2]) # 2 qubit circuit
)
U_prime = cirq.unitary(
StateAnsatz(θ_B).on(Q[-1], Q[-2])
)
A = unitary_to_tensor(U)
B = unitary_to_tensor(U_prime)
for n in n_range:
pass
if __name__=="__main__":
from circuits import ShallowFullStateAnsatz
np.random.seed(0)
p0 = np.random.randn(15)
p1 = np.random.randn(15)
print('p0: ', p0)
print('p1: ', p1)
ov = exact_overlap(p0, p1, Ansatz=ShallowFullStateAnsatz) ** 2
print(ov)