From 45f789dc5d21873555dfffcc9e758130194e25c5 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Mon, 18 Jun 2018 17:14:03 +0200 Subject: [PATCH] Drop empty loops --- qctoolkit/hardware/program.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index 03be4ef72..3c48be0e8 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -4,6 +4,7 @@ from copy import deepcopy from enum import Enum from fractions import Fraction +import warnings import numpy as np @@ -261,6 +262,21 @@ def flatten_and_balance(self, depth: int) -> None: else: i += 1 + def remove_empty_loops(self): + new_children = [] + for child in self: + if child.is_leaf(): + if child.waveform is None: + if child._measurements: + warnings.warn("Dropping measurement since there is no waveform attached") + else: + new_children.append(child) + else: + child.remove_empty_loops() + new_children.append(child) + self[:] = new_children + + class ChannelSplit(Exception): def __init__(self, channel_sets): self.channel_sets = channel_sets @@ -332,6 +348,8 @@ def repeat_measurements(child_loop, rep_count): iterable = itertools.chain((loop,), iterable) except StopIteration: pass + for program in self.programs.values(): + program.remove_empty_loops() @property def programs(self) -> Dict[FrozenSet[ChannelID], Loop]: