Skip to content

Commit

Permalink
Fix Barrier broadcast arguments (#11113) (#11114)
Browse files Browse the repository at this point in the history
This commit fixes an issue with the Barrier class's
broadcast_arguments() method. Previously the Barrier class was
overriding broadcast_arguments(), however this custom implementation
resulted in an identical output to the Instruction's implementation
(its parent class) but stripped out all the error checking to determine
if the arguments aligned with the size of the instruction. This could
result in creating a corrupt circuit that had a mismatch between the
barrier width and the number of qargs. For example:

```
circuit = QuantumCircuit(1)
circuit.append(Barrier(42), [0])
```

would not error despite trying to add a 42 qubit barrier on qubit 0.
This would result in weird errors such as invalid qpy generation that
are confusing to debug. This commit fixes this by deleting the
broadcast_arguments() implementation for Barrier so it will just
depend on the inherited implementation from Instruction which will
return an identical result but check the instruction is valid.

(cherry picked from commit 5df2439)

Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
mergify[bot] and mtreinish authored Oct 25, 2023
1 parent c0b639a commit ec5a615
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 0 additions & 3 deletions qiskit/circuit/barrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,5 @@ def inverse(self):
"""Special case. Return self."""
return Barrier(self.num_qubits)

def broadcast_arguments(self, qargs, cargs):
yield [qarg for sublist in qargs for qarg in sublist], []

def c_if(self, classical, val):
raise QiskitError("Barriers are compiler directives and cannot be conditional.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed an issue with the :class:`.Barrier` class. When adding a
:class:`.Barrier` instance to a :class:`.QuantumCircuit` with the
:meth:`.QuantumCircuit.append` method previously there was no validation
that the size of the barrier matched the qargs specified.
4 changes: 3 additions & 1 deletion test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ddt import data, ddt

from qiskit import BasicAer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
from qiskit.circuit import Gate, Instruction, Measure, Parameter
from qiskit.circuit import Gate, Instruction, Measure, Parameter, Barrier
from qiskit.circuit.bit import Bit
from qiskit.circuit.classicalregister import Clbit
from qiskit.circuit.exceptions import CircuitError
Expand Down Expand Up @@ -156,6 +156,8 @@ def test_append_rejects_bad_arguments_opaque(self, bad_arg):
qc.append(inst, bad_arg, [0, 1])
with self.assertRaisesRegex(CircuitError, "The amount of clbit arguments"):
qc.append(inst, [0, 1], bad_arg)
with self.assertRaisesRegex(CircuitError, "The amount of qubit arguments"):
qc.append(Barrier(4), bad_arg)

def test_anding_self(self):
"""Test that qc &= qc finishes, which can be prone to infinite while-loops.
Expand Down

0 comments on commit ec5a615

Please sign in to comment.