/
utils.py
executable file
·358 lines (300 loc) · 11.8 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
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
from scipy.io import savemat
import pickle
import logging
import os
import hashlib
import numpy as np
from scipy import sparse
import json
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 make_params_string(args):
return ("%s_"*16)[:-1] %args
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
def load_trajectory(traj):
"""Generic load"""
pkl_file = open(traj, 'rb')
pkl_dict = pickle.load(pkl_file)
pkl_file.close()
return pkl_dict
def save(file, pkl_dict):
"""Generic save"""
pkl_file = open(file, 'wb')
pickle.dump(pkl_dict, pkl_file, protocol=0)
pkl_file.close()
def get_params(traj):
"""Get parameters from trajectory file name.
"""
params={}
things = traj[4:].split('-')
first = things[0].split('_')
params['seed'] = int(first[-1])
params['regime'] = str('_'.join(first[:-1])).split('/')[-1]
params['ntraj'] = int(things[1])
try:
(delta_t1,delta_t2,Nfock_a,Nfock_j,
duration,downsample,method,num_systems,
R,EPS,noise_amp,trans_phase,drive) = things[2:]
params['delta_t'] = float("".join([delta_t1,'-',delta_t2]))
except:
(delta_t,Nfock_a,Nfock_j,
duration,downsample,method,num_systems,
R,EPS,noise_amp,trans_phase,drive) = things[2:]
params['delta_t'] = float(delta_t)
drive = drive[:-4] ## drop the .pkl extension...
params['Nfock_j'] = int(Nfock_j)
params['duration'] = float(duration)
params['downsample'] = int(downsample)
params['sdeint_method_name'] = str(method)
params['num_systems'] = int(num_systems)
params['R'] = float(R)
params['eps'] =float(EPS)
params['noise_amp'] =float(noise_amp)
params['trans_phase'] =float(trans_phase)
params['drive_second_system'] = True if drive == "True" else False
return params
## Which values to use to distinguish groups of files
bools = {'seed': False,
'regime': True,
'ntraj': True,
'delta_t': True,
'Nfock_a': True,
'Nfock_j': True,
'duration': True,
'downsample': True,
'sdeint_method_name': True,
'num_systems': True,
'R': True,
'eps': True,
'lambd': True,
'noise_amp': True,
'trans_phase': True,
'drive_second_system': True}
def files_by_params(files, bools, min_seed = None, max_seed=None, duration=None):
"""
Return a list of lists, each one having the unique files with distinct params determined by params_bool
"""
## Ensure all files have the same extension
extension = files[0].split('.')[-1]
assert all(f.split('.')[-1] == extension for f in files)
params_each_file = {}
for f in files:
name = f.split('/')[-1]
try:
params_each_file[f] = get_params(name)
except: ## couldn't use the first one (for original pickle files)
try:
params_each_file[f] = get_params_json(name) ## works for JSON or pickle from JSON.
except:
raise ValueError("could not get parameters from file %s." %name)
if max_seed is not None:
params_each_file = {f:p for f,p in params_each_file.items() if p['seed'] <= max_seed}
if min_seed is not None:
params_each_file = {f:p for f,p in params_each_file.items() if p['seed'] >= min_seed}
if duration:
params_each_file = {f:p for f,p in params_each_file.items() if p['duration'] == duration}
relevant_params_each_file = {f : tuple(sorted((k,v) for k,v in p.items() if bools[k]))
for f, p in params_each_file.items()}
all_possible_params = set(relevant_params_each_file.values())
groups = {p : [f for f in relevant_params_each_file if relevant_params_each_file[f] == p]
for p in all_possible_params}
return list(groups.values())
def make_hash(traj):
"""We make a name using a hash because there could be multiple
trajectories in traj_list feeding into a single set of diffusion maps"""
hash_code = hashlib.sha256(traj.encode('utf-8'))
return hash_code.hexdigest()
def dim_check(H, Ls):
"""Make sure the dimensions match.
Args:
H (possibly sparse matrix)
Ls (list of possibly sparse matrices)
Ensures that H and each L in Ls is a square
matrix of the same size.
"""
N = H.shape[0]
assert H.shape == (N, N)
assert all(L.shape == (N, N) for L in Ls)
return N
def preprocess_operators(H1, H2, L1s, L2s, ops_on_whole_space):
"""Make sure operators are defined over the entire space.
First, check that all dimensions match. Then return either the given
operators, or extend them so that they are defined on the Kronecker product
Hilbert space.
Args:
H1: square matrix
Hamiltonian matrix of system 1
H2: square matrix
Hamiltonian matrix of system 2
L1s: list of square matrices
Lindblad matrices of system 1
L2s: list of square matrices
Lindblad matrices of system 2
ops_on_whole_space: boolean
whether the given operators are defined on the whole space
Returns: H1, H2, L1s, L2s
Operators extended to whole space if necessary
"""
N1 = dim_check(H1, L1s)
N2 = dim_check(H2, L2s)
if ops_on_whole_space:
assert(N1 == N2)
return H1, H2, L1s, L2s
else:
I1 = np.eye(N1)
I2 = np.eye(N2)
H1 = sparse.csr_matrix(np.kron(H1.todense(), I2))
H2 = sparse.csr_matrix(np.kron(I1, H2.todense()))
L1s = [sparse.csr_matrix(np.kron(L1.todense(), I2)) for L1 in L1s]
L2s = [sparse.csr_matrix(np.kron(I1, L2.todense())) for L2 in L2s]
return H1, H2, L1s, L2s
def get_params_json(file):
"""Get parameters from Json file.
This is different from the older get_params -- the convention for the JSON
files is to use underscores instead of dashes to simplify parsing.
Args:
file (string): name of file
Returns:
params (dict): maps each parameter to the value indicated by the file
name.
"""
file = file.split('/')[-1] ## make sure it wasn't a directory...
p = {}
args = file.split('_')
if len(args) == 16: ## if regime is not included
[_, seed, ntraj, delta_t, Nfock_a,
Nfock_j, duration, downsample,
sdeint_method_name, num_systems,
R, eps, noise_amp, lambd, trans_phase, drive_second_system] = args
elif len(args) == 17:
[_, regime, seed, ntraj, delta_t, Nfock_a,
Nfock_j, duration, downsample,
sdeint_method_name, num_systems,
R, eps, noise_amp, lambd, trans_phase, drive_second_system] = args
p['regime'] = regime
elif len(args) == 18:
[_, regime1, regime2, seed, ntraj, delta_t, Nfock_a,
Nfock_j, duration, downsample,
sdeint_method_name, num_systems,
R, eps, noise_amp, lambd, trans_phase, drive_second_system] = args
p['regime'] = regime1 + "_" + regime2
else:
raise ValueError("Unknown name structure for file %s." %file)
p['seed'] = int(seed)
p['ntraj'] = int(ntraj)
p['delta_t'] = float(delta_t)
p['Nfock_a'] = int(Nfock_a)
p['Nfock_j'] = int(Nfock_j)
p['duration'] = float(duration)
p['downsample'] = int(downsample)
p['sdeint_method_name'] = sdeint_method_name
p['num_systems'] = int(num_systems)
p['R'] = float(R)
p['eps'] = float(eps)
p['noise_amp'] = float(noise_amp)
p['lambd'] = float(lambd)
p['trans_phase'] = float(trans_phase)
if drive_second_system == "True":
p['drive_second_system'] = True
else:
p['drive_second_system'] = False
return p
def get_by_params_json(files, params):
"""Get all files with specified parameters.
Args:
files (list of strings): List of files
params (dict): dictionary with specific parameters whose values should
be chosen.
Returns:
selected_files (list of strings): subset of the files from the input
which have the correct parameters.
"""
return [f for f in files if all(get_params_json(f)[p] == params[p] for p in params)]
def load_json_seq(file_path):
"""Loads data from JSON file.
Args:
file_path (string): name of JSON file with expectation values.
Returns:
data (numpy array): Numpy array with loaded expectation values.
"""
with open(file_path) as json_data:
trajectories = json.load(json_data)
return np.array([[np.array(p['real']) + 1j*np.array(p['imag'])
for p in traj]
for traj in trajectories], dtype=complex)
def sorted_eigs(e_vals, e_vecs):
'''
Then sort the eigenvectors and eigenvalues
s.t. the eigenvalues monotonically decrease.
K is the number of eigenvalues/eigenvectors.
Args:
e_vals ([K,] ndarray): input eigenvalues
e_vecs ([dim, K] ndarray): corresponding eigenvectors
Returns:
e_vals ([K,] ndarray): Sorted output eigenvalues
e_vecs ([dim, K] ndarray): Corresponding sorted eigenvectors
'''
l = zip(e_vals, e_vecs.T)
l = sorted(l,key = lambda z: -z[0])
return np.asarray([el[0] for el in l]), np.asarray([el[1] for el in l]).T