SWAP Insertion
A SWAP exchanges the full states of two qubits, so after every SWAP the compiler must update its logical-to-physical mapping table or later gates hit the wrong qubits. On most gate-based hardware one SWAP costs three CX gates, so SWAP count, native two-qubit count, and depth must be read together.
What does a SWAP actually do?
A SWAP exchanges the complete quantum states of two qubits: whatever was on wire A is now on wire B, and vice versa. It is the workhorse of routing — repeated SWAPs walk a state across the chip one connection at a time.
Crucially, a SWAP changes where every logical qubit lives. Compilers maintain a mapping table — logical qubit to current physical wire — and must update it after every single SWAP. A stale table is a classic compiler bug: later gates execute on physically valid wires that hold the wrong logical states, nothing crashes, and the answers are simply wrong.
You’ve read the opening of chapter 5 — 7 more sections follow, with worked examples and circuits you can run on the page. A free account unlocks every chapter of every course (paid plans aren’t live yet — early readers get everything free).