mirror of https://github.com/Qiskit/qiskit.git
Add mid-circuit measurement before conditional operation in random_circuit (#10401)
* Add mid-circuit measurement before conditional operation in random_circuit * fixed format * Update releasenotes/notes/fix_9016-2e8bc2cb10b5e204.yaml Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --------- Co-authored-by: Luciano Bello <bel@zurich.ibm.com> Co-authored-by: Kevin Krsulich <kevin@krsulich.net> Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Co-authored-by: Kevin Krsulich <kevin.krsulich@ibm.com>
This commit is contained in:
parent
1774d631bb
commit
511a6405d0
|
@ -144,7 +144,7 @@ def random_circuit(
|
|||
qubits = np.array(qc.qubits, dtype=object, copy=True)
|
||||
|
||||
# Apply arbitrary random operations in layers across all qubits.
|
||||
for _ in range(depth):
|
||||
for layer_number in range(depth):
|
||||
# We generate all the randomness for the layer in one go, to avoid many separate calls to
|
||||
# the randomisation routines, which can be fairly slow.
|
||||
|
||||
|
@ -175,7 +175,7 @@ def random_circuit(
|
|||
# We've now generated everything we're going to need. Now just to add everything. The
|
||||
# conditional check is outside the two loops to make the more common case of no conditionals
|
||||
# faster, since in Python we don't have a compiler to do this for us.
|
||||
if conditional:
|
||||
if conditional and layer_number != 0:
|
||||
is_conditional = rng.random(size=len(gate_specs)) < 0.1
|
||||
condition_values = rng.integers(
|
||||
0, 1 << min(num_qubits, 63), size=np.count_nonzero(is_conditional)
|
||||
|
@ -191,6 +191,7 @@ def random_circuit(
|
|||
):
|
||||
operation = gate(*parameters[p_start:p_end])
|
||||
if is_cond:
|
||||
qc.measure(qc.qubits, cr)
|
||||
# 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
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
fixes:
|
||||
- |
|
||||
When the parameter ``conditional=True`` is set in
|
||||
``qiskit.circuit.random.random_circuit``, the conditional operations will
|
||||
be preceded by a full mid-circuit measurment.
|
||||
Fixes `#9016 <https://github.com/Qiskit/qiskit-terra/issues/9016>`__
|
|
@ -68,3 +68,17 @@ class TestCircuitRandom(QiskitTestCase):
|
|||
# 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)
|
||||
|
||||
def test_random_mid_circuit_measure_conditional(self):
|
||||
"""Test random circuit with mid-circuit measurements for conditionals."""
|
||||
num_qubits = depth = 2
|
||||
circ = random_circuit(num_qubits, depth, conditional=True, seed=4)
|
||||
self.assertEqual(circ.width(), 2 * num_qubits)
|
||||
op_names = [instruction.operation.name for instruction in circ]
|
||||
# Before a condition, there needs to be measurement in all the qubits.
|
||||
self.assertEqual(4, len(op_names))
|
||||
self.assertEqual(["measure"] * num_qubits, op_names[1 : 1 + num_qubits])
|
||||
conditions = [
|
||||
bool(getattr(instruction.operation, "condition", None)) for instruction in circ
|
||||
]
|
||||
self.assertEqual([False, False, False, True], conditions)
|
||||
|
|
Loading…
Reference in New Issue