Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add insert_barrier argument to UnitaryOverlap #12321

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.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()