---
title: Quantum lab
heading: Energy levels by matrix, a finite well by bisection, tunneling and the uncertainty principle in code
eyebrow: B9 · Lesson 5 · Quantum lab
xp: 50
---

::: hook
The first four lessons solved the box with hard walls exactly and estimated everything else: finite wells, barriers, wave packets. A computer removes the need to estimate. In this lab you'll find a box's energy levels as the eigenvalues of a matrix, solve a finite well's ground state by bisection, compute the exact chance of tunneling through a barrier and see when the simple exponential estimate is trustworthy, and check the uncertainty principle on a wave packet with the FFT.
:::

::: play
- An electron of energy E meets a barrier of height V and thickness d. The solid curve is the exact chance of getting through, the dashed one the simple estimate $\frac{16E(V - E)}{V^2}e^{-2\kappa d}$, on a log scale against thickness.
- For thick barriers the two curves lie on top of each other. For thin ones the estimate overshoots, and at zero thickness it gives a nonsense value.
- The readout gives the exact transmission through 1 nm.
:::

```sim plot
view: graph
x: d = 0..2 nm
y: T = 0.00000001..1 log
slider: E = 0.2..2 step 0.1 = 1 eV
slider: V = 2.5..5 step 0.1 = 3 eV
plot: 1/(1 + V^2*sinh(sqrt(2*9.109e-31*(V - E)*1.602e-19)/1.0546e-34*d*1e-9)^2/(4*E*(V - E))) | label: exact | samples: 400
plot: 16*E*(V - E)/V^2*exp(-2*sqrt(2*9.109e-31*(V - E)*1.602e-19)/1.0546e-34*d*1e-9) | label: estimate | dashed | color: ch2 | samples: 400
readout: exact transmission through 1 nm = 1/(1 + V^2*sinh(sqrt(2*9.109e-31*(V - E)*1.602e-19)/1.0546e-34*1e-9)^2/(4*E*(V - E))) | digits: 4
```

::: predict
## A GaAs well 10 nm wide (effective mass 0.067) has walls 0.3 eV high instead of hard walls. How does its ground state compare with the hard-walled box's 56 meV?

- [ ] The same: the electron never reaches the walls
- [x] Lower, about 34 meV
- [ ] Higher, about 80 meV
- [ ] Zero: a finite well can't hold the electron

::: reveal
Lower, about 34 meV. With finite walls the wave leaks a little way into them, so it fits a slightly longer half-wavelength than the well's width, and a longer wavelength means less energy. Here the leak is large enough to bring the energy down to 61% of the hard-wall value, which is why real quantum-well lasers emit at longer wavelengths than the simple box predicts.
:::
:::

::: nameit
## Levels from a matrix

On a grid of spacing Δx, the second derivative is $\frac{\psi_{i+1} - 2\psi_i + \psi_{i-1}}{\Delta x^2}$, so the Schrödinger equation $-\frac{\hbar^2}{2m}\psi'' = E\psi$ becomes a matrix eigenvalue problem. The matrix has $\frac{\hbar^2}{m\Delta x^2}$ on its diagonal and $-\frac{\hbar^2}{2m\Delta x^2}$ beside it, and its eigenvalues are the energy levels. Any potential can be added to the diagonal.

## A finite well

For a well of depth $V_0$ and width a, the even states satisfy $k\tan\frac{ka}{2} = \kappa$, with $k = \frac{\sqrt{2mE}}{\hbar}$ inside and $\kappa = \frac{\sqrt{2m(V_0 - E)}}{\hbar}$ in the walls. The ground state lies below both $V_0$ and the hard-wall value, and **bisection** finds it by halving an interval where the function changes sign.

## Exact tunneling

Through a rectangular barrier of height $V_0$ and thickness d, with $E < V_0$,

$$T = \left[1 + \frac{V_0^2\sinh^2\kappa d}{4E(V_0 - E)}\right]^{-1},$$

which for $\kappa d \gg 1$ becomes $\frac{16E(V_0 - E)}{V_0^2}e^{-2\kappa d}$.

## Uncertainty by FFT

A wave packet's momentum distribution is its Fourier transform. For a Gaussian $\psi = e^{-x^2/4\sigma^2}$, the spreads of $|\psi|^2$ in x and of its transform in k multiply to exactly $\frac12$, the smallest product the uncertainty principle allows.
:::

::: example
## How accurately does a 500-point grid reproduce the levels of a 1 nm box?

::: solution
1. **Compare the levels.** The matrix gives the three lowest levels below, against the exact values (computed with the same constants).

| Level | Grid (eV) | Exact (eV) | Error |
|---|---|---|---|
| 1 | 0.376106 | 0.376108 | 0.0003% |
| 2 | 1.504411 | 1.504430 | 0.0013% |
| 3 | 3.384868 | 3.384968 | 0.003% |

2. **How the error scales.** The errors grow as $n^2$ for higher states and shrink as $\frac{1}{N^2}$ with more points.
3. **Where the grid struggles.** The grid struggles most with the highest states, which wiggle fastest, but the first few levels come out to four or five digits.
4. **Other wells.** The same code with any potential on the diagonal solves wells nobody can solve on paper.

**Answer:** very closely: the first three levels are off by 0.0003%, 0.0013% and 0.003%.
:::
:::

::: example
## What is the exact chance that a 1 eV electron tunnels through a 3 eV barrier 1 nm thick, and how good is the simple estimate? And through 0.2 nm?

::: solution
1. **Decay constant.**
   $$\begin{aligned} \kappa &= \frac{\sqrt{2m \times 2\text{ eV}}}{\hbar} \\ &= 7.24 \times 10^9\ \text{m}^{-1} \end{aligned}$$
   so κd = 7.24 for the 1 nm barrier.
2. **The 1 nm barrier.** The exact formula gives $1.813 \times 10^{-6}$, and the estimate agrees to six digits.
3. **The 0.2 nm barrier.** Here κd = 1.45, and the exact value is 0.180 while the estimate gives 0.196, 9% high.
4. **Where the estimate works.** The simple exponential holds whenever κd is more than about 2, which covers almost every barrier that matters in devices.

**Answer:** through 1 nm the chance is $1.813 \times 10^{-6}$, and the estimate agrees to six digits; through 0.2 nm it's 0.180, and the estimate is 9% high.
:::
:::

::: example
## What are the wavelengths of the first three Balmer lines, from $E_n = -\frac{13.6\text{ eV}}{n^2}$?

::: solution
1. **Photon energies.** Drops to level 2 from levels 3, 4 and 5 release
   $$13.6\left(\frac14 - \frac{1}{n^2}\right)\ \text{eV}$$
   which is 1.889, 2.550 and 2.856 eV.
2. **Wavelengths.** Those are 656.4, 486.2 and 434.1 nm: red, blue-green and violet.
3. **Compare with measurement.** The measured lines are at 656.3, 486.1 and 434.0 nm, so the simple model is off by about a part in 10 000.
4. **Why they differ.** The gap comes mostly from the proton not being infinitely heavy, and correcting for the reduced mass closes most of it.

**Answer:** 656.4, 486.2 and 434.1 nm (red, blue-green and violet).
:::
:::

::: check
kind: python

## Step 1. Write `box_levels(L, N, k)` that builds the N × N finite-difference matrix for an electron in a box of width L (N interior points, spacing $\frac{L}{N + 1}$) and returns its k lowest eigenvalues in eV. Use ħ = 1.0546 × 10⁻³⁴ J·s, m = 9.109 × 10⁻³¹ kg and 1.602 × 10⁻¹⁹ J/eV.

```python starter
import numpy as np
hb, me, q = 1.0546e-34, 9.109e-31, 1.602e-19

def box_levels(L, N, k=3):
    return np.zeros(k)

print(box_levels(1e-9, 500))
```

```python solution
import numpy as np
hb, me, q = 1.0546e-34, 9.109e-31, 1.602e-19

def box_levels(L, N, k=3):
    dx = L / (N + 1)
    c = hb**2 / (2 * me * dx**2)
    H = np.diag(np.full(N, 2 * c)) + np.diag(np.full(N - 1, -c), 1) + np.diag(np.full(N - 1, -c), -1)
    return np.linalg.eigvalsh(H)[:k] / q

print(box_levels(1e-9, 500))
```

```python tests
import numpy as np
E1 = (np.pi * 1.0546e-34)**2 / (2 * 9.109e-31 * 1e-18) / 1.602e-19
E = box_levels(1e-9, 500)
exact = E1 * np.array([1, 4, 9])
assert np.all(np.abs(E / exact - 1) < 2e-4), f"Expect 0.376, 1.504 and 3.385 eV; got {E}."
e1 = abs(box_levels(1e-9, 100, 1)[0] / E1 - 1)
e2 = abs(box_levels(1e-9, 200, 1)[0] / E1 - 1)
assert 3 < e1 / e2 < 5, "Doubling the points should cut the error about fourfold."
```

::: right
Right. The eigenvalues reproduce $n^2E_1$, with errors falling as the square of the spacing.
:::
:::

::: check
kind: python

## Step 2. Write `well_ground(V0, a, mr)` that finds the ground state (in eV) of a finite well of depth V0 eV and width a meters for a particle of mass mr times the electron's, by bisection on $k\tan\frac{ka}{2} - \kappa$ between nearly 0 and the smaller of V0 and the hard-wall energy.

```python starter
import math
hb, me, q = 1.0546e-34, 9.109e-31, 1.602e-19

def well_ground(V0, a, mr):
    return (math.pi * hb / a)**2 / (2 * mr * me) / q

print(well_ground(0.3, 10e-9, 0.067))
```

```python solution
import math
hb, me, q = 1.0546e-34, 9.109e-31, 1.602e-19

def well_ground(V0, a, mr):
    m = mr * me
    def f(E):
        k = math.sqrt(2 * m * E * q) / hb
        kap = math.sqrt(2 * m * (V0 - E) * q) / hb
        return k * math.tan(k * a / 2) - kap
    lo = 1e-9
    hi = min(V0, (math.pi * hb / a)**2 / (2 * m) / q) * (1 - 1e-12)
    for _ in range(200):
        mid = (lo + hi) / 2
        if f(mid) > 0:
            hi = mid
        else:
            lo = mid
    return (lo + hi) / 2

print(well_ground(0.3, 10e-9, 0.067))
```

```python tests
import math
E = well_ground(0.3, 10e-9, 0.067)
assert abs(E - 0.034214) < 1e-5, f"The 0.3 eV, 10 nm GaAs well has its ground state at 34.2 meV; got {E * 1000:.3f} meV."
deep = well_ground(10000, 10e-9, 0.067)
hard = (math.pi * 1.0546e-34 / 10e-9)**2 / (2 * 0.067 * 9.109e-31) / 1.602e-19
assert deep < hard and abs(deep / hard - 1) < 0.01, "A very deep well approaches the hard-wall box from below."
assert well_ground(0.01, 10e-9, 0.067) < 0.01, "Even a shallow well holds a bound state below its rim."
```

::: right
Right. A very deep well approaches the hard-wall value, and even a shallow one keeps at least one bound state, a property of every one-dimensional well.
:::
:::

::: check
kind: python

## Step 3. Write `T_exact(E, V0, d)` and `T_approx(E, V0, d)` for an electron (energies in eV, d in meters) tunneling through a rectangular barrier, using the exact formula and the exponential estimate.

```python starter
import math
hb, me, q = 1.0546e-34, 9.109e-31, 1.602e-19

def T_exact(E, V0, d):
    return math.exp(-d)

def T_approx(E, V0, d):
    return math.exp(-d)

print(T_exact(1, 3, 1e-9), T_approx(1, 3, 1e-9))
```

```python solution
import math
hb, me, q = 1.0546e-34, 9.109e-31, 1.602e-19

def kappa(E, V0):
    return math.sqrt(2 * me * (V0 - E) * q) / hb

def T_exact(E, V0, d):
    k = kappa(E, V0)
    return 1 / (1 + V0**2 * math.sinh(k * d)**2 / (4 * E * (V0 - E)))

def T_approx(E, V0, d):
    return 16 * E * (V0 - E) / V0**2 * math.exp(-2 * kappa(E, V0) * d)

print(T_exact(1, 3, 1e-9), T_approx(1, 3, 1e-9))
```

```python tests
assert abs(T_exact(1, 3, 1e-9) / 1.8130e-6 - 1) < 1e-3, f"Through 1 nm: 1.813e-6; got {T_exact(1, 3, 1e-9):.4e}."
assert abs(T_approx(1, 3, 1e-9) / T_exact(1, 3, 1e-9) - 1) < 1e-5, "For a thick barrier the estimate agrees closely."
assert abs(T_exact(1, 3, 0.2e-9) - 0.18007) < 1e-4 and abs(T_approx(1, 3, 0.2e-9) - 0.19607) < 1e-4, "Through 0.2 nm the estimate is 9% high."
assert abs(T_exact(1, 3, 1e-15) - 1) < 1e-6, "A vanishing barrier lets everything through."
```

::: right
Right. The estimate needs κd well above 1; for thinner barriers the sinh no longer behaves like an exponential.
:::
:::

::: check
kind: python

## Step 4. Write `packet(sigma, N)` that samples $\psi = e^{-x^2/4\sigma^2}$ on N points across 40σ, computes the standard deviations of $|\psi|^2$ in x and of $|\tilde\psi|^2$ in k (with `np.fft.fft` and `np.fft.fftfreq`), and returns `(dx, dk, dx * dk)`.

```python starter
import numpy as np

def packet(sigma, N=4096):
    return sigma, 1 / sigma, 1.0

print(packet(1e-9))
```

```python solution
import numpy as np

def packet(sigma, N=4096):
    x = np.linspace(-20 * sigma, 20 * sigma, N, endpoint=False)
    psi = np.exp(-x**2 / (4 * sigma**2))
    P = psi**2 / np.sum(psi**2)
    dx = np.sqrt(np.sum(P * x**2) - np.sum(P * x)**2)
    k = 2 * np.pi * np.fft.fftfreq(N, x[1] - x[0])
    Pk = np.abs(np.fft.fft(psi))**2
    Pk = Pk / np.sum(Pk)
    dk = np.sqrt(np.sum(Pk * k**2) - np.sum(Pk * k)**2)
    return float(dx), float(dk), float(dx * dk)

print(packet(1e-9))
```

```python tests
dx, dk, prod = packet(1e-9)
assert abs(dx / 1e-9 - 1) < 1e-6, f"The packet's spread in x is σ = 1 nm; got {dx}."
assert abs(prod - 0.5) < 1e-6, f"A Gaussian packet has Δx Δk = 1/2 exactly; got {prod:.8f}."
assert abs(packet(3e-9)[1] * 3e-9 - 0.5) < 1e-6, "A wider packet has a proportionally narrower spread of k."
```

::: right
Right. The product is exactly ½ whatever σ is, the smallest the uncertainty principle allows: squeezing the packet in position spreads it in momentum.
:::
:::

::: check
kind: python

## Step 5. Write `balmer(nmax)` returning the wavelengths in nm of hydrogen's lines from levels 3 to nmax down to level 2, using 13.6 eV and hc = 1239.84 eV·nm, and `series_limit()` returning the shortest Balmer wavelength.

```python starter
def balmer(nmax):
    return [656.0]

def series_limit():
    return 400.0

print(balmer(5), series_limit())
```

```python solution
def balmer(nmax):
    return [1239.84 / (13.6 * (1 / 4 - 1 / n**2)) for n in range(3, nmax + 1)]

def series_limit():
    return 1239.84 / (13.6 / 4)

print(balmer(5), series_limit())
```

```python tests
b = balmer(5)
assert len(b) == 3 and abs(b[0] - 656.386) < 0.01 and abs(b[1] - 486.212) < 0.01 and abs(b[2] - 434.118) < 0.01, f"Expect 656.4, 486.2 and 434.1 nm; got {b}."
assert abs(series_limit() - 364.659) < 0.01, "The lines crowd toward 364.7 nm."
assert all(x > series_limit() for x in balmer(40)), "Every line lies above the series limit."
```

::: right
Right. The lines crowd ever closer toward 364.7 nm, and beyond it the spectrum is continuous.
:::
:::

::: check
kind: parts
given: E = 0.5 | 1 | 1.5; V = 2 | 4; d = 0.5 | 1.5

## An electron of {E} eV meets a barrier {V} eV high and {d} nm thick.

::: part
answer: sqrt(2*9.109e-31*(V - E)*1.602e-19)/1.0546e-34*d*1e-9
tolerance: 0.5%

### What is κd?

::: misses
- [any] $\kappa = \frac{\sqrt{2m(V - E)}}{\hbar}$.
:::
:::

::: part
answer: 1/(1 + V^2*sinh(sqrt(2*9.109e-31*(V - E)*1.602e-19)/1.0546e-34*d*1e-9)^2/(4*E*(V - E)))
tolerance: 0.5%

### What is the exact transmission?

::: misses
- [any] $T = \left[1 + \frac{V^2\sinh^2\kappa d}{4E(V - E)}\right]^{-1}$.
:::
:::
:::

::: check
given: N = 50 | 100 | 400
answer: (1 + 1/N)^2
tolerance: 0.5%

## A finite-difference grid with N interior points in a box spaces them $\frac{L}{N + 1}$ apart. By roughly what factor would the spacing squared shrink, going from N to 2N + 1 points? (Answer as spacing² at N over spacing² at 2N + 1, divided by 4.)

::: right
Right. Going from N to 2N + 1 points exactly halves the spacing, so the error, which goes as its square, falls fourfold.
:::

::: misses
- [any] The spacings are $\frac{L}{N + 1}$ and $\frac{L}{2N + 2}$.
:::
:::

::: explain
## Why does a finite well's ground state sit lower than a hard-walled box of the same width?

::: model
In a box with hard walls the wave must vanish exactly at the walls, so half a wavelength fits the width exactly. With finite walls the wave doesn't stop at the edge: it decays exponentially into the walls, so the oscillating part inside no longer has to reach zero at the edge, and the half-wavelength effectively stretches past the width. A longer wavelength means a smaller momentum and less kinetic energy, so every level drops. The shallower the walls, the farther the wave leaks and the larger the drop.
:::
:::

::: practice

::: check
given: L = 0.5 | 2; n = 1 | 3
answer: 0.376*n^2/L^2
unit: eV
tolerance: 0.5%

## Level {n} of an electron in a {L} nm box?

::: misses
- [any] $n^2 \times \frac{0.376\text{ eV}}{L^2}$.
:::
:::

::: check
given: a = 3 | 6 | 9
answer: 1239.84/(13.6*(1/4 - 1/a^2))
unit: nm
tolerance: 0.5%

## Wavelength of the Balmer line from level {a}?

::: misses
- [any] $\frac{1239.8}{13.6(1/4 - 1/n^2)}$ nm.
:::
:::
:::
