Skip to content

Commit

Permalink
Fix QuantumCircuit.compose with Index exprs (#12396)
Browse files Browse the repository at this point in the history
This was an oversight in d6c74c2 (gh-12310), where an `ExprVisitor`
was missed in the testing.
  • Loading branch information
jakelishman authored May 14, 2024
1 parent 8985b24 commit 58a383d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions qiskit/circuit/_classical_resource_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ def visit_binary(self, node, /):

def visit_cast(self, node, /):
return expr.Cast(node.operand.accept(self), node.type, implicit=node.implicit)

def visit_index(self, node, /):
return expr.Index(node.target.accept(self), node.index.accept(self), node.type)
12 changes: 11 additions & 1 deletion test/python/circuit/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,13 +820,16 @@ def test_expr_condition_is_mapped(self):
b_src = ClassicalRegister(2, "b_src")
c_src = ClassicalRegister(name="c_src", bits=list(a_src) + list(b_src))
source = QuantumCircuit(QuantumRegister(1), a_src, b_src, c_src)
target_var = source.add_input("target_var", types.Uint(2))

test_1 = lambda: expr.lift(a_src[0])
test_2 = lambda: expr.logic_not(b_src[1])
test_3 = lambda: expr.logic_and(expr.bit_and(b_src, 2), expr.less(c_src, 7))
test_4 = lambda: expr.bit_xor(expr.index(target_var, 0), expr.index(target_var, 1))
source.if_test(test_1(), inner.copy(), [0], [])
source.if_else(test_2(), inner.copy(), inner.copy(), [0], [])
source.while_loop(test_3(), inner.copy(), [0], [])
source.if_test(test_4(), inner.copy(), [0], [])

a_dest = ClassicalRegister(2, "a_dest")
b_dest = ClassicalRegister(2, "b_dest")
Expand All @@ -840,12 +843,19 @@ def test_expr_condition_is_mapped(self):
self.assertEqual(len(dest.cregs), 3)
mapped_reg = dest.cregs[-1]

expected = QuantumCircuit(dest.qregs[0], a_dest, b_dest, mapped_reg)
expected = QuantumCircuit(dest.qregs[0], a_dest, b_dest, mapped_reg, inputs=[target_var])
expected.if_test(expr.lift(a_dest[0]), inner.copy(), [0], [])
expected.if_else(expr.logic_not(b_dest[1]), inner.copy(), inner.copy(), [0], [])
expected.while_loop(
expr.logic_and(expr.bit_and(b_dest, 2), expr.less(mapped_reg, 7)), inner.copy(), [0], []
)
# `Var` nodes aren't remapped, but this should be passed through fine.
expected.if_test(
expr.bit_xor(expr.index(target_var, 0), expr.index(target_var, 1)),
inner.copy(),
[0],
[],
)
self.assertEqual(dest, expected)

def test_expr_target_is_mapped(self):
Expand Down

0 comments on commit 58a383d

Please sign in to comment.