From 5fae9a54ba136129e4dfb320966e30a0e39e6602 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Thu, 2 Nov 2023 10:20:24 -0500 Subject: [PATCH 1/5] Allow barriers in overlap circuit inputs --- qiskit/circuit/library/overlap.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qiskit/circuit/library/overlap.py b/qiskit/circuit/library/overlap.py index 15c39435441d..a98e21687564 100644 --- a/qiskit/circuit/library/overlap.py +++ b/qiskit/circuit/library/overlap.py @@ -15,6 +15,7 @@ from qiskit.circuit import QuantumCircuit, Gate from qiskit.circuit.parametervector import ParameterVector from qiskit.circuit.exceptions import CircuitError +from qiskit.circuit.library import Barrier class UnitaryOverlap(QuantumCircuit): @@ -101,7 +102,7 @@ def _check_unitary(circuit): """Check a circuit is unitary by checking if all operations are of type ``Gate``.""" for instruction in circuit.data: - if not isinstance(instruction.operation, Gate): + if not isinstance(instruction.operation, [Gate, Barrier]): raise CircuitError( ( "One or more instructions cannot be converted to" From dddea0bc0ab87c427e0fc614a0498f05a76076a2 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Mon, 6 Nov 2023 09:50:04 -0600 Subject: [PATCH 2/5] Minor fixes plus test with barriers --- qiskit/circuit/library/overlap.py | 4 ++-- test/python/circuit/library/test_overlap.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/qiskit/circuit/library/overlap.py b/qiskit/circuit/library/overlap.py index a98e21687564..ed86d8abb9a2 100644 --- a/qiskit/circuit/library/overlap.py +++ b/qiskit/circuit/library/overlap.py @@ -15,7 +15,7 @@ from qiskit.circuit import QuantumCircuit, Gate from qiskit.circuit.parametervector import ParameterVector from qiskit.circuit.exceptions import CircuitError -from qiskit.circuit.library import Barrier +from qiskit.circuit import Barrier class UnitaryOverlap(QuantumCircuit): @@ -102,7 +102,7 @@ def _check_unitary(circuit): """Check a circuit is unitary by checking if all operations are of type ``Gate``.""" for instruction in circuit.data: - if not isinstance(instruction.operation, [Gate, Barrier]): + if not isinstance(instruction.operation, (Gate, Barrier)): raise CircuitError( ( "One or more instructions cannot be converted to" diff --git a/test/python/circuit/library/test_overlap.py b/test/python/circuit/library/test_overlap.py index a9a5f0ae1e2a..1bc8b14b4f20 100644 --- a/test/python/circuit/library/test_overlap.py +++ b/test/python/circuit/library/test_overlap.py @@ -122,6 +122,16 @@ def test_mismatching_qubits(self): with self.assertRaises(CircuitError): _ = UnitaryOverlap(unitary1, unitary2) + def test_barrier(self): + """Test that barriers on input circuits are well handled""" + unitary1 = EfficientSU2(1, reps=0) + unitary1.barrier() + unitary2 = EfficientSU2(1, reps=1) + unitary2.barrier() + + _ = UnitaryOverlap(unitary1, unitary2) + + if __name__ == "__main__": unittest.main() From 3541831d7bb64bef4a43cce53eeae293277b2b1d Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Mon, 6 Nov 2023 09:56:08 -0600 Subject: [PATCH 3/5] Check parameter number on barrier test, similar to other non-failing tests. --- test/python/circuit/library/test_overlap.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/python/circuit/library/test_overlap.py b/test/python/circuit/library/test_overlap.py index 1bc8b14b4f20..3122f7240ee5 100644 --- a/test/python/circuit/library/test_overlap.py +++ b/test/python/circuit/library/test_overlap.py @@ -128,9 +128,8 @@ def test_barrier(self): unitary1.barrier() unitary2 = EfficientSU2(1, reps=1) unitary2.barrier() - - _ = UnitaryOverlap(unitary1, unitary2) - + overlap = UnitaryOverlap(unitary1, unitary2) + self.assertEqual(overlap.num_parameters, unitary1.num_parameters + unitary2.num_parameters) if __name__ == "__main__": From 11925667a020ab72120dbf64bfd68c6636874200 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Mon, 6 Nov 2023 09:57:10 -0600 Subject: [PATCH 4/5] test order --- test/python/circuit/library/test_overlap.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/python/circuit/library/test_overlap.py b/test/python/circuit/library/test_overlap.py index 3122f7240ee5..fa5e4af05c0d 100644 --- a/test/python/circuit/library/test_overlap.py +++ b/test/python/circuit/library/test_overlap.py @@ -82,6 +82,15 @@ def test_partial_parameterized_inputs2(self): overlap = UnitaryOverlap(unitary1, unitary2) self.assertEqual(overlap.num_parameters, unitary1.num_parameters) + def test_barrier(self): + """Test that barriers on input circuits are well handled""" + unitary1 = EfficientSU2(1, reps=0) + unitary1.barrier() + unitary2 = EfficientSU2(1, reps=1) + unitary2.barrier() + overlap = UnitaryOverlap(unitary1, unitary2) + self.assertEqual(overlap.num_parameters, unitary1.num_parameters + unitary2.num_parameters) + def test_measurements(self): """Test that exception is thrown for measurements""" unitary1 = EfficientSU2(2) @@ -122,15 +131,6 @@ def test_mismatching_qubits(self): with self.assertRaises(CircuitError): _ = UnitaryOverlap(unitary1, unitary2) - def test_barrier(self): - """Test that barriers on input circuits are well handled""" - unitary1 = EfficientSU2(1, reps=0) - unitary1.barrier() - unitary2 = EfficientSU2(1, reps=1) - unitary2.barrier() - overlap = UnitaryOverlap(unitary1, unitary2) - self.assertEqual(overlap.num_parameters, unitary1.num_parameters + unitary2.num_parameters) - if __name__ == "__main__": unittest.main() From 3386ed25728ad6a49b3fe62aa84671fa9c2468f4 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Mon, 6 Nov 2023 12:14:23 -0600 Subject: [PATCH 5/5] Add release note --- .../notes/overlap-circuit-barriers-63b9b1be9c1da100.yaml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 releasenotes/notes/overlap-circuit-barriers-63b9b1be9c1da100.yaml diff --git a/releasenotes/notes/overlap-circuit-barriers-63b9b1be9c1da100.yaml b/releasenotes/notes/overlap-circuit-barriers-63b9b1be9c1da100.yaml new file mode 100644 index 000000000000..dcae6e5585f1 --- /dev/null +++ b/releasenotes/notes/overlap-circuit-barriers-63b9b1be9c1da100.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixed a bug which caused :class:`~qiskit.circuit.library.UnitaryOverlap` to error upon initialization if given an input circuit containing a barrier.