Routing Strategies — SWAP‑Based Routing Under the Hood
From Abstract Circuits to Hardware Reality
Quantum circuits are written in an abstract world where any qubit can interact with any other qubit. Real quantum hardware does not work that way. Physical qubits are connected in limited topologies, and two‑qubit gates can only run on qubits that are directly linked. The transpiler bridges this gap by inserting SWAP gates to move quantum information around. This article explores two fundamental SWAP‑based routing strategies: Basic Swap and Lookahead Swap.
Setting Up Your Environment
We will use a virtual environment named medium_quantum_venv to keep dependencies isolated.
For Windows:
python -m venv medium_quantum_venv
medium_quantum_venv\Scripts\activateFor macOS/Linux:
python3 -m venv medium_quantum_venv
source medium_quantum_venv/bin/activateInstalling Dependencies
pip install qiskit==2.5.0 qiskit-aer jupyterLaunch Jupyter Notebook:
jupyter notebookYou can also use Google Colab for this tutorial.
Complete Notebook
The complete Jupyter notebook with all code examples is available at:
GitHub Repository:
Get Rahul Tech Articles’s stories in your inbox
Join Medium for free to get updates from this writer.
What We Will Build
We will create a simple quantum circuit with a CNOT gate between qubits that are not directly connected on a target device. Then we will transpile this circuit using two different routing strategies:
- Basic Swap: The simplest approach, inserting SWAP gates with minimal effort.
- Lookahead Swap: A more intelligent strategy that looks ahead to choose optimal SWAP placements.
For each strategy, we will:
- Define a coupling map (device connectivity)
- Build a quantum circuit
- Transpile using the specific routing method
- Visualise the resulting circuit
- Simulate and verify the output
Basic Swap Routing
Basic Swap is the simplest routing strategy. When it encounters a two‑qubit gate acting on qubits that are not connected, it inserts SWAP gates along the shortest path until the qubits become adjacent. It does not consider future gates, making it fast but often suboptimal.
from qiskit import QuantumCircuit, transpile
from qiskit.transpiler import CouplingMap
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram
import matplotlib.pyplot as plt
from IPython.display import display
coupling_map = CouplingMap([[0, 1], [1, 2]])
qc = QuantumCircuit(3)
qc.cx(0, 2)
qc.measure_all()
qc_basic = transpile(
qc,
coupling_map=coupling_map,
routing_method='basic',
optimization_level=0
)
print("Basic Swap transpiled circuit:")
display(qc_basic.draw('mpl'))
print(f"Gate count: {qc_basic.count_ops()}")
print(f"Circuit depth: {qc_basic.depth()}")
sim = AerSimulator()
result_basic = sim.run(qc_basic, shots=1024).result()
counts_basic = result_basic.get_counts()
print(f"Measurement results: {counts_basic}")Expected Output:
The transpiler inserted a SWAP gate to transfer the quantum state between qubits 0 and 2 via qubit 1. The measurement results show the expected superposition state.
Lookahead Swap Routing
Lookahead Swap is more sophisticated. Instead of making myopic decisions, it performs a beam search to evaluate multiple possible SWAP sequences. It looks ahead up to a certain depth (default 4 SWAPs) and width (default 4 candidates) to choose the SWAP that maximises the number of future gates that can be executed. This often results in fewer total SWAP gates for complex circuits.
qc_lookahead = transpile(
qc,
coupling_map=coupling_map,
routing_method='lookahead',
optimization_level=0
)
print("Lookahead Swap transpiled circuit:")
display(qc_lookahead.draw('mpl'))
print(f"Gate count: {qc_lookahead.count_ops()}")
print(f"Circuit depth: {qc_lookahead.depth()}")
result_lookahead = sim.run(qc_lookahead, shots=1024).result()
counts_lookahead = result_lookahead.get_counts()
print(f"Measurement results: {counts_lookahead}")Expected Output:
For this simple circuit, both strategies produce identical results. The difference becomes apparent with larger, more complex circuits where lookahead can avoid unnecessary SWAPs.
Comparing the Two Strategies
Visualising Both Circuits Side by Side
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
qc_basic.draw('mpl', ax=ax1)
ax1.set_title('Basic Swap')
qc_lookahead.draw('mpl', ax=ax2)
ax2.set_title('Lookahead Swap')
plt.tight_layout()
plt.show()Expected Output:
Two circuit diagrams, displayed side by side, show the same structure in this simple example.
Note: Because our example contains only one CNOT gate, both routing algorithms yield identical circuits. For a more complex circuit with multiple two‑qubit gates, Lookahead often produces a lower gate count and depth.
Key Takeaways
Basic Swap is straightforward and fast. It works well for small circuits or when execution speed is more important than gate count. However, it can insert more SWAP gates than necessary because it only considers the current gate.
Lookahead Swap is more intelligent. Evaluating multiple future scenarios often produces circuits with fewer SWAP gates. This comes at the cost of higher computational overhead, making it better suited for larger circuits where optimisation matters.
Both strategies are available as routing plugins in Qiskit 2.5.0. The choice depends on your priorities: speed versus optimality.









