forked from tabakg/quantum_state_diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
103 lines (84 loc) · 3.3 KB
/
utils.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
from scipy.io import savemat
import numpy as np
import pickle
import logging
def print_params(params):
'''print params will print a dictioary of parameters to the screen for the user
:param params: the dictionary of parameters
'''
for key,value in params.items():
logging.info("Parameter %s set to %s",key,value)
def save2mat(data, file_name, obs, params=None):
''' uses scipy.io savemat to save a .mat file of the data
:param data: the data dictionary object
:param file_name: the file name (with extension) to save to
:param obs:
:param params: extra params to add to the save object
'''
logging.info("Saving result to %s.mat", file_name)
mdict = prepare_save(data,file_name,obs,params)
savemat("%s.mat" %file_name, mdict)
logging.info("Data saved to .mat file %s",file_name)
def save2pkl(data,file_name,obs, params=None):
''' save2pkl saves to a pickle file
:param data: the data dictionary object
:param file_name: the file name (with extension) to save to
:param obs:
:param params: extra params to add to the save object
'''
logging.info("Saving result to %s.pkl", file_name)
mdict = prepare_save(data,file_name,obs,params)
output = open("%s.pkl" %file_name, 'wb')
pickle.dump(mdict,output,protocol=0)
output.close()
logging.info("Data saved to pickle file %s", file_name)
def prepare_save(data, file_name, obs, params=None):
"""prepare_save: takes an mcdata object and the observables and stores the states,
expectations, times, observable labels (str and latex), random seeds,
number of trajectories as:
{
"psis": psis, # shape (ntraj, ntimes, dim_psi) complex128
"expects": expects, # shape (ntraj, ntimes, num_observables) complex128
"times": times, # shape (ntimes) float64
"observable_str": observable_str, # shape (num_observables) string
"observable_latex": observable_latex, # shape (num_observables) string
"seeds": seeds, # shape (ntraj) int
}
Additional parameters can be passed using the params argument, as a python dictionary.
"""
ntraj = data['psis'].shape[0]
assert ntraj == data['obsq_expects'].shape[0]
assert ntraj >= 1
psis = data['psis']
expects = data['obsq_expects']
times = data['tspan']
observable_str = [str(o) for o in obs]
observable_latex = [o._repr_latex_() for o in obs]
seeds = data['seeds']
mdict = {
"psis": psis,
"expects": expects,
"times": times,
"observable_str": observable_str,
"observable_latex": observable_latex,
"seeds": seeds,
}
if params != None:
mdict.update(params) ## other paramters (optional)
return mdict
# default are for absorptive bistability
def make_nparams(W,k,g,g0,DD,TT,Cn=10.5,
kn=.12, yn=11.3, DDn=0, TTn=0., J = 0.5):
g0n = np.sqrt(2.*kn*Cn)
Wn = yn*kn/np.sqrt(2)/g0n
nparams = {
W: Wn/np.sqrt(2*kn),
k: 2*kn,
g: 2./np.sqrt(2*J),
g0: -g0n/np.sqrt(2*J),
DD: DDn,
TT: TTn,
}
xrs = np.linspace(0, 10)
yrs = 2*Cn*xrs/(1+xrs**2) + xrs
return nparams