-
Notifications
You must be signed in to change notification settings - Fork 36
/
c3.py
executable file
·471 lines (420 loc) · 16.4 KB
/
c3.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
"""Object that deals with the model learning."""
import os
import time
import hjson
import pickle
import itertools
import numpy as np
import tensorflow as tf
from typing import List, Dict
from c3.optimizers.optimizer import Optimizer
from c3.utils.utils import log_setup
from c3.libraries.estimators import (
dv_g_LL_prime,
g_LL_prime_combined,
g_LL_prime,
neg_loglkh_multinom_norm,
)
class C3(Optimizer):
"""
Object that deals with the model learning.
Parameters
----------
dir_path : str
Filepath to save results
sampling : str
Sampling method from the sampling library
batch_sizes : list
Number of points to select from each dataset
seqs_per_point : int
Number of sequences that use the same parameter set
pmap : ParameterMap
Identifiers for the parameter vector
state_labels : list
Identifiers for the qubit subspaces
callback_foms : list
Figures of merit to additionally compute and store
algorithm : callable
From the algorithm library
run_name : str
User specified name for the run, will be used as root folder
options : dict
Options to be passed to the algorithm
"""
def __init__(
self,
dir_path,
sampling,
batch_sizes,
pmap,
seqs_per_point=None,
state_labels=None,
callback_foms=[],
algorithm=None,
run_name=None,
options={},
):
"""Initiliase."""
super().__init__(pmap=pmap, algorithm=algorithm)
self.sampling = sampling
self.batch_sizes = batch_sizes
self.seqs_per_point = seqs_per_point
self.state_labels = state_labels
self.callback_foms = callback_foms
self.inverse = False
self.options = options
self.learn_data = {}
self.fom = g_LL_prime_combined
self.__dir_path = dir_path
self.__run_name = run_name
def log_setup(self) -> None:
"""
Create the folders to store data.
Parameters
----------
dir_path : str
Filepath
run_name : str
User specified name for the run
"""
dir_path = os.path.abspath(self.__dir_path)
run_name = self.__run_name
if run_name is None:
run_name = "-".join(
[self.algorithm.__name__, self.sampling.__name__, self.fom.__name__]
)
self.logdir = log_setup(dir_path, run_name)
self.logname = "model_learn.log"
# shutil.copy2(self.__real_model_folder, self.logdir)
def read_data(self, datafiles: Dict[str, str]) -> None:
"""
Open data files and read in experiment results.
Parameters
----------
datafiles : dict
List of paths for files that contain learning data.
"""
self.__real_model_folder = os.path.dirname(list(datafiles.values())[0])
for target, datafile in datafiles.items():
with open(datafile, "rb+") as file:
self.learn_data[target] = pickle.load(file)
def select_from_data(self, batch_size) -> List[int]:
"""
Select a subset of each dataset to compute the goal function on.
Parameters
----------
batch_size : int
Number of points to select
Returns
-------
list
Indeces of the selected data points.
"""
# TODO fix when batch size is 1 (atm it does all)
learn_from = self.learn_from
sampling = self.sampling
indeces = sampling(learn_from, batch_size)
total_size = len(learn_from)
all_indxs = list(range(total_size))
if self.inverse:
selected_indeces = list(set(all_indxs) - set(indeces))
else:
selected_indeces = indeces
return selected_indeces
def learn_model(self) -> None:
"""
Performs the model learning by minimizing the figure of merit.
"""
self.log_setup()
self.start_log()
print(f"C3:STATUS:Saving as: {os.path.abspath(self.logdir + self.logname)}")
x_init = self.pmap.get_parameters_scaled()
try:
self.algorithm(
x_init,
fun=self.fct_to_min,
fun_grad=self.fct_to_min_autograd,
grad_lookup=self.lookup_gradient,
options=self.options,
)
except KeyboardInterrupt:
pass
with open(self.logdir + "best_point_" + self.logname, "r") as file:
best_params = hjson.loads(file.readlines()[1])["params"]
self.pmap.set_parameters(best_params)
self.pmap.model.update_model()
self.end_log()
self.confirm()
def confirm(self) -> None:
"""
Compute the validation set, i.e. the value of the goal function on all points
of the dataset that were not used for learning.
"""
self.logname = "confirm.log"
self.inverse = True
self.start_log()
print(f"C3:STATUS:Saving as: {os.path.abspath(self.logdir + self.logname)}")
x_best = self.pmap.get_parameters_scaled()
self.evaluation = -1
try:
self.goal_run(x_best)
except KeyboardInterrupt:
pass
def _one_par_sim_vals(
self, current_params: tf.constant, m: dict, ipar: int, target: str
) -> tf.float64:
seqs_pp = self.seqs_per_point
m = self.learn_from[ipar]
gateset_params = m["params"]
sequences = m["seqs"][:seqs_pp]
self.pmap.set_parameters_scaled(current_params)
self.pmap.str_parameters()
self.pmap.model.update_model()
self.pmap.set_parameters(gateset_params, self.gateset_opt_map)
# We find the unique gates used in the sequence and compute
# only those.
self.exp.opt_gates = list(set(itertools.chain.from_iterable(sequences)))
self.exp.get_gates()
pops = self.exp.evaluate(sequences)
sim_vals, pops = self.exp.process(
labels=self.state_labels[target], populations=pops
)
return sim_vals
def _log_one_dataset(
self, data_set: dict, ipar: int, indeces: list, sim_vals: list, count: int
) -> None:
seqs_pp = self.seqs_per_point
m_vals = data_set["results"][:seqs_pp]
m_stds = np.array(data_set["result_stds"][:seqs_pp])
m_shots = data_set["shots"][:seqs_pp]
sequences = data_set["seqs"][:seqs_pp]
with open(self.logdir + self.logname, "a") as logfile:
logfile.write(
f"\n Parameterset {ipar + 1}, #{count} of {len(indeces)}:\n"
f"{str(self.exp.pmap)}\n"
)
logfile.write(
"Sequence Simulation Experiment Std Shots" " Diff\n"
)
for iseq in range(len(sequences)):
m_val = np.array(m_vals[iseq])
m_std = np.array(m_stds[iseq])
shots = np.array(m_shots[iseq])
sim_val = sim_vals[iseq].numpy()
with open(self.logdir + self.logname, "a") as logfile:
for ii in range(len(sim_val)):
logfile.write(
f"{iseq + 1:8} "
f"{float(sim_val[ii]):8.6f} "
f"{float(m_val[ii]):8.6f} "
f"{float(m_std[ii]):8.6f} "
f"{float(shots[0]):8} "
f"{float(m_val[ii]-sim_val[ii]): 8.6f}\n"
)
logfile.flush()
def goal_run(self, current_params: tf.constant) -> tf.float64:
"""
Evaluate the figure of merit for the current model parameters.
Parameters
----------
current_params : tf.Tensor
Current model parameters
Returns
-------
tf.float64
Figure of merit
"""
exp_values = []
sim_values = []
exp_stds = []
exp_shots = []
goals = []
seq_weigths = []
count = 0
seqs_pp = self.seqs_per_point
# TODO: seq per point is not constant. Remove.
for target, data in self.learn_data.items():
self.learn_from = data["seqs_grouped_by_param_set"]
self.gateset_opt_map = data["opt_map"]
indeces = self.select_from_data(self.batch_sizes[target])
for ipar in indeces:
count += 1
data_set = self.learn_from[ipar]
m_vals = data_set["results"][:seqs_pp]
m_stds = data_set["result_stds"][:seqs_pp]
m_shots = data_set["shots"][:seqs_pp]
sequences = data_set["seqs"][:seqs_pp]
num_seqs = len(sequences)
if target == "all":
num_seqs = len(sequences) * 3
sim_vals = self._one_par_sim_vals(
current_params, data_set, ipar, target
)
if target == "all":
one_goal = neg_loglkh_multinom_norm(
m_vals,
tf.stack(sim_vals),
tf.constant(m_stds, dtype=tf.float64),
tf.constant(m_shots, dtype=tf.float64),
)
else:
one_goal = g_LL_prime(
m_vals,
tf.stack(sim_vals),
tf.constant(m_stds, dtype=tf.float64),
tf.constant(m_shots, dtype=tf.float64),
)
exp_stds.extend(m_stds)
exp_shots.extend(m_shots)
goals.append(one_goal.numpy())
seq_weigths.append(num_seqs)
sim_values.extend(sim_vals)
exp_values.extend(m_vals)
self._log_one_dataset(data_set, ipar, indeces, sim_vals, count)
goal = g_LL_prime_combined(goals, seq_weigths)
# TODO make gradient free function use any fom
with open(self.logdir + self.logname, "a") as logfile:
logfile.write("\nFinished batch with ")
logfile.write("{}: {}\n".format("g_LL_prime_combined", goal))
for cb_fom in self.callback_foms:
val = float(cb_fom(exp_values, sim_values, exp_stds, exp_shots).numpy())
logfile.write("{}: {}\n".format(cb_fom.__name__, val))
logfile.flush()
self.optim_status["params"] = [
par.numpy().tolist() for par in self.pmap.get_parameters()
]
self.optim_status["goal"] = goal
self.optim_status["time"] = time.asctime()
self.evaluation += 1
return goal
def goal_run_with_grad(self, current_params):
"""
Same as goal_run but with gradient. Very resource intensive. Unoptimized at the
moment.
"""
exp_values = []
sim_values = []
exp_stds = []
exp_shots = []
goals = []
grads = []
seq_weigths = []
count = 0
for target, data in self.learn_data.items():
self.learn_from = data["seqs_grouped_by_param_set"]
self.gateset_opt_map = data["opt_map"]
indeces = self.select_from_data(self.batch_sizes[target])
for ipar in indeces:
count += 1
data_set = self.learn_from[ipar]
seqs_pp = self.seqs_per_point
m_vals = data_set["results"][:seqs_pp]
m_stds = np.array(data_set["result_stds"][:seqs_pp])
m_shots = data_set["shots"][:seqs_pp]
sequences = data_set["seqs"][:seqs_pp]
num_seqs = len(sequences)
if target == "all":
num_seqs = len(sequences) * 3
with tf.GradientTape() as t:
t.watch(current_params)
sim_vals = self._one_par_sim_vals(
current_params, data_set, ipar, target
)
if target == "all":
one_goal = neg_loglkh_multinom_norm(
m_vals,
tf.stack(sim_vals),
tf.constant(m_stds, dtype=tf.float64),
tf.constant(m_shots, dtype=tf.float64),
)
else:
one_goal = g_LL_prime(
m_vals,
tf.stack(sim_vals),
tf.constant(m_stds, dtype=tf.float64),
tf.constant(m_shots, dtype=tf.float64),
)
exp_stds.extend(m_stds)
exp_shots.extend(m_shots)
seq_weigths.append(num_seqs)
goals.append(one_goal.numpy())
grads.append(t.gradient(one_goal, current_params).numpy())
sim_values.extend(sim_vals)
exp_values.extend(m_vals)
self._log_one_dataset(data_set, ipar, indeces, sim_vals, count)
goal = g_LL_prime_combined(goals, seq_weigths)
grad = dv_g_LL_prime(goals, grads, seq_weigths)
# print(f"{seq_weigths=}\n{goals=}\n{grads=}\n{goal=}\n{grad=}\n")
with open(self.logdir + self.logname, "a") as logfile:
logfile.write("\nFinished batch with ")
logfile.write("{}: {}\n".format(self.fom.__name__, goal))
for cb_fom in self.callback_foms:
val = float(cb_fom(exp_values, sim_values, exp_stds, exp_shots).numpy())
logfile.write("{}: {}\n".format(cb_fom.__name__, val))
logfile.flush()
self.optim_status["params"] = [
par.numpy().tolist() for par in self.pmap.get_parameters()
]
self.optim_status["goal"] = goal
self.optim_status["gradient"] = list(grad.flatten())
self.optim_status["time"] = time.asctime()
self.evaluation += 1
return goal, grad
def goal_run_with_grad_no_batch(self, current_params):
"""
Same as goal_run but with gradient. Very resource intensive. Unoptimized at the
moment.
"""
exp_values = []
sim_values = []
exp_stds = []
exp_shots = []
count = 0
seqs_pp = self.seqs_per_point
with tf.GradientTape() as t:
t.watch(current_params)
for target, data in self.learn_data.items():
self.learn_from = data["seqs_grouped_by_param_set"]
self.gateset_opt_map = data["opt_map"]
indeces = self.select_from_data(self.batch_sizes[target])
for ipar in indeces:
count += 1
data_set = self.learn_from[ipar]
m_vals = data_set["results"][:seqs_pp]
sim_vals = self._one_par_sim_vals(
current_params, data_set, ipar, target
)
sim_values.extend(sim_vals)
exp_values.extend(m_vals)
self._log_one_dataset(data_set, ipar, indeces, sim_vals, count)
if target == "all":
goal = neg_loglkh_multinom_norm(
exp_values,
tf.stack(sim_values),
tf.constant(exp_stds, dtype=tf.float64),
tf.constant(exp_shots, dtype=tf.float64),
)
else:
goal = g_LL_prime(
exp_values,
tf.stack(sim_values),
tf.constant(exp_stds, dtype=tf.float64),
tf.constant(exp_shots, dtype=tf.float64),
)
grad = t.gradient(goal, current_params).numpy()
goal = goal.numpy()
with open(self.logdir + self.logname, "a") as logfile:
logfile.write("\nFinished batch with ")
logfile.write("{}: {}\n".format(self.fom.__name__, goal))
for cb_fom in self.callback_foms:
val = float(cb_fom(exp_values, sim_values, exp_stds, exp_shots).numpy())
logfile.write("{}: {}\n".format(cb_fom.__name__, val))
logfile.flush()
self.optim_status["params"] = [
par.numpy().tolist() for par in self.pmap.get_parameters()
]
self.optim_status["goal"] = goal
self.optim_status["gradient"] = list(grad.flatten())
self.optim_status["time"] = time.asctime()
self.evaluation += 1
return goal, grad