From 3256ec4706fd2211d68345cb8ac0073e293ec3b7 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 25 Oct 2023 14:40:18 -0400 Subject: [PATCH] Fix Barrier broadcast arguments 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. --- qiskit/circuit/barrier.py | 3 --- .../notes/fix-barrier-arg-list-check-ff69f37ede6bdf6c.yaml | 7 +++++++ test/python/circuit/test_circuit_operations.py | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/fix-barrier-arg-list-check-ff69f37ede6bdf6c.yaml diff --git a/qiskit/circuit/barrier.py b/qiskit/circuit/barrier.py index 4bb37558bc20..54e6b2c06191 100644 --- a/qiskit/circuit/barrier.py +++ b/qiskit/circuit/barrier.py @@ -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.") diff --git a/releasenotes/notes/fix-barrier-arg-list-check-ff69f37ede6bdf6c.yaml b/releasenotes/notes/fix-barrier-arg-list-check-ff69f37ede6bdf6c.yaml new file mode 100644 index 000000000000..b620a72666f3 --- /dev/null +++ b/releasenotes/notes/fix-barrier-arg-list-check-ff69f37ede6bdf6c.yaml @@ -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. diff --git a/test/python/circuit/test_circuit_operations.py b/test/python/circuit/test_circuit_operations.py index 1cd66723ecfd..11770d896bc6 100644 --- a/test/python/circuit/test_circuit_operations.py +++ b/test/python/circuit/test_circuit_operations.py @@ -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 @@ -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.