/
factory.py
1760 lines (1478 loc) · 77.5 KB
/
factory.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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Report generation functions.
"""
#***************************************************************************************************
# Copyright 2015, 2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain rights
# in this software.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 or in the LICENSE file in the root pyGSTi directory.
#***************************************************************************************************
import collections as _collections
import os as _os
import time as _time
import warnings as _warnings
import zipfile as _zipfile
import numpy as _np
from pygsti.report import Report as _Report
from pygsti.report import autotitle as _autotitle
from pygsti.report import merge_helpers as _merge
from pygsti.report import reportables as _reportables
from pygsti.report import section as _section
from pygsti.report import workspace as _ws
from pygsti._version import version as _pygsti_version
from pygsti import tools as _tools
from pygsti.models.explicitmodel import ExplicitOpModel as _ExplicitOpModel
from pygsti.baseobjs.statespace import StateSpace as _StateSpace
from pygsti.objectivefns import objectivefns as _objfns
from pygsti.objectivefns import wildcardbudget as _wildcardbudget
from pygsti.circuits.circuit import Circuit as _Circuit
from pygsti.circuits.circuitlist import CircuitList as _CircuitList
from pygsti.circuits.circuitstructure import PlaquetteGridCircuitStructure as _PlaquetteGridCircuitStructure
from pygsti.baseobjs.label import Label as _Lbl
from pygsti.baseobjs.verbosityprinter import VerbosityPrinter as _VerbosityPrinter
from pygsti.tools.legacytools import deprecate as _deprecated_fn
from pygsti.objectivefns.wildcardbudget import PrimitiveOpsSingleScaleWildcardBudget
#maybe import these from drivers.longsequence so they stay synced?
ROBUST_SUFFIX_LIST = [".robust", ".Robust", ".robust+", ".Robust+"] # ".wildcard" (not a separate estimate anymore)
DEFAULT_NONMARK_ERRBAR_THRESHOLD = 100000.0 # essentially disable since we have better ways of quantifying this now
def _add_new_labels(running_lbls, current_lbls):
"""
Simple routine to add current-labels to a list of
running-labels without introducing duplicates and
preserving order as best we can.
"""
if running_lbls is None:
return current_lbls[:] # copy!
elif running_lbls != current_lbls:
for lbl in current_lbls:
if lbl not in running_lbls:
running_lbls.append(lbl)
return running_lbls
def _add_new_estimate_labels(running_lbls, estimates, combine_robust):
"""
Like _add_new_labels but perform robust-suffix processing.
In particular, if `combine_robust == True` then do not add
labels which have a ".robust" counterpart.
"""
current_lbls = list(estimates.keys())
def _add_lbl(lst, lbl):
if combine_robust and any([(lbl + suffix in current_lbls)
for suffix in ROBUST_SUFFIX_LIST]):
return # don't add label
lst.append(lbl) # add label
if running_lbls is None:
running_lbls = []
if running_lbls != current_lbls:
for lbl in current_lbls:
if lbl not in running_lbls:
_add_lbl(running_lbls, lbl)
return running_lbls
#def _robust_estimate_has_same_models(estimates, est_lbl):
# lbl_robust = est_lbl+ROBUST_SUFFIX
# if lbl_robust not in estimates: return False #no robust estimate
#
# for mdl_lbl in list(estimates[est_lbl].goparameters.keys()) \
# + ['final iteration estimate']:
# if mdl_lbl not in estimates[lbl_robust].models:
# return False #robust estimate is missing mdl_lbl!
#
# mdl = estimates[lbl_robust].models[mdl_lbl]
# if estimates[est_lbl].models[mdl_lbl].frobeniusdist(mdl) > 1e-8:
# return False #model mismatch!
#
# return True
def _get_viewable_crf(est, est_lbl, mdl_lbl, verbosity=0):
printer = _VerbosityPrinter.create_printer(verbosity)
if est.has_confidence_region_factory(mdl_lbl, 'final'):
crf = est.create_confidence_region_factory(mdl_lbl, 'final')
if crf.can_construct_views():
return crf
else:
printer.log(
("Note: Confidence interval factory for {estlbl}.{gslbl} "
"model exists but cannot create views. This could be "
"because you forgot to create a Hessian *projection*"
).format(estlbl=est_lbl, gslbl=mdl_lbl))
else:
printer.log(
("Note: no factory to compute confidence "
"intervals for the '{estlbl}.{gslbl}' model."
).format(estlbl=est_lbl, gslbl=mdl_lbl))
return None
def create_offline_zip(output_dir="."):
"""
Creates a zip file containing the a directory ("offline") of files need to display "offline" reports.
This offline directory is often generated by reports when `connected=False` is specified..
For offline reports to display, the "offline" folder must be placed
in the same directory as the report's HTML file. This function can
be used to easily obtain a copy of the offline folder for the purpose
of sharing offline reports with other people. If you're just creating
your own offline reports using pyGSTi, the offline folder is
automatically copied into it's proper position - so you don't need
to call this function.
Parameters
----------
output_dir : str, optional
The directory in which "offline.zip" should be place.
Returns
-------
None
"""
templatePath = _os.path.join(_os.path.dirname(_os.path.abspath(__file__)),
"templates")
zipFName = _os.path.join(output_dir, "offline.zip")
zipHandle = _zipfile.ZipFile(zipFName, 'w', _zipfile.ZIP_DEFLATED)
for root, _, files in _os.walk(_os.path.join(templatePath, "offline")):
for f in files:
fullPath = _os.path.join(root, f)
zipHandle.write(fullPath, _os.path.relpath(fullPath, templatePath))
zipHandle.close()
# TODO remove
def _set_toggles(results_dict, brevity, combine_robust):
#Determine when to get circuit weight (scaling) values and show via
# ColorBoxPlots below by checking whether any estimate has "weights"
# parameter (a dict) with > 0 entries.
toggles = {}
toggles["ShowScaling"] = False
toggles["ShowUnmodeledError"] = False
for res in results_dict.values():
for est in res.estimates.values():
weights = est.parameters.get("weights", None)
if (weights is not None and len(weights) > 0):
toggles["ShowScaling"] = True
if est.parameters.get("unmodeled_error", None):
toggles["ShowUnmodeledError"] = True
toggles['BrevityLT1'] = bool(brevity < 1)
toggles['BrevityLT2'] = bool(brevity < 2)
toggles['BrevityLT3'] = bool(brevity < 3)
toggles['BrevityLT4'] = bool(brevity < 4)
toggles['CombineRobust'] = bool(combine_robust)
return toggles
def _create_master_switchboard(ws, results_dict, confidence_level,
nmthreshold, printer, fmt,
combine_robust, idt_results_dict=None, embed_figures=True):
"""
Creates the "master switchboard" used by several of the reports
"""
if isinstance(results_dict, _collections.OrderedDict):
dataset_labels = list(results_dict.keys())
else:
dataset_labels = sorted(list(results_dict.keys()))
est_labels = None
gauge_opt_labels = None
Ls = None
for results in results_dict.values():
est_labels = _add_new_estimate_labels(est_labels, results.estimates,
combine_robust)
loc_Ls = results.circuit_lists['final'].xs \
if isinstance(results.circuit_lists['final'], _PlaquetteGridCircuitStructure) else [0]
Ls = _add_new_labels(Ls, loc_Ls)
for est in results.estimates.values():
gauge_opt_labels = _add_new_labels(gauge_opt_labels,
list(est.goparameters.keys()))
Ls = list(sorted(Ls)) # make sure Ls are sorted in increasing order
# XXX i suspect it's not actually this easy
# if fmt == "latex" and len(Ls) > 0:
# swLs = [Ls[-1]] # "switched Ls" = just take the single largest L
# else:
# swLs = Ls # switch over all Ls
swLs = Ls
multidataset = bool(len(dataset_labels) > 1)
multiest = bool(len(est_labels) > 1)
multiGO = bool(len(gauge_opt_labels) > 1)
#multiL = bool(len(swLs) > 1)
switchBd = ws.Switchboard(
["Dataset", "Estimate", "Gauge-Opt", "max(L)"],
[dataset_labels, est_labels, gauge_opt_labels, list(map(str, swLs))],
["dropdown", "dropdown", "buttons", "slider"], [0, 0, 0, len(swLs) - 1],
show=[multidataset, multiest, multiGO, False], # "global" switches only + gauge-opt (OK if doesn't apply)
use_loadable_items=embed_figures
)
switchBd.add("ds", (0,))
switchBd.add("prep_fiducials", (0,))
switchBd.add("meas_fiducials", (0,))
switchBd.add("fiducials_tup", (0,))
switchBd.add("germs", (0,))
switchBd.add("eff_ds", (0, 1))
switchBd.add("modvi_ds", (0, 1))
switchBd.add("wildcard_budget", (0, 1, 2))
switchBd.add("wildcard_budget_optional", (0, 1, 2))
switchBd.add("scaled_submxs_dict", (0, 1))
switchBd.add("mdl_target", (0, 1))
switchBd.add("params", (0, 1))
switchBd.add("objfn_builder", (0, 1))
switchBd.add("objfn_builder_modvi", (0, 1))
switchBd.add("clifford_compilation", (0, 1))
switchBd.add("meta_stdout", (0, 1))
switchBd.add("profiler", (0, 1))
switchBd.add("mdl_gaugeinv", (0, 1))
switchBd.add("mdl_gaugeinv_ep", (0, 1))
switchBd.add("mdl_final", (0, 1, 2))
switchBd.add("mdl_eval_projected", (0, 1, 2))
switchBd.add("mdl_target_and_final", (0, 1, 2)) # general only!
switchBd.add("goparams", (0, 1, 2))
switchBd.add("mdl_current", (0, 1, 3))
switchBd.add("mdl_current_modvi", (0, 1, 3))
switchBd.add("circuits_current", (0, 3)) # current L value (iteration)
switchBd.add("circuits_final", (0,)) # final L value (iteration)
switchBd.add("mdl_all", (0, 1))
switchBd.add("mdl_all_modvi", (0, 1))
switchBd.add("circuits_all", (0,)) # a list of circuit lists, one per L-val (iteration)
switchBd.add("mdl_final_grid", (2,))
switchBd.add("idtresults", (0,))
if confidence_level is not None:
switchBd.add("cri", (0, 1, 2))
switchBd.add("cri_gaugeinv", (0, 1))
for d, dslbl in enumerate(dataset_labels):
results = results_dict[dslbl]
prep_fiducials = results.circuit_lists.get('prep fiducials', None)
meas_fiducials = results.circuit_lists.get('meas fiducials', None)
germs = results.circuit_lists.get('germs', None)
NA = ws.NotApplicable()
if prep_fiducials is None:
prep_fiducials = results.data.edesign.prep_fiducials \
if hasattr(results.data.edesign, 'prep_fiducials') else NA
if meas_fiducials is None:
meas_fiducials = results.data.edesign.meas_fiducials \
if hasattr(results.data.edesign, 'meas_fiducials') else NA
if germs is None:
germs = results.data.edesign.germs \
if hasattr(results.data.edesign, 'germs') else NA
switchBd.ds[d] = results.dataset
switchBd.prep_fiducials[d] = prep_fiducials
switchBd.meas_fiducials[d] = meas_fiducials
switchBd.fiducials_tup[d] = (prep_fiducials, meas_fiducials) \
if (prep_fiducials is not NA and meas_fiducials is not NA) else NA
switchBd.germs[d] = germs
switchBd.circuits_final[d] = results.circuit_lists['final']
loc_Ls = results.circuit_lists['final'].xs \
if isinstance(results.circuit_lists['final'], _PlaquetteGridCircuitStructure) else [0]
for iL, L in enumerate(swLs): # allow different results to have different Ls
if L in loc_Ls:
k = loc_Ls.index(L)
switchBd.circuits_current[d, iL] = results.circuit_lists['iteration'][k]
switchBd.circuits_all[d] = results.circuit_lists['iteration']
if idt_results_dict is not None:
switchBd.idtresults[d] = idt_results_dict.get(dslbl, None)
for i, lbl in enumerate(est_labels):
est = results.estimates.get(lbl, None)
if est is None: continue
for suffix in ROBUST_SUFFIX_LIST:
if combine_robust and lbl.endswith(suffix):
est_modvi = results.estimates.get(lbl[:-len(suffix)], est)
break
else:
est_modvi = est
switchBd.objfn_builder[d, i] = est.parameters.get(
'final_objfn_builder', _objfns.ObjectiveFunctionBuilder.create_from('logl'))
switchBd.objfn_builder_modvi[d, i] = est_modvi.parameters.get(
'final_objfn_builder', _objfns.ObjectiveFunctionBuilder.create_from('logl'))
switchBd.params[d, i] = est.parameters
switchBd.clifford_compilation[d, i] = est.parameters.get("clifford compilation", 'auto')
if switchBd.clifford_compilation[d, i] == 'auto':
switchBd.clifford_compilation[d, i] = find_std_clifford_compilation(
est.models['target'], printer)
switchBd.profiler[d, i] = est_modvi.parameters.get('profiler', None)
switchBd.meta_stdout[d, i] = est_modvi.meta.get('stdout', [('LOG', 1, "No standard output recorded")])
GIRepLbl = 'final iteration estimate' # replace with a gauge-opt label if it has a CI factory
if confidence_level is not None:
if _get_viewable_crf(est, lbl, GIRepLbl) is None:
for l in gauge_opt_labels:
if _get_viewable_crf(est, lbl, l) is not None:
GIRepLbl = l; break
# NOTE on modvi_ds (the dataset used in model violation plots)
# if combine_robust is True, modvi_ds is the unscaled dataset.
# if combine_robust is False, modvi_ds is the effective dataset
# for the estimate (potentially just the unscaled one)
#if this estimate uses robust scaling or wildcard budget
NA = ws.NotApplicable()
if est.parameters.get("weights", None):
effds, scale_subMxs = est.create_effective_dataset(True)
switchBd.eff_ds[d, i] = effds
switchBd.scaled_submxs_dict[d, i] = {'scaling': scale_subMxs, 'scaling.colormap': "revseq"}
switchBd.modvi_ds[d, i] = results.dataset if combine_robust else effds
else:
switchBd.modvi_ds[d, i] = results.dataset
switchBd.eff_ds[d, i] = NA
switchBd.scaled_submxs_dict[d, i] = NA
wildcard = est.parameters.get("unmodeled_error", None)
if isinstance(wildcard, dict): # this is either a serialized budget object
#or a dictionary of serialized budget objects. Let's check which:
#technically the following could get broken by a user naming a gauge-opt
#opt suite 'module', I'll think of a better fix for this at some point.
if 'module' in wildcard.keys():
wildcard = _wildcardbudget.WildcardBudget.from_nice_serialization(wildcard)
else:
wildcard = {lbl:_wildcardbudget.WildcardBudget.from_nice_serialization(budget) for lbl,budget in wildcard.items()}
for j, gokey in enumerate(gauge_opt_labels):
switchBd.wildcard_budget_optional[d, i, j] = None
if wildcard is not None:
if isinstance(wildcard, _wildcardbudget.WildcardBudget):
switchBd.wildcard_budget[d, i, j] = wildcard
switchBd.wildcard_budget_optional[d, i, j] = wildcard
elif isinstance(wildcard, dict):
switchBd.wildcard_budget[d, i, j] = wildcard.get(gokey, NA)
switchBd.wildcard_budget_optional[d, i, j] = wildcard.get(gokey, None)
else:
switchBd.wildcard_budget[d, i, j] = NA
switchBd.mdl_target[d, i] = est.models['target']
switchBd.mdl_gaugeinv[d, i] = est.models[GIRepLbl]
try:
switchBd.mdl_gaugeinv_ep[d, i] = _tools.project_to_target_eigenspace(est.models[GIRepLbl],
est.models['target'])
except AttributeError: # Implicit models don't support everything, like set_all_parameterizations
switchBd.mdl_gaugeinv_ep[d, i] = None
except _np.linalg.LinAlgError: # Failure to compute a best-case gauge transform can happen w/reduced models
switchBd.mdl_gaugeinv_ep[d, i] = None
except (ValueError, AssertionError): # if target is badly off, e.g. an imaginary part assertion
switchBd.mdl_gaugeinv_ep[d, i] = None
switchBd.mdl_final[d, i, :] = [est.models.get(l, NA) for l in gauge_opt_labels]
switchBd.mdl_target_and_final[d, i, :] = \
[[est.models['target'], est.models[l]] if (l in est.models) else NA
for l in gauge_opt_labels]
#Add some logic to allow for the value of the gaugeoptparams dict to be None
#(so far this only shows up in certain ModelTest scenarios).
switchBd.goparams[d, i, :] = [est.goparameters.get(l, NA) if est.goparameters.get(l, NA) is not None
else NA for l in gauge_opt_labels]
for iL, L in enumerate(swLs): # allow different results to have different Ls
if L in loc_Ls:
k = loc_Ls.index(L)
switchBd.mdl_current[d, i, iL] = est.models['iteration %d estimate' % k]
switchBd.mdl_current_modvi[d, i, iL] = est_modvi.models['iteration %d estimate' % k]
switchBd.mdl_all[d, i] = [est.models['iteration %d estimate' % k] for k in range(est.num_iterations)]
switchBd.mdl_all_modvi[d, i] = [est_modvi.models['iteration %d estimate' % k]
for k in range(est_modvi.num_iterations)]
if confidence_level is not None:
misfit_sigma = est.misfit_sigma()
for il, l in enumerate(gauge_opt_labels):
if l in est.models:
switchBd.cri[d, i, il] = None # default
crf = _get_viewable_crf(est, lbl, l, printer - 2)
if crf is not None:
#Check whether we should use non-Markovian error bars:
# If fit is bad, check if any reduced fits were computed
# that we can use with in-model error bars. If not, use
# experimental non-markovian error bars.
region_type = "normal" if misfit_sigma <= nmthreshold \
else "non-markovian"
switchBd.cri[d, i, il] = crf.view(confidence_level, region_type)
else: switchBd.cri[d, i, il] = NA
# "Gauge Invariant Representation" model
# If we can't compute CIs for this, ignore SILENTLY, since any
# relevant warnings/notes should have been given above.
switchBd.cri_gaugeinv[d, i] = None # default
crf = _get_viewable_crf(est, lbl, GIRepLbl)
if crf is not None:
region_type = "normal" if misfit_sigma <= nmthreshold \
else "non-markovian"
switchBd.cri_gaugeinv[d, i] = crf.view(confidence_level, region_type)
results_list = [results_dict[dslbl] for dslbl in dataset_labels]
for i, gokey in enumerate(gauge_opt_labels):
if multidataset:
switchBd.mdl_final_grid[i] = [
[(res.estimates[el].models.get(gokey, None)
if el in res.estimates else None) for el in est_labels]
for res in results_list]
else:
switchBd.mdl_final_grid[i] = [
(results_list[0].estimates[el].models.get(gokey, None)
if el in results_list[0].estimates else None) for el in est_labels]
if multidataset:
switchBd.add_unswitched('mdl_target_grid', [
[(res.estimates[el].models.get('target', None)
if el in res.estimates else None) for el in est_labels]
for res in results_list])
else:
switchBd.add_unswitched('mdl_target_grid', [
(results_list[0].estimates[el].models.get('target', None)
if el in results_list[0].estimates else None) for el in est_labels])
return switchBd, dataset_labels, est_labels, gauge_opt_labels, Ls, swLs
def _construct_idtresults(idt_idle_op, idt_pauli_dicts, gst_results_dict, printer):
"""
Constructs a dictionary of idle tomography results, parallel
to the GST results in `gst_results_dict`, where possible.
"""
if idt_pauli_dicts is None:
return {}
idt_results_dict = {}
from ..extras import idletomography as _idt
autodict = bool(idt_pauli_dicts == "auto")
for ky, results in gst_results_dict.items():
if autodict:
for est in results.estimates.values():
if 'target' in est.models:
idt_target = est.models['target']
break
else: continue # can't find any target models
idt_pauli_dicts = _idt.determine_paulidicts(idt_target)
if idt_pauli_dicts is None:
continue # automatic creation failed -> skip
qubit_labels = idt_target.state_space.sole_tensor_product_block_labels
GiStr = _Circuit((idt_idle_op,), line_labels=qubit_labels)
circuits_final = results.circuit_lists['final']
if not isinstance(circuits_final, _CircuitList): continue
circuit_struct = _PlaquetteGridCircuitStructure.cast(circuits_final)
if GiStr not in circuit_struct.ys: continue
try: # to get a dimension -> nQubits
estLabels = list(results.estimates.keys())
estimate0 = results.estimates[estLabels[0]]
dim = estimate0.models['target'].dim
nQubits = int(round(_np.log2(dim) // 2))
idStr = ('Gi',) if 'Gi' in estimate0.models['target'].primitive_op_labels else ((),)
except:
printer.log(" ! Skipping idle tomography on %s dataset (can't get # qubits) !" % ky)
continue # skip if we can't get dimension
maxLengths = circuit_struct.xs
# just use "L0" (first maxLength) - all should have same fidpairs
plaq = circuit_struct.plaquette(maxLengths[0], GiStr)
pauli_fidpairs = _idt.fidpairs_to_pauli_fidpairs(list(plaq.fidpairs.values()), idt_pauli_dicts, nQubits)
idt_advanced = {'pauli_fidpairs': pauli_fidpairs, 'jacobian mode': "together"}
printer.log(" * Running idle tomography on %s dataset *" % ky)
idtresults = _idt.do_idle_tomography(nQubits, results.dataset, maxLengths, idt_pauli_dicts,
maxweight=2, # HARDCODED for now (FUTURE)
idle_string=idStr, advanced_options=idt_advanced)
idt_results_dict[ky] = idtresults
return idt_results_dict
def _create_single_metric_switchboard(ws, results_dict, b_gauge_inv,
dataset_labels, est_labels=None, embed_figures=True):
op_labels = None
for results in results_dict.values():
for est in results.estimates.values():
if 'target' in est.models:
op_labels = _add_new_labels(op_labels,
list(est.models['target'].operations.keys()))
if b_gauge_inv:
metric_abbrevs = ["evinf", "evagi", "evnuinf", "evnuagi", "evdiamond",
"evnudiamond"]
else:
metric_abbrevs = ["inf", "agi", "trace", "diamond", "nuinf", "nuagi",
"frob"]
metric_names = [_reportables.info_of_opfn_by_name(abbrev)[0].replace('|', ' ')
for abbrev in metric_abbrevs]
if len(dataset_labels) > 1: # multidataset
metric_switchBd = ws.Switchboard(
["Metric", "Operation"], [metric_names, op_labels],
["dropdown", "dropdown"], [0, 0], show=[True, True],
use_loadable_items=embed_figures)
metric_switchBd.add("op_label", (1,))
metric_switchBd.add("metric", (0,))
metric_switchBd.add("cmp_table_title", (0, 1))
metric_switchBd.op_label[:] = op_labels
for i, gl in enumerate(op_labels):
metric_switchBd.cmp_table_title[:, i] = ["%s %s" % (gl, nm) for nm in metric_names]
else:
metric_switchBd = ws.Switchboard(
["Metric"], [metric_names],
["dropdown"], [0], show=[True],
use_loadable_items=embed_figures)
metric_switchBd.add("metric", (0,))
metric_switchBd.add("cmp_table_title", (0,))
metric_switchBd.cmp_table_title[:] = metric_names
metric_switchBd.metric[:] = metric_abbrevs
return metric_switchBd
@_deprecated_fn('pygsti.report.construct_standard_report(...).write_html(...)')
def create_general_report(results, filename, title="auto",
confidence_level=None,
linlog_percentile=5, errgen_type="logGTi",
nmthreshold=DEFAULT_NONMARK_ERRBAR_THRESHOLD, precision=None,
comm=None, ws=None, auto_open=False,
cachefile=None, brief=False, connected=False,
link_to=None, resizable=True, autosize='initial',
verbosity=1):
"""
DEPRECATED: use pygsti.report.create_standard_report(...)
.. deprecated:: v0.9.9
`create_general_report` will be removed in the next major release of pyGSTi. It is replaced by
`construct_standard_report`, which returns a :class:`Report` object.
"""
_warnings.warn(
('create_general_report(...) will be removed from pyGSTi.\n'
' This function only ever existed in beta versions and will\n'
' be removed completely soon. Please update this call with:\n'
' pygsti.report.create_standard_report(...).write_html(...)\n'))
@_deprecated_fn('construct_standard_report(...).write_html(...)')
def create_standard_report(results, filename, title="auto",
confidence_level=None, comm=None, ws=None,
auto_open=False, link_to=None, brevity=0,
advanced_options=None, verbosity=1):
"""
Create a "standard" GST report, containing details about each estimate in `results` individually.
Either a PDF or HTML report is generated, based on whether `filename` ends
in ".pdf" or not. In the richer HTML-mode, switches (drop-down boxes,
buttons, etc.) allow the viewer to choose which estimate is displayed. The
estimates in multiple :class:`Results` objects can be viewed by providing
a dictionary of `Results` objects as the `results` argument. Note that
when comparing many estimates it is often more convenient to view the report
generated by :func:`create_comparison_report`, which is organized for this
purpose.
In PDF-mode this interactivity is not possible and so `results` may contain
just a *single* estimate. The chief advantage of this more limited mode
is that is produces a highly-portable and self-contained PDF file.
.. deprecated:: v0.9.9
`create_standard_report` will be removed in the next major release of pyGSTi. It is replaced by
`construct_standard_report`, which returns a :class:`Report` object.
Parameters
----------
results : Results
An object which represents the set of results from one *or more* GST
estimation runs, typically obtained from running
:func:`run_long_sequence_gst` or :func:`run_stdpractice_gst`, OR a
dictionary of such objects, representing multiple GST runs to be
compared (typically all with *different* data sets). The keys of this
dictionary are used to label different data sets that are selectable
in the report.
filename : string, optional
The output filename where the report file(s) will be saved. If
None, then no output file is produced (but returned Workspace
still caches all intermediate results).
title : string, optional
The title of the report. "auto" causes a random title to be
generated (which you may or may not like).
confidence_level : int, optional
If not None, then the confidence level (between 0 and 100) used in
the computation of confidence regions/intervals. If None, no
confidence regions or intervals are computed.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
ws : Workspace, optional
The workspace used as a scratch space for performing the calculations
and visualizations required for this report. If you're creating
multiple reports with similar tables, plots, etc., it may boost
performance to use a single Workspace for all the report generation.
auto_open : bool, optional
If True, automatically open the report in a web browser after it
has been generated.
link_to : list, optional
If not None, a list of one or more items from the set
{"tex", "pdf", "pkl"} indicating whether or not to
create and include links to Latex, PDF, and Python pickle
files, respectively. "tex" creates latex source files for
tables; "pdf" renders PDFs of tables and plots ; "pkl" creates
Python versions of plots (pickled python data) and tables (pickled
pandas DataFrams).
brevity : int, optional
Amount of detail to include in the report. Larger values mean smaller
"more briefr" reports, which reduce generation time, load time, and
disk space consumption. In particular:
- 1: Plots showing per-sequences quantities disappear at brevity=1
- 2: Reference sections disappear at brevity=2
- 3: Germ-level estimate tables disappear at brevity=3
- 4: Everything but summary figures disappears at brevity=4
advanced_options : dict, optional
A dictionary of advanced options for which the default values are usually
are fine. Here are the possible keys of `advanced_options`:
- connected : bool, optional
Whether output HTML should assume an active internet connection. If
True, then the resulting HTML file size will be reduced because it
will link to web resources (e.g. CDN libraries) instead of embedding
them.
- cachefile : str, optional
filename with cached workspace results
- linlogPercentile : float, optional
Specifies the colorscale transition point for any logL or chi2 color
box plots. The lower `(100 - linlogPercentile)` percentile of the
expected chi2 distribution is shown in a linear grayscale, and the
top `linlogPercentile` is shown on a logarithmic colored scale.
- errgen_type: {"logG-logT", "logTiG", "logGTi"}
The type of error generator to compute. Allowed values are:
- "logG-logT" : errgen = log(gate) - log(target_op)
- "logTiG" : errgen = log( dot(inv(target_op), gate) )
- "logGTi" : errgen = log( dot(gate, inv(target_op)) )
- nmthreshold : float, optional
The threshold, in units of standard deviations, that triggers the
usage of non-Markovian error bars. If None, then non-Markovian
error bars are never computed.
- precision : int or dict, optional
The amount of precision to display. A dictionary with keys
"polar", "sci", and "normal" can separately specify the
precision for complex angles, numbers in scientific notation, and
everything else, respectively. If an integer is given, it this
same value is taken for all precision types. If None, then
`{'normal': 6, 'polar': 3, 'sci': 0}` is used.
- resizable : bool, optional
Whether plots and tables are made with resize handles and can be
resized within the report.
- autosize : {'none', 'initial', 'continual'}
Whether tables and plots should be resized, either initially --
i.e. just upon first rendering (`"initial"`) -- or whenever
the browser window is resized (`"continual"`).
- embed_figures: bool, optional
Whether figures should be embedded in the generated report.
- combine_robust : bool, optional
Whether robust estimates should automatically be combined with
their non-robust counterpart when displayed in reports. (default
is True).
- confidence_interval_brevity : int, optional
Roughly specifies how many figures will have confidence intervals
(when applicable). Defaults to '1'. Smaller values mean more
tables will get confidence intervals (and reports will take longer
to generate).
- idt_basis_dicts : tuple, optional
Tuple of (prepDict,measDict) pauli-basis dictionaries, which map
between 1-qubit Pauli basis strings (e.g. `'-X'` or `'Y'`) and tuples
of gate names (e.g. `('Gx','Gx')`). If given, idle tomography will
be performed on the 'Gi' gate and included in the report.
- idt_idle_oplabel : Label, optional
The label identifying the idle gate (for use with idle tomography).
- colorboxplot_bgcolor : str, optional
Background color for the color box plots in this report. Can be common
color names, e.g. `"black"`, or string RGB values, e.g. `"rgb(255,128,0)"`.
verbosity : int, optional
How much detail to send to stdout.
Returns
-------
Workspace
The workspace object used to create the report
"""
# Wrap a call to the new factory method
ws = ws or _ws.Workspace()
report = construct_standard_report(
results, title, confidence_level, comm, ws, advanced_options, verbosity
)
advanced_options = advanced_options or {}
precision = advanced_options.get('precision', None)
if filename is not None:
if filename.endswith(".pdf"):
report.write_pdf(
filename, build_options=advanced_options,
brevity=brevity, precision=precision,
auto_open=auto_open, verbosity=verbosity
)
else:
resizable = advanced_options.get('resizable', True)
autosize = advanced_options.get('autosize', 'initial')
connected = advanced_options.get('connected', False)
single_file = filename.endswith(".html")
report.write_html(
filename, auto_open=auto_open, link_to=link_to,
connected=connected, build_options=advanced_options,
brevity=brevity, precision=precision,
resizable=resizable, autosize=autosize,
single_file=single_file, verbosity=verbosity
)
return ws
@_deprecated_fn('construct_nqnoise_report(...).write_html(...)')
def create_nqnoise_report(results, filename, title="auto",
confidence_level=None, comm=None, ws=None,
auto_open=False, link_to=None, brevity=0,
advanced_options=None, verbosity=1):
"""
Creates a report designed to display results containing for n-qubit noisy model estimates.
Such models are characterized by the fact that gates and SPAM objects may
not have dense representations (or it may be very expensive to compute them)
, and that these models are likely :class:`CloudNoiseModel` objects or have
similar structure.
.. deprecated:: v0.9.9
`create_nqnoise_report` will be removed in the next major release of pyGSTi. It is replaced by
`construct_standard_report`, which returns a :class:`Report` object.
Parameters
----------
results : Results
An object which represents the set of results from one *or more* GST
estimation runs, typically obtained from running
:func:`run_long_sequence_gst` or :func:`run_stdpractice_gst`, OR a
dictionary of such objects, representing multiple GST runs to be
compared (typically all with *different* data sets). The keys of this
dictionary are used to label different data sets that are selectable
in the report.
filename : string, optional
The output filename where the report file(s) will be saved. If
None, then no output file is produced (but returned Workspace
still caches all intermediate results).
title : string, optional
The title of the report. "auto" causes a random title to be
generated (which you may or may not like).
confidence_level : int, optional
If not None, then the confidence level (between 0 and 100) used in
the computation of confidence regions/intervals. If None, no
confidence regions or intervals are computed.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
ws : Workspace, optional
The workspace used as a scratch space for performing the calculations
and visualizations required for this report. If you're creating
multiple reports with similar tables, plots, etc., it may boost
performance to use a single Workspace for all the report generation.
auto_open : bool, optional
If True, automatically open the report in a web browser after it
has been generated.
link_to : list, optional
If not None, a list of one or more items from the set
{"tex", "pdf", "pkl"} indicating whether or not to
create and include links to Latex, PDF, and Python pickle
files, respectively. "tex" creates latex source files for
tables; "pdf" renders PDFs of tables and plots ; "pkl" creates
Python versions of plots (pickled python data) and tables (pickled
pandas DataFrams).
brevity : int, optional
Amount of detail to include in the report. Larger values mean smaller
"more briefr" reports, which reduce generation time, load time, and
disk space consumption. In particular:
- 1: Plots showing per-sequences quantities disappear at brevity=1
- 2: Reference sections disappear at brevity=2
- 3: Germ-level estimate tables disappear at brevity=3
- 4: Everything but summary figures disappears at brevity=4
advanced_options : dict, optional
A dictionary of advanced options for which the default values are usually
are fine. Here are the possible keys of `advanced_options`:
- connected : bool, optional
Whether output HTML should assume an active internet connection. If
True, then the resulting HTML file size will be reduced because it
will link to web resources (e.g. CDN libraries) instead of embedding
them.
- cachefile : str, optional
filename with cached workspace results
- linlogPercentile : float, optional
Specifies the colorscale transition point for any logL or chi2 color
box plots. The lower `(100 - linlogPercentile)` percentile of the
expected chi2 distribution is shown in a linear grayscale, and the
top `linlogPercentile` is shown on a logarithmic colored scale.
- nmthreshold : float, optional
The threshold, in units of standard deviations, that triggers the
usage of non-Markovian error bars. If None, then non-Markovian
error bars are never computed.
- precision : int or dict, optional
The amount of precision to display. A dictionary with keys
"polar", "sci", and "normal" can separately specify the
precision for complex angles, numbers in scientific notation, and
everything else, respectively. If an integer is given, it this
same value is taken for all precision types. If None, then
`{'normal': 6, 'polar': 3, 'sci': 0}` is used.
- resizable : bool, optional
Whether plots and tables are made with resize handles and can be
resized within the report.
- autosize : {'none', 'initial', 'continual'}
Whether tables and plots should be resized, either initially --
i.e. just upon first rendering (`"initial"`) -- or whenever
the browser window is resized (`"continual"`).
- combine_robust : bool, optional
Whether robust estimates should automatically be combined with
their non-robust counterpart when displayed in reports. (default
is True).
- confidence_interval_brevity : int, optional
Roughly specifies how many figures will have confidence intervals
(when applicable). Defaults to '1'. Smaller values mean more
tables will get confidence intervals (and reports will take longer
to generate).
- colorboxplot_bgcolor : str, optional
Background color for the color box plots in this report. Can be common
color names, e.g. `"black"`, or string RGB values, e.g. `"rgb(255,128,0)"`.
verbosity : int, optional
How much detail to send to stdout.
Returns
-------
Workspace
The workspace object used to create the report
"""
# Wrap a call to the new factory method
ws = ws or _ws.Workspace()
report = construct_nqnoise_report(
results, title, confidence_level, comm, ws, advanced_options, verbosity
)
advanced_options = advanced_options or {}
precision = advanced_options.get('precision', None)
if filename is not None:
if filename.endswith(".pdf"):
report.write_pdf(
filename, build_options=advanced_options,
brevity=brevity, precision=precision,
auto_open=auto_open, verbosity=verbosity
)
else:
resizable = advanced_options.get('resizable', True)
autosize = advanced_options.get('autosize', 'initial')
connected = advanced_options.get('connected', False)
single_file = filename.endswith(".html")
report.write_html(
filename, auto_open=auto_open, link_to=link_to,
connected=connected, build_options=advanced_options,
brevity=brevity, precision=precision,
resizable=resizable, autosize=autosize,
single_file=single_file, verbosity=verbosity
)
return ws
@_deprecated_fn('construct_standard_report(...).write_notebook(...)')
def create_report_notebook(results, filename, title="auto",
confidence_level=None,
auto_open=False, connected=False, verbosity=0):
"""
Create a "report notebook".
A Jupyter ipython notebook file which, when its cells are executed, will generate
similar figures to those contained in an html report (via
:func:`create_standard_report`).
A notebook report allows the user to interact more flexibly with the data
underlying the figures, and to easily generate customized variants on the
figures. As such, this type of report will be most useful for experts
who want to tinker with the standard analysis presented in the static
HTML or LaTeX format reports.
.. deprecated:: v0.9.9
`create_report_notebook` will be removed in the next major release of pyGSTi. It is replaced by
the `Report.write_notebook`
Parameters
----------
results : Results
An object which represents the set of results from one *or more* GST
estimation runs, typically obtained from running
:func:`run_long_sequence_gst` or :func:`run_stdpractice_gst`, OR a
dictionary of such objects, representing multiple GST runs to be
compared (typically all with *different* data sets). The keys of this
dictionary are used to label different data sets that are selectable
(via setting Python variables) in the report.
filename : string, optional
The output filename where the report file(s) will be saved. Must end
in ".ipynb".