Skip to content

Commit

Permalink
Update Label Attribute Conventions
Browse files Browse the repository at this point in the history
Update the convention used for the Label class attribute, and add a property for accessing this attribute externally.
  • Loading branch information
Corey Ostrove committed Jun 6, 2024
1 parent 5b8da09 commit 22838c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
25 changes: 17 additions & 8 deletions pygsti/baseobjs/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ def expand_subcircuits(self):
:class:`CircuitLabel` objects).
"""
return (self,) # most labels just expand to themselves

@property
def is_simple(self):
"""
Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
"""

return self.IS_SIMPLE



class LabelTup(Label, tuple):
Expand All @@ -200,7 +209,7 @@ class LabelTup(Label, tuple):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= True
IS_SIMPLE = True

@classmethod
def init(cls, name, state_space_labels):
Expand Down Expand Up @@ -436,7 +445,7 @@ class LabelTupWithTime(Label, tuple):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= True
IS_SIMPLE = True

@classmethod
def init(cls, name, state_space_labels, time=0.0):
Expand Down Expand Up @@ -680,7 +689,7 @@ class LabelStr(Label, str):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= True
IS_SIMPLE = True

@classmethod
def init(cls, name, time=0.0):
Expand Down Expand Up @@ -853,7 +862,7 @@ class LabelTupTup(Label, tuple):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= False
IS_SIMPLE = False

@classmethod
def init(cls, tup_of_tups):
Expand Down Expand Up @@ -1108,7 +1117,7 @@ class LabelTupTupWithTime(Label, tuple):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= False
IS_SIMPLE = False

@classmethod
def init(cls, tup_of_tups, time=None):
Expand Down Expand Up @@ -1369,7 +1378,7 @@ class CircuitLabel(Label, tuple):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= True
IS_SIMPLE = True

def __new__(cls, name, tup_of_layers, state_space_labels, reps=1, time=None):
# Note: may need default args for all but 1st for pickling!
Expand Down Expand Up @@ -1641,7 +1650,7 @@ class LabelTupWithArgs(Label, tuple):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= True
IS_SIMPLE = True

@classmethod
def init(cls, name, state_space_labels, time=0.0, args=()):
Expand Down Expand Up @@ -1909,7 +1918,7 @@ class LabelTupTupWithArgs(Label, tuple):

#flag used in certain Circuit subroutines
#Whether this is a "simple" (opaque w/a true name, from a circuit perspective) label or not.
is_simple= False
IS_SIMPLE = False

@classmethod
def init(cls, tup_of_tups, time=None, args=()):
Expand Down
18 changes: 9 additions & 9 deletions pygsti/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _label_to_nested_lists_of_simple_labels(lbl, default_sslbls=None, always_ret
""" Convert lbl into nested lists of *simple* labels """
if not isinstance(lbl, _Label): # if not a Label, make into a label,
lbl = _Label(lbl) # e.g. a string or list/tuple of labels, etc.
if lbl.is_simple: # a *simple* label - the elements of our lists
if lbl.IS_SIMPLE: # a *simple* label - the elements of our lists
if lbl.sslbls is None and default_sslbls is not None:
lbl = _Label(lbl.name, default_sslbls)
return [lbl] if always_return_list else lbl
Expand All @@ -120,7 +120,7 @@ def _accumulate_explicit_sslbls(obj):
"""
ret = set()
if isinstance(obj, _Label):
if not obj.is_simple:
if not obj.IS_SIMPLE:
for lbl in obj.components:
ret.update(_accumulate_explicit_sslbls(lbl))
else: # a simple label
Expand Down Expand Up @@ -1027,7 +1027,7 @@ def copy(self, editable='auto'):
if editable:
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]
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)
Expand Down Expand Up @@ -1107,7 +1107,7 @@ def _layer_components(self, ilayer):
""" Get the components of the `ilayer`-th layer as a list/tuple. """
#(works for static and non-static Circuits)
if self._static:
if self._labels[ilayer].is_simple: return [self._labels[ilayer]]
if self._labels[ilayer].IS_SIMPLE: return [self._labels[ilayer]]
else: return self._labels[ilayer].components
else:
return self._labels[ilayer] if isinstance(self._labels[ilayer], list) \
Expand Down Expand Up @@ -2762,7 +2762,7 @@ def mapper_func(gatename): return mapper.get(gatename, None) \

def map_names(obj): # obj is either a simple label or a list
if isinstance(obj, _Label):
if obj.is_simple: # *simple* label
if obj.IS_SIMPLE: # *simple* label
new_name = mapper_func(obj.name)
newobj = _Label(new_name, obj.sslbls) \
if (new_name is not None) else obj
Expand Down Expand Up @@ -3401,7 +3401,7 @@ def size(self):
#TODO HERE -update from here down b/c of sub-circuit blocks
if self._static:
def size(lbl): # obj a Label, perhaps compound
if lbl.is_simple: # a simple label
if lbl.IS_SIMPLE: # a simple label
return len(lbl.sslbls) if (lbl.sslbls is not None) else len(self._line_labels)
else:
return sum([size(sublbl) for sublbl in lbl.components])
Expand Down Expand Up @@ -3456,7 +3456,7 @@ def num_nq_gates(self, nq):
"""
if self._static:
def cnt(lbl): # obj a Label, perhaps compound
if lbl.is_simple: # a simple label
if lbl.IS_SIMPLE: # a simple label
return 1 if (lbl.sslbls is not None) and (len(lbl.sslbls) == nq) else 0
else:
return sum([cnt(sublbl) for sublbl in lbl.components])
Expand Down Expand Up @@ -3484,7 +3484,7 @@ def num_multiq_gates(self):
"""
if self._static:
def cnt(lbl): # obj a Label, perhaps compound
if lbl.is_simple: # a simple label
if lbl.IS_SIMPLE: # a simple label
return 1 if (lbl.sslbls is not None) and (len(lbl.sslbls) >= 2) else 0
else:
return sum([cnt(sublbl) for sublbl in lbl.components])
Expand All @@ -3507,7 +3507,7 @@ def _togrid(self, identity_name):
for layercomp in self._layer_components(ilayer):
if isinstance(layercomp, _Label):
comp_label = layercomp
if layercomp.is_simple:
if layercomp.IS_SIMPLE:
comp_sslbls = layercomp.sslbls
else:
#We can't intelligently flatten compound labels that occur within a layer-label yet...
Expand Down

0 comments on commit 22838c9

Please sign in to comment.