Complex amplitudes and probabilities¶
This lesson uses a two-element NumPy array to represent one qubit. After installation, you only need Python lists, functions and basic arithmetic. The aim is to distinguish a complex amplitude from a measurement probability.
A normalized state is written |ψ⟩ = α|0⟩ + β|1⟩. Its amplitudes satisfy |α|² + |β|² = 1. Measuring in the computational basis gives probabilities |α|² and |β|². In Python, the imaginary unit is 1j; abs(z)**2 computes a complex number's squared magnitude.
For the state (1, i)/√2, predict both probabilities. Then compare this state with |+⟩ = (1, 1)/√2. They have the same computational-basis probabilities but different relative phases.
"""Compare complex amplitudes, basis probabilities and a state overlap."""
from __future__ import annotations
import json
import numpy as np
def experiment() -> dict[str, object]:
state = np.array([1, 1j], dtype=complex) / np.sqrt(2)
plus = np.array([1, 1], dtype=complex) / np.sqrt(2)
return {
"amplitudes_real": state.real.tolist(),
"amplitudes_imag": state.imag.tolist(),
"basis_probabilities": (np.abs(state) ** 2).tolist(),
"norm_squared": float(np.vdot(state, state).real),
"plus_overlap_probability": float(abs(np.vdot(plus, state)) ** 2),
}
if __name__ == "__main__":
print(json.dumps(experiment(), sort_keys=True))
python examples/learning/foundations/complex_vectors.py
{
"amplitudes_imag": [
0.0,
0.7071067811865475
],
"amplitudes_real": [
0.7071067811865475,
0.0
],
"basis_probabilities": [
0.4999999999999999,
0.4999999999999999
],
"norm_squared": 0.9999999999999998,
"plus_overlap_probability": 0.4999999999999998
}
np.vdot(a, b) conjugates the first vector before taking the dot product. That conjugation is why np.vdot(state, state) gives the norm squared. A plain np.dot would give the wrong normalization for this complex state.
The two basis probabilities are 1/2. The overlap probability |⟨+|ψ⟩|² is also 1/2, not one: matching basis probabilities does not make two states identical. Small deviations from decimal values such as 1.0 are normal floating-point roundoff.
Try replacing 1j by -1j, then by 1. Predict the overlap before running. Finally multiply the entire state by 1j. Does a global phase change either probability?
Check your calculation
With -1j, the overlap with plus remains 1/2. With 1, the state becomes plus, so the overlap is one. Multiplying the whole state by a unit-magnitude phase changes neither measurement probabilities nor squared overlaps. Changing the phase of only one amplitude is a relative-phase change and can affect other measurements.