B10 · Lesson 18 · Matrix mechanics lab
Spin operators, measurement, precession and Rabi oscillation as matrices in code
Lessons 15 to 17 put quantum mechanics in the language of vectors and operators. For a spin or a qubit the vectors have just two components and the operators are 2 × 2 matrices, so everything can be computed exactly with a few lines of NumPy: commutators, the probabilities of measuring along any axis, time evolution by a matrix exponential, and the Rabi oscillation that drives every qubit gate. In this lab you'll build those tools and check each against the formulas, then use the same machinery on a chain of coupled wells. Units are those where ħ = 1.
- A two-level system is driven with Rabi frequency Ω and detuning Δ. The curve is the population of the upper level against time, from Rabi's formula.
- On resonance the population swings fully between 0 and 1; off resonance it swings faster and never fully flips.
- Step 4 reproduces this curve by exponentiating the 2 × 2 Hamiltonian, with no formula at all.
Predict
A spin points up along z and is measured along an axis tilted 60° toward x. What is the chance of getting "up" along that axis?
75%, which is . In matrix language, it's the squared overlap of the state with the eigenvector of that has the larger eigenvalue. Step 2 computes it for any axis.
Name it
Spin as matrices
With ħ = 1, with the Pauli matrices
which obey and its cyclic versions.
Measurement
The spin along a unit vector n is . Its eigenvectors are the possible outcomes, and the chance of each is the squared size of the state's overlap with it: np.linalg.eigh gives both eigenvalues and eigenvectors.
Time evolution
For a time-independent Hamiltonian, . With from eigh, . A spin in a field along z, , precesses about z at ω.
Rabi oscillation
In the frame rotating with the drive, a two-level system with Rabi frequency Ω and detuning Δ has , and exponentiating it reproduces Rabi's formula, .
Larger matrices
The same code handles N coupled wells: the tight-binding matrix with beside the diagonal has eigenvalues , the beginnings of a band.
Worked example
A spin-½ points along +x and sits in a field that makes it precess about z at 1 cycle per unit time. What is ⟨S_x⟩ after a quarter and a half cycle?
Show the solution
- Evolve the state. With , evolve the state by the matrix exponential.
- Read off ⟨S_x⟩. It's 0.5 at t = 0, 0 at t = 0.25 (the spin points along y) and −0.5 at t = 0.5.
- Compare with a classical moment. The expectation values precess exactly like a classical magnetic moment, which is Ehrenfest's theorem for spin: the averages obey the classical equations even though each measurement gives only ±½.
Answer ⟨S_x⟩ is 0 after a quarter cycle and −0.5 after a half cycle.
Worked example
A qubit is driven with Ω = Δ = 2π × 1 (frequencies in cycles per unit time). What is its upper population at t = 0.3, and what's the most it ever reaches?
Show the solution
- Exponentiate the Hamiltonian. Exponentiate this for t = 0.3, apply it to the state, and square the second component: That gives 0.4722.
- Check with Rabi's formula.
- The most it reaches. With equal detuning and drive, the population never exceeds ½. Detuning tilts the rotation axis on the Bloch sphere away from the equator, so the state circles without reaching the south pole.
Answer the upper population is 0.4722 at t = 0.3, and it never exceeds ½.
Worked example
Eight identical wells in a row are coupled to their neighbors by t = 1. What are the lowest three levels?
Show the solution
- The levels. The matrix with −1 beside the diagonal has eigenvalues which are −1.879, −1.532 and −1.000 for j = 1, 2, 3.
- Check numerically.
eigvalshreproduces them to rounding. - The band. All eight levels lie between −2 and +2, the band of width 4t of lesson 31. With more wells the levels fill the band densely: a crystal's band is the limit of this matrix as N grows.
Answer the lowest three levels are −1.879, −1.532 and −1.000.
Check
Step 1. Write spin_ops() returning the three spin matrices (ħ = 1), and commutator(A, B).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write prob_up(state, n) returning the chance that a spin in state (a 2-component array) is found "up" along the unit vector n, using the eigenvectors of . spin_ops is included.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write evolve(H, psi, t) returning through the eigendecomposition of the Hermitian matrix H, and sx_mean(psi) returning ⟨S_x⟩. spin_ops is included.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 4. Write rabi_numeric(W, D, t) that exponentiates the rotating-frame Hamiltonian (W and D in rad per unit time) from the state (1, 0) and returns the upper population. evolve is included.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 5. Write chain_levels(n, t) returning the sorted eigenvalues of n identical wells coupled to neighbors by t, the matrix with −t beside the diagonal.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A spin up along z is measured along an axis tilted 60°. What is the chance of "up"?
Check
A two-level system is driven with Rabi frequency 1 and detuning 0.5 (same units). What is the largest upper population it reaches?
Explain why
Why does computing through the eigenvectors of H give the exact time evolution?
In the basis of H's eigenvectors, the Hamiltonian is diagonal, and each eigenvector simply picks up the phase as time passes, without mixing with the others. So to evolve any state, you write it in that basis, multiply each component by its phase, and transform back: that's . Nothing is approximated, and no time steps are needed, because a time-independent Hamiltonian's evolution is completely determined by its eigenvalues and eigenvectors. The same reasoning is why stationary states are the natural building blocks of every solution.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.