The SWAP gate is a two-qubit quantum logic gate that exchanges the states of two qubits, so that whatever state the first qubit held moves to the second and vice versa. In quantum computing, it is a basic building block of quantum circuits and the main tool compilers use to move quantum information between qubits that are not physically connected.
Although the gate looks trivial on paper, it carries real cost on hardware. A SWAP is usually decomposed into three CNOT gates, so every SWAP inserted for routing adds depth, time and error to a circuit. Understanding the gate therefore matters both for circuit design and for qubit routing on real processors.
How the SWAP gate acts on two qubits
The SWAP gate maps the computational basis states as |00⟩ to |00⟩, |01⟩ to |10⟩, |10⟩ to |01⟩ and |11⟩ to |11⟩. Because it is linear and unitary, it exchanges arbitrary superpositions as well, including entangled states shared with other qubits. The gate is its own inverse, so applying it twice returns the original state.
Its 4 × 4 unitary matrix is a permutation matrix: it has ones on the diagonal for |00⟩ and |11⟩ and swaps the two middle basis states. The gate is also Hermitian, which means it is an observable as well as an operation, with eigenvalue +1 on the symmetric subspace and −1 on the antisymmetric singlet state.
The following table summarizes the action of the gate on the four basis states and the key algebraic properties.
| Property | Value | Consequence |
|---|---|---|
| Input |01⟩ | Output |10⟩ | Qubit values change places |
| Input |00⟩ or |11⟩ | Unchanged | Symmetric states are fixed points |
| Inverse | SWAP itself | Applying it twice is the identity |
| Eigenvalues | +1 (three times), −1 (once) | Singlet state picks up a sign |
| Entangling power | Zero | Cannot create entanglement alone |
| Clifford group | Member | Efficiently simulable classically |
Decomposing SWAP into native gates
Most processors do not offer SWAP natively, so compilers express it through the native gate set. The standard identity uses three alternating CNOT gates: a CNOT from qubit A to B, then from B to A, then again from A to B. This construction is the quantum counterpart of the XOR swap trick from classical programming.
Because the SWAP gate needs three entangling operations in most hardware, its error is roughly three times that of a single two-qubit gate. Some platforms reduce this cost with alternative native interactions, which the table below compares.
| Native two-qubit gate | Gates per SWAP | Typical platform |
|---|---|---|
| CNOT or CZ | 3 plus single-qubit gates | Superconducting and trapped-ion devices |
| iSWAP | 3 plus single-qubit gates | Tunable-coupler superconducting chips |
| √iSWAP | 3 | Some superconducting processors |
| Mølmer–Sørensen (XX) | 3 | Trapped-ion devices |
| Physical ion reordering | No gates needed | Shuttling trapped-ion architectures |
The key point is that SWAP has the maximal interaction content of a two-qubit gate in the Cartan (KAK) decomposition, so it can never be built from fewer than three uses of a CNOT-class gate. This is a hard limit, not an implementation detail.
Why routing on real hardware needs SWAP gates
Real processors have limited qubit connectivity: a superconducting chip on a heavy-hex or square lattice lets each qubit interact only with two to four neighbors. When an algorithm requires a two-qubit gate between distant qubits, the compiler inserts a chain of SWAP gates to bring the states next to each other.
This process, called qubit routing or qubit mapping, is solved by heuristics such as SABRE because finding the optimal sequence is NP-hard in general. Routing overhead often dominates the gate count of compiled circuits on sparse lattices.
The following decision table shows how compilers and designers typically limit SWAP overhead.
| Situation | Recommended approach | Reason |
|---|---|---|
| Interacting qubits far apart | Smart initial layout | Fewer SWAPs needed later |
| SWAP followed by CNOT on same pair | Merge into one block | Resynthesis saves gates |
| Dense all-to-all algorithm | Choose ion-trap or neutral-atom hardware | Higher native connectivity |
| SWAP at the circuit end | Relabel qubits in software | Classical permutation is free |
| Repeated layers of interactions | Use SWAP networks | Predictable linear depth |
How SWAP compares with related gates
The SWAP gate sits among several gates that exchange or condition information. It is easy to confuse with the iSWAP gate and the Fredkin gate, yet each has a distinct role in circuit design.
| Gate | Qubits | Main difference from SWAP |
|---|---|---|
| SWAP | 2 | Pure exchange, no entanglement created |
| iSWAP | 2 | Exchange with phase i, entangling |
| √SWAP | 2 | Half exchange, entangling and universal with single-qubit gates |
| Fredkin (controlled-SWAP) | 3 | Swap only if control qubit is 1 |
| CNOT | 2 | Conditional flip, entangling |
The controlled-SWAP version underpins the SWAP test, a short circuit that estimates the overlap between two quantum states by measuring a single ancilla qubit. The SWAP test is used in quantum machine learning and state comparison.
Limitations and common misconceptions
Several mistakes recur when engineers first work with SWAP gates. The most frequent is treating a SWAP as a free operation, when on most hardware it is one of the most expensive gates in a circuit.
- SWAP does not copy a state: the no-cloning theorem forbids that, and the gate only moves states.
- SWAP alone is not universal: combined with single-qubit gates it still cannot create entanglement.
- Final SWAPs are often unnecessary, because the compiler can track the permutation classically.
- SWAP errors accumulate quickly on long routing chains and can dominate total circuit error.
Put simply
A SWAP gate makes two qubits trade places in content. Imagine two boxes that each hold a note. The gate moves the note from the first box to the second and the other note back. Nothing is lost and nothing is copied.
On a real quantum chip, qubits can talk only to their neighbors. When two distant qubits need to work together, the computer passes the information along step by step with SWAP gates. Each step costs time and adds a little error. So fewer SWAPs mean a more reliable result.
In practice
A research team runs a variational chemistry algorithm on a 27-qubit superconducting processor with a heavy-hex layout. The ideal circuit needs two-qubit interactions between qubits that sit four positions apart on the chip. The compiler inserts three SWAP gates per interaction to bring the states together, which adds nine CNOT gates for each such link.
At a two-qubit error rate of about 0.8 percent, this routing overhead noticeably lowers the measured energy accuracy. The team then changes the initial qubit layout so that the most frequently interacting qubits start next to each other and lets the compiler track the final permutation in software. The compiled circuit uses roughly 40 percent fewer CNOT gates, and the computed ground-state energy moves closer to the exact classical reference value.