forked from tabakg/quantum_state_diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_quantum_trajectory.py
271 lines (216 loc) · 7.84 KB
/
make_quantum_trajectory.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
#!/usr/bin/env python
'''
Make Quantum Trajectory
Author: Gil Tabak
Date: Nov 3, 2016
Generating trajectories using quantum state diffusion. We will be primairly
interested in the absorptive bistability (Jaynes Cummings model)
I store trajectory files as *.pkl files or *.mat files. This way I can easily
load them into another notebook, or load the trajectories to matlab.
Requires Python 3.
'''
from qnet.algebra.operator_algebra import *
from qnet.algebra.circuit_algebra import *
import qnet.algebra.state_algebra as sa
from quantum_state_diffusion import qsd_solve
from utils import (
make_nparams,
save2mat,
save2pkl,
print_params
)
import sdeint
import argparse
import numpy as np
import numpy.linalg as la
import logging
import os
import pickle
from scipy import sparse
from sympy import sqrt
import sys
# Log everything to stdout
logging.basicConfig(stream=sys.stdout,level=logging.DEBUG)
def get_parser():
'''get_parser returns the arg parse object, for use by an external application (and this script)
'''
parser = argparse.ArgumentParser(
description="generating trajectories using quantum state diffusion")
################################################################################
# Simulation Parameters
################################################################################
# Seed
parser.add_argument("--seed",
dest='seed',
help="Seed to set for the simulation.",
type=int,
default=1)
# Number of trajectories
parser.add_argument("--ntraj",
dest='ntraj',
help="number of trajectories, should be kept at 1 if run via slurm",
type=int,
default=1)
# Duration
parser.add_argument("--duration",
dest='duration',
help="Duration (iterations = duration / divided by delta_t)",
type=int,
default=10)
# Delta T
parser.add_argument("--delta_t",
dest='deltat',
help="Parameter delta_t",
type=float,
default=2e-3)
# Nfock_a
parser.add_argument("--Nfock_a",
dest='nfocka',
help="Parameter N_focka",
type=int,
default=50)
# Nfock_j
parser.add_argument("--Nfock_j",
dest='nfockj',
help="Parameter N_fockj",
type=int,
default=2)
# How much to downsample results
parser.add_argument("--downsample",
dest='downsample',
help="How much to downsample results",
type=int,
default=100)
################################################################################
# Output Variables
################################################################################
# Does the user want to quiet output?
parser.add_argument("--quiet",
dest='quiet',
action="store_true",
help="Turn off logging (debug and info)",
default=False)
# Does the user want to quiet output?
parser.add_argument("--output_dir",
dest='outdir',
type=str,
help="Output folder. If not defined, will use PWD.",
default=None)
# Save to pickle?
parser.add_argument("--save2pkl",
dest='save2pkl',
action="store_true",
help="Save pickle file to --output_dir",
default=False)
# Save to mat?
parser.add_argument("--save2mat",
dest='save2mat',
action="store_true",
help="Save .mat file to --output_dir",
default=False)
return parser
def main():
parser = get_parser()
try:
args = parser.parse_args()
except:
sys.exit(0)
# Set up commands from parser
params = dict()
ntraj = params['Ntraj'] = args.ntraj
seed = params['seed'] = args.seed
duration = params['duration'] = args.duration
delta_t = params['delta_t'] = args.deltat
Nfock_a = params['Nfock_a'] = args.nfocka
Nfock_j = params['Nfock_j'] = args.nfockj
downsample = params['downsample'] = args.downsample
# Does the user want to print verbose output?
quiet = args.quiet
if not quiet:
print_params(params=params)
# How much to downsample results
logging.info("Downsample set to %s",downsample)
## Names of files and output
Regime = "absorptive_bistable"
param_str = "%s-%s-%s-%s-%s-%s" %(seed,ntraj,delta_t,Nfock_a,Nfock_j,duration)
outdir = ""
if args.outdir != None:
outdir = args.outdir
file_name = '%s/QSD_%s_%s' %(outdir,Regime,param_str)
# Saving options
save_mat = args.save2mat
save_pkl = args.save2pkl
if save_mat == False and save_pkl == False:
logging.warning("Both pickle and mat save are disabled, no data will be saved.")
logging.warning("You can modify this with args --save2pkl and --save2mat")
# ## Make Operators
a = Destroy(1)
ad = a.dag()
sm = LocalSigma(2, 1,0)/sqrt(2)
sp = sm.dag()
sz = sp*sm - sm*sp
j = Jminus(2)
jp = j.dag()
jz = Jz(2)
jx = (jp + j) / 2.
jy = (jp - j) / 2.
# ## Make SLH Model
k,g0,g = symbols("kappa, g0,gamma", positive=True)
DD, TT = symbols("Delta, Theta", real=True)
W = symbols("Omega")
L = [sqrt(k)*a,
sqrt(g)*j]
H = -I*g0*(a*jp - ad * j) + DD*jz + TT*ad*a
S = identity_matrix(2)
slh = SLH(S, L, H).coherent_input(W,0)
slh
## Numerical parameters
a.space.dimension = Nfock_a
j.space.dimension = Nfock_j
if Regime == "absorptive_bistable":
logging.info("Regime is set to %s", Regime)
nparams = make_nparams(W=W,k=k,g=g,g0=g0,DD=DD,TT=TT)
else:
logging.error("Unknown regime, %s, or not implemented yet.", Regime)
raise ValueError("Unknown regime, or not implemented yet.")
Hq, Lqs = slh.substitute(nparams).HL_to_qutip()
## Observables
obs = (a, j, jz, a*a, a.dag()*a, a*jp, jp, jx, jy)
obsq = [o.to_qutip(full_space=slh.space) for o in obs]
tspan = np.arange(0,duration,delta_t)
psi0 = qutip.tensor(qutip.basis(Nfock_a,0),qutip.basis(Nfock_j,0)).data
H = Hq.data
Ls = [Lq.data for Lq in Lqs]
obsq = [ob.data for ob in obsq]
### Run simulation
D = qsd_solve(H=H,
psi0=psi0,
tspan=tspan,
Ls=Ls,
sdeint_method=sdeint.itoEuler,
obsq = obsq,
ntraj = ntraj,
seed = seed,
normalize_state=True)
### include time in results
D.update({'tspan':tspan})
### downsample
D_downsampled = {'psis' : D['psis'][:,::downsample],
'obsq_expects' : D['obsq_expects'][:,::downsample],
'seeds' : D['seeds'],
'tspan' : D['tspan'][::downsample] }
### Save results
if save_mat:
logging.info("Saving mat file...")
save2mat(data=D_downsampled,
file_name=file_name,
obs=obs,
params=params)
if save_pkl:
logging.info("Saving pickle file...")
save2pkl(data=D_downsampled,
file_name=file_name,
obs=obs,
params=params)
if __name__ == '__main__':
main()