Single-Qubit Gates
A quantum gate is a unitary transformation that rotates the state on the Bloch sphere. A matrix is unitary when:
which is exactly the statement that probabilities still sum to one after the gate runs: . It also makes gates invertible â every gate has an adjoint you can run backwards, which is why uncomputation works at all.
The parameter count sets expectations. A single-qubit unitary has four real parameters less one global phase, so three that matter: three Euler angles, one rotation about , one about , one about again. Any single-qubit gate can therefore be written as , and no single-qubit gate ever needs more than three parameters to describe. Two-qubit gates need fifteen. That growth is the practical reason circuits are built from many simple gates rather than few complex ones.
| Gate | Matrix | Bloch action | Typical use |
|---|---|---|---|
| rotation about | flips toward | dynamical decoupling | |
| rotation about | flips toward | state preparation | |
| rotation about | phase only | echo, virtual Z | |
| about | superposition |
Pauli Gates
The Pauli matrices are the fundamental single-qubit gates and the basis of almost every measurement you will run:
Pauli-X (the quantum NOT):
It swaps and is a rotation about the -axis of the Bloch sphere.
Pauli-Y:
Pauli-Z (phase flip):
leaving untouched and mapping . The minus sign is unobservable in a bare measurement â both basis states keep the same probability â and becomes visible the moment the state is interfered with a Hadamard afterwards. That distinction is why phase errors are silent to direct readout and only show up in process tomography or repeated interference.
The three anticommute, , which is the algebraic reason a single gate cannot rotate two axes at once and why sequences like ââ are needed to average several noise directions away. Products of Paulis with coefficients form the Pauli group, the set whose simultaneous eigenstates are the stabilisers used in error correction.
Hadamard Gate
The Hadamard creates superposition from a basis state:
with the defining behaviour:
- : it is its own inverse
On the Bloch sphere it is a rotation about the axis halfway between and , which swaps those two axes â a fact that pays off immediately, since lets you move a phase flip to a bit flip and back. Measuring in the basis is just followed by a measurement, so one Hadamard substitutes for an entirely separate readout channel.
The cost of a Hadamard on hardware is not zero: it is a virtual-Z plus a calibrated microwave pulse on most superconducting devices, priced at roughly a tenth of a two-qubit gate, and it is counted in single-qubit gate error budgets rather than being free.
Phase Gates
S gate (the phase gate):
T gate (the gate):
The relationships are the ones worth memorising: , , and . The nickname " gate" is historical and misleading â the actual phase applied is â but the name stuck.
Together , and generate a dense subgroup of the single-qubit unitaries, so Clifford plus T is universal. The split matters for error correction: Clifford gates (, , CNOT) can be tracked in software by the stabiliser formalism and cost almost nothing to implement fault-tolerantly, while gates require magic-state distillation and dominate the resource count of any fault-tolerant circuit. A surface-code factoring circuit with millions of logical gates might use a few percent of them as gates and consume most of its physical qubits preparing the magic states those need.
The practical metric is therefore T-count, not total gate count. Reducing a circuit's T-count from 1000 to 400 is worth far more than removing 400 Hadamards.
Two-Qubit Gates
CNOT (controlled-NOT):
The target flips if and only if the control reads . It is the primary entangling gate: applied to on it produces the Bell state
The controlled-phase variant CZ differs only by a basis change on one wire: , which follows from . On hardware CZ and CNOT swap roles depending on the device â transmon qubits naturally implement a tunable-coupler CZ â and the transpiler converts between them for free, so which one you write matters far less than which couplings your layout allows.
Entangling power is the resource that actually limits algorithm depth. On a device with a heavy-hex coupling graph, an all-to-all algorithm on 20 qubits needs a SWAP network of roughly 30 extra two-qubit gates before any algorithmic gate runs, tripling the error budget of the first layer.
Toffoli Gate (CCNOT)
The Toffoli flips its target when both controls are :
As an matrix it is the identity with its last two rows exchanged â the only pair that moves is . Two facts make it central to both classical and quantum computing:
- Classically universal â any Boolean function of inputs can be built from Toffoli gates alone, with NOT for the negative cases.
- Quantum-universal alongside Hadamard â {Toffoli, } generates a dense subgroup, so a fault-tolerant scheme only has to implement these two.
Its price is the point. The standard exact decomposition costs 6 CNOT gates, 7 T gates and a handful of single-qubit rotations â so one classical AND is more expensive than an entire Bell-state preparation. That asymmetry is why quantum arithmetic circuits are quoted in T-count and why reversible classical logic inside a quantum algorithm is treated as a cost to minimise rather than a convenience.
SWAP Gate
SWAP exchanges two qubits:
decomposing into three CNOTs:
On a device with fixed nearest-neighbour coupling, SWAP is the routing primitive: when an algorithm asks for a two-qubit gate between distant qubits, the compiler inserts SWAPs to bring them together, and each SWAP is three native two-qubit gates. Circuits transpiled for heavy-hex hardware commonly double or triple their two-qubit gate count this way, which is why initial layout selection is an optimisation problem in its own right.
A lighter variant, the SWAP test, applies a controlled-SWAP and measures the control to estimate the overlap between two states â the primitive behind quantum kernel methods, and the reason a stored copy of a reference state has to exist for the test to run at all.
Python: Gate Simulator
Building the standard gates and checking the properties discussed above rather than asserting them:
import numpy as np
X = np.array([[0, 1], [1, 0]], dtype=complex)
Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
Z = np.array([[1, 0], [0, -1]], dtype=complex)
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)
CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
[0, 0, 0, 1], [0, 0, 1, 0]], dtype=complex)
def unitary(U):
return np.allclose(U.conj().T @ U, np.eye(U.shape[0]))
def bell_state():
KET_00 = np.kron(np.array([1, 0]), np.array([1, 0]))
return CNOT @ np.kron(H, np.eye(2)) @ KET_00
for name, U in [("X", X), ("Y", Y), ("Z", Z), ("H", H), ("CNOT", CNOT)]:
print(f"{name:5s} unitary={unitary(U)} hermitian={np.allclose(U, U.conj().T)}")
bell = bell_state()
probs = np.abs(bell) ** 2
print(f"Bell amplitudes: {np.round(bell, 4)}")
print(f"Bell probabilities: {np.round(probs, 4)} sum={probs.sum():.4f}")
print(f"amplitude ratio |11>/<00| = {abs(bell[3] / bell[0]):.4f}")
print(f"self-inverse CNOT: {np.allclose(CNOT @ CNOT, np.eye(4))}")
The probability vector comes out as [0.5, 0, 0, 0.5] â equal weight on and , nothing in between â and the ratio of those two amplitudes is exactly 1 with zero imaginary part, which is what distinguishes from the other three Bell states. Every gate reports unitary, as required, and Hermitian as well: , , , and CNOT are all their own inverses, a property the last line checks directly.
Python: Identities and Euler Decomposition
Checking the algebraic claims numerically, including the claim that three angles describe any single-qubit gate:
import numpy as np
X = np.array([[0, 1], [1, 0]], dtype=complex)
Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
Z = np.array([[1, 0], [0, -1]], dtype=complex)
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)
CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
[0, 0, 0, 1], [0, 0, 1, 0]], dtype=complex)
def rz(phi):
return np.diag([np.exp(-1j * phi / 2), np.exp(1j * phi / 2)])
def ry(theta):
c, s = np.cos(theta / 2), np.sin(theta / 2)
return np.array([[c, -s], [s, c]], dtype=complex)
def zyz(U):
"""Euler ZYZ angles of a single-qubit gate, up to global phase."""
U = U / np.sqrt(np.linalg.det(U)) # normalise into SU(2)
a, b = U[0, 0], U[0, 1]
if abs(b) < 1e-12:
return -np.angle(a), 0.0, -np.angle(a)
alpha = 0.5 * (-2 * np.angle(a) - 2 * np.angle(-b))
gamma = 0.5 * (-2 * np.angle(a) + 2 * np.angle(-b))
beta = 2 * np.arccos(np.clip(abs(a), 0.0, 1.0))
return alpha, beta, gamma
def err(A, B):
return float(np.max(np.abs(A - B)))
T = np.diag([1, np.exp(1j * np.pi / 4)]).astype(complex)
S = np.diag([1, 1j]).astype(complex)
I2 = np.eye(2, dtype=complex)
print("HZH == X ", err(H @ Z @ H, X) < 1e-12)
print("S == T @ T ", err(S, T @ T) < 1e-12)
print("T^4 == Z ", err(np.linalg.matrix_power(T, 4), Z) < 1e-12)
print("X Y == i Z ", err(X @ Y, 1j * Z) < 1e-12)
print("H^2 == I ", err(H @ H, I2) < 1e-12)
CZ = np.diag([1, 1, 1, -1]).astype(complex)
from_cnot = np.kron(I2, H) @ CNOT @ np.kron(I2, H)
print("CZ from CNOT ", err(from_cnot, CZ) < 1e-12)
SWAP = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]], dtype=complex)
CNOT21 = np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0]], dtype=complex)
print("SWAP = 3 CNOTs ", err(CNOT @ CNOT21 @ CNOT, SWAP) < 1e-12)
rng = np.random.default_rng(12)
G = rng.normal(size=(2, 2)) + 1j * rng.normal(size=(2, 2))
Q, R = np.linalg.qr(G) # QR of a Gaussian matrix is Haar-random
phases = np.diag(R) / np.abs(np.diag(R))
U = Q * phases # column phases, still unitary
U_su2 = U / np.sqrt(np.linalg.det(U)) # drop the global phase, land in SU(2)
alpha, beta, gamma = zyz(U)
recon = rz(alpha) @ ry(beta) @ rz(gamma)
print(f"U unitary: {np.allclose(U.conj().T @ U, np.eye(2))}")
print(f"ZYZ error: {err(recon, U_su2):.2e}")
print(f"ZYZ angles: alpha={alpha:.4f} beta={beta:.4f} gamma={gamma:.4f}")
print(f"recon unitary: {np.allclose(recon.conj().T @ recon, np.eye(2))}")
Every identity check returns True, the SWAP built from three CNOTs matches the permutation matrix exactly, and the ZYZ reconstruction lands at machine precision â confirming that three real numbers really do specify any single-qubit gate. The one subtlety worth noticing is the normalisation step: dividing by the square root of the determinant moves an arbitrary unitary into , which is where the ZYZ parameterisation lives; the discarded factor is a global phase and never affects any measurement.
Gate Decomposition Theorems
Two theorems set the cost of turning a mathematical gate into something a device can run.
Solovay-Kitaev: any single-qubit gate can be approximated to precision using gates from a fixed finite universal set, with . Working through the numbers: reaching accuracy costs on the order of 100 gates from the set . This was the workhorse result for fifteen years because it needs only a finite gate list â no arbitrary rotations required.
Ross-Selinger: when arbitrary single-qubit rotations can be synthesised rather than approximated from a finite set, the T-count for precision drops to about , roughly 100 T gates at with far fewer accompanying gates. Modern compilers use this route, and it is why the practical rule today is: synthesise rotations optimally, spend the T budget on them, and count T gates rather than total gates.
The corollary for algorithm design is that precision is cheap. per gate accumulates to about error over a thousand gates, well inside what error correction tolerates, and pushing to quadruples the log term rather than the gate count. Choose precision from the algorithm's error budget instead of defaulting to full double-precision angles from a textbook.
Common Mistakes
Counting Hadamards as free because they are single-qubit gates
Fix: Put every single-qubit gate in the error budget at roughly a tenth of a two-qubit gate, and remember that a transpiled heavy-hex layout adds SWAPs worth three two-qubit gates each
Optimising total gate count on a fault-tolerant circuit
Fix: Count T gates instead â Clifford gates are tracked in software while each T gate needs magic-state distillation, so removing Hadamards saves almost nothing
Writing CZ and CNOT as if they were different resources
Fix: They differ by a Hadamard on one wire, so convert freely and optimise for the coupling map instead: what matters is which qubits are adjacent
Assuming a phase error is visible in measurement
Fix: A Z rotation leaves outcome probabilities unchanged; detect it with an interference step such as a Hadamard before readout or through process tomography
Key Takeaways
- Any single-qubit gate is three Euler angles â Rz, Ry, Rz â while two-qubit gates need fifteen, which is why circuits are built from many simple gates rather than few complex ones
- H, S, T and CNOT form a universal set with H and the Paulis as Clifford gates: those are tracked in software under stabiliser formalism and cost almost nothing fault-tolerantly, so T-count is the metric that matters
- Toffoli costs six CNOTs and seven T gates in its exact decomposition, which is why reversible classical logic inside a quantum algorithm is a cost to minimise rather than a convenience
- Solovay-Kitaev approximates any rotation in about a hundred gates at 1e-3 precision while Ross-Selinger cuts the T-count to roughly three log squared of one over epsilon, so precision should be chosen from the algorithm's error budget