-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_oct.py
executable file
·729 lines (691 loc) · 34 KB
/
run_oct.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#!/usr/bin/env python
"""Run an Krotov-optimiztion , and propagate the optimized pulse, on the given
runfolder.
Runfolder must contain pulse.guess, config, and target_gate.dat.
After Krotov optimization, runfolder will contain the additional files
oct.log (log file), pulse.dat (optimized pulse), oct_iters.dat (OCT iteration
data), config.oct (copy of config with automatically determined value of
lambda_a). After propagation, the files U.dat (result of propagating
pulse.dat), and prop.log (log file) will also exist.
No optimization is done if pulse.dat already exists and either
(a) the oct iter in the header matches the "iter_stop" in the config, or
(b) the header contains the word "converged"
If optimization is performed, it starts from the guess pulse. Continuation from
an existing pulse.dat happens only if the --continue option is given.
Using the --pre-simplex option, a simplex optimization of pulse.guess may be
performed. This overwrites pulse.guess with an optimized guess pulse, and
creates the files pulse.json (analytic approximation of the original
pulse.guess), pulse_opt.json (analytic simplex-optimized pulse), simplex.log
(log file), and pulse.guess.pre_simplex (original pulse.guess file)
"""
import os
import re
import json
import zeta_systematic_variation
import subprocess as sp
import QDYN
import logging
import numpy as np
import click
from oct_propagate import propagate
from numpy.random import random
from glob import glob
from clusterjob.utils import read_file
from stage2_simplex import get_temp_runfolder, run_simplex
from QDYN.pulse import Pulse
from analytical_pulses import AnalyticalPulse
from notebook_utils import (get_w_d_from_config, read_target_gate,
pulse_config_compat, ensure_ham_files, J_target, avg_freq,
max_freq_delta)
MAX_TRIALS = 200
def reset_pulse(pulse, iter):
"""Reset pulse at the given iteration to the last available snapshot,
assuming that snapshots are available using the same name as pulse, with
the iter append to the file name (e.g. iter.dat.100 for iter.dat).
Snapshots must be at least 10 iterations older than the current pulse"""
snapshot_list = glob("%s.*"%pulse)
snapshots = {}
logger = logging.getLogger(__name__)
logger.debug("resetting in iter %d", iter)
logger.debug("available snapshots: %s", str(snapshot_list))
for snapshot in snapshot_list:
try:
snapshot_iter = int(os.path.splitext(snapshot)[1][1:])
snapshots[snapshot_iter] = snapshot
except ValueError:
pass # ignore pulse.dat.prev
snapshot_iters = sorted(snapshots.keys())
os.unlink(pulse)
while len(snapshot_iters) > 0:
snapshot_iter = snapshot_iters.pop()
if (iter == 0) or (snapshot_iter + 10 < iter):
logger.debug("accepted snapshot: %s (iter %d)",
snapshots[snapshot_iter], snapshot_iter)
QDYN.shutil.copy(snapshots[snapshot_iter], pulse)
return
else:
logger.debug("rejected snapshot: %s (iter %d)",
snapshots[snapshot_iter], snapshot_iter)
logger.debug("no accepted snapshot")
def write_oct_config(template, config, target, iter_stop=None, max_megs=None,
max_hours=None, J_T_conv=None, J_T=None, lbfgs=False):
"""Write a new config file based on template, but with an updated OCT and
user_strings section. The `target` parameter must be 'PE' (implies PE
functional), 'SQ' (implies LI functional), or the name of a file inside the
runfolder (usually target_gate.dat) that contains an explicit target gate
(implies SM functional). The remaining parameters set the corresponding
options in the OCT section.
"""
with open(template, 'r') as in_fh, open(config, 'w') as out_fh:
if target == 'PE':
method = 'krotov2'
gate = 'CPHASE'
J_T = 'PE'
elif target == 'SQ':
method = 'krotov2'
gate = 'unity'
J_T = 'LI'
else:
if lbfgs:
method = 'lbfgs'
elif J_T == 'LI':
method = 'krotov2'
else:
method = 'krotovpk'
gate = target
if J_T is None:
J_T = 'SM'
if lbfgs:
J_T = 'RE'
section = ''
for line in in_fh:
m = re.match(r'^\s*(?P<section>[a-z_]+)\s*:', line)
if m:
section = m.group('section')
if section == 'oct':
if iter_stop is not None:
line = re.sub(r'iter_stop\s*=\s*\d+',
r'iter_stop = %d'%(iter_stop), line)
if max_megs is not None:
line = re.sub(r'max_megs\s*=\s*\d+',
r'max_megs = %d'%(max_megs), line)
if max_hours is not None:
line = re.sub(r'max_hours\s*=\s*\d+',
r'max_hours = %d'%(max_hours), line)
if J_T_conv is not None:
line = re.sub(r'J_T_conv\s*=\s*[\deE+-]+',
r'J_T_conv = %e'%(J_T_conv), line)
line = re.sub(r'type\s*=\s*\w+', r'type = %s'%(method), line)
if "iter_dat" in line and 'params_file' not in line:
line = re.sub(r'(iter_dat\s*=\s*[\w.]+,)',
r'\1 params_file = oct_params.dat,', line)
elif section == 'user_strings':
line = re.sub(r'gate\s*=\s*[\w.]+', r'gate = '+gate, line)
line = re.sub(r'J_T\s*=\s*\w+', r'J_T = '+J_T, line)
out_fh.write(line)
def num_pulse_to_analytic(runfolder, formula, rwa=True, randomize=False,
num_pulse='pulse.guess', analytical_pulse='pulse.json'):
"""Read a numerical guess pulse from `num_pulse` and map it as closely as
possible to an analytical pulse, to be stored in `analytical_pulse`. The
analytic pulse will have the given formula.
If `randomize` is True, instead of trying to achieve a close fit between
`num_pulse` and `analytical_pulse`, make a "trivial" guess for the
`analytical_pulse` to make it roughtly equivalent to the `num_pulse` (e.g.,
match the amplitude), and then apply a 10% random variation on all the
analytic pulse parameters. This may be useful to try to get out of a local
optimization minimum.
If `num_pulse` does not exist, `analytical_pulse` is removed. If both
`num_pulse` and `analytical_pulse` exist, and `analytical_pulse` is newer
than `num_pulse`, all files are left untouched; if it is older, it is
replaced by a new fit.
"""
logger = logging.getLogger(__name__)
pulse_guess = os.path.join(runfolder, num_pulse)
pulse_json = os.path.join(runfolder, analytical_pulse)
config = os.path.join(runfolder, 'config')
# handle existing files
try:
p_guess = Pulse(filename=pulse_guess)
except (IOError, ValueError) as exc_info:
# p_guess does not exist or is unreadable
if os.path.isfile(pulse_json):
os.unlink(pulse_json)
logger.error(str(exc_info.value))
return
if os.path.isfile(pulse_json):
if (os.path.getctime(pulse_json) > os.path.getctime(pulse_guess)):
# already matched
return
# perform the fit
if formula == '1freq_rwa':
assert(rwa)
w_d = get_w_d_from_config(config)
E0 = np.max(np.abs(p_guess.amplitude))
if randomize:
w_d += 0.2 * (random() - 1.0) * w_d # 10% variation
E0 += 0.2 * (random() - 1.0) * E0 # 10% variation
parameters = {'E0': E0, 'T': p_guess.T, 'w_L': w_d, 'w_d': w_d}
vary = ['E0', ]; bounds = {'E0': (0.5*E0, 1.5*E0)}
scipy_options = None
elif formula == 'CRAB_rwa':
assert(rwa)
w_d = get_w_d_from_config(config)
E0 = np.max(np.abs(p_guess.amplitude))
if randomize:
w_d += 0.2 * (random() - 1.0) * w_d # 10% variation
E0 += 0.2 * (random() - 1.0) * E0 # 10% variation
parameters = {'E0': E0, 'T': p_guess.T, 'w_d': w_d,
'r': (random(5)-0.5), 'a': random(5),
'b': random(5)}
else:
parameters = {'E0': E0, 'T': p_guess.T, 'w_d': w_d,
'r': np.zeros(5), 'a': np.zeros(5),
'b': np.zeros(5)}
vary = ['E0', 'r', 'a', 'b'];
bounds = {'E0': (0.8*E0, 1.2*E0), 'r': (-0.5, 0.5), 'a': (0, 1),
'b': (0, 1)}
scipy_options ={'maxfev': 50000}
else:
raise ValueError("Don't know what to do with formula %s" % formula)
if randomize:
nt = len(p_guess.amplitude) + 1
guess_analytical = AnalyticalPulse(formula, p_guess.T, nt, parameters,
t0=0.0, time_unit=p_guess.time_unit,
ampl_unit=p_guess.ampl_unit, freq_unit=p_guess.freq_unit,
mode=p_guess.mode)
else:
try:
guess_analytical = AnalyticalPulse.create_from_fit(p_guess,
formula=formula, parameters=parameters,
vary=vary, bounds=bounds, method='curve_fit')
except RuntimeError:
# curve-fit may violate the bounds (-> RuntimeError), in which case
# L-BFGS-B will (hopefully) find a solution that honors them
guess_analytical = AnalyticalPulse.create_from_fit(p_guess,
formula=formula, parameters=parameters,
vary=vary, bounds=bounds, method='L-BFGS-B')
guess_analytical.write(pulse_json)
logger.debug("Mapped %s to analytic %s: %s"
% (num_pulse, analytical_pulse, guess_analytical.header))
def switch_to_analytical_guess(runfolder, num_guess='pulse.guess',
analytical_guess='pulse_opt.json', backup='pulse.guess.pre_simplex',
nt_min=2000):
"""Replace `num_guess` with `analytical_guess` (converted to a numeric
pulse), while backing up the original `num_guess` to `backup`.
If `num_guess` does not exist (but `analytical_guess` does), simply convert
`analytical_guess` to `num_guess` without creating a backup)
"""
pulse_guess = os.path.join(runfolder, num_guess)
pulse_opt_json = os.path.join(runfolder, analytical_guess)
pulse_guess_pre_simplex = os.path.join(runfolder, backup)
config = os.path.join(runfolder, 'config')
if not os.path.isfile(pulse_guess):
if os.path.isfile(pulse_guess_pre_simplex):
QDYN.shutil.copy(pulse_guess_pre_simplex, pulse_guess)
if not os.path.isfile(pulse_opt_json):
if os.path.isfile(pulse_guess_pre_simplex):
QDYN.shutil.copy(pulse_guess_pre_simplex, pulse_guess)
if os.path.isfile(pulse_guess):
QDYN.shutil.copy(pulse_guess, pulse_guess_pre_simplex)
if os.path.isfile(pulse_opt_json):
p_guess = AnalyticalPulse.read(pulse_opt_json)
if p_guess.nt < nt_min:
p_guess.nt = nt_min
if p_guess.formula_name == '1freq_rwa':
assert p_guess.parameters['w_L'] == p_guess.parameters['w_d']
p_guess.pulse().write(pulse_guess)
pulse_config_compat(p_guess, config, adapt_config=True)
def systematic_scan(runfolder, template_pulse, scan_params_json,
outfile='pulse_systematic_scan.json', target='target_gate.dat',
rwa=False, use_threads=False):
"""Read a dictionary from the json file scan_params_json that must map
``param => array of values``, where `param` is a parameter in the
analytical formula of the pulse defined in the json file `template_pulse`.
Propagate for each possible value combination and write the pulse that
yields the best figure of merit to `outfile`
"""
logger = logging.getLogger(__name__)
if not rwa:
raise NotImplementedError("LAB frame not supported")
pulse0 = AnalyticalPulse.read(os.path.join(runfolder, template_pulse))
with open(os.path.join(runfolder, scan_params_json)) as in_fh:
vary = json.load(in_fh)
for key in vary:
if key not in pulse0.parameters:
raise ValueError(("Key %s is not a parameter for the "
"analytic pulse formula") % (key, pulse1.formula_name))
def worker(args):
rf, pulse_json = args
U = propagate(rf, pulse_json, target=target, rwa=True, force=True,
keep=None, use_threads=use_threads)
return U
if target in ['PE', 'SQ']:
U_tgt = None
else:
U_tgt = read_target_gate(os.path.join(runfolder, target))
def fig_of_merit(U):
if target in ['PE', 'SQ']:
C = U.closest_unitary().concurrence()
max_loss = np.max(1.0 - U.logical_pops())
result = J_target(target, C, max_loss)
else:
result = 1.0 - U.F_avg(U_tgt)
logger.debug("Systematic variation -> %s" % result)
return result
table = zeta_systematic_variation.systematic_variation(runfolder, pulse0,
vary, fig_of_merit, n_procs=1, _worker=worker)
pulse1 = pulse0.copy()
row = table.iloc[0]
for key in row.keys():
if key in pulse1.parameters:
pulse1.parameters[key] = row[key]
if rwa:
w_d = avg_freq(pulse1) # GHz
w_max = max_freq_delta(pulse1, w_d) # GHZ
pulse1.parameters['w_d'] = w_d
pulse1.nt = int(max(2000, 100 * w_max * pulse1.T))
logger.debug("Systematic variation yielded parameters: %s"
% str(pulse1.parameters))
pulse1.write(os.path.join(runfolder, outfile))
def run_pre_krotov_simplex(runfolder, formula_or_json_file, vary='default',
target='target_gate.dat', rwa=False, randomize=False, E0_min=0.0):
"""Run a simplex pre-optimization, resulting in file 'pulse_opt.json' in
the runfolder. If `formula_or_json_file` is a formula, the starting point
of the optimization is an analytic approximation to a numeric pulse in
'pulse.guess', which will be written to 'pulse.json'. If
`formula_or_json_file` is the name of a json file inside the runfolder, the
analytic pulse described in that json file is the starting point for the
optimization.
The result of the simplex optimization will be written to pulse_opt.json
If 'pulse_opt.json' already exists and is newer than the guess pulse file,
nothing is done.
Pulse amplitudes < E0_min will be set to E0_min
"""
logger = logging.getLogger(__name__)
guess = formula_or_json_file
pulse_json = os.path.join(runfolder, guess)
pulse_opt_json = os.path.join(runfolder, 'pulse_opt.json')
config = os.path.join(runfolder, 'config')
if not os.path.isfile(pulse_json):
# We assume that a formula name was given
formula = formula_or_json_file
num_pulse_to_analytic(runfolder, formula, rwa,
randomize, num_pulse='pulse.guess',
analytical_pulse='pulse.json')
guess = 'pulse.json'
pulse_json = os.path.join(runfolder, guess)
if os.path.isfile(pulse_opt_json):
if (os.path.getctime(pulse_opt_json) > os.path.getctime(pulse_json)):
logger.debug("%s already up to date" % pulse_opt_json)
return
# if we're going to do a new pre-simplex optimization, we have to
# delete any files that will be generated by Krotov, as they are
# now invalid (starting from the wrong guess pulse)
for file in ['oct.log', 'pulse.dat', 'oct_iters.dat', 'config.oct',
'U.dat', 'prop.log', 'simplex.log']:
file = os.path.join(runfolder, file)
if os.path.isfile(file):
os.unlink(file)
# run simplex optimization (pulse.json -> pulse_opt.json)
if target in ['PE', 'SQ']:
target_gate = target
extra_files_to_copy=[]
else:
target_gate_dat = os.path.join(runfolder, target)
target_gate = read_target_gate(target_gate_dat)
extra_files_to_copy=[target]
assert re.search(r'prop_guess\s*=\s*F', read_file(config))
assert re.search(r'oct_outfile\s*=\s*pulse.dat', read_file(config))
logger.debug("Running simplex to optimize for target %s" % target)
run_simplex(runfolder, target=target_gate, rwa=rwa,
prop_pulse_dat='pulse.dat',
extra_files_to_copy=extra_files_to_copy,
guess_pulse=guess, opt_pulse='pulse_opt.json', vary=vary,
fixed_parameters=['T', 'w_d', 'freq_1', 'freq_2'],
E0_min=E0_min)
def run_oct(runfolder, target='target_gate.dat', rwa=False,
continue_oct=False, g_a_int_min_initial=1.0e-5, g_a_int_max=1.0e-1,
g_a_int_converged=1.0e-7, iter_stop=None, J_T=None, lbfgs=False,
use_threads=False):
"""Run optimal control on the given runfolder. Adjust lambda_a if
necessary. Target may either be 'PE', 'SQ', or the name of file defining a
gate, inside the runfolder.
Assumes that the runfolder contains the files config and pulse.guess, the
file defined by `target`, and optionally, pulse.dat, and oct_iters.dat.
Creates (overwrites) the files pulse.dat and oct_iters.dat.
Also, a file config.oct is created that contains the last update to
lambda_a. The original config file will remain unchanged.
"""
logger = logging.getLogger(__name__)
temp_runfolder = get_temp_runfolder(runfolder)
QDYN.shutil.mkdir(temp_runfolder)
config = os.path.join(runfolder, 'config')
temp_config = os.path.join(temp_runfolder, 'config')
temp_pulse_dat = os.path.join(temp_runfolder, 'pulse.dat')
write_oct_config(config, temp_config, target, iter_stop=iter_stop,
J_T=J_T, lbfgs=lbfgs)
required_files = ['pulse.guess']
if target not in ['PE', 'SQ']:
required_files.append(target)
files_to_copy = list(required_files)
if continue_oct:
files_to_copy.extend(['pulse.dat', 'oct_iters.dat'])
for file in files_to_copy:
if os.path.isfile(os.path.join(runfolder, file)):
QDYN.shutil.copy(os.path.join(runfolder, file), temp_runfolder)
logger.debug("%s to temp_runfolder %s", file, temp_runfolder)
else:
if file in required_files:
raise IOError("%s does not exist in %s" % (file, runfolder))
logger.info("Starting optimization of %s (in %s)", runfolder,
temp_runfolder)
with open(os.path.join(runfolder, 'oct.log'), 'w', 0) as stdout:
ensure_ham_files(temp_runfolder, rwa=rwa, stdout=stdout)
# we assume that the value for lambda_a is badly chosen and iterate
# over optimizations until we find a good value
bad_lambda = True
pulse_explosion = False
trial = 0
given_up = False
while bad_lambda:
trial += 1
if trial > MAX_TRIALS:
bad_lambda = False
given_up = True
break # give up
env = os.environ.copy()
env['OMP_NUM_THREADS'] = '1'
if use_threads:
env['OMP_NUM_THREADS'] = '4'
oct_proc = sp.Popen(['tm_en_oct', '.'], cwd=temp_runfolder,
env=env, stdout=sp.PIPE)
iter = 0
g_a_int = 0.0
while True: # monitor STDOUT from oct
line = oct_proc.stdout.readline()
if line != '':
stdout.write(line)
m = re.search(r'^\s*(\d+) \| [\d.E+-]+ \| ([\d.E+-]+) \|',
line)
if m:
iter = int(m.group(1))
try:
g_a_int = float(m.group(2))
except ValueError:
# account for Fortran dropping the 'E' in negative
# 3-digit exponents
g_a_int = float(m.group(2).replace('-','E-'))
# Every 50 iterations, we take a snapshot of the current
# pulse, so that "bad lambda" restarts continue from there
if (iter > 0) and (iter % 50 == 0):
QDYN.shutil.copy(temp_pulse_dat,
temp_pulse_dat+'.'+str(iter))
# if the pulse changes in first iteration are too small, we
# lower lambda_a, unless lambda_a was previously adjusted
# to avoid exploding pulse values
if ( (iter == 1)
and (g_a_int < g_a_int_min_initial)
and (not pulse_explosion) ):
logger.debug("pulse update too small: %g < %g"
% (g_a_int, g_a_int_min_initial))
logger.debug("Kill %d" % oct_proc.pid)
oct_proc.kill()
scale_lambda_a(temp_config, 0.5)
reset_pulse(temp_pulse_dat, iter)
break # next bad_lambda loop
# if the pulse update explodes, we increase lambda_a (and
# prevent it from decreasing again)
if ( ('amplitude exceeds maximum value' in line)
or ('Loss of monotonic convergence' in line)
or (g_a_int > g_a_int_max) ):
pulse_explosion = True
if "Loss of monotonic convergence" in line:
logger.debug("loss of monotonic conversion")
else:
if (g_a_int > g_a_int_max):
logger.debug("g_a_int = %g > %g",
g_a_int, g_a_int_max)
logger.debug("pulse explosion")
logger.debug("Kill %d" % oct_proc.pid)
oct_proc.kill()
scale_lambda_a(temp_config, 1.25)
reset_pulse(temp_pulse_dat, iter)
break # next bad_lambda loop
# if there are no significant pulse changes anymore, we
# stop the optimization prematurely
if iter > 10 and g_a_int < g_a_int_converged:
logger.debug(("pulse update insignificant "
"(converged): g_a_int = %g < %g")
% (g_a_int, g_a_int_converged))
logger.debug("Kill %d" % oct_proc.pid)
oct_proc.kill()
bad_lambda = False
# add a comment to pulse.dat to mark it converged
p = Pulse(filename=temp_pulse_dat)
p.preamble.append("# converged")
p.write(temp_pulse_dat)
break # effectively break from bad_lambda loop
else: # line == ''
# OCT finished
bad_lambda = False
break # effectively break from bad_lambda loop
for file in ['pulse.dat', 'oct_iters.dat']:
if os.path.isfile(os.path.join(temp_runfolder, file)):
QDYN.shutil.copy(os.path.join(temp_runfolder, file), runfolder)
if os.path.isfile(os.path.join(temp_runfolder, 'config')):
QDYN.shutil.copy(os.path.join(temp_runfolder, 'config'),
os.path.join(runfolder, 'config.oct'))
QDYN.shutil.rmtree(temp_runfolder)
logger.debug("Removed temp_runfolder %s", temp_runfolder)
if given_up:
# Giving up is permanent, so we can mark the guess pulse as final
# by storing it as the optimized pulse. That should prevent pointlessly
# re-runing OCT
if not os.path.isfile(os.path.join(runfolder, 'pulse.dat')):
QDYN.shutil.copy(os.path.join(runfolder, 'pulse.guess'),
os.path.join(runfolder, 'pulse.dat'))
logger.info("Finished optimization (given up after too many "
"attempts): %s" % runfolder)
else:
logger.info("Finished optimization: %s" % runfolder)
def scale_lambda_a(config, factor):
"""Scale lambda_a in the given config file with the given factor"""
QDYN.shutil.copy(config, '%s~'%config)
logger = logging.getLogger(__name__)
lambda_a_pt = r'oct_lambda_a\s*=\s*([\deE.+-]+)'
with open('%s~'%config) as in_fh, open(config, 'w') as out_fh:
lambda_a = None
for line in in_fh:
m = re.search(lambda_a_pt, line)
if m:
lambda_a = float(m.group(1))
lambda_a_new = lambda_a * factor
logger.debug("%s: lambda_a: %.2e -> %.2e"
% (config, lambda_a, lambda_a_new))
line = re.sub(lambda_a_pt,
'oct_lambda_a = %.2e'%(lambda_a_new), line)
out_fh.write(line)
if lambda_a is None:
raise ValueError("no lambda_a in %s" % config)
def get_iter_stop(config):
"""Extract the value of iter_stop from the given config file"""
with open(config) as in_fh:
for line in in_fh:
m = re.search(r'iter_stop\s*=\s*(\d+)', line)
if m:
return int(m.group(1))
return None
@click.command(help=__doc__)
@click.help_option('--help', '-h')
@click.option('--target', metavar='TARGET', default='target_gate.dat',
show_default=True,
help="Optimization target. Can be 'PE', 'SQ', or the name of a gate "
"file inside the runfolder.")
@click.option('--J_T_re', 'J_T_re', is_flag=True, default=False,
help='If TARGET is a gate file, use a phase sensitive functional '
'instead of the default square-modulus functional')
@click.option('--J_T_LI', 'J_T_LI', is_flag=True, default=False,
help='If TARGET is a gate file, use the local-invariants-functional '
'instead of the default square-modulus functional')
@click.option('--lbfgs', is_flag=True, default=False,
help='If TARGET is a gate file, use the lbfgs optimization method. '
'Implies --J_T_re')
@click.option('--rwa', is_flag=True, default=False,
help="Perform all calculations in the RWA.")
@click.option('--continue', 'cont', is_flag=True, default=False,
help="Continue from an existing pulse.dat")
@click.option( '--debug', is_flag=True, default=False,
help="Enable debugging output")
@click.option('--threads', 'use_threads', is_flag=True, default=False,
help="Use 4 OpenMP threads (16 if --prop-rho)")
@click.option('--prop-only', is_flag=True, default=False,
help="Only propagate, instead of doing OCT")
@click.option(
'--prop-rho', is_flag=True, default=False,
help="Do the propagation in Liouville space.")
@click.option('--prop-n_qubit', type=int,
help="In the (post-oct) propagation, use the given "
"number of qubit levels, instead of the number specified in the "
"config file. Does not affect OCT.")
@click.option('--prop-n_cavity', type=int,
help="In the (post-OCT) propagation, use the given "
"number of cavity levels, instead of the number specified in the "
"config file. Does not affect OCT.")
@click.option('--rho-pop-plot', is_flag=True, default=False,
help="In combination with --prop-rho and --keep, "
"produce a population plot")
@click.option('--keep', is_flag=True, default=False,
help="Keep all files from the propagation")
@click.option('--pre-simplex', 'formula_or_json_file',
help="Run simplex pre-optimization before Krotov. Parameter may "
"either be the name of a pulse formula, or the name of a json file. "
"If it is a formula name, an analytic approximation to the existing "
"file 'pulse.guess' will be the guess pulse for the simplex "
"optimization. If it is a json file, then the analytic pulse "
"described in that file will be the guess pulse.")
@click.option('--vary', multiple=True,
help='If given in conjunction with '
'--pre-simplex, the parameter that will be varied in the simplex '
'search. Can be given multiple times to vary more than one parameter. '
'If not given, the parameters to be varied are chosen automatically')
@click.option('--scan', metavar='SCAN_PARAMS_JSON',
help="If given in conjunction with --pre-simplex, perform a systematic "
"scan of parameters before doing the simplex-pre-optimization. The file "
"SCAN_PARAMS_JSON must be a json dump of a dictionary that maps parameter "
"names to values to try. All possible combinations will be tried, and the "
"one with the best figure of merit will be the starting point for the "
"simplex optimization")
@click.option('--randomize', is_flag=True, default=False,
help="In combination with --pre-simplex, start from "
"a randomized analytic guess pulse, instead of one matching the "
"original numerical pulse.guess as closely as possible.")
@click.option('--g_a_int_min_initial', default=1.0e-5, type=float,
help="The smallest acceptable value "
"for g_a_int in the first OCT iteration. For any smaller value,"
"lambda_a is deemed too big, and will be adjusted.")
@click.option('--g_a_int_max', default=1.0e-1, type=float,
help="The largest acceptable value for "
"g_a_int. Any larger value is taken as a 'pulse explosion', "
"requiring lambda_a to be increased.")
@click.option('--g_a_int_converged', default=1.0e-7, type=float,
help="The smallest value for g_a_int "
"before the optimization is assumed to be converged.")
@click.option('--iter_stop', type=int,
help="The iteration number after which to stop OCT")
@click.option('--nt-min', default=2000, type=int,
help="The minimum nt to be used when converting an "
"analytical pulse to a numerical one.")
@click.option('--E0-min', 'E0_min', default=0.0, type=float,
help="In combination with --pre-simplex, the minimum pulse amplitude "
"that will be considered for a guess pulse to Krotov")
@click.argument('runfolder', type=click.Path(exists=True, dir_okay=True,
file_okay=False))
def main(target, J_T_re, J_T_LI, lbfgs, rwa, cont, debug, use_threads,
prop_only, prop_rho, prop_n_qubit, prop_n_cavity, rho_pop_plot, keep,
formula_or_json_file, vary, scan, randomize, g_a_int_min_initial,
g_a_int_max, g_a_int_converged, iter_stop, nt_min, E0_min, runfolder):
assert 'SCRATCH_ROOT' in os.environ, \
"SCRATCH_ROOT environment variable must be defined"
if iter_stop is None:
iter_stop = get_iter_stop(os.path.join(runfolder, 'config'))
pulse_file = (os.path.join(runfolder, 'pulse.dat'))
logger = logging.getLogger()
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
if prop_only:
perform_optimization = False
else:
perform_optimization = True
if os.path.isfile(pulse_file):
pulse = Pulse(pulse_file)
if pulse.oct_iter <= 1:
os.unlink(pulse_file)
logger.debug("pulse.dat in %s removed as invalid", runfolder)
else:
if pulse.oct_iter == iter_stop:
logger.info("OCT for %s already complete", runfolder)
perform_optimization = False
for line in pulse.preamble:
if "converged" in line:
perform_optimization = False
if perform_optimization:
if formula_or_json_file is not None:
if os.path.isfile(pulse_file) and cont:
logger.debug("Skip simplex, continuing existing pulse")
else:
if len(vary) == 0:
vary = 'default'
if scan is None:
logger.debug("Starting simplex")
# {formula_or_json_file} -> pulse_opt.json
run_pre_krotov_simplex(runfolder, formula_or_json_file,
vary=vary, target=target, rwa=rwa,
randomize=randomize, E0_min=E0_min)
else:
# {formula_or_json_file} -> pulse_systematic_scan.json
if os.path.isfile(
os.path.join(runfolder, formula_or_json_file)):
logger.debug("Starting systematic variation")
systematic_scan(runfolder, formula_or_json_file, scan,
outfile='pulse_systematic_scan.json',
target=target, rwa=rwa,
use_threads=use_threads)
else:
raise NotImplementedError("Scan is implemented only "
"for starting from an analytic pulse file, "
"not from a formula")
# pulse_systematic_scan.json -> pulse_opt.json
logger.debug("Starting simplex")
run_pre_krotov_simplex(runfolder,
'pulse_systematic_scan.json',
vary=vary, target=target, rwa=rwa,
randomize=randomize, E0_min=E0_min)
switch_to_analytical_guess(runfolder, num_guess='pulse.guess',
analytical_guess='pulse_opt.json',
backup='pulse.guess.pre_simplex', nt_min=nt_min)
if os.path.isfile(os.path.join(runfolder, 'U.dat')):
# if we're doing a new oct, we should delete U.dat
os.unlink(os.path.join(runfolder, 'U.dat'))
if os.path.isfile(os.path.join(runfolder, 'U_closest_PE.dat')):
os.unlink(os.path.join(runfolder, 'U_closest_PE.dat'))
J_T = None
if J_T_re:
J_T = 'RE'
if J_T_LI:
J_T = 'LI'
run_oct(runfolder, target=target, rwa=rwa, continue_oct=cont,
g_a_int_min_initial=g_a_int_min_initial,
g_a_int_max=g_a_int_max,
g_a_int_converged=g_a_int_converged,
iter_stop=iter_stop, J_T=J_T,
lbfgs=lbfgs, use_threads=use_threads)
if not os.path.isfile(os.path.join(runfolder, 'U.dat')):
propagate(runfolder, 'pulse.dat', rwa=rwa, rho=prop_rho,
rho_pop_plot=rho_pop_plot, n_qubit=prop_n_qubit,
n_cavity=prop_n_cavity, keep=keep, target=target,
use_threads=use_threads)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()