Skip to content

Commit

Permalink
* Added pass to remove unneeded SWAP gates.
Browse files Browse the repository at this point in the history
  • Loading branch information
eggerdj committed Oct 8, 2024
1 parent 56f08bf commit d9ca47c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions qopt_best_practices/transpilation/preset_qaoa_passmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from qiskit.circuit.library import CXGate

from qopt_best_practices.transpilation.qaoa_construction_pass import QAOAConstructionPass
from qopt_best_practices.transpilation.swap_cancellation_pass import SwapToFinalMapping


def qaoa_swap_strategy_pm(config: Dict[str, Any]):
Expand Down Expand Up @@ -39,6 +40,7 @@ def qaoa_swap_strategy_pm(config: Dict[str, Any]):
swap_strategy,
edge_coloring,
),
SwapToFinalMapping(),
HighLevelSynthesis(basis_gates=basis_gates),
InverseCancellation(gates_to_cancel=[CXGate()]),
QAOAConstructionPass(num_layers),
Expand Down
41 changes: 40 additions & 1 deletion test/test_qaoa_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
from qiskit.primitives import StatevectorEstimator
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes.routing.commuting_2q_gate_routing import SwapStrategy
from qiskit.transpiler.passes import HighLevelSynthesis
from qiskit.transpiler.passes.routing.commuting_2q_gate_routing import (
SwapStrategy,
FindCommutingPauliEvolutions,
Commuting2qGateRouter,
)

from qopt_best_practices.transpilation.qaoa_construction_pass import QAOAConstructionPass
from qopt_best_practices.transpilation.preset_qaoa_passmanager import qaoa_swap_strategy_pm
from qopt_best_practices.transpilation.swap_cancellation_pass import SwapToFinalMapping


class TestQAOAConstruction(TestCase):
Expand Down Expand Up @@ -95,3 +101,36 @@ def test_depth_two_qaoa_pass(self):
)

self.assertAlmostEqual(value, expected)

def test_swap_construction(self):
"""Test that redundent SWAP gates are removed."""
cost_op = SparsePauliOp.from_list(
[("IIIIZZ", 1), ("IIZZII", 1), ("ZZIIII", 1), ("IIZIIZ", 1)],
)

ansatz = QAOAAnsatz(
cost_op, reps=1, initial_state=QuantumCircuit(6), mixer_operator=QuantumCircuit(6)
)

# Test with the SWAP removal
qaoa_pm = PassManager(
[
HighLevelSynthesis(basis_gates=["PauliEvolution"]),
FindCommutingPauliEvolutions(),
Commuting2qGateRouter(SwapStrategy.from_line(range(6))),
SwapToFinalMapping(),
]
)

self.assertEqual(qaoa_pm.run(ansatz).count_ops()["swap"], 2)

# Test without the SWAP removal
qaoa_pm = PassManager(
[
HighLevelSynthesis(basis_gates=["PauliEvolution"]),
FindCommutingPauliEvolutions(),
Commuting2qGateRouter(SwapStrategy.from_line(range(6))),
]
)

self.assertEqual(qaoa_pm.run(ansatz).count_ops()["swap"], 3)

0 comments on commit d9ca47c

Please sign in to comment.