Pricing…Open Lab
Chapter 05 of 12 · ~14 min

SWAP Insertion

A SWAP trades the full states of two qubits. So after every SWAP, the compiler must update its table of which real qubit holds which of your qubits. If it forgets, later gates hit the wrong qubits. On most chips one SWAP costs three CX gates. So read the SWAP count, the two-qubit gate count, and the depth together.

What does a SWAP actually do?

Why care? SWAPs are often the biggest hidden cost in a compiled circuit. They do no useful work for your program. They only move things around.

A SWAP trades the complete quantum states of two neighbouring qubits. Whatever was on wire A is now on wire B, and the other way round. SWAPs are the main tool of routing. Routing means moving qubit states across the chip so that gates land on qubits that are wired together. Repeated SWAPs walk a state across the chip, one link at a time.

An everyday example: moving people through a crowded hallway. You can't jump over anyone. So two neighbours trade places, then the next two, and so on, until the person you want reaches the door. Here the example breaks. In a hallway, you can look and see who is where. In a quantum chip, you can't peek without ruining the state. So the compiler has to keep careful notes.

Those notes are key, because a SWAP changes where each of your qubits lives. The compiler keeps a mapping table. It lists, for each logical qubit (a qubit in your program), its current physical wire (a real qubit on the chip). The table must be updated after every single SWAP. A table that is out of date, or stale, is a classic compiler bug. Later gates run on real, valid wires. But those wires hold the wrong states. Nothing crashes. The answers are just wrong.

What the rest of this chapter covers
  1. Worked example: a SWAP built from three CXs
  2. Can you walk a state down the line?INTERACTIVE
  3. Worked example: what those SWAPs cost
  4. What does the stale-table bug look like?
  5. What does the correct compiler produce?INTERACTIVE
  6. Can you make the bug happen?INTERACTIVE
  7. Where does this overhead show up in the metrics?
Keep learning with Pro

You’ve read the opening of chapter 5. Pro unlocks the other 7 sections — plus every chapter of every course, with circuits you can run right on the page. That’s $11.99 a month, about the price of a coffee, or $99.99 a year (save 30%). The first chapter of every course, and the whole math course, stay free.

Start learning with ProSee plansRead chapter 1 free
SWAP Insertion · QPU137