The SWAP Gate
Suppose two qubits are sitting side by side and you simply want them to trade places —
whatever the first one holds, hand it to the second, and vice versa. That is the entire job of the
SWAP gate. It sounds almost too dull to bother naming: no flip, no phase, no
superposition trickery, just an exchange of two labels. Yet SWAP turns out to be one of the busiest gates
in any real quantum computer — not because of what it computes, but because of where the qubits happen to
sit. On hardware where a qubit can only talk to its immediate neighbours, SWAP is how you shuffle
distant qubits together so they can finally interact. Understanding this humble gate is really
understanding the traffic problem at the heart of running quantum circuits on actual chips.
The rule: exchange the two qubits
SWAP acts on two qubits. Its rule is a single line: send the joint state
|ab\rangle to |ba\rangle. Whatever the first qubit
was, the second becomes it, and whatever the second was, the first becomes it. Watch what happens to each
of the four
basis states:
|00\rangle \to |00\rangle, \qquad |01\rangle \to |10\rangle, \qquad |10\rangle \to |01\rangle, \qquad |11\rangle \to |11\rangle.
The two equal strings, |00\rangle and
|11\rangle, look identical after the exchange — swapping two matching bits
changes nothing. Only the two mixed strings actually move:
|01\rangle and |10\rangle trade places. So SWAP is,
exactly, the operation that interchanges the middle two basis states and leaves the
outer two fixed.
As a matrix
Written in the ordered basis |00\rangle, |01\rangle, |10\rangle, |11\rangle,
SWAP is the 4\times 4 permutation matrix that swaps the two middle rows:
\mathrm{SWAP} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}.
The first and last rows are the plain identity — |00\rangle and
|11\rangle are pinned in place. The middle 2\times 2
block is the off-diagonal \begin{psmallmatrix} 0 & 1 \\ 1 & 0 \end{psmallmatrix},
which interchanges |01\rangle and |10\rangle. Because
it only permutes basis states it is unitary, it is manifestly
symmetric (swapping qubit 1 with 2 is the same as swapping 2 with 1), and swapping the
same pair back again shows it is its own inverse:
\mathrm{SWAP}^2 = I.
The circuit symbol
In a circuit diagram each qubit is a horizontal wire read left to right in time. SWAP is drawn as two
× marks — one on each wire — joined by a vertical line. It is deliberately
symmetric: there is no control and no target, because neither qubit is special. The wires simply cross
over, and whatever entered the top comes out the bottom and vice versa.
Three CNOTs make a SWAP
Real hardware rarely offers SWAP as a built-in primitive; instead it is built from the two-qubit
gate the machine does have, usually the
CNOT. The classic
identity is that three CNOTs in a row make a SWAP, alternating which qubit is the
control:
\mathrm{SWAP} \;=\; \mathrm{CNOT}_{1\to 2}\;\cdot\;\mathrm{CNOT}_{2\to 1}\;\cdot\;\mathrm{CNOT}_{1\to 2}.
Here \mathrm{CNOT}_{1\to 2} means qubit 1 controls a flip on qubit 2,
and \mathrm{CNOT}_{2\to 1} is the reverse. Read left to right as gates applied
in time. Step through the equivalent circuit below, then trace it on a basis state in the next section to
see why it works.
Worked example 1: trace SWAP on |01\rangle
By the rule. The input is |ab\rangle = |01\rangle, so
a = 0 and b = 1. Exchanging gives
|ba\rangle = |10\rangle. Done.
By the three CNOTs. Track the pair of bits through each gate. Start at
|01\rangle (control = first bit, target flips when its control is
1):
- \mathrm{CNOT}_{1\to 2}: qubit 1 is 0, so
qubit 2 is left alone. |01\rangle \to |01\rangle.
- \mathrm{CNOT}_{2\to 1}: now qubit 2 is the control and it is
1, so flip qubit 1 from 0 to
1. |01\rangle \to |11\rangle.
- \mathrm{CNOT}_{1\to 2}: qubit 1 is now 1, so
flip qubit 2 from 1 to 0.
|11\rangle \to |10\rangle.
The chain runs |01\rangle \to |01\rangle \to |11\rangle \to |10\rangle, landing
on |10\rangle — exactly what the rule gave. The two routes agree, confirming
the three-CNOT circuit really is a SWAP.
Worked example 2: SWAP does not entangle
Here is a fact that separates SWAP from its cousins CNOT and
controlled-Z:
SWAP never creates entanglement. It only relabels which wire carries which state. Take a
clean product state — qubit 1 in |0\rangle and qubit 2 in
|+\rangle = \tfrac{1}{\sqrt2}(|0\rangle + |1\rangle):
|0\rangle \otimes |+\rangle = \tfrac{1}{\sqrt2}\big(|00\rangle + |01\rangle\big).
Apply SWAP term by term. It fixes |00\rangle and turns
|01\rangle into |10\rangle:
\tfrac{1}{\sqrt2}\big(|00\rangle + |01\rangle\big) \;\xrightarrow{\;\mathrm{SWAP}\;}\; \tfrac{1}{\sqrt2}\big(|00\rangle + |10\rangle\big) = |+\rangle \otimes |0\rangle.
The output factors cleanly as |+\rangle \otimes |0\rangle — still a
product state, just with the two qubits' roles exchanged. Compare this with the
H-then-CNOT recipe, where CNOT on a superposed control produces the
non-factoring Bell state. SWAP has no such power: feed it a product and you always get a product
back. It moves quantum information from place to place without ever mixing it together.
Why SWAP matters: routing on real hardware
On many real devices — superconducting chips especially — a qubit can only interact with its
nearest neighbours on the chip's grid. But an algorithm may call for a two-qubit gate
between qubits that are physically far apart. The compiler's fix is to insert
SWAP gates that walk one qubit, step by step, along the chip until it sits next to its
partner; apply the desired gate; then optionally SWAP it back. These
SWAP networks are a genuine, dominant cost of compiling circuits to constrained
hardware, and reducing them is a whole subfield of quantum compilation.
Two close relatives are worth knowing. The controlled-SWAP — one control qubit that
decides whether two targets are exchanged — is the
Fredkin gate,
a universal reversible gate. And the √SWAP ("root-SWAP"), the gate that applied
twice gives a SWAP, is a genuinely entangling two-qubit gate that some hardware provides
natively; together with single-qubit rotations it is universal for quantum computation.
- SWAP is a two-qubit gate that exchanges the two qubits:
|ab\rangle \to |ba\rangle. On the basis it fixes
|00\rangle and |11\rangle and swaps
|01\rangle \leftrightarrow |10\rangle;
- its matrix is the 4\times 4 permutation
\begin{psmallmatrix}1&0&0&0\\0&0&1&0\\0&1&0&0\\0&0&0&1\end{psmallmatrix};
it is unitary, symmetric, and its own inverse
(\mathrm{SWAP}^2 = I);
- it equals three CNOTs:
\mathrm{CNOT}_{1\to 2}\,\mathrm{CNOT}_{2\to 1}\,\mathrm{CNOT}_{1\to 2};
- it is not entangling — a product state stays a product state; SWAP relocates
information, it never mixes it;
- its main practical role is routing: on nearest-neighbour hardware, SWAP networks
move distant qubits adjacent so a two-qubit gate can act — a major compilation cost. The
controlled-SWAP is the Fredkin gate, and √SWAP is a native
entangling gate on some devices.
Picture a superconducting processor as a city grid where every qubit lives at one intersection and can
only shake hands with the qubits directly north, south, east, and west of it. Your algorithm, written in
the abstract, cheerfully asks for a gate between a qubit in the top-left corner and one in the
bottom-right — neighbours on paper, strangers on silicon. There is no direct wire between them, so the
compiler does what a courier does in a maze of one-way streets: it SWAPs the corner qubit with its
neighbour, then that neighbour with the next, ferrying the state hop by hop across the chip until the two
finally sit side by side and the real gate can fire. Every one of those hops is three physical CNOTs. The
elegant algorithm on the whiteboard becomes, on the machine, a surprising amount of pure
logistics — and much of the art of quantum compilation is planning these routes to keep the
SWAP traffic down.
Two things to keep straight. First, SWAP's simple picture — "the wires just cross" — hides a real cost.
On hardware it is usually not a primitive; it is compiled into three
CNOTs, each an error-prone two-qubit operation. So the SWAP overhead forced by limited
connectivity is one of the biggest practical costs in running a circuit, not a free relabelling. Second,
do not confuse "moves information" with "processes information": SWAP creates no
entanglement whatsoever. It permutes basis states, so a product state comes out a product state
every time. If you need two qubits to become correlated you need an entangling gate like CNOT, CZ, or
√SWAP — a plain SWAP only ever relocates states, it never braids them together.