Skip to content

Commit

Permalink
Merge 9fa621c into d14fdb4
Browse files Browse the repository at this point in the history
  • Loading branch information
garrison committed Apr 25, 2024
2 parents d14fdb4 + 9fa621c commit 1ee6110
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion circuit_knitting/cutting/cutting_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def _remove_resets_in_zero_state(
circuit = circuit.copy()

# Keep up with which qubits have at least one non-reset instruction
active_qubits = set()
active_qubits: set[int] = set()
remove_ids = []
for i, inst in enumerate(circuit.data):
qargs = [circuit.find_bit(q).index for q in inst.qubits]
Expand All @@ -419,6 +419,9 @@ def _remove_resets_in_zero_state(
else:
for q in qargs:
active_qubits.add(q)
# Early terminate once all qubits have become active
if len(active_qubits) == circuit.num_qubits:
break

for i in sorted(remove_ids, reverse=True):
del circuit.data[i]
Expand Down Expand Up @@ -446,6 +449,9 @@ def _remove_final_resets(
else:
for q in qargs:
qubit_ended.discard(q)
# Early terminate once all qubits have been touched
if not qubit_ended:
break

for i in sorted(remove_ids, reverse=True):
del circuit.data[i]
Expand Down
23 changes: 22 additions & 1 deletion test/cutting/test_cutting_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def test_optimize_single_reset_in_diff_qubits(self):
_remove_final_resets(circuit)
self.assertEqual(expected, circuit)

def test_optimize_single_reset(self):
def test_optimize_single_final_reset(self):
"""Remove a single final reset
qr0:--[H]--|0>-- ==> qr0:--[H]--
"""
Expand All @@ -291,6 +291,27 @@ def test_optimize_single_reset(self):

self.assertEqual(expected, circuit)

def test_optimize_single_final_reset_2(self):
"""Remove a single final reset on two qubits
qr0:--[H]--|0>-- ==> qr0:--[H]-------
--[X]--[S]-- --[X]--[S]--
"""
qr = QuantumRegister(2, "qr")
circuit = QuantumCircuit(qr)
circuit.h(0)
circuit.x(1)
circuit.reset(0)
circuit.s(1)

expected = QuantumCircuit(qr)
expected.h(0)
expected.x(1)
expected.s(1)

_remove_final_resets(circuit)

self.assertEqual(expected, circuit)

def test_dont_optimize_non_final_reset(self):
"""Do not remove reset if not final instruction
qr0:--|0>--[H]-- ==> qr0:--|0>--[H]--
Expand Down

0 comments on commit 1ee6110

Please sign in to comment.