Skip to content

Commit

Permalink
Merge pull request #338 from terrorfisch/more_efficient_loop_comparison
Browse files Browse the repository at this point in the history
More efficient loop comparison
  • Loading branch information
terrorfisch committed Aug 15, 2018
2 parents 6027d69 + 90bf1c6 commit 17dc51f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 2 additions & 3 deletions qctoolkit/_program/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from qctoolkit.utils.types import ChannelID, TimeType
from qctoolkit._program.instructions import AbstractInstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction,\
STOPInstruction, CHANInstruction, Waveform, MEASInstruction, Instruction
from qctoolkit.comparable import Comparable
from qctoolkit.utils.tree import Node, is_tree_circular
from qctoolkit.utils.types import MeasurementWindow
from qctoolkit.utils import is_integer
Expand All @@ -21,7 +20,7 @@
__all__ = ['Loop', 'MultiChannelProgram', 'make_compatible']


class Loop(Comparable, Node):
class Loop(Node):
"""Build a loop tree. The leaves of the tree are loops with one element."""
def __init__(self,
parent: Union['Loop', None]=None,
Expand All @@ -44,7 +43,7 @@ def __init__(self,

@property
def compare_key(self) -> Tuple:
return self._waveform, self.repetition_count, self._measurements, tuple(c.compare_key for c in self)
return self._waveform, self.repetition_count, self._measurements, super().compare_key

def append_child(self, loop: Optional['Loop']=None, **kwargs) -> None:
# do not invalidate but update cached duration
Expand Down
6 changes: 3 additions & 3 deletions qctoolkit/_program/waveforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ def get_sub_waveform_sort_key(waveform):
return tuple(sorted(tuple('{}_stringified_numeric_channel'.format(ch) if isinstance(ch, int) else ch
for ch in waveform.defined_channels)))

self._sub_waveforms = sorted(flatten_sub_waveforms(sub_waveforms),
key=get_sub_waveform_sort_key)
self._sub_waveforms = tuple(sorted(flatten_sub_waveforms(sub_waveforms),
key=get_sub_waveform_sort_key))

if not all(waveform.duration == self._sub_waveforms[0].duration for waveform in self._sub_waveforms[1:]):
raise ValueError(
Expand Down Expand Up @@ -423,7 +423,7 @@ def defined_channels(self) -> Set[ChannelID]:
@property
def compare_key(self) -> Any:
# sort with channels
return tuple(sub_waveform.compare_key for sub_waveform in self._sub_waveforms)
return self._sub_waveforms

def unsafe_sample(self,
channel: ChannelID,
Expand Down
8 changes: 7 additions & 1 deletion qctoolkit/utils/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from copy import copy as shallow_copy
import weakref

from qctoolkit.comparable import Comparable


__all__ = ['Node']

Expand All @@ -14,7 +16,7 @@ def make_empty_weak_reference() -> weakref.ref:
_NodeType = TypeVar('_NodeType', bound='Node')


class Node:
class Node(Comparable):
debug = False

def __init__(self: _NodeType,
Expand Down Expand Up @@ -141,6 +143,10 @@ def locate(self: _NodeType, location: Tuple[int, ...]) -> _NodeType:
else:
return self

@property
def compare_key(self) -> List:
return self.__children


def is_tree_circular(root: Node) -> Union[None, Tuple[List[Node], int]]:
NodeStack = namedtuple('NodeStack', ['node', 'stack'])
Expand Down

0 comments on commit 17dc51f

Please sign in to comment.