Skip to content

Commit b4b3451

Browse files
authored
Merge pull request #561 from sandialabs/bugfix-fpr-plaquettes
Change LGST Circuit Storage with FPR (Color Boxplot Fix)
2 parents 86142fd + 72b7b2d commit b4b3451

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

Diff for: pygsti/circuits/gstcircuits.py

+43-44
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
from pygsti.tools import listtools as _lt
2727
from pygsti.tools.legacytools import deprecate as _deprecated_fn
2828

29+
from typing import Dict
30+
ordereddict = Dict
31+
2932

3033
def _create_raw_lsgst_lists(op_label_src, prep_strs, effect_strs, germ_list, max_length_list,
3134
fid_pairs=None, trunc_scheme="whole germ powers", nest=True,
@@ -325,7 +328,8 @@ def create_lsgst_circuit_lists(op_label_src, prep_fiducials, meas_fiducials, ger
325328
indexing a string within prep_strs and effect_strs, respectively, so
326329
that prepStr = prep_strs[iPrepStr] and effectStr =
327330
effect_strs[iEffectStr]. If a dictionary, keys are germs (elements
328-
of germ_list) and values are lists of 2-tuples specifying the pairs
331+
of germ_list) or tuples of germs and length values
332+
and values are lists of 2-tuples specifying the pairs
329333
to use for that germ.
330334
331335
trunc_scheme : str, optional
@@ -414,9 +418,8 @@ def create_lsgst_circuit_lists(op_label_src, prep_fiducials, meas_fiducials, ger
414418
[c.str for c in prep_fiducials]
415419
[c.str for c in meas_fiducials]
416420

417-
#print('Germs: ', germs)
418-
419421
def filter_ds(circuits, ds, missing_lgst):
422+
"""Filter a list of circuits so that only those appearing in dataset are included."""
420423
if ds is None: return circuits[:]
421424
filtered_circuits = []
422425
for opstr in circuits:
@@ -444,8 +447,7 @@ def add_to_plaquettes(pkey_dict, plaquette_dict, base_circuit, maxlen, germ, pow
444447
for i in reversed(inds_to_remove):
445448
del fidpair_indices[i]
446449

447-
fidpairs = _collections.OrderedDict([((j, i), (prep_fiducials[i], meas_fiducials[j]))
448-
for i, j in fidpair_indices])
450+
fidpairs: ordereddict = {(j, i): (prep_fiducials[i], meas_fiducials[j]) for i, j in fidpair_indices}
449451

450452
if base_circuit not in plaquette_dict:
451453
pkey_dict[base_circuit] = (maxlen, germ)
@@ -528,7 +530,7 @@ def add_to_plaquettes(pkey_dict, plaquette_dict, base_circuit, maxlen, germ, pow
528530
if nest:
529531
#keep track of running quantities used to build circuit structures
530532
running_plaquette_keys = {} # base-circuit => (maxlength, germ) key for final plaquette dict
531-
running_plaquettes = _collections.OrderedDict() # keep consistent ordering in produced circuit list.
533+
running_plaquettes: ordereddict = dict() # keep consistent ordering in produced circuit list.
532534
running_unindexed = []
533535
running_maxLens = []
534536

@@ -554,7 +556,7 @@ def add_to_plaquettes(pkey_dict, plaquette_dict, base_circuit, maxlen, germ, pow
554556
unindexed = running_unindexed
555557
else: # create a new cs for just this maxLen
556558
pkey = {} # base-circuit => (maxlength, germ) key for final plaquette dict
557-
plaquettes = _collections.OrderedDict()
559+
plaquettes: ordereddict = dict()
558560
maxLens = [maxLen]
559561
unindexed = []
560562

@@ -586,39 +588,39 @@ def add_to_plaquettes(pkey_dict, plaquette_dict, base_circuit, maxlen, germ, pow
586588
if power == 0 and len(germ) != 0:
587589
continue
588590

589-
# Switch on fidpair dicts with germ or (germ, L) keys
590-
key = germ
591-
if fidpair_germ_power_keys:
592-
key = (germ, maxLen_thisgerm)
593-
594-
if rndm is None:
595-
fiducialPairsThisIter = fidPairDict.get(key, allPossiblePairs) \
596-
if fidPairDict is not None else allPossiblePairs
597-
#if fiducialPairsThisIter==allPossiblePairs:
598-
# print('Couldn\'t find ', key, ' using allPossiblePairs')
599-
#print('FiducialPairsThisIter: ', fiducialPairsThisIter)
600-
elif fidPairDict is not None:
601-
pair_indx_tups = fidPairDict.get(key, allPossiblePairs)
602-
remainingPairs = [(i, j)
603-
for i in range(len(prep_fiducials))
604-
for j in range(len(meas_fiducials))
605-
if (i, j) not in pair_indx_tups]
606-
nPairsRemaining = len(remainingPairs)
607-
nPairsToChoose = nPairsToKeep - len(pair_indx_tups)
608-
nPairsToChoose = max(0, min(nPairsToChoose, nPairsRemaining))
609-
assert(0 <= nPairsToChoose <= nPairsRemaining)
610-
# FUTURE: issue warnings when clipping nPairsToChoose?
611-
612-
fiducialPairsThisIter = fidPairDict[key] + \
613-
[remainingPairs[k] for k in
614-
sorted(rndm.choice(nPairsRemaining, nPairsToChoose,
615-
replace=False))]
616-
617-
else: # rndm is not None and fidPairDict is None
618-
assert(nPairsToKeep <= nPairs) # keep_fraction must be <= 1.0
619-
fiducialPairsThisIter = \
620-
[allPossiblePairs[k] for k in
621-
sorted(rndm.choice(nPairs, nPairsToKeep, replace=False))]
591+
if include_lgst and i == 0:
592+
fiducialPairsThisIter = allPossiblePairs
593+
else:
594+
# Switch on fidpair dicts with germ or (germ, L) keys
595+
key = germ
596+
if fidpair_germ_power_keys:
597+
key = (germ, maxLen_thisgerm)
598+
599+
if rndm is None:
600+
fiducialPairsThisIter = fidPairDict.get(key, allPossiblePairs) \
601+
if fidPairDict is not None else allPossiblePairs
602+
elif fidPairDict is not None:
603+
pair_indx_tups = fidPairDict.get(key, allPossiblePairs)
604+
remainingPairs = [(i, j)
605+
for i in range(len(prep_fiducials))
606+
for j in range(len(meas_fiducials))
607+
if (i, j) not in pair_indx_tups]
608+
nPairsRemaining = len(remainingPairs)
609+
nPairsToChoose = nPairsToKeep - len(pair_indx_tups)
610+
nPairsToChoose = max(0, min(nPairsToChoose, nPairsRemaining))
611+
assert(0 <= nPairsToChoose <= nPairsRemaining)
612+
# FUTURE: issue warnings when clipping nPairsToChoose?
613+
614+
fiducialPairsThisIter = fidPairDict[key] + \
615+
[remainingPairs[k] for k in
616+
sorted(rndm.choice(nPairsRemaining, nPairsToChoose,
617+
replace=False))]
618+
619+
else: # rndm is not None and fidPairDict is None
620+
assert(nPairsToKeep <= nPairs) # keep_fraction must be <= 1.0
621+
fiducialPairsThisIter = \
622+
[allPossiblePairs[k] for k in
623+
sorted(rndm.choice(nPairs, nPairsToKeep, replace=False))]
622624

623625
add_to_plaquettes(pkey, plaquettes, germ_power, maxLen, germ, power,
624626
fiducialPairsThisIter, dscheck, missing_list)
@@ -630,8 +632,7 @@ def add_to_plaquettes(pkey_dict, plaquette_dict, base_circuit, maxlen, germ, pow
630632
unindexed = unindexed[:]
631633

632634
lsgst_structs.append(
633-
_PlaquetteGridCircuitStructure(_collections.OrderedDict([(pkey[base], plaq)
634-
for base, plaq in plaquettes.items()]),
635+
_PlaquetteGridCircuitStructure({pkey[base]:plaq for base, plaq in plaquettes.items()},
635636
maxLens, germs, "L", "germ", unindexed, op_label_aliases,
636637
circuit_weights_dict=None, additional_circuits_location='start', name=None))
637638
tot_circuits += len(lsgst_structs[-1]) # only relevant for non-nested case
@@ -641,11 +642,9 @@ def add_to_plaquettes(pkey_dict, plaquette_dict, base_circuit, maxlen, germ, pow
641642

642643
printer.log("--- Circuit Creation ---", 1)
643644
printer.log(" %d circuits created" % tot_circuits, 2)
644-
#print("Total Number of Circuits: ", tot_circuits)
645645
if dscheck:
646646
printer.log(" Dataset has %d entries: %d utilized, %d requested circuits were missing"
647647
% (len(dscheck), tot_circuits, len(missing_list)), 2)
648-
#print(len(missing_lgst))
649648
if len(missing_list) > 0 or len(missing_lgst) > 0:
650649
MAX = 10 # Maximum missing-seq messages to display
651650
missing_msgs = [("Prep: %s, Germ: %s, L: %d, Meas: %s, Circuit: %s" % tup) for tup in missing_list[0:MAX + 1]] \

0 commit comments

Comments
 (0)