3 Single-qubit state and gates
This chapter introduces the single-qubit state as a pair of complex amplitudes that encode both probability (via squared magnitudes) and direction/phase. It presents a practical mental model and visualization called a state table, which lists outcomes, amplitudes, magnitudes, directions, and probabilities, and explains how phases influence computation even when probabilities stay the same. The chapter shows how to represent states in Python as lists of complex numbers, enforce normalization, compute magnitude and phase, and build a minimal simulator scaffold to initialize and inspect states.
The chapter then develops single-qubit gates and their effects, framing rotations as multiplication by a unit-magnitude complex number and highlighting the X, Z, Phase P(φ), Hadamard H, RZ(θ), and Y gates, followed by the RX and RY rotation families. It emphasizes that these are linear, unitary transformations that can be written as 2×2 matrices, have inverses (adjoints), and preserve total probability. A compact Python implementation encodes gates as nested lists and applies them with a generic transform that recombines amplitudes; circuits are built by sequencing such gates, with utilities to print and visualize the resulting states.
Finally, the chapter explains measurement as probabilistic sampling of outcomes according to the state’s distribution and shows how to simulate this with classical random sampling, where empirical frequencies converge to theoretical probabilities with more shots. It demonstrates practical single-qubit applications: preparing the uniform distribution (e.g., with H or π/2 rotations), encoding any Bernoulli distribution using RY with an angle chosen from p, and encoding real numbers either in an amplitude’s magnitude (via RY) or in an amplitude’s phase (via H followed by a Phase rotation). While a single qubit does not deliver quantum speedups, it is a useful vehicle for controlled sampling, data encoding, and for building intuition and tooling that generalize to multi-qubit systems.
Concepts introduced in this chapter.
Table 3.1 A single-qubit state has two possible outcomes.
Table 3.2 A single-qubit state has two amplitudes, each corresponding to a possible outcome.
A "clock" representation of a quantum state and the unit color wheel.
Table 3.3 A single-qubit state has two amplitudes. Each amplitude has a magnitude and direction.
Table 3.4 The probability of each outcome is the squared magnitude of the corresponding amplitude.
Table 3.5 The general form of a single-qubit state.
Table 3.6 The general form of a single-qubit state with amplitudes in polar form.
Table 3.7 The general form of a single-qubit state with amplitudes in polar form.
Table 3.8 The expanded state table for the example state encoded in Python above.
The steps of a single-qubit quantum computation.
The effect of the X-gate on a pair of amplitudes
The effect of the X-gate on an example pair of amplitudes.
The effect of the Z-gate on a pair of amplitudes.
The effect of the Z-gate on a pair of probabilities with direction
The effect of the Z-gate on an example a pair of amplitudes.
The effect of the \(P(\phi)\) gate on a pair of amplitudes.
The effect of the \(P(\phi)\) gate on a pair of probabilities with direction.
The effect of the \(P(\pi/3)\) gate on our example state.
The effect of the H-gate on a pair of amplitudes.
The effect of the H-gate on our example pair of amplitudes.
The effect of the H-gate on the default single-qubit state.
The effect of the \(R_Z\)-gate on a pair of amplitudes
The effect of the \(R_Z\)-gate on a pair of probabilities with direction
The effect of the \(R_Z(\frac{\pi}{3})\)-gate on our example pair of amplitudes.
The effect of the \(Y\)-gate on a pair of amplitudes
The effect of the Y-gate on a pair of probabilities with direction
The effect of the Y-gate on our example pair of amplitudes.
Table 3.9 Two-by-two matrix representations for the X, Y, Z, H, P, \(R_Z\) gates.
The effect of the \(R_X(\frac{\pi}{3})\)-gate on our example pair of amplitudes.
The effect of the \(R_Y(\frac{\pi}{3})\)-gate on our example pair of amplitudes.
Table 3.10 Gate inverses.
The steps of a single-qubit quantum computation.
A circuit diagram representation of the gate sequence \(HP(\frac{\pi}{3})XR_Y(\frac{2\pi}{3})\).
Table 3.11 The table state of a single-qubit state after the transformations in figure 3.26.
Example single-qubit circuit.
Mental model of the single-qubit circuit in figure 3.27, with counts of the outcomes from previous executions of the computation.
Ten measurements and the resulting outcome counts.
The Bernoulli distribution for \(p = 0.8\).
Table 3.12 The state table after encoding the value \(x = 273.5\) in a single-qubit state in the magnitude of outcome 0.
Table 3.13 The state table after encoding the value \(x = 273.5\) in a single-qubit state in the phase of the amplitude corresponding to outcome 1.
Summary
- Single-qubit computations are the simplest form of quantum computing. They are comparable to tossing a coin and have two possible outcomes.
- We can simulate a one-qubit computation using a two-element list of complex numbers. This list is initialized at the start, then undergoes simple transformations (known as single-qubit gates).
- The most common single-qubit gates are X, Y, Z, P, H, and rotations (denoted as \(R_X\), \(R_Y\), and \(R_Z\)). These rotations require an angle parameter.
- We can visualize the state of a single-qubit system with state tables that include amplitudes and other useful derived properties (directions, magnitudes, probabilities).
- Sequences of gates are called quantum circuits.
- Quantum computations are non-deterministic. When repeated, they yield different outcomes with distributions that reflect the state of the quantum system.
- The outcomes of a repeated quantum computation are aggregated into counts, which compose the typical output of a quantum computer.
- Single-qubit computations can be used to generate truly random numbers from Bernoulli distributions.
FAQ
What is a single-qubit quantum state?
A single-qubit state is a pair of complex amplitudes [z0, z1] such that |z0|^2 + |z1|^2 = 1. Outcome 0 occurs with probability |z0|^2 and outcome 1 with probability |z1|^2. The two complex numbers can be described by magnitude (length) and direction (phase).How do I represent and validate a single-qubit state in Python?
- Use a list of two complex numbers; the index is the outcome.- The default (initialized) state is [1, 0], meaning outcome 0 with 100% probability.
- Validate that there are exactly two amplitudes and their squared magnitudes sum to 1.
from math import sqrt
def init_state():
return [1, 0]
def prepare_state(*a):
state = list(a)
assert len(state) == 2
assert abs(sum(abs(x)**2 for x in state) - 1) < 1e-9
return state
What are magnitude and direction (phase) of an amplitude and how do I compute them?
- Magnitude: |z| = abs(z).- Direction/phase θ: atan2(z.imag, z.real) in radians; convert to degrees with degrees = θ * 180 / pi.
- In visual tables, magnitude is the bar length and direction is the bar color.
from math import atan2, pi
z = 0.3 + 0.4j
mag = abs(z) # 0.5
theta = atan2(z.imag, z.real) # radians
deg = theta * 180 / piHow does “rotation is multiplication” work for amplitudes?
Multiplying a complex number by (cos φ + i sin φ) rotates it counterclockwise by angle φ without changing its magnitude. A handy helper is cis(θ) = cos θ + i sin θ.from math import cos, sin
def cis(theta): return cos(theta) + 1j*sin(theta)
z_new = cis(phi) * z_old # rotate z_old by phiWhat do the basic single-qubit gates do to the amplitudes?
- X (NOT): swaps amplitudes → [z1, z0].- Z: multiplies the 1-side by −1 → [z0, −z1] (probabilities unchanged, phase of z1 flips by 180°).
- Phase P(φ): rotates the 1-side by φ → [z0, cis(φ)·z1] (probabilities unchanged).
- H (Hadamard): [ (z0+z1)/√2, (z0−z1)/√2 ] (rebalances probabilities and phases).
- RZ(θ): [cis(−θ/2)·z0, cis(θ/2)·z1] (probabilities unchanged, phases shift).
- Y: equivalent to [-i·z1, i·z0] (swaps probabilities and shifts phases).
What are RX and RY gates and why are they useful?
- RX(θ): [[cos(θ/2), −i sin(θ/2)], [−i sin(θ/2), cos(θ/2)]]. Changes both magnitudes and phases; useful for phase-sensitive rotations about the X axis.- RY(θ): [[cos(θ/2), −sin(θ/2)], [sin(θ/2), cos(θ/2)]]. Starting from |0⟩, keeps amplitudes real; acts like a “bias dial” for setting probabilities explicitly.
- Composition rule: R_axis(θ2) R_axis(θ1) = R_axis(θ1 + θ2).
What is the general form of a single-qubit gate and how do I apply it in code?
A single-qubit gate is a 2×2 complex matrix U = [[a, b], [c, d]]. Applied to state [z0, z1], the new state is [a·z0 + b·z1, c·z0 + d·z1].def transform(state, gate):
z0, z1 = state
state[0] = gate[0][0]*z0 + gate[0][1]*z1
state[1] = gate[1][0]*z0 + gate[1][1]*z1Do gates have inverses and how do I find them?
Yes. Quantum gates are unitary and always invertible. Examples:- X, Y, Z, H are their own inverses (U⁻¹ = U).
- P(φ)⁻¹ = P(−φ). R_axis(θ)⁻¹ = R_axis(−θ).
- In general, the inverse is the adjoint (conjugate transpose): if U = [[a,b],[c,d]] then U⁻¹ = U† = [[conj(a), conj(c)], [conj(b), conj(d)]].
How do I simulate measuring a single-qubit state?
- The probability distribution is [|z0|^2, |z1|^2]. Draw samples using these weights. Repeating many samples makes frequencies approach the true probabilities.from random import choices
from collections import Counter
probs = [abs(z)**2 for z in state]
samples = choices([0,1], probs, k=1000)
counts = Counter(samples) # e.g., {0: 726, 1: 274}What practical things can I do with a single qubit in this chapter?
- Uniform distribution (fair coin): apply H (or RY(pi/2), RX(pi/2)) to |0⟩ to get 50/50 outcomes.- Bernoulli distribution with parameter p: apply RY(θ) to |0⟩ with θ = 2·acos(sqrt(p)); then P(0)=p, P(1)=1−p.
- Encode a number in magnitude: for x in [0,1000), set θ = 2·acos(x/1000) and apply RY(θ); then |z0| ≈ x/1000.
- Encode a number in phase: apply H then P(π·x/1000); the phase of z1 encodes x (scaled by π).
Building Quantum Software with Python ebook for free