diff --git a/pygsti/circuits/circuit.py b/pygsti/circuits/circuit.py index c84586da1..969fc2f8a 100644 --- a/pygsti/circuits/circuit.py +++ b/pygsti/circuits/circuit.py @@ -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 @@ -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) \ @@ -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) \ @@ -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) @@ -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) @@ -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): """ @@ -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]) @@ -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): """ @@ -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: @@ -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 diff --git a/test/test_packages/cmp_chk_files/idt_nQsequenceCache.pkl b/test/test_packages/cmp_chk_files/idt_nQsequenceCache.pkl index 15588af2c..e2c2043ac 100644 Binary files a/test/test_packages/cmp_chk_files/idt_nQsequenceCache.pkl and b/test/test_packages/cmp_chk_files/idt_nQsequenceCache.pkl differ diff --git a/test/test_packages/iotest/test_stdinput.py b/test/test_packages/iotest/test_stdinput.py index 9a35589c7..f803597dc 100644 --- a/test/test_packages/iotest/test_stdinput.py +++ b/test/test_packages/iotest/test_stdinput.py @@ -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)