Chapter 5: Quantum Gates
In the previous chapter you met the qubit - a quantum system whose state is a vector in a two-dimensional complex vector space. A qubit sitting in a fixed state is not very useful; the power of quantum computing comes from transforming qubits. The quantum analogue of a classical logic gate is called a quantum gate, and it is the fundamental building block of every quantum algorithm.
This chapter is your reference guide to the most important single-qubit gates. We will meet each gate three ways: as a matrix, as an operation on basis states, and as a rotation on the Bloch sphere. By the end, you will be able to read and write quantum circuits fluently.
5.1 The Pauli Gates: X, Y, Z
The three Pauli gates are named after the physicist Wolfgang Pauli. Together with the identity matrix $I$, they form a basis for all 2x2 Hermitian matrices. Each Pauli gate corresponds to a 180-degree rotation around one of the three axes of the Bloch sphere.
The X Gate (Bit Flip)
The X gate is the quantum analogue of the classical NOT gate. Its matrix is:
$$X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}$$The X gate swaps the amplitudes of $|0\rangle$ and $|1\rangle$:
$$X|0\rangle = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\begin{pmatrix} 1 \\ 0 \end{pmatrix} = \begin{pmatrix} 0 \\ 1 \end{pmatrix} = |1\rangle$$ $$X|1\rangle = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\begin{pmatrix} 0 \\ 1 \end{pmatrix} = \begin{pmatrix} 1 \\ 0 \end{pmatrix} = |0\rangle$$On the Bloch sphere, $X$ is a rotation of $\pi$ radians (180 degrees) around the x-axis. It flips the north pole to the south pole and vice versa, which is why it is called the bit flip gate.
Try it yourself - apply the X gate to $|0\rangle$ and observe that you always measure $|1\rangle$:
The Z Gate (Phase Flip)
The Z gate leaves $|0\rangle$ unchanged but flips the sign (phase) of $|1\rangle$:
$$Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}$$ $$Z|0\rangle = |0\rangle \qquad Z|1\rangle = -|1\rangle$$On the Bloch sphere, $Z$ is a rotation of $\pi$ radians around the z-axis. If the qubit is at the north or south pole, $Z$ has no observable effect - the global phase $-1$ on $|1\rangle$ cannot be detected by measurement in the computational basis. But the phase flip becomes critical when the qubit is in a superposition. For instance:
$$Z\left(\frac{|0\rangle + |1\rangle}{\sqrt{2}}\right) = \frac{|0\rangle - |1\rangle}{\sqrt{2}}$$The state changed from $|+\rangle$ to $|-\rangle$ - the qubit has been rotated to the opposite side of the Bloch sphere's equator. Run the sandbox below to see this. First an H gate creates the superposition $|+\rangle$, then Z flips the phase. Apply another H to convert the phase difference back into a measurable bit flip:
The Y Gate (Bit and Phase Flip)
The Y gate combines both a bit flip and a phase flip. Its matrix involves the imaginary unit $i$:
$$Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}$$ $$Y|0\rangle = i|1\rangle \qquad Y|1\rangle = -i|0\rangle$$On the Bloch sphere, $Y$ is a rotation of $\pi$ radians around the y-axis. It simultaneously flips the bit and introduces a relative phase factor of $i$. Verify it below:
Summary of the Pauli Gates
| Gate | Matrix | Action on $|0\rangle$ | Action on $|1\rangle$ | Bloch Rotation |
|---|---|---|---|---|
| $X$ | $\begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}$ | $|1\rangle$ | $|0\rangle$ | $\pi$ around x-axis |
| $Y$ | $\begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}$ | $i|1\rangle$ | $-i|0\rangle$ | $\pi$ around y-axis |
| $Z$ | $\begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}$ | $|0\rangle$ | $-|1\rangle$ | $\pi$ around z-axis |
5.2 The Hadamard Gate: Creating Superposition
If the Pauli gates are the workhorses of quantum computing, the Hadamard gate is the star. It is the primary tool for creating superposition, and it appears in nearly every quantum algorithm you will encounter. Its matrix is:
$$H = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}$$The Hadamard gate maps the computational basis states to the diagonal basis states (also called the Hadamard basis or the $\pm$ basis):
$$H|0\rangle = \frac{|0\rangle + |1\rangle}{\sqrt{2}} = |+\rangle$$ $$H|1\rangle = \frac{|0\rangle - |1\rangle}{\sqrt{2}} = |-\rangle$$The state $|+\rangle$ lies on the positive x-axis of the Bloch sphere, and $|-\rangle$ lies on the negative x-axis. Starting from the north pole ($|0\rangle$), the Hadamard gate rotates the qubit to the equator - into an equal superposition of $|0\rangle$ and $|1\rangle$.
Geometrically, $H$ is a rotation of $\pi$ radians around the axis that bisects the x-axis and z-axis (the direction $\frac{\hat{x} + \hat{z}}{\sqrt{2}}$). This is why it swaps the x-axis and z-axis of the Bloch sphere.
Run this sandbox to create a superposition and observe the roughly 50/50 measurement outcomes:
Now verify that two Hadamard gates cancel each other. You should measure $|0\rangle$ every time:
Why the Hadamard Is So Important
The Hadamard gate converts between two complementary bases: the computational basis ($|0\rangle$, $|1\rangle$) and the Hadamard basis ($|+\rangle$, $|-\rangle$). This basis-switching ability is the engine behind quantum parallelism. In Deutsch's algorithm, Grover's algorithm, and the quantum Fourier transform, the Hadamard gate is what puts qubits into superposition so that a quantum circuit can evaluate a function on all inputs simultaneously.
There is also a deep relationship between $H$ and the Pauli gates:
$$HXH = Z \qquad HZH = X \qquad HYH = -Y$$The Hadamard gate conjugates $X$ into $Z$ and vice versa. This means that a bit flip error in the Hadamard basis looks like a phase flip error in the computational basis - a key idea in quantum error correction.
5.3 Phase Gates: S, T, and Rz
The Z gate applies a phase of $-1$ (equivalently, $e^{i\pi}$) to $|1\rangle$. What if we want to apply a smaller phase? The S gate and T gate are two of the most commonly used fractional-phase gates.
The S Gate (Phase Gate, $\sqrt{Z}$)
The S gate applies a phase of $i = e^{i\pi/2}$ to $|1\rangle$:
$$S = \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix}$$ $$S|0\rangle = |0\rangle \qquad S|1\rangle = i|1\rangle$$On the Bloch sphere, $S$ is a rotation of $\pi/2$ (90 degrees) around the z-axis. Applying it twice gives the Z gate: $S^2 = Z$. This is why the S gate is sometimes called $\sqrt{Z}$.
The inverse of the S gate is written $S^\dagger$ (pronounced "S dagger"). It applies a phase of $-i = e^{-i\pi/2}$:
$$S^\dagger = \begin{pmatrix} 1 & 0 \\ 0 & -i \end{pmatrix}$$The T Gate ($\sqrt{S}$, $\pi/8$ Gate)
The T gate applies an even smaller phase of $e^{i\pi/4}$ to $|1\rangle$:
$$T = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}$$ $$T|0\rangle = |0\rangle \qquad T|1\rangle = e^{i\pi/4}|1\rangle$$On the Bloch sphere, $T$ is a rotation of $\pi/4$ (45 degrees) around the z-axis. Applying it twice gives the S gate: $T^2 = S$. The T gate is sometimes called the $\pi/8$ gate because it can be written as $e^{i\pi/8} \cdot R_z(\pi/4)$, where the global phase factor $e^{i\pi/8}$ is unobservable.
Its inverse, $T^\dagger$, applies a phase of $e^{-i\pi/4}$:
$$T^\dagger = \begin{pmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{pmatrix}$$The Phase Gate Hierarchy
The Z, S, and T gates form a natural hierarchy of phase rotations around the z-axis:
| Gate | Phase on $|1\rangle$ | Z-rotation Angle | Relationship |
|---|---|---|---|
| $Z$ | $e^{i\pi} = -1$ | $\pi$ | $Z = S^2 = T^4$ |
| $S$ | $e^{i\pi/2} = i$ | $\pi/2$ | $S = T^2$ |
| $T$ | $e^{i\pi/4}$ | $\pi/4$ | Fundamental |
Why Phase Matters
Consider this concrete example. We prepare $|+\rangle = \frac{1}{\sqrt{2}}(|0\rangle + |1\rangle)$, apply a Z gate to get $|-\rangle = \frac{1}{\sqrt{2}}(|0\rangle - |1\rangle)$, and then apply a final H. The Hadamard converts $|-\rangle$ back to $|1\rangle$:
$$H \cdot Z \cdot H |0\rangle = H \cdot Z |+\rangle = H|-\rangle = |1\rangle$$Without the Z gate, $H \cdot H|0\rangle = |0\rangle$. The phase flip caused the final measurement outcome to change from 0 to 1 with certainty. The phase was invisible at the intermediate step but became measurable after the final Hadamard "decoded" it into a bit-flip. This pattern - encode into superposition, manipulate phases, decode back to computational basis - is the template for nearly all quantum algorithms.
5.4 Arbitrary Rotations: Rx, Ry, Rz
The Pauli, Hadamard, S, and T gates perform rotations by fixed angles. But quantum computing often requires continuous rotations by arbitrary angles. The parameterized rotation gates $R_x(\theta)$, $R_y(\theta)$, and $R_z(\theta)$ generalize the Pauli gates to rotations by any angle $\theta$.
$R_x(\theta)$: Rotation Around the X-Axis
$$R_x(\theta) = e^{-i\theta X/2} = \begin{pmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}$$When $\theta = \pi$, this gives $R_x(\pi) = -iX$ (the X gate up to a global phase). On the Bloch sphere, $R_x(\theta)$ rotates the state vector by $\theta$ radians around the x-axis.
Use the slider below to explore how the angle $\theta$ affects the qubit state. At $\theta = 0$, the qubit stays in $|0\rangle$. At $\theta = \pi$ ($\approx 3.14$), it flips to $|1\rangle$. At intermediate angles, you get partial superpositions:
OPENQASM 3.0;
qubit[1] q;
bit[1] c;
rx({angle}) q[0];
c = measure q;
$R_y(\theta)$: Rotation Around the Y-Axis
$$R_y(\theta) = e^{-i\theta Y/2} = \begin{pmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}$$The $R_y$ gate is special because its matrix entries are all real. This means it creates superpositions without introducing complex phases - the resulting state always lies in the xz-plane of the Bloch sphere. When $\theta = \pi$, we get $R_y(\pi) = -iY$ (the Y gate up to global phase).
OPENQASM 3.0;
qubit[1] q;
bit[1] c;
ry({angle}) q[0];
c = measure q;
$R_z(\theta)$: Rotation Around the Z-Axis
$$R_z(\theta) = e^{-i\theta Z/2} = \begin{pmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{pmatrix}$$The $R_z$ gate is diagonal - it does not change the measurement probabilities, only the relative phase between $|0\rangle$ and $|1\rangle$. When $\theta = \pi$, we recover the Z gate (up to global phase): $R_z(\pi) = -iZ$. The S and T gates are also special cases:
$$S = e^{i\pi/4} R_z(\pi/2) \qquad T = e^{i\pi/8} R_z(\pi/4)$$OPENQASM 3.0;
qubit[1] q;
bit[1] c;
h q[0];
rz({angle}) q[0];
c = measure q;
How Rotation Gates Generalize the Pauli Gates
Each Pauli gate is a special case of its corresponding rotation gate at angle $\pi$ (up to a global phase of $-i$):
$$R_x(\pi) = -iX \qquad R_y(\pi) = -iY \qquad R_z(\pi) = -iZ$$The global phase $-i$ is physically unobservable, so for all practical purposes $R_x(\pi)$ "is" the X gate, and likewise for Y and Z. The rotation gates give us a continuous family of operations, parameterized by a single angle, that interpolate smoothly between the identity ($\theta = 0$) and the Pauli gate ($\theta = \pi$).
5.5 Gates as Matrices, Matrices as Gates
Every quantum gate is a unitary matrix. This is not a convention or a simplification - it is a requirement imposed by the laws of quantum mechanics. A matrix $U$ is unitary if:
$$U^\dagger U = U U^\dagger = I$$where $U^\dagger$ is the conjugate transpose of $U$ (transpose the matrix and take the complex conjugate of every entry), and $I$ is the identity matrix. Let us verify this for the Hadamard gate:
$$H^\dagger H = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \cdot \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} = \frac{1}{2}\begin{pmatrix} 2 & 0 \\ 0 & 2 \end{pmatrix} = I \quad \checkmark$$(Since $H$ is real and symmetric, $H^\dagger = H$, so $H$ is also Hermitian.)
Why Unitarity?
The unitarity requirement has deep physical meaning:
- Probability conservation. If $|\psi\rangle$ is a valid quantum state (with $\langle\psi|\psi\rangle = 1$), then $U|\psi\rangle$ is also a valid quantum state. Unitary transformations preserve the total probability.
- Reversibility. Every unitary matrix has an inverse ($U^{-1} = U^\dagger$), so every quantum gate can be undone. Quantum computation is inherently reversible - there is no quantum analogue of an AND gate that irreversibly destroys information.
- Norm preservation. Unitary matrices preserve the length of vectors: $\|U|\psi\rangle\| = \||\psi\rangle\|$. On the Bloch sphere, this means gates are rotations - they move the state vector around the sphere without changing its length.
Verifying Unitarity
Let us verify unitarity for the Y gate:
$$Y^\dagger = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}^\dagger = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix} = Y$$ $$Y^\dagger Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}\begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} = I \quad \checkmark$$And for the T gate:
$$T^\dagger T = \begin{pmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{pmatrix}\begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} = I \quad \checkmark$$Not Every Matrix Is a Valid Gate
The matrix $\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}$ is not unitary. If we applied it to $|1\rangle$, we would get $|0\rangle + |1\rangle$, which has norm $\sqrt{2}$ instead of 1. The probabilities would not sum to 1. Only unitary matrices preserve the delicate normalization of quantum states.
Exercise: Achieve a Target State
Write a QASM program that prepares the state $|-\rangle = \frac{1}{\sqrt{2}}(|0\rangle - |1\rangle)$. Hint: you will need two gates.
Prepare the qubit in the state $|-\rangle = \frac{1}{\sqrt{2}}(|0\rangle - |1\rangle)$. When measured, this state gives 50% $|0\rangle$ and 50% $|1\rangle$, just like $|+\rangle$. But on the Bloch sphere, $|-\rangle$ points in the $-x$ direction. Use the X and H gates.
5.6 Composing Gates: The Quantum Circuit Model
Individual gates are useful, but the real power comes from composing multiple gates into a quantum circuit. In the circuit model of quantum computation, a computation is described as a sequence of gates applied to qubits along horizontal wires, read from left to right.
Circuit Notation
In a quantum circuit diagram:
- Each horizontal line (wire) represents a qubit, initialized to $|0\rangle$ on the left.
- Gates are drawn as labeled boxes on the wires, applied from left to right.
- A measurement symbol at the end of a wire indicates a computational-basis measurement.
- Time flows from left to right - the leftmost gate is applied first.
Matrix Multiplication and Gate Ordering
When we compose gates, the corresponding matrices are multiplied in reverse order. If a circuit applies gate $A$ first, then gate $B$, the combined operation is:
$$|\psi_{\text{final}}\rangle = B \cdot A \cdot |\psi_{\text{initial}}\rangle$$This reverse ordering comes from the standard convention for matrix multiplication: the matrix closest to the state vector acts first. For example, the circuit $H \to Z \to H$ applied to $|0\rangle$ is computed as:
$$HZH|0\rangle$$where $H$ on the right acts first. We already showed that this equals $|1\rangle$. Let us verify by multiplying the matrices:
$$HZH = \frac{1}{2}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} = X$$Remarkable: sandwiching a Z gate between two Hadamards produces an X gate. This demonstrates how gate composition can create entirely new operations from simpler parts.
Building Multi-Gate Circuits
Try the following circuit that composes several gates. First we apply H to create a superposition, then T to add a $\pi/4$ phase, then H again. The result is a state that is neither $|0\rangle$ nor $|1\rangle$ nor an equal superposition:
The combined matrix for this circuit is:
$$HTH = \frac{1}{2}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} = \frac{1}{2}\begin{pmatrix} 1 + e^{i\pi/4} & 1 - e^{i\pi/4} \\ 1 - e^{i\pi/4} & 1 + e^{i\pi/4} \end{pmatrix}$$The probability of measuring $|1\rangle$ is $|1 - e^{i\pi/4}|^2/4 \approx 0.146$, so you should see roughly 85% $|0\rangle$ and 15% $|1\rangle$.
Longer Circuits
There is no limit to how many gates you can compose. The following circuit applies a sequence of rotations, demonstrating how a complex transformation can be built from simple pieces. Experiment by modifying the gates and observing how the output changes:
Circuit Equivalence
Two circuits are equivalent if they produce the same unitary matrix (up to a global phase). Finding shorter or more efficient circuits that implement the same transformation is a central problem in quantum compilation. For example, these three circuits all implement the same operation (the X gate):
- A single X gate
- $HZH$
- $R_x(\pi)$ (up to global phase)
- $R_y(\pi) \cdot R_z(\pi)$ (up to global phase)
A quantum compiler's job is to take a desired unitary operation and decompose it into the native gates available on a specific quantum processor - typically a small set like $\{R_z, \sqrt{X}, \text{CNOT}\}$.
Exercise: Match the Circuit
Write a circuit that produces approximately 75% $|0\rangle$ and 25% $|1\rangle$. Hint: use $R_y$ with an appropriate angle. Recall that $R_y(\theta)|0\rangle = \cos(\theta/2)|0\rangle + \sin(\theta/2)|1\rangle$, so you need $\sin^2(\theta/2) \approx 0.25$, which gives $\theta \approx \pi/3$.
Prepare a qubit state that measures $|0\rangle$ approximately 75% of the time and $|1\rangle$ approximately 25% of the time. Use a single rotation gate.
Chapter Summary
This chapter introduced the fundamental single-qubit quantum gates. Here are the key takeaways:
- The Pauli gates (X, Y, Z) are 180-degree rotations around the three axes of the Bloch sphere. X flips bits, Z flips phases, and Y does both.
- The Hadamard gate creates superposition by mapping $|0\rangle \to |+\rangle$ and $|1\rangle \to |-\rangle$. It is its own inverse and conjugates X into Z.
- The phase gates (S, T) are fractional rotations around the z-axis. S rotates by $\pi/2$ and T by $\pi/4$.
- Rotation gates ($R_x$, $R_y$, $R_z$) generalize the Pauli gates to continuous rotations by arbitrary angles.
- Every quantum gate is a unitary matrix satisfying $U^\dagger U = I$. Unitarity guarantees probability conservation and reversibility.
- Gates are composed by matrix multiplication (in reverse order of application). The quantum circuit model reads left to right, but the matrix product reads right to left.
You now have all the single-qubit tools needed for quantum computation. In the next chapter, we will expand to multiple qubits and discover the most uniquely quantum resource of all: entanglement.