Skip to content

Commit 2477cfd

Browse files
Fix variable remapping of Delay expressions in QuantumCircuit.compose (#15020) (#15023)
The `Delay.duration` field was fairly recently updated to allow it to be an expression, but we forgot to add it to the tracked list of expression locations in `QuantumCircuit.compose`. (cherry picked from commit 650270f) Co-authored-by: Jake Lishman <[email protected]>
1 parent d53bbc4 commit 2477cfd

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

qiskit/circuit/quantumcircuit.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,9 @@ def map_vars(op):
23202320
n_op = Store(
23212321
variable_mapper.map_expr(n_op.lvalue), variable_mapper.map_expr(n_op.rvalue)
23222322
)
2323+
elif isinstance(n_op, Delay) and n_op.unit == "expr":
2324+
n_op = n_op.copy()
2325+
n_op.duration = variable_mapper.map_expr(n_op.duration)
23232326
return n_op.copy() if n_op is op and copy else n_op
23242327

23252328
instructions = source._data.copy(copy_instructions=copy)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
:meth:`.QuantumCircuit.compose` will now correctly remap anyway variables and stretches used in
5+
:class:`~.circuit.Delay` instructions when the ``var_remap`` argument is specified.

test/python/circuit/test_compose.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,19 @@ def test_stretch_remap_to_avoid_collisions(self):
869869
self.assertEqual([a1, c], list(out.iter_captured_stretches()))
870870
self.assertEqual([a1, c], list(out.iter_stretches()))
871871

872+
def test_remap_stretch_inside_var(self):
873+
"""Test that the variable remapper checks inside `Delay` nodes."""
874+
qc = QuantumCircuit(1)
875+
a = qc.add_stretch("a")
876+
qc.delay(expr.mul(2, a), 0)
877+
878+
other = QuantumCircuit(1)
879+
b = other.add_stretch("b")
880+
other.delay(expr.mul(2, b), 0)
881+
882+
actual = QuantumCircuit(1).compose(other, var_remap={b: a})
883+
self.assertEqual(qc, actual)
884+
872885
def test_simple_inline_captures(self):
873886
"""We should be able to inline captures onto other variables."""
874887
a = expr.Var.new("a", types.Bool())

0 commit comments

Comments
 (0)