Skip to content

Commit

Permalink
Fix typing error in random_circuit conditionals (#9651)
Browse files Browse the repository at this point in the history
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 94e9480)
  • Loading branch information
jakelishman authored and mergify[bot] committed Feb 24, 2023
1 parent d705295 commit 3fb23e5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion qiskit/circuit/random/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
<https://github.com/Qiskit/qiskit-terra/issues/9649>`__.
7 changes: 6 additions & 1 deletion test/python/circuit/test_random_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 3fb23e5

Please sign in to comment.