Skip to content

Commit

Permalink
add insert_barrier argument to UnitaryOverlap
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung committed May 1, 2024
1 parent a41690d commit 3213a12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion qiskit/circuit/library/overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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)


Expand Down
15 changes: 15 additions & 0 deletions test/python/circuit/library/test_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).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()

0 comments on commit 3213a12

Please sign in to comment.