Skip to content

Commit 62cf3ee

Browse files
committed
Address review comments.
1 parent e84b48e commit 62cf3ee

5 files changed

Lines changed: 126 additions & 101 deletions

File tree

qiskit/circuit/classical/expr/visitors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ def iter_vars(node: expr.Expr) -> typing.Iterator[expr.Var]:
138138
for node in expr.iter_vars(expr.bit_and(expr.bit_not(cr1), cr2)):
139139
if isinstance(node.var, ClassicalRegister):
140140
print(node.var.name)
141+
142+
.. seealso::
143+
:func:`iter_identifiers`
144+
Get an iterator over all identifier nodes in the expression, including
145+
both :class:`~.expr.Var` and :class:`~.expr.Stretch` nodes.
141146
"""
142147
yield from node.accept(_VAR_WALKER)
143148

@@ -158,6 +163,10 @@ def iter_identifiers(node: expr.Expr) -> typing.Iterator[typing.Union[expr.Var,
158163
for node in expr.iter_vars(expr.bit_and(expr.bit_not(cr1), cr2)):
159164
if isinstance(node.var, ClassicalRegister):
160165
print(node.var.name)
166+
167+
.. seealso::
168+
:func:`iter_vars`
169+
Get an iterator over just the :class:`~.expr.Var` nodes in the expression.
161170
"""
162171
yield from node.accept(_IDENT_WALKER)
163172

qiskit/circuit/controlflow/builder.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ def add_uninitialized_var(self, var: expr.Var):
122122
"""
123123

124124
@abc.abstractmethod
125-
def add_stretch(self, var: expr.Stretch):
126-
"""Add a stretch variable to the circuit scope.
125+
def add_stretch(self, stretch: expr.Stretch):
126+
"""Add a stretch to the circuit scope.
127127
128128
Args:
129-
var: the variable to add, if valid.
129+
stretch: the stretch to add, if valid.
130130
131131
Raises:
132-
CircuitError: if the variable cannot be added, such as because it invalidly shadows or
132+
CircuitError: if the stretch cannot be added, such as because it invalidly shadows or
133133
redefines an existing name.
134134
"""
135135

@@ -146,14 +146,14 @@ def remove_var(self, var: expr.Var):
146146
"""
147147

148148
@abc.abstractmethod
149-
def remove_stretch(self, var: expr.Stretch):
150-
"""Remove a stretch variable from the locals of this scope.
149+
def remove_stretch(self, stretch: expr.Stretch):
150+
"""Remove a stretch from the locals of this scope.
151151
152-
This is only called in the case that an exception occurred while initializing the variable,
152+
This is only called in the case that an exception occurred while initializing the stretch,
153153
and is not exposed to users.
154154
155155
Args:
156-
var: the variable to remove. It can be assumed that this was already the subject of an
156+
stretch: the stretch to remove. It can be assumed that this was already the subject of an
157157
:meth:`add_stretch` call.
158158
"""
159159

@@ -169,25 +169,19 @@ def use_var(self, var: expr.Var):
169169
Args:
170170
var: the variable to validate.
171171
172-
Returns:
173-
the same variable.
174-
175172
Raises:
176173
CircuitError: if the variable is not valid for this scope.
177174
"""
178175

179176
@abc.abstractmethod
180-
def use_stretch(self, var: expr.Stretch):
181-
"""Called for every stretch variable being used by some circuit instruction.
177+
def use_stretch(self, stretch: expr.Stretch):
178+
"""Called for every stretch being used by some circuit instruction.
182179
183180
Args:
184-
var: the variable to validate.
185-
186-
Returns:
187-
the same variable.
181+
stretch: the stretch to validate.
188182
189183
Raises:
190-
CircuitError: if the variable is not valid for this scope.
184+
CircuitError: if the stretch is not valid for this scope.
191185
"""
192186

193187
@abc.abstractmethod
@@ -206,16 +200,16 @@ def get_var(self, name: str) -> Optional[expr.Var]:
206200

207201
@abc.abstractmethod
208202
def get_stretch(self, name: str) -> Optional[expr.Stretch]:
209-
"""Get the stretch variable (if any) in scope with the given name.
203+
"""Get the stretch (if any) in scope with the given name.
210204
211205
This should call up to the parent scope if in a control-flow builder scope, in case the
212-
variable exists in an outer scope.
206+
stretch exists in an outer scope.
213207
214208
Args:
215209
name: the name of the symbol to lookup.
216210
217211
Returns:
218-
the stretch variable if it is found, otherwise ``None``.
212+
the stretch if it is found, otherwise ``None``.
219213
"""
220214

221215
@abc.abstractmethod
@@ -514,30 +508,36 @@ def add_uninitialized_var(self, var: expr.Var):
514508
raise CircuitError(f"cannot add '{var}' as its name shadows the existing '{previous}'")
515509
self._vars_local[var.name] = var
516510

517-
def add_stretch(self, var: expr.Stretch):
511+
def add_stretch(self, stretch: expr.Stretch):
518512
if self._built:
519513
raise CircuitError("Cannot add resources after the scope has been built.")
520514
# We can shadow a name if it was declared in an outer scope, but only if we haven't already
521515
# captured it ourselves yet.
522-
if (previous := self._vars_local.get(var.name)) is not None:
523-
raise CircuitError(f"cannot add '{var}' as its name shadows the existing '{previous}'")
524-
if (previous := self._stretches_local.get(var.name)) is not None:
525-
if previous == var:
526-
raise CircuitError(f"'{var}' is already present in the scope")
527-
raise CircuitError(f"cannot add '{var}' as its name shadows the existing '{previous}'")
528-
if var.name in self._vars_capture or var.name in self._stretches_capture:
529-
raise CircuitError(f"cannot add '{var}' as its name shadows the existing '{previous}'")
530-
self._stretches_local[var.name] = var
516+
if (previous := self._vars_local.get(stretch.name)) is not None:
517+
raise CircuitError(
518+
f"cannot add '{stretch}' as its name shadows the existing '{previous}'"
519+
)
520+
if (previous := self._stretches_local.get(stretch.name)) is not None:
521+
if previous == stretch:
522+
raise CircuitError(f"'{stretch}' is already present in the scope")
523+
raise CircuitError(
524+
f"cannot add '{stretch}' as its name shadows the existing '{previous}'"
525+
)
526+
if stretch.name in self._vars_capture or stretch.name in self._stretches_capture:
527+
raise CircuitError(
528+
f"cannot add '{stretch}' as its name shadows the existing '{previous}'"
529+
)
530+
self._stretches_local[stretch.name] = stretch
531531

532532
def remove_var(self, var: expr.Var):
533533
if self._built:
534534
raise RuntimeError("exception handler 'remove_var' called after scope built")
535535
self._vars_local.pop(var.name)
536536

537-
def remove_stretch(self, var: expr.Stretch):
537+
def remove_stretch(self, stretch: expr.Stretch):
538538
if self._built:
539539
raise RuntimeError("exception handler 'remove_stretch' called after scope built")
540-
self._stretches_local.pop(var.name)
540+
self._stretches_local.pop(stretch.name)
541541

542542
def get_var(self, name: str):
543543
if (out := self._vars_local.get(name)) is not None:
@@ -563,19 +563,19 @@ def use_var(self, var: expr.Var):
563563
self._parent.use_var(var)
564564
self._vars_capture[var.name] = var
565565

566-
def use_stretch(self, var: expr.Stretch):
567-
if (local := self._vars_local.get(var.name)) is not None:
568-
raise CircuitError(f"cannot use '{var}' which is shadowed by the local '{local}'")
569-
if (local := self._stretches_local.get(var.name)) is not None:
570-
if local == var:
566+
def use_stretch(self, stretch: expr.Stretch):
567+
if (local := self._vars_local.get(stretch.name)) is not None:
568+
raise CircuitError(f"cannot use '{stretch}' which is shadowed by the local '{local}'")
569+
if (local := self._stretches_local.get(stretch.name)) is not None:
570+
if local == stretch:
571571
return
572-
raise CircuitError(f"cannot use '{var}' which is shadowed by the local '{local}'")
573-
if self._stretches_capture.get(var.name) == var:
572+
raise CircuitError(f"cannot use '{stretch}' which is shadowed by the local '{local}'")
573+
if self._stretches_capture.get(stretch.name) == stretch:
574574
return
575-
if self._parent.get_stretch(var.name) != var:
576-
raise CircuitError(f"cannot close over '{var}', which is not in scope")
577-
self._parent.use_stretch(var)
578-
self._stretches_capture[var.name] = var
575+
if self._parent.get_stretch(stretch.name) != stretch:
576+
raise CircuitError(f"cannot close over '{stretch}', which is not in scope")
577+
self._parent.use_stretch(stretch)
578+
self._stretches_capture[stretch.name] = stretch
579579

580580
def use_qubit(self, qubit: Qubit):
581581
self._instructions.add_qubit(qubit, strict=False)
@@ -585,15 +585,15 @@ def iter_local_vars(self):
585585
return self._vars_local.values()
586586

587587
def iter_local_stretches(self):
588-
"""Iterator over the stretch variables currently declared in this scope."""
588+
"""Iterator over the stretches currently declared in this scope."""
589589
return self._stretches_local.values()
590590

591591
def iter_captured_vars(self):
592592
"""Iterator over the variables currently captured in this scope."""
593593
return self._vars_capture.values()
594594

595595
def iter_captured_stretches(self):
596-
"""Iterator over the stretch variables currently captured in this scope."""
596+
"""Iterator over the stretches currently captured in this scope."""
597597
return self._stretches_capture.values()
598598

599599
def peek(self) -> CircuitInstruction:

0 commit comments

Comments
 (0)