Skip to content

Commit

Permalink
Update Instruction.condition_bits for runtime classical expressions (
Browse files Browse the repository at this point in the history
…#11325) (#11328)

I didn't even know this property existed and it wasn't tested directly,
but the IBM provider uses it during its custom scheduling passes, so
until removal, it should be kept updated.

(cherry picked from commit 5bbda4a)

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
mergify[bot] and jakelishman authored Nov 28, 2023
1 parent 13ed4ac commit 1cded9b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
7 changes: 3 additions & 4 deletions qiskit/circuit/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
The property :attr:`.Instruction.condition_bits` will now correctly handle runtime classical
expressions (:mod:`qiskit.circuit.classical`).
44 changes: 34 additions & 10 deletions test/python/circuit/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 1cded9b

Please sign in to comment.