From 743739b1eea3984394f0a1e58268f331bb517cb0 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Fri, 17 May 2024 16:49:16 -0400 Subject: [PATCH] add insert_barrier argument to UnitaryOverlap (#12321) * add insert_barrier argument to UnitaryOverlap * set fold=-1 in circuit drawing --- qiskit/circuit/library/overlap.py | 10 +++++++++- test/python/circuit/library/test_overlap.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/qiskit/circuit/library/overlap.py b/qiskit/circuit/library/overlap.py index 38f5fb9184e1..2db6a80eedcc 100644 --- a/qiskit/circuit/library/overlap.py +++ b/qiskit/circuit/library/overlap.py @@ -59,7 +59,12 @@ class UnitaryOverlap(QuantumCircuit): """ def __init__( - self, unitary1: QuantumCircuit, unitary2: QuantumCircuit, prefix1="p1", prefix2="p2" + self, + unitary1: QuantumCircuit, + unitary2: QuantumCircuit, + prefix1: str = "p1", + prefix2: str = "p2", + insert_barrier: bool = False, ): """ Args: @@ -69,6 +74,7 @@ def __init__( if it is parameterized. Defaults to ``"p1"``. prefix2: The name of the parameter vector associated to ``unitary2``, if it is parameterized. Defaults to ``"p2"``. + insert_barrier: Whether to insert a barrier between the two unitaries. Raises: CircuitError: Number of qubits in ``unitary1`` and ``unitary2`` does not match. @@ -95,6 +101,8 @@ def __init__( # Generate the actual overlap circuit super().__init__(unitaries[0].num_qubits, name="UnitaryOverlap") self.compose(unitaries[0], inplace=True) + if insert_barrier: + self.barrier() self.compose(unitaries[1].inverse(), inplace=True) diff --git a/test/python/circuit/library/test_overlap.py b/test/python/circuit/library/test_overlap.py index a603f28037b1..1a95e3ba9155 100644 --- a/test/python/circuit/library/test_overlap.py +++ b/test/python/circuit/library/test_overlap.py @@ -131,6 +131,21 @@ def test_mismatching_qubits(self): with self.assertRaises(CircuitError): _ = UnitaryOverlap(unitary1, unitary2) + def test_insert_barrier(self): + """Test inserting barrier between circuits""" + unitary1 = EfficientSU2(1, reps=1) + unitary2 = EfficientSU2(1, reps=1) + overlap = UnitaryOverlap(unitary1, unitary2, insert_barrier=True) + self.assertEqual(overlap.count_ops()["barrier"], 1) + self.assertEqual( + str(overlap.draw(fold=-1, output="text")).strip(), + """ + ┌───────────────────────────────────────┐ ░ ┌──────────────────────────────────────────┐ +q: ┤ EfficientSU2(p1[0],p1[1],p1[2],p1[3]) ├─░─┤ EfficientSU2_dg(p2[0],p2[1],p2[2],p2[3]) ├ + └───────────────────────────────────────┘ ░ └──────────────────────────────────────────┘ +""".strip(), + ) + if __name__ == "__main__": unittest.main()