Curriculum / Quantum Hardware & NISQ / SWAP Networks: Routing on Linear Topology

Lesson 9 of 20Code challengePro+150 XP

SWAP Networks: Routing on Linear Topology

Implement qubit routing for circuits on hardware with limited connectivity.

SWAP Networks: Routing on Linear Topology

Real superconducting hardware has limited qubit connectivity. When your algorithm needs a gate between non-adjacent qubits, the compiler inserts SWAP gates to move the qubit state to an adjacent position. This is called qubit routing or qubit mapping and is one of the largest sources of overhead in NISQ circuits.

Why Connectivity Limits Matter

On linear topology q0-q1-q2-q3, only adjacent pairs can interact:

  • q0 can only gate with q1
  • q1 can gate with q0 or q2
  • q2 can gate with q1 or q3
  • q3 can only gate with q2

Any algorithm requiring a gate between q0 and q3 must route: SWAP q1/q2 (or q2/q3) to bring the target qubits adjacent. Each SWAP costs 3 CNOTs.

Building GHZ on Linear Topology

The 4-qubit GHZ state can be built efficiently on linear topology:

  1. 1.H on q0 (create superposition)
  2. 2.CX(q0, q1): entangle q1
  3. 3.CX(q1, q2): entangle q2 (using q1 as relay)
  4. 4.CX(q2, q3): entangle q3 (using q2 as relay)

This cascades the entanglement along the chain, each CX using only adjacent qubits. Total: 1 H + 3 CX = 4 gates, depth 4.

The Non-Adjacent Problem

Now try to create a controlled interaction between q0 and q3 (distance 3 on the chain). You need to route q3 to be adjacent to q0 via SWAPs:

Method: SWAP q3 to position q2 (3 CNOTs), then SWAP to q1 (3 CNOTs), now q3's state is at position q1 and can interact with q0. Total: 2 SWAPs x 3 CNOTs = 6 extra CNOTs before the actual gate.

For an algorithm with many such long-range interactions, SWAP overhead can 3-5x the circuit depth.

Routing Heuristics

Modern quantum compilers (Qiskit, Cirq) use routing algorithms to minimize SWAP count:

  • SABRE: Iterative routing that locally minimizes swaps
  • Lookahead routing: Plans several gates ahead to avoid expensive routing situations
  • Layout selection: Choose the initial qubit mapping (which logical qubit goes to which physical qubit) to minimize total SWAPs

This is the opening of the lesson. The full walkthrough, the interactive circuit, and the graded challenge continue inside myqubit.

How this lesson works

A hands-on coding challenge. You write Qiskit-compatible Python in the browser editor, run it instantly via WebAssembly, watch the circuit and Bloch sphere react, and pass automatic output checks. The AI tutor Qubitus gives Socratic hints if you get stuck.

Part of: Quantum Hardware & NISQ

Explore the physics of real quantum computers, understand noise, and learn near-term algorithms designed for today's noisy hardware.

This lesson is part of Pro

Unlock Quantum Hardware & NISQ and all 10 tracks with Pro: $12.99/month, $79/year, or $97 lifetime. Start with the free track first if you are new.