Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions opensquirrel/passes/merger/two_qubit_gates_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,17 @@ def merge(self, ir: IR, qubit_register_size: int) -> None:

"""
graph = build_graph(ir)
if len(graph.nodes) == 1:
return

groups = group_gates(graph)
statements = []

for group in groups:
statement_indices, qubit_indices = group
if len(qubit_indices) == 2 and len(statement_indices) > 1:
merged_gate = _merge_gate_group(ir, group)
statements.append(merged_gate)
if len(qubit_indices) == 2:
statement = _merge_gate_group(ir, group)
else:
statements.append(ir.statements[statement_indices.pop()])
statement = ir.statements[statement_indices.pop()]

if not (isinstance(statement, Gate) and statement.is_identity()):
statements.append(statement)

ir.statements = statements
8 changes: 7 additions & 1 deletion tests/passes/merger/test_two_qubit_gates_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def merger() -> TwoQubitGatesMerger:
(CircuitBuilder(1).H(0).to_circuit(), CircuitBuilder(1).H(0).to_circuit()),
(CircuitBuilder(2).CNOT(0, 1).to_circuit(), CircuitBuilder(2).CNOT(0, 1).to_circuit()),
(CircuitBuilder(2).CNOT(0, 1).CNOT(1, 0).CNOT(0, 1).to_circuit(), CircuitBuilder(2).SWAP(0, 1).to_circuit()),
(CircuitBuilder(2).SWAP(0, 1).SWAP(0, 1).to_circuit(), CircuitBuilder(2).I(0).to_circuit()),
(CircuitBuilder(2).SWAP(0, 1).CZ(0, 1).S(0).S(1).to_circuit(), CircuitBuilder(2).ISWAP(0, 1).to_circuit()),
(CircuitBuilder(2).H(1).CNOT(0, 1).H(1).to_circuit(), CircuitBuilder(2).CZ(0, 1).to_circuit()),
(CircuitBuilder(2).CV(0, 1).CV(0, 1).to_circuit(), CircuitBuilder(2).CNOT(0, 1).to_circuit()),
Expand Down Expand Up @@ -296,3 +295,10 @@ def test_two_qubit_gates_merger_with_non_instructions(
"Both should be either TwoQubitGate or SingleQubitGate."
)
raise TypeError(msg)


def test_two_qubit_gates_merger_filters_out_identity_gates(merger: TwoQubitGatesMerger) -> None:
circuit = CircuitBuilder(2).SWAP(0, 1).SWAP(0, 1).to_circuit()
merger.merge(circuit.ir, circuit.qubit_register_size)

assert len(circuit.ir.statements) == 0
Loading