Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing error in random_circuit conditionals (backport #9651) #9656

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)