From 3fb23e5d2b8e76396920904a2d75dc2ef43593a0 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Fri, 24 Feb 2023 18:42:43 +0000 Subject: [PATCH] Fix typing error in `random_circuit` conditionals (#9651) The `condition` field is expected to be a comparison of a register or bit with a Python bigint (or bool, which is a subclass). This function could previously output fixed-width Numpy types, however, which could cause problems with subsequent bitmasks if the constructed masker/maskee _was_ a Python bigint and couldn't fit in the Numpy type. The more recent `IfElseOp` enforces the correct typing, it's just the old form that doesn't. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit 94e9480ddb5d1304a45ad8b463668ebe144a43d2) --- qiskit/circuit/random/utils.py | 3 ++- .../fix-random-circuit-conditional-6067272319986c63.yaml | 7 +++++++ test/python/circuit/test_random_circuit.py | 7 ++++++- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/fix-random-circuit-conditional-6067272319986c63.yaml diff --git a/qiskit/circuit/random/utils.py b/qiskit/circuit/random/utils.py index 2b8911252d34..2a079b304fab 100644 --- a/qiskit/circuit/random/utils.py +++ b/qiskit/circuit/random/utils.py @@ -191,7 +191,8 @@ def random_circuit( ): operation = gate(*parameters[p_start:p_end]) if is_cond: - operation.condition = (cr, condition_values[c_ptr]) + # The condition values are required to be bigints, not Numpy's fixed-width type. + operation.condition = (cr, int(condition_values[c_ptr])) c_ptr += 1 qc._append(CircuitInstruction(operation=operation, qubits=qubits[q_start:q_end])) else: diff --git a/releasenotes/notes/fix-random-circuit-conditional-6067272319986c63.yaml b/releasenotes/notes/fix-random-circuit-conditional-6067272319986c63.yaml new file mode 100644 index 000000000000..cb4752a05749 --- /dev/null +++ b/releasenotes/notes/fix-random-circuit-conditional-6067272319986c63.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixed a bug in :func:`.random_circuit` with 64 or more qubits and ``conditional=True``, where + the resulting circuit could have an incorrectly typed value in its condition, causing a variety + of failures during transpilation or other circuit operations. Fixed `#9649 + `__. diff --git a/test/python/circuit/test_random_circuit.py b/test/python/circuit/test_random_circuit.py index db5acb851012..50ab408730ec 100644 --- a/test/python/circuit/test_random_circuit.py +++ b/test/python/circuit/test_random_circuit.py @@ -13,7 +13,7 @@ """Test random circuit generation utility.""" -from qiskit.circuit import QuantumCircuit +from qiskit.circuit import QuantumCircuit, ClassicalRegister, Clbit from qiskit.circuit import Measure from qiskit.circuit.random import random_circuit from qiskit.converters import circuit_to_dag @@ -63,3 +63,8 @@ def test_large_conditional(self): conditions = (getattr(instruction.operation, "condition", None) for instruction in circ) conditions = [x for x in conditions if x is not None] self.assertNotEqual(conditions, []) + for (register, value) in conditions: + self.assertIsInstance(register, (ClassicalRegister, Clbit)) + # Condition values always have to be Python bigints (of which `bool` is a subclass), not + # any of Numpy's fixed-width types, for example. + self.assertIsInstance(value, int)