diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py index 84e3f344bf07..9fbe77a04f82 100644 --- a/qiskit/circuit/instruction.py +++ b/qiskit/circuit/instruction.py @@ -618,12 +618,11 @@ def repeat(self, n): @property def condition_bits(self) -> List[Clbit]: """Get Clbits in condition.""" + from qiskit.circuit.controlflow import condition_resources # pylint: disable=cyclic-import + if self.condition is None: return [] - if isinstance(self.condition[0], Clbit): - return [self.condition[0]] - else: # ClassicalRegister - return list(self.condition[0]) + return list(condition_resources(self.condition).clbits) @property def name(self): diff --git a/releasenotes/notes/fix-instruction-condition-bits-17694f98628b30ad.yaml b/releasenotes/notes/fix-instruction-condition-bits-17694f98628b30ad.yaml new file mode 100644 index 000000000000..4f044b6a468f --- /dev/null +++ b/releasenotes/notes/fix-instruction-condition-bits-17694f98628b30ad.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + The property :attr:`.Instruction.condition_bits` will now correctly handle runtime classical + expressions (:mod:`qiskit.circuit.classical`). diff --git a/test/python/circuit/test_instructions.py b/test/python/circuit/test_instructions.py index fc60be60fdc1..d111d2d21181 100644 --- a/test/python/circuit/test_instructions.py +++ b/test/python/circuit/test_instructions.py @@ -18,16 +18,20 @@ import numpy as np -from qiskit.circuit import Gate -from qiskit.circuit import Parameter -from qiskit.circuit import Instruction, InstructionSet -from qiskit.circuit import QuantumCircuit -from qiskit.circuit import QuantumRegister, ClassicalRegister, Qubit, Clbit -from qiskit.circuit.library.standard_gates.h import HGate -from qiskit.circuit.library.standard_gates.rz import RZGate -from qiskit.circuit.library.standard_gates.x import CXGate -from qiskit.circuit.library.standard_gates.s import SGate -from qiskit.circuit.library.standard_gates.t import TGate +from qiskit.circuit import ( + Gate, + Parameter, + Instruction, + InstructionSet, + QuantumCircuit, + QuantumRegister, + ClassicalRegister, + Qubit, + Clbit, + IfElseOp, +) +from qiskit.circuit.library import HGate, RZGate, CXGate, SGate, TGate +from qiskit.circuit.classical import expr from qiskit.test import QiskitTestCase from qiskit.circuit.exceptions import CircuitError from qiskit.circuit.random import random_circuit @@ -426,6 +430,26 @@ def test_repr_of_instructions(self): ), ) + def test_instruction_condition_bits(self): + """Test that the ``condition_bits`` property behaves correctly until it is deprecated and + removed.""" + bits = [Clbit(), Clbit()] + cr1 = ClassicalRegister(2, "cr1") + cr2 = ClassicalRegister(2, "cr2") + body = QuantumCircuit(cr1, cr2, bits) + + def key(bit): + return body.find_bit(bit).index + + op = IfElseOp((bits[0], False), body) + self.assertEqual(op.condition_bits, [bits[0]]) + + op = IfElseOp((cr1, 3), body) + self.assertEqual(op.condition_bits, list(cr1)) + + op = IfElseOp(expr.logic_and(bits[1], expr.equal(cr2, 3)), body) + self.assertEqual(sorted(op.condition_bits, key=key), sorted([bits[1]] + list(cr2), key=key)) + def test_instructionset_c_if_direct_resource(self): """Test that using :meth:`.InstructionSet.c_if` with an exact classical resource always works, and produces the expected condition."""