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, and you will understand why certain gate combinations unlock the full power of quantum computation.
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 $\pi$ radian (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 ($|0\rangle$) to the south pole ($|1\rangle$) and vice versa, which is why it is called the bit flip gate.
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 of $-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. The H gate
creates the superposition $|+\rangle$, then Z flips the phase. Try adding another
h q[0]; line before the measurement 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. Because the global phase $i$ is unobservable, measuring $Y|0\rangle = i|1\rangle$ still yields $|1\rangle$ with certainty. 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}$$A clean way to remember this: $H = \frac{1}{\sqrt{2}}(X + Z)$. The Hadamard sits exactly halfway between a bit flip and a phase flip. It maps the computational basis states to the diagonal basis (also called 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.
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 in the Hadamard basis looks like a phase flip 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. Each gate in the sequence is the square root of the one above it:
| 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 |
The general phase gate $P(\phi)$ extends this pattern to any angle. It applies a phase of $e^{i\phi}$ to $|1\rangle$:
$$P(\phi) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\phi} \end{pmatrix}$$With this notation, $Z = P(\pi)$, $S = P(\pi/2)$, and $T = P(\pi/4)$. The phase gate $P(\phi)$ differs from the rotation gate $R_z(\phi)$ by a global phase factor, which we will discuss in Section 5.4.
Why Phase Matters: The "Invisible" Phase Made Visible
Consider this demonstration. We prepare $|+\rangle$, apply Z to get $|-\rangle$, then apply H to decode:
$$H \cdot Z \cdot H |0\rangle = H \cdot Z |+\rangle = H|-\rangle = |1\rangle$$Without the Z gate, $HH|0\rangle = |0\rangle$. The phase flip changed the final outcome from 0 to 1 with certainty. The phase was invisible at the intermediate step (measuring after Z alone gives 50/50), but became fully measurable after the final Hadamard "decoded" it. This pattern - encode into superposition, manipulate phases, decode back - 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$ around the corresponding Bloch sphere axis.
Each rotation gate is the matrix exponential of its corresponding Pauli matrix:
$$R_x(\theta) = e^{-i\theta X/2} \qquad R_y(\theta) = e^{-i\theta Y/2} \qquad R_z(\theta) = e^{-i\theta Z/2}$$$R_x(\theta)$: Rotation Around the X-Axis
$$R_x(\theta) = \begin{pmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}$$At $\theta = \pi$, this gives $R_x(\pi) = -iX$. On the Bloch sphere, $R_x(\theta)$ rotates by $\theta$ around the x-axis. Use the slider to explore:
$R_y(\theta)$: Rotation Around the Y-Axis
$$R_y(\theta) = \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, so it creates superpositions without complex phases. The state always lies in the xz-plane of the Bloch sphere. It is particularly useful for state preparation:
$$R_y(\theta)|0\rangle = \cos\frac{\theta}{2}|0\rangle + \sin\frac{\theta}{2}|1\rangle$$This gives a probability of $\cos^2(\theta/2)$ for measuring $|0\rangle$ and $\sin^2(\theta/2)$ for measuring $|1\rangle$. By choosing $\theta$ appropriately, you can prepare any desired probability distribution between $|0\rangle$ and $|1\rangle$.
$R_z(\theta)$: Rotation Around the Z-Axis
$$R_z(\theta) = \begin{pmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{pmatrix}$$The $R_z$ gate is diagonal - it does not change measurement probabilities, only the relative phase. When $\theta = \pi$, we recover $R_z(\pi) = -iZ$. The S and T gates are special cases: $S = e^{i\pi/4} R_z(\pi/2)$ and $T = e^{i\pi/8} R_z(\pi/4)$. Below, we apply H before $R_z$ so the phase rotation is visible on the Bloch sphere:
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 $R_x(\pi)$ "is" the X gate for all practical purposes. The rotation gates give us a continuous family that interpolates between the identity ($\theta = 0$) and the Pauli gate ($\theta = \pi$).
The Euler Decomposition
A remarkable result: any single-qubit gate can be decomposed into three rotations:
$$U = e^{i\alpha} R_z(\beta) R_y(\gamma) R_z(\delta)$$for some angles $\alpha, \beta, \gamma, \delta$. This Euler decomposition means $R_y$ and $R_z$ are sufficient to reach any point on the Bloch sphere.
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.
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 Hadamard gate. Since $H$ is real and symmetric, $H^\dagger = H$:
$$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$$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.
Comprehensive Gate Reference
The following table collects every gate from this chapter. Keep it handy as a reference:
| Gate | Matrix | Bloch Rotation | QASM |
|---|---|---|---|
| $I$ | $\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}$ | None (identity) | - |
| $X$ | $\begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}$ | $\pi$ around x | x q[0]; |
| $Y$ | $\begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}$ | $\pi$ around y | y q[0]; |
| $Z$ | $\begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}$ | $\pi$ around z | z q[0]; |
| $H$ | $\frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}$ | $\pi$ around $\frac{\hat{x}+\hat{z}}{\sqrt{2}}$ | h q[0]; |
| $S$ | $\begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix}$ | $\pi/2$ around z | s q[0]; |
| $S^\dagger$ | $\begin{pmatrix} 1 & 0 \\ 0 & -i \end{pmatrix}$ | $-\pi/2$ around z | sdg q[0]; |
| $T$ | $\begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}$ | $\pi/4$ around z | t q[0]; |
| $T^\dagger$ | $\begin{pmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{pmatrix}$ | $-\pi/4$ around z | tdg q[0]; |
| $R_x(\theta)$ | $\begin{pmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}$ | $\theta$ around x | rx(theta) q[0]; |
| $R_y(\theta)$ | $\begin{pmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{pmatrix}$ | $\theta$ around y | ry(theta) q[0]; |
| $R_z(\theta)$ | $\begin{pmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{pmatrix}$ | $\theta$ around z | rz(theta) q[0]; |
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$$The matrix closest to the state vector acts first. For the circuit H then Z then H:
$$HZH|0\rangle$$The rightmost $H$ acts first. We showed in Section 5.3 that this equals $|1\rangle$. Multiplying out 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. Experiment with this longer circuit and observe how each gate affects the output:
Circuit Equivalence
Two circuits are equivalent if they produce the same unitary matrix
(up to global phase). For example, x q[0];, $HZH$, and $R_x(\pi)$ all
implement the X gate. A quantum compiler decomposes desired operations into the native
gate set of a specific processor - typically something like $\{R_z, \sqrt{X}, \text{CNOT}\}$.
The Universal Single-Qubit Gate
QASM provides the general single-qubit gate U(theta,phi,lambda):
Every gate in this chapter is a special case: $X = U(\pi, 0, \pi)$, $H = U(\pi/2, 0, \pi)$, $R_y(\theta) = U(\theta, 0, 0)$.
Exercises
Exercise 5.1: Prepare the $|-\rangle$ State
Write a circuit that prepares the state $|-\rangle = \frac{1}{\sqrt{2}}(|0\rangle - |1\rangle)$. When measured in the computational basis, you should see approximately 50% $|0\rangle$ and 50% $|1\rangle$. The key test: if you add an H gate before measuring, you should get $|1\rangle$ with 100% probability (since $H|-\rangle = |1\rangle$).
Hint: Start from $|0\rangle$ and think about which gates take you to the negative x-axis of the Bloch sphere.
Exercise 5.2: The 75/25 Split
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. All three are self-inverse: $X^2 = Y^2 = Z^2 = I$.
- 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$: $HXH = Z$.
- The phase gates ($S$, $T$) are fractional rotations around the z-axis. $S$ rotates by $\pi/2$, $T$ by $\pi/4$, and they satisfy $T^2 = S$, $S^2 = Z$.
- Rotation gates ($R_x$, $R_y$, $R_z$) generalize the Pauli gates to continuous rotations by arbitrary angles. They are defined as $R_k(\theta) = e^{-i\theta\sigma_k/2}$.
- Every quantum gate is a unitary matrix satisfying $U^\dagger U = I$. Unitarity guarantees probability conservation, reversibility, and norm preservation.
- 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.
- Any single-qubit gate can be decomposed as $e^{i\alpha} R_z(\beta) R_y(\gamma) R_z(\delta)$ (Euler decomposition), meaning $R_y$ and $R_z$ are sufficient for arbitrary single-qubit operations.
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.