Skip to content

Commit

Permalink
Minor Updates/Fixes
Browse files Browse the repository at this point in the history
Minor unit test updates to fix errors in test_packages. Also fixes some wonky formatting left over from profiling.
  • Loading branch information
Corey Ostrove committed May 31, 2024
1 parent 83e3b81 commit e5872d8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
53 changes: 37 additions & 16 deletions pygsti/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ def __init__(self, layer_labels=(), line_labels='auto', num_lines=None, editable
compilable_layer_indices_tup = ()

#Set *all* class attributes (separated so can call bare_init separately for fast internal creation)
self._bare_init(labels, my_line_labels, editable, name, stringrep, occurrence, compilable_layer_indices_tup)
self._bare_init(labels, my_line_labels, editable, name, stringrep,
occurrence, compilable_layer_indices_tup)


@classmethod
Expand Down Expand Up @@ -656,7 +657,8 @@ def tup(self):
if self._line_labels in (('*',), ()): # No line labels
return self._labels + comp_lbl_flag + self._compilable_layer_indices_tup
else:
return self._labels + ('@',) + self._line_labels + comp_lbl_flag + self._compilable_layer_indices_tup
return self._labels + ('@',) + self._line_labels + comp_lbl_flag \
+ self._compilable_layer_indices_tup
else:
if self._line_labels in (('*',), ()):
return self._labels + ('@',) + ('@', self._occurrence_id) \
Expand All @@ -671,7 +673,8 @@ def tup(self):
if self._line_labels in (('*',), ()): # No line labels
return self.layertup + comp_lbl_flag + self._compilable_layer_indices_tup
else:
return self.layertup + ('@',) + self._line_labels + comp_lbl_flag + self._compilable_layer_indices_tup
return self.layertup + ('@',) + self._line_labels + comp_lbl_flag\
+ self._compilable_layer_indices_tup
else:
if self._line_labels in (('*',), ()):
return self.layertup + ('@',) + ('@', self._occurrence_id) \
Expand Down Expand Up @@ -842,7 +845,8 @@ def __add__(self, x):

if not isinstance(x, Circuit):
assert(all([isinstance(l, _Label) for l in x])), "Only Circuits and Label-tuples can be added to Circuits!"
new_line_labels = set(sum([l.sslbls for l in x if l.sslbls is not None], self._line_labels)) #trick for concatenating multiple tuples
new_line_labels = set(sum([l.sslbls for l in x if l.sslbls is not None],
self._line_labels)) #trick for concatenating multiple tuples
#new_line_labels.update(self._line_labels)
new_line_labels = sorted(list(new_line_labels))
return Circuit._fastinit(self.layertup + x, new_line_labels, editable=False)
Expand Down Expand Up @@ -928,7 +932,8 @@ def sandwich(self, x, y):
assert(isinstance(x, tuple) and isinstance(y, tuple)), 'Only tuples of labels are currently supported by `sandwich` method.'
combined_sandwich_labels = x + y
assert(all([isinstance(l, _Label) for l in combined_sandwich_labels])), "Only Circuits and Label-tuples can be added to Circuits!"
new_line_labels = set(sum([l.sslbls for l in combined_sandwich_labels if l.sslbls is not None], self._line_labels)) #trick for concatenating multiple tuples
new_line_labels = set(sum([l.sslbls for l in combined_sandwich_labels if l.sslbls is not None],
self._line_labels)) #trick for concatenating multiple tuples
new_line_labels = sorted(list(new_line_labels))
return Circuit._fastinit(x + self.layertup + y, new_line_labels, editable=False)

Expand Down Expand Up @@ -1041,21 +1046,32 @@ def copy(self, editable='auto'):
if self._static:
#static and editable circuits have different conventions for _labels.
editable_labels =[[lbl] if lbl._is_simple else list(lbl.components) for lbl in self._labels]
return ret._copy_init(editable_labels, self._line_labels, editable, self._name, self._str, self._occurrence_id, self._compilable_layer_indices_tup)
return ret._copy_init(editable_labels, self._line_labels, editable,
self._name, self._str, self._occurrence_id,
self._compilable_layer_indices_tup)
else:
#copy the editable labels (avoiding shallow copy issues)
editable_labels = [sublist.copy() for sublist in self._labels]
return ret._copy_init(editable_labels, self._line_labels, editable, self._name, self._str, self._occurrence_id, self._compilable_layer_indices_tup)
return ret._copy_init(editable_labels, self._line_labels, editable,
self._name, self._str, self._occurrence_id,
self._compilable_layer_indices_tup)
else: #create static copy
if self._static:
#if presently static leverage precomputed hashable_tup and hash.
#These values are only used by _copy_init if the circuit being
#created is static, and are ignored otherwise.
return ret._copy_init(self._labels, self._line_labels, editable, self._name, self._str, self._occurrence_id, self._compilable_layer_indices_tup, self._hashable_tup, self._hash)
return ret._copy_init(self._labels, self._line_labels, editable,
self._name, self._str, self._occurrence_id,
self._compilable_layer_indices_tup,
self._hashable_tup, self._hash)
else:
static_labels = tuple([layer_lbl if isinstance(layer_lbl, _Label) else _Label(layer_lbl) for layer_lbl in self._labels])
static_labels = tuple([layer_lbl if isinstance(layer_lbl, _Label) else _Label(layer_lbl)
for layer_lbl in self._labels])
hashable_tup = self._tup_copy(static_labels)
return ret._copy_init(static_labels, self._line_labels, editable, self._name, self._str, self._occurrence_id, self._compilable_layer_indices_tup, hashable_tup, hash(hashable_tup))
return ret._copy_init(static_labels, self._line_labels,
editable, self._name, self._str, self._occurrence_id,
self._compilable_layer_indices_tup,
hashable_tup, hash(hashable_tup))

def clear(self):
"""
Expand Down Expand Up @@ -1236,8 +1252,9 @@ def get_sslbls(lbl): return lbl.sslbls
if not strict: lines = "auto" # since we may have included lbls on other lines
# don't worry about string rep for now...

return Circuit._fastinit(tuple(ret) if self._static else ret, tuple(lines) if self._static else lines,
not self._static)
return Circuit._fastinit(tuple(ret) if self._static else ret,
tuple(lines) if self._static else lines,
not self._static)
else:
return _Label(ret[0])

Expand Down Expand Up @@ -2598,11 +2615,13 @@ def replace_layer(self, old_layer, new_layer):
cpy = self.copy(editable=False) # convert our layers to Labels
return Circuit._fastinit(tuple([new_layer if lbl == old_layer else lbl
for lbl in cpy._labels]), self._line_labels, editable=False,
occurrence=self._occurrence_id, compilable_layer_indices_tup=self._compilable_layer_indices_tup)
occurrence=self._occurrence_id,
compilable_layer_indices_tup=self._compilable_layer_indices_tup)
else: # static case: so self._labels is a tuple of Labels
return Circuit(tuple([new_layer if lbl == old_layer else lbl
for lbl in self._labels]), self._line_labels, editable=False,
occurrence=self._occurrence_id, compilable_layer_indices=self._compilable_layer_indices_tup)
occurrence=self._occurrence_id,
compilable_layer_indices=self._compilable_layer_indices_tup)

def replace_layers_with_aliases(self, alias_dict):
"""
Expand Down Expand Up @@ -3886,7 +3905,8 @@ def from_cirq(cls, circuit, qubit_conversion=None, cirq_gate_conversion= None,
#append the default.
circuit_layers.append(_Label(()))
else:
circuit_layers.append(_Label(global_idle_replacement_label, tuple(sorted([qubit_conversion[qubit] for qubit in all_cirq_qubits]))))
circuit_layers.append(_Label(global_idle_replacement_label,
tuple(sorted([qubit_conversion[qubit] for qubit in all_cirq_qubits]))))
elif isinstance(global_idle_replacement_label, _Label):
circuit_layers.append(global_idle_replacement_label)
else:
Expand Down Expand Up @@ -3921,7 +3941,8 @@ def from_cirq(cls, circuit, qubit_conversion=None, cirq_gate_conversion= None,
#append the default.
circuit_layers.append(_Label(()))
else:
circuit_layers.append(_Label(global_idle_replacement_label, tuple(sorted([qubit_conversion[qubit] for qubit in all_cirq_qubits]))))
circuit_layers.append(_Label(global_idle_replacement_label,
tuple(sorted([qubit_conversion[qubit] for qubit in all_cirq_qubits]))))
elif isinstance(global_idle_replacement_label, _Label):
circuit_layers.append(global_idle_replacement_label)
#check whether any of the elements are implied idles, and if so use flag
Expand Down
Binary file modified test/test_packages/cmp_chk_files/idt_nQsequenceCache.pkl
Binary file not shown.
2 changes: 1 addition & 1 deletion test/test_packages/iotest/test_stdinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_parse_circuit(self):
for s, expected in string_tests:
result, line_labels, occurrence_id, compilable_indices = self.std.parse_circuit_raw(s, lookup=lkup)
self.assertEqual(line_labels, None)
self.assertEqual(compilable_indices, None)
self.assertEqual(compilable_indices, ())
circuit_result = Circuit(result, line_labels="auto", expand_subcircuits=True)
#use "auto" line labels since none are parsed.
self.assertEqual(circuit_result.tup, expected)
Expand Down

0 comments on commit e5872d8

Please sign in to comment.