-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_do_calculation.py
361 lines (299 loc) · 18.2 KB
/
_do_calculation.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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
from _helper_functions import *
from _mbes import *
from numba import njit
def solve_mbes(mbes_func, init, pdf, tlistpump, tlistdecay, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#*args: function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args, ret), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution(solpump, init, pdf, tlistpump)
if tlistdecay is not None:
initdecay = solpump[-1,:].copy()
#make sure that the last six arguments correspond to the one defined in the mbes function in _mbes
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes), full_output=False)
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, sold_ret, infodict
else:
return solp_ret, infodict
def test_jac_2ndorder(jac_func, init, pdf, tlistpump, tlistdecay, gs, *args):
J = np.zeros((len(init.view(np.float64)), len(init.view(np.float64))))
args=(*args, gs, *create_sorting_indices(gs), J)
newf = njit(jac_func)
return newf(init.view(np.float64), 0, *args), newf, args
def solve_mbes_2ndorder(mbes_func, init, pdf, tlistpump, tlistdecay, gs, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#gs: coupling list
#spins: detuning spins from the cavity
#*args: additional function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
jacf = get_jacobian(mbes_func)
if jacf:
jacf = njit(jacf)
sortidx = create_sorting_indices(gs)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args, gs, *sortidx, ret), jac=jacf)
solp_ret = prepare_solution_2ndorder(solpump, init, pdf, tlistpump)
if tlistdecay is not None:
initdecay = solpump[-1,:].copy()
#make sure that the last six arguments correspond to the one defined in the mbes function in _mbes
soldecay, infodict2 = odeintz(mbes_2ndorder, initdecay, tlistdecay, args=(0., *args[-5:], gs, *sortidx, ret), jac=jacf, full_output=True)
sold_ret = prepare_solution_2ndorder(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, sold_ret, infodict
else:
return solp_ret, infodict
def setup_mbes(mbes_func, init, pdf, tlistpump, tlistdecay, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#*args: function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
jac = get_jacobian(mbes_func)
jac = None
problem_julia_func(mbes_func, np.asarray(init, dtype=np.complex128), tlistpump, tlistdecay, args=(*args, ret), jac=jac)
def setup_mbes_2ndorder(mbes_func, init, pdf, tlistpump, tlistdecay, gs, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#gs: coupling list
#spins: detuning spins from the cavity
#*args: additional function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
jac = get_jacobian(mbes_func)
jac = None
if not jac:
sorts = create_sorting_indices_small(gs)
else:
sorts = create_sorting_indices(gs)
problem_julia_func(mbes_func, init, tlistpump, tlistdecay, args=(*args, gs, *sorts, ret), jac=jac)
def test_mbes_2ndorder2(mbes_func, init, pdf, tlistpump, tlistdecay, gs, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#gs: coupling list
#spins: detuning spins from the cavity
#*args: additional function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
return problem_julia_func(mbes_func, init, tlistpump, tlistdecay, args=(*args, gs, *create_sorting_indices(gs), ret), jac=None)
def test_mbes_2ndorder(mbes_func, init, pdf, tlistpump, tlistdecay, gs, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#gs: coupling list
#spins: detuning spins from the cavity
#*args: additional function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
return mbes_func(0, init.view(np.complex128), *(*args, gs, *create_sorting_indices(gs), ret))
def test_mbes_2ndorder_julia(mbes_func, init, pdf, tlistpump, tlistdecay, gs, *args):
ret = np.empty(len(init), dtype=np.complex128).view(np.float64)
func = test_julia(mbes_func, init, tlistpump, tlistdecay, args=(*args, gs, *create_sorting_indices(gs), ret), jac=None)
Main.ret = ret
Main.init = init.view(np.float64)
Main.pars = (np.array([args[0]]), )
res = Main.eval("f(ret, init, pars, 0)")
return res
def solve_mbes_general(mbes_func, init, pdf, tlist, *args):
#most general version of solving mbes. Use for example if you have time dependent pump and detuning, where you can define the driving amplitude and the detuning of the spins accordingly
ret = np.empty(len(init), dtype=np.complex128)
solution, infodict = odeintz(mbes_func, init, tlist, args=(*args,ret), jac=get_jacobian(mbes_func))
solution_ret = prepare_solution(solution, init, pdf, tlist)
return solution_ret, infodict
def solve_mbes_only_decay(init, pdf, tlistdecay, *args):
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistdecay: timelist for which the decay is computed
#*args: function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
soldecay, infodict = odeintz(mbes, init, tlistdecay, args=(0, *args, ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, init, pdf, tlistdecay)
return sold_ret, infodict
def solve_mbes_without_cavity(mbes_func, init, pdf, tlistpump, tlistdecay, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#*args: function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args,ret), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution_no_cav(solpump, init, pdf, tlistpump)
if tlistdecay is not None:
initdecay = solpump[-1,:].copy()
#make sure that the last six arguments correspond to the one defined in the mbes function in _mbes
soldecay = odeintz(mbes_no_cavity, initdecay, tlistdecay, args=(*args[-5:], ret), jac=get_jacobian(mbes_no_cavity))
sold_ret = prepare_solution_no_cav(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, sold_ret, infodict
else:
return solp_ret, infodict
def solve_mbes_wait(mbes_func, init, pdf, tlistinit, tlistprobe, tlistdecay, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistinit: timelist before the pulse is played
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#*args: function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
initinter = init.copy()
solinter = odeintz(mbes, initinter, tlistinit, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
soli_ret = prepare_solution(solinter, initinter, pdf, tlistinit)
initprobe = solinter[-1,:].copy()
solprobe, infodict = odeintz(mbes_func, initprobe, tlistprobe, args=(*args,ret), jac=get_jacobian(mbes_func))
solpr_ret = prepare_solution(solprobe, initprobe, pdf, tlistprobe)
if tlistdecay is not None:
initdecay = solprobe[-1,:].copy()
#make sure that the last six arguments correspond to the one defined in the mbes function in _mbes
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return soli_ret, solpr_ret, sold_ret, infodict
else:
return soli_ret, solpr_ret, infodict
def solve_mbes_cpmg(mbes_func, init, pdf, tlistpump, tlistinter, tlistdecay, *args):
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args, ret), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution(solpump, init, pdf, tlistpump)
initinter = solpump[-1,:].copy()
solinter = odeintz(mbes, initinter, tlistinter, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
soli_ret = prepare_solution(solinter, initinter, pdf, tlistinter)
initpump2 = solinter[-1,:].copy()
solpump2 = odeintz(mbes, initpump2, tlistpump, args=(-1j*args[0]*2, *args[-6:], ret), jac=get_jacobian(mbes))
solp2_ret = prepare_solution(solpump2, initpump2, pdf, tlistpump)
initdecay = solpump2[-1,:].copy()
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, soli_ret, solp2_ret, sold_ret, infodict
def solve_mbes_cpmg_time(mbes_func, init, pdf, tlistpump, tlistinter, tlistdecay, *args):
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args, ret), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution(solpump, init, pdf, tlistpump)
initinter = solpump[-1,:].copy()
solinter = odeintz(mbes, initinter, tlistinter, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
soli_ret = prepare_solution(solinter, initinter, pdf, tlistinter)
initpump2 = solinter[-1,:].copy()
dt = tlistpump[1]-tlistpump[0]
tlistpump2 = np.arange(0, 2*tlistpump[-1]+dt, dt)
solpump2 = odeintz(mbes, initpump2, tlistpump2, args=(-1j*args[0], *args[-6:], ret), jac=get_jacobian(mbes))
solp2_ret = prepare_solution(solpump2, initpump2, pdf, tlistpump2)
initdecay = solpump2[-1,:].copy()
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, soli_ret, solp2_ret, sold_ret, infodict
def solve_hahn_cpmg(mbes_func, init, pdf, tlistpump, tlistinter, tlistdecay, *args):
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args,ret ), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution(solpump, init, pdf, tlistpump)
initinter = solpump[-1,:].copy()
solinter = odeintz(mbes, initinter, tlistinter, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
soli_ret = prepare_solution(solinter, initinter, pdf, tlistinter)
initpump2 = solinter[-1,:].copy()
solpump2 = odeintz(mbes, initpump2, tlistpump, args=(args[0]*2, *args[-6:], ret), jac=get_jacobian(mbes))
solp2_ret = prepare_solution(solpump2, initpump2, pdf, tlistpump)
initdecay = solpump2[-1,:].copy()
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, soli_ret, solp2_ret, sold_ret, infodict
def solve_mbes_hahn_time(mbes_func, init, pdf, tlistpump, tlistinter, tlistdecay, *args):
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args, ret), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution(solpump, init, pdf, tlistpump)
initinter = solpump[-1,:].copy()
solinter = odeintz(mbes, initinter, tlistinter, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
soli_ret = prepare_solution(solinter, initinter, pdf, tlistinter)
initpump2 = solinter[-1,:].copy()
dt = tlistpump[1]-tlistpump[0]
tlistpump2 = np.arange(0, 2*tlistpump[-1]+dt, dt)
solpump2 = odeintz(mbes, initpump2, tlistpump2, args=(args[0], *args[-6:], ret), jac=get_jacobian(mbes))
solp2_ret = prepare_solution(solpump2, initpump2, pdf, tlistpump2)
initdecay = solpump2[-1,:].copy()
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, soli_ret, solp2_ret, sold_ret, infodict
def solve_mbes_pump_probe(mbes_func, init, pdf, tlistpump, tlistinter, tlistprobe, tlistdecay, ampl_probe, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistinter: timelist between the two pulses
#tlistprobe: timelist for which the probe pulse is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#*args: function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args, ret), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution(solpump, init, pdf, tlistpump)
initinter = solpump[-1,:].copy()
solinter = odeintz(mbes, initinter, tlistinter, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
soli_ret = prepare_solution(solinter, initinter, pdf, tlistinter)
initprobe = solinter[-1,:].copy()
solprobe = odeintz(mbes, initprobe, tlistprobe, args=(ampl_probe, *args[-6:], ret), jac=get_jacobian(mbes))
solpr_ret = prepare_solution(solprobe, initprobe, pdf, tlistprobe)
if tlistdecay is not None:
initdecay = solprobe[-1,:].copy()
#make sure that the last six arguments correspond to the one defined in the mbes function in _mbes
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, soli_ret, solpr_ret, sold_ret, infodict
else:
return solp_ret, soli_ret, solpr_ret, infodict
def get_pulses_from_cavity_field(a, af, sm, tlist, tlistf, kappa, deltac, gs):
dtf = tlistf[1]-tlistf[0]
dt = tlist[1]-tlist[0]
dadtf = np.gradient(af, dtf)
dadt = dadtf[::int(dt/dtf)]
sumspins = np.array([sm[i,:]*gs for i in range(len(sm))])
imiq = dadt+kappa*a+1j*deltac*a-np.sum(sumspins, axis=1)
ipulse = imiq.real
qpulse = imiq.imag
return ipulse, qpulse
def solve_obes(obes_func, init, pdf, tlistpump, tlistdecay, *args):
#mbes_func: MBE functions to solve (defined in _mbes)
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#tlistdecay: timelist for which the decay is computed (keep None if you don't want any decay)
#*args: function arguments to pass to the MBE functions
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(obes_func, init, tlistpump, args=(*args,ret ), jac=get_jacobian(obes_func))
solp_ret = prepare_solution_no_cav(solpump, init, pdf, tlistpump)
if tlistdecay is not None:
initdecay = solpump[-1,:].copy()
#make sure that the last six arguments correspond to the one defined in the mbes function in _mbes
soldecay = odeintz(obes, initdecay, tlistdecay, args=(0, *args[-4:], ret), jac=get_jacobian(obes))
sold_ret = prepare_solution_no_cav(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, sold_ret, infodict
else:
return solp_ret, infodict
def solve_mbes_floquet(mbes_func, init, pdf, tlistpump, tlistdecay, *args):
#init: inital state
#pdf: pdf for spins, necessary for normalizing the solutions
#tlistpump: timelist for which the pump tone is turned on
#*args: function arguments to pass to the MBE functions
#reshape idx is for if we have a a g_list
ret = np.empty(len(init), dtype=np.complex128)
solpump, infodict = odeintz(mbes_func, init, tlistpump, args=(*args, ret), jac=get_jacobian(mbes_func))
solp_ret = prepare_solution(solpump, init, pdf, tlistpump)
if tlistdecay is not None:
initdecay = solpump[-1,:].copy()
#make sure that the last six arguments correspond to the one defined in the mbes function in _mbes
soldecay = odeintz(mbes, initdecay, tlistdecay, args=(0, *args[-6:], ret), jac=get_jacobian(mbes))
sold_ret = prepare_solution(soldecay, initdecay, pdf, tlistdecay)
return solp_ret, sold_ret, infodict
else:
return solp_ret, infodict
return solp_ret, infodict