-
Notifications
You must be signed in to change notification settings - Fork 0
/
adiabat.py
346 lines (267 loc) · 10.8 KB
/
adiabat.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
import qutip as qt
import matplotlib.pyplot as pl
import matplotlib as mpl
import numpy as np
import datetime
import os
import itertools
import time
from scipy.optimize import curve_fit
import sys
''' Call must set two of the arrays to the vec desired and leave other two '''
def wigner4d(rho, xvec):
if(rho.type == 'ket'):
rho = qt.ket2dm(rho)
elif(rho.type is not 'oper'):
print('invalid type')
num_points = len(xvec)
N = rho.shape[0]
I = qt.qeye(N)
a = qt.tensor(qt.destroy(N), qt.qeye(N))
b = qt.tensor(qt.qeye(N), qt.destroy(N))
PJ = (1j * np.pi * a.dag() * a).expm() * (1j * np.pi * b.dag() * b).expm()
W = np.zeros((num_points, num_points), dtype = complex)
for i, j in itertools.product(range(num_points), range(num_points)):
print(i)
DA = qt.tensor(qt.displace(N, xvec[i]), I)
DB = qt.tensor(I, qt.displace(N, xvec[j]))
W[i][j] = (rho * DA * DB * PJ * DB.dag() * DA.dag()).tr()
return np.real(W)
def c_quad(t, args):
t += args['start_time']
if isinstance(t, float):
return (max(1.0-args['v'] * t, 0.0))**2
else:
return (np.maximum(1.0-args['v'] * t, np.zeros(len(t))))**2
''' Testing if middle state is steady state '''
#def c_linear(t, args):
# if isinstance(t, float):
# return (max(.15-args['v'] * t, 0.0))**2
# else:
# return np.power(np.maximum(.15-args['v'] * t, np.zeros(len(t))), 2)
def make_plots(plot_num, num_steps, times, states, psi0, psi_final, N, plot_filepath):
start_fidelity = np.zeros(num_steps)
final_fidelity = np.zeros_like(start_fidelity)
for i in range(num_steps):
start_fidelity[i] = round(qt.fidelity(states[i], psi0), 4)
final_fidelity[i] = round(qt.fidelity(states[i], psi_final), 4)
fit_start = 20
y_fit = final_fidelity[fit_start:]
x_fit = times[fit_start:]
''' Fidelity with starting cat state '''
fig = pl.figure(figsize=(10,15))
pl.subplot(3, 1, 1)
pl.plot(times, start_fidelity, '.')
pl.title('Fidelity with starting cat state')
''' Fidelity with final cat state '''
pl.subplot(3, 1, 2)
pl.plot(times, final_fidelity, '.')
try:
popt, pcov = curve_fit(lambda x_fit,a_fit,b_fit,c_fit : a_fit * np.exp(b_fit * x_fit) + c_fit, x_fit, y_fit, p0 = (-1, -.1, 0))
a_fit,b_fit,c_fit = popt
print(a_fit, b_fit, c_fit)
pl.plot(x_fit, a_fit * np.exp(b_fit * x_fit) + c_fit)
pl.title(str(a_fit) + ',' + str(b_fit) + ',' + str(c_fit))
except:
print('fit didnt work')
p = (1j * np.pi * (a.dag() * a + b.dag() * b)).expm()
parity = np.zeros(num_steps)
for i in range(num_steps):
parity[i] = round(qt.expect(p, states[i]), 4)
pl.subplot(3, 1, 3)
pl.plot(times, parity)
pl.title('joint parity')
pl.savefig(plot_filepath + 'Fidelity' + str(plot_num) + '.png')
pl.clf()
np.savetxt(plot_filepath + 'fidelity' + str(plot_num) + '.txt', [times, final_fidelity])
print('building cat array')
beta_steps = 50
cat_state_arr = []
beta_arr = np.linspace(0, alpha, beta_steps)
for j in range(beta_steps):
cat_state_arr.append((np.round(np.cos(theta/2), 5) * qt.tensor(qt.coherent(N, alpha-beta_arr[j]),
qt.coherent(N, beta_arr[j]))
+ np.round(np.sin(theta/2), 5) * np.exp(1j * phi) * qt.tensor(qt.coherent(N, -alpha+beta_arr[j]),
qt.coherent(N, -beta_arr[j]))).unit())
print('calculating 2d fidelity')
fidelity_arr = np.zeros((num_steps, beta_steps))
for i in range(num_steps):
#print(i)
for j in range(beta_steps):
fidelity_arr[i][j] = round(qt.fidelity(states[i], cat_state_arr[j]), 4)
pl.subplot(3,1,1)
pl.imshow(fidelity_arr.transpose(), extent = (0, max_time, beta_arr[-1]/alpha, beta_arr[0]/alpha))
pl.colorbar()
pl.ylabel('beta/alpha')
pl.grid(True)
pl.subplot(3,1,2)
pl.plot(times, beta_arr[np.argmax(fidelity_arr, axis=1)]/alpha, label='current state')
pl.plot(times, np.sqrt(c_quad(times, args)), label='steady state')
pl.ylabel('beta/alpha')
pl.legend()
pl.grid(True)
pl.subplot(3,1,3)
pl.plot(times, np.max(fidelity_arr, axis=1))
pl.ylabel('fidelity')
pl.xlabel('time')
pl.tight_layout()
pl.grid(True)
pl.savefig(plot_filepath + '2d_fidelity' + str(plot_num) + '.png')
pl.clf()
fig = pl.figure(figsize=(5 * num_steps, 10))
for i in range(num_steps):
''' Cavity A occupation '''
pl.subplot(4, num_steps, i+1)
pl.plot(range(N), states[i].ptrace(0).diag())
pl.title('Cavity A occupation')
''' Cavity A occupation '''
pl.subplot(4, num_steps, i+1 + num_steps)
pl.plot(range(N), states[i].ptrace(1).diag())
pl.title('Cavity B occupation')
pl.savefig(plot_filepath + 'Occupation' + str(plot_num) + '.png')
pl.clf()
num_points = 40
xvec_2d = np.linspace(-6, 6, num_points)
fig = pl.figure(figsize=(2.5 * num_steps, 10))
for i in range(num_steps):
''' Cavity A wigner '''
pl.subplot(4, num_steps, i+1)
W = qt.wigner(states[i].ptrace(0), xvec_2d, xvec_2d) * np.pi
pl.contourf(xvec_2d, xvec_2d, W, np.linspace(-1.0, 1.0, 41, endpoint=True), cmap=mpl.cm.RdBu_r)
pl.title('Cavity A wigner')
pl.colorbar(ticks = np.linspace(-1.0, 1.0, 11, endpoint=True))
''' Cavity B wigner '''
pl.subplot(4, num_steps, i+1 + num_steps)
W = qt.wigner(states[i].ptrace(1), xvec_2d, xvec_2d) * np.pi
pl.contourf(xvec_2d, xvec_2d, W, np.linspace(-1.0, 1.0, 41, endpoint=True), cmap=mpl.cm.RdBu_r)
pl.title('Cavity B wigner')
pl.colorbar(ticks = np.linspace(-1.0, 1.0, 11, endpoint=True))
pl.savefig(plot_filepath + '2d_wigner' + str(plot_num) + '.png')
pl.clf()
#num_points = 5
#xvec = np.linspace(-4, 4, num_points)
#t_start = time.time()
#
#fig = pl.figure(figsize=(5 * num_steps, 5))
#for n in range(num_steps):
# ''' reA vs reB '''
# pl.subplot(1, num_steps, n+1)
# W = wigner4d(states[n], xvec)
# pl.contourf(xvec, xvec, W, np.linspace(-1.0, 1.0, 41, endpoint=True), cmap=mpl.cm.RdBu_r)
# pl.title('reA vs reB t=' + str(times[n]))
# pl.colorbar(ticks = np.linspace(-1.0, 1.0, 11, endpoint=True))
#
#print(str(num_steps) + ' plots created in ' + str(time.time()-t_start) + ' sec')
#
#pl.savefig(plot_filepath + 'ReRe_wigner_cuts' + str(plot_num) + '.png')
#pl.clf()
pl.close('all')
print(sys.argv)
#theta = float(sys.argv[1]) * np.pi
#phi = float(sys.argv[2]) * np.pi
#lam = complex(sys.argv[3])
#gamma = float(sys.argv[4])
#v = float(sys.argv[5])
theta = np.pi / 2
phi = 0
lam = -5j
gamma = 10
v = .1
alpha = np.sqrt(-2j * lam.conjugate())
''' Parameters '''
N = int(round(np.absolute(alpha)**2))
while qt.coherent(N, alpha).full()[-1]/np.max(qt.coherent(N, alpha).full()) > .01:
N+=1
print('N=', N)
loss = 0.
joint_loss = 1
eps1 = lam
kappa1 = 1
eps2 = gamma * eps1
kappa2 = gamma
''' Solver time steps '''
num_steps = 40
max_time = 1.0/v
times = np.linspace(0.0, max_time, num_steps, endpoint=True)
break_points = [0, 2, 20, num_steps]
''' Initial condition '''
#alpha = 0
#beta = np.sqrt(-2j * lam.conjugate())
''' Testing middle state is steady state '''
beta = alpha
psi0 = (np.round(np.cos(theta/2), 5) * qt.tensor(qt.coherent(N, alpha-beta),
qt.coherent(N, beta))
+ np.round(np.sin(theta/2), 5) * np.exp(1j * phi) * qt.tensor(qt.coherent(N, -alpha+beta),
qt.coherent(N, -beta))).unit()
''' guess at final condition '''
beta = 0
psi_final = (np.round(np.cos(theta/2), 5) * qt.tensor(qt.coherent(N, alpha-beta), qt.coherent(N, beta))
+ np.round(np.sin(theta/2), 5) * np.exp(1j * phi) * qt.tensor(qt.coherent(N, -alpha+beta), qt.coherent(N, -beta))).unit()
''' Setup operators '''
a = qt.tensor(qt.destroy(N), qt.qeye(N))
b = qt.tensor(qt.qeye(N), qt.destroy(N))
drive_term = (a + b) **2
confinment_term = b ** 2
loss_ops = [kappa1**.5 * drive_term, kappa2**.5 * confinment_term]
H = [eps1 * drive_term + eps1.conjugate() * drive_term.dag(),
[eps2 * confinment_term + eps2.conjugate() * confinment_term.dag(), c_quad]]
date = list(str(datetime.datetime.now())[:19])
date[13] = '-'
date[16] = '-'
''' SUPER IMPORTANT: change the filepath to wherever you want the plots saved '''
qutip_filepath = 'C:/Users/Wang Lab/Documents/qutip/'
plot_filepath = qutip_filepath + 'out/adiabat/' + ''.join(date) + '/'
data_filepath = qutip_filepath + 'data/'
if not os.path.exists(plot_filepath):
os.makedirs(plot_filepath)
if not os.path.exists(data_filepath):
os.makedirs(data_filepath)
''' Saving textfile with information about the run '''
np.savetxt(plot_filepath + 'header.txt', [0],
header =
'N = ' + str(N) +
'\n lam = ' + str(lam) +
'\n gamma = ' + str(gamma) +
'\n loss = ' + str(loss) +
'\n joint_loss = ' + str(joint_loss) +
'\n alpha = ' + str(alpha) +
'\n beta = ' + str(beta) +
'\n theta = ' + str(theta) +
'\n phi = ' + str(phi) +
'\n v = ' + str(v) +
'\n num_steps = ' + str(num_steps) +
'\n max_time = ' + str(max_time))
''' Solve the system or load from save'''
args = {'v': v, 'start_time':0.0}
if 1:
print('solving...')
opts = qt.Options(store_states=True, nsteps=100000)
psi_current = psi0
states = []
for i in range(1, len(break_points)):
print('sub_simulation #' + str(i))
args['start_time'] = times[break_points[i-1]]
result = qt.mesolve(H, psi_current, times[break_points[i-1]:break_points[i]]- times[break_points[i-1]]
, loss_ops, [a.dag() * a, b.dag() * b],
options=opts, progress_bar = True, args = args)
states += result.states
print('solved!')
psi_current = states[-1]
print('plotting')
plot_num = i
args['start_time'] = 0.0
if i == len(break_points)-1:
plot_filepath += 'full/'
os.makedirs(plot_filepath)
make_plots(plot_num, break_points[i], times[0:break_points[i]], states, psi0, psi_final, N, plot_filepath)
qt.fileio.qsave(result, name = data_filepath + 'result' + str(plot_num))
else:
print('loading result')
try:
states = []
for i in range(1, len(break_points)):
result = qt.fileio.qload(data_filepath + 'result' + str(plot_num))
states += result.states
except:
print('result needs to be solved')
make_plots(0, num_steps, times, states, psi0, psi_final, N, plot_filepath)