Skip to content

Commit db5ffbc

Browse files
committed
Remove code duplication
1 parent d48aa2b commit db5ffbc

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

qupulse/program/measurement.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ def _with_new_frame(self, measurements):
8686
self._frames[-1].keep = True
8787
return frame.commands
8888

89+
def _with_loop_scope(self, measurements, loop_name, loop_count):
90+
new_commands = yield from self._with_new_frame(measurements)
91+
if new_commands is None:
92+
return
93+
parent = self._frames[-1]
94+
95+
self._label_counter += 1
96+
label_idx = self._label_counter
97+
parent.commands.append(LoopLabel(idx=label_idx, runtime_name=loop_name, count=loop_count))
98+
parent.commands.extend(new_commands)
99+
parent.commands.append(LoopJmp(idx=label_idx))
100+
89101
def inner_scope(self, scope: Scope) -> Scope:
90102
"""This function is necessary to inject program builder specific parameter implementations into the build
91103
process."""
@@ -115,16 +127,7 @@ def measure(self, measurements: Optional[Sequence[MeasurementWindow]]):
115127
def with_repetition(self, repetition_count: RepetitionCount,
116128
measurements: Optional[Sequence[MeasurementWindow]] = None) -> Iterable['ProgramBuilder']:
117129
"""Measurements that are added to the new builder are dropped if the builder is empty upon exit"""
118-
new_commands = yield from self._with_new_frame(measurements)
119-
if new_commands is None:
120-
return
121-
parent = self._frames[-1]
122-
123-
self._label_counter += 1
124-
label_idx = self._label_counter
125-
parent.commands.append(LoopLabel(idx=label_idx, runtime_name=None, count=repetition_count))
126-
parent.commands.extend(new_commands)
127-
parent.commands.append(LoopJmp(idx=label_idx))
130+
yield from self._with_loop_scope(measurements, loop_name=None, loop_count=repetition_count)
128131

129132
@contextlib.contextmanager
130133
def with_sequence(self,
@@ -152,17 +155,8 @@ def new_subprogram(self, global_transformation: 'Transformation' = None) -> Cont
152155
def with_iteration(self, index_name: str, rng: range,
153156
measurements: Optional[Sequence[MeasurementWindow]] = None) -> Iterable['ProgramBuilder']:
154157
self._ranges.append((index_name, rng))
155-
new_commands = yield from self._with_new_frame(measurements)
158+
yield from self._with_loop_scope(measurements, loop_name=index_name, loop_count=len(rng))
156159
self._ranges.pop()
157-
if new_commands is None:
158-
return
159-
parent = self._frames[-1]
160-
161-
self._label_counter += 1
162-
label_idx = self._label_counter
163-
parent.commands.append(LoopLabel(idx=label_idx, runtime_name=index_name, count=len(rng)))
164-
parent.commands.extend(new_commands)
165-
parent.commands.append(LoopJmp(idx=label_idx))
166160

167161
def time_reversed(self) -> ContextManager['ProgramBuilder']:
168162
self._frames.append(MeasurementFrame([], False))

0 commit comments

Comments
 (0)