Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The Wavefunction

What is the meaning of a wave-function ψ\psi ?

Probability Refresher

Random Variables

Probability Distributions

Further Exploration

Source
import matplotlib.pyplot as plt
import numpy as np

# Discrete probabilities (fair dice)
outcomes = np.arange(1, 7)
probs = np.ones_like(outcomes) / 6

plt.bar(outcomes, probs)
plt.xlabel("Dice outcome")
plt.ylabel("Probability")
plt.title("PMF of a Fair Die")
plt.show()
<Figure size 640x480 with 1 Axes>
Source
# Continuous uniform distribution on [a,b]
a, b = 0, 1
x = np.linspace(-0.2, 1.2, 200)
pdf = np.where((x >= a) & (x <= b), 1/(b-a), 0)

plt.plot(x, pdf, lw=2)
plt.fill_between(x, pdf, alpha=0.3)
plt.xlabel("x")
plt.ylabel("p(x)")
plt.title("Uniform Distribution on [0,1]")
plt.show()
<Figure size 640x480 with 1 Axes>
Source
from scipy.stats import norm

x = np.linspace(-4, 4, 200)
pdf = norm.pdf(x, loc=0, scale=1)

plt.plot(x, pdf, label="N(0,1)")
plt.fill_between(x, pdf, alpha=0.2)
plt.xlabel("x")
plt.ylabel("p(x)")
plt.title("Gaussian PDF")
plt.legend()
plt.show()
<Figure size 640x480 with 1 Axes>
Source
# -----------------------------
# Correct sampling for H-atom 1s
# -----------------------------
N = 6000
a0 = 1.0

# For 1s: if x = 2r/a0, then x ~ Gamma(k=3, theta=1)
x = np.random.gamma(shape=3.0, scale=1.0, size=N)
r = 0.5 * a0 * x

# Isotropic angles
u = np.random.rand(N)
theta = np.arccos(1 - 2*u)
phi = 2*np.pi*np.random.rand(N)

# Convert to Cartesian and take a 2D projection
x3 = r * np.sin(theta) * np.cos(phi)
y3 = r * np.sin(theta) * np.sin(phi)

# Histogram for the shell probability density P(r) (per unit r)
bins = np.linspace(0, 6*a0, 80)
hist, edges = np.histogram(r, bins=bins, density=True)
centers = 0.5*(edges[1:] + edges[:-1])

# Analytical radial distribution P(r) = 4 r^2 |psi|^2 = 4 r^2 / (a0^3*pi) * exp(-2r/a0) * pi
# Simplifies to: P(r) = (4/a0^3) r^2 exp(-2r/a0), which integrates to 1
rr = np.linspace(0, bins[-1], 400)
P_r = (4.0/(a0**3)) * rr**2 * np.exp(-2*rr/a0)

# Side-by-side subplots: dot cloud and radial distribution

fig, axs = plt.subplots(1, 2, figsize=(12, 5))

# Left: simulated dot cloud
axs[0].scatter(x3, y3, s=1, alpha=0.5)
axs[0].set_title("Hydrogen 1s: position observations (2D projection)")
axs[0].set_xlabel("x [$a_0$]")
axs[0].set_ylabel("y [$a_0$]")
axs[0].set_aspect('equal')

# Right: radial distribution histogram + analytical curve
axs[1].plot(centers, hist, lw=2, label="Monte Carlo (histogram)")
axs[1].plot(rr, P_r, lw=2, linestyle="--", label="Analytical 1s")
axs[1].set_title("Radial probability distribution")
axs[1].set_xlabel("r [$a_0$]")
axs[1].set_ylabel("$P(r)$")
axs[1].legend()

plt.tight_layout()
plt.show()
<Figure size 1200x500 with 2 Axes>

Normalization of wavefunction

Source
import numpy as np
import matplotlib.pyplot as plt

# Define x values
x = np.linspace(0, 1, 500)

# Unnormalized psi
psi_unnorm = x
psi2_unnorm = psi_unnorm**2

# Normalized psi (C = sqrt(3))
psi_norm = np.sqrt(3) * x
psi2_norm = psi_norm**2

# Compute integrals for annotation
area_unnorm = np.trapezoid(psi2_unnorm, x)
area_norm = np.trapezoid(psi2_norm, x)

# Create the figure and axes
fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# --- Plot 1: Unnormalized psi² ---
axes[0].fill_between(x, psi2_unnorm, alpha=0.4, color="blue")
axes[0].set_title("Unnormalized $|\psi(x)|^2$")
axes[0].set_xlabel("x")
axes[0].set_ylabel(r"$|\psi(x)|^2$")
axes[0].grid(True)
axes[0].text(0.5, 0.2, rf"Area = {area_unnorm:.2f}", 
             transform=axes[0].transAxes, fontsize=12, color="blue")

# --- Plot 2: Normalized psi² ---
axes[1].fill_between(x, psi2_norm, alpha=0.4, color="green")
axes[1].set_title("Normalized $|\psi(x)|^2$")
axes[1].set_xlabel("x")
axes[1].set_ylabel(r"$|\psi(x)|^2$")
axes[1].grid(True)
axes[1].text(0.5, 0.2, rf"Area = {area_norm:.2f}", 
             transform=axes[1].transAxes, fontsize=12, color="green")

plt.tight_layout()
plt.show()
<>:24: SyntaxWarning: invalid escape sequence '\p'
<>:33: SyntaxWarning: invalid escape sequence '\p'
<>:24: SyntaxWarning: invalid escape sequence '\p'
<>:33: SyntaxWarning: invalid escape sequence '\p'
/tmp/ipykernel_3080/3267431332.py:24: SyntaxWarning: invalid escape sequence '\p'
  axes[0].set_title("Unnormalized $|\psi(x)|^2$")
/tmp/ipykernel_3080/3267431332.py:33: SyntaxWarning: invalid escape sequence '\p'
  axes[1].set_title("Normalized $|\psi(x)|^2$")
<Figure size 1200x500 with 2 Axes>

What can we do with probability distribution functions (PDF)?

What about quantities which correspond to operators?

x=x1p2+x2p2+...\langle x \rangle = x_1 p_2+x_2 p_2 + ...
x=xp(x)dx\langle x \rangle = \int x\cdot p(x) dx
f=f(x)p(x)dx\langle f \rangle = \int f(x) \cdot p(x) dx
A=A^p(x)dx=ψ(x)A^ψ(x)dx\langle A \rangle = \int \hat{A} p(x) \cdot dx = \int \psi^{*}(x) \cdot \hat{A} \psi(x) \cdot dx
Average quantityCorresponding operator
E=ψ(x)H^ψ(x)dx\langle E \rangle=\int \psi^{*}(x) \hat{H} \psi(x) dxH^=22md2dx2+V(x)\hat{H}=-\frac{\hbar^2}{2m}\frac{d^2}{dx^2}+V(x)
K=ψ(x)K^ψ(x)dx\langle K \rangle=\int \psi^{*}(x) \hat{K}\psi(x) dx K^=22md2dx2\hat{K}=-\frac{\hbar^2}{2m}\frac{d^2}{dx^2}
p=ψ(x)p^ψ(x)dx\langle p \rangle=\int \psi^{*}(x) \hat{p} \psi(x) dxp^=iddx\hat{p}=-i\hbar\frac{d}{dx}
p2=ψ(x)p^2ψ(x)dx\langle p^2 \rangle=\int \psi^{*}(x) \hat{p}^2 \psi(x) dxp^2=2d2dx2\hat{p}^2=-\hbar^2\frac{d^2}{dx^2}
Source
import numpy as np
import matplotlib.pyplot as plt
from math import erf, sqrt

# ----- Parameters -----
sigma = 1.0  # width of the Gaussian (harmonic oscillator ground state)
x0, x1 = -1.0, 1.0  # interval to integrate over

# ----- Define |psi(x)|^2 for a normalized Gaussian -----
def prob_density(x, sigma):
    # |psi(x)|^2 = (1/(sqrt(2*pi)*sigma)) * exp(-x^2/(2*sigma^2))
    return (1.0/(np.sqrt(2*np.pi)*sigma)) * np.exp(-x**2/(2*sigma**2))

# Probability via the error function (analytic CDF of a normal distribution)
def interval_probability(a, b, sigma):
    return 0.5*(erf(b/(sqrt(2)*sigma)) - erf(a/(sqrt(2)*sigma)))

P = interval_probability(x0, x1, sigma)

# ----- Plot -----
xs = np.linspace(-4*sigma, 4*sigma, 800)
ys = prob_density(xs, sigma)

fig = plt.figure(figsize=(10, 5))

# Title with the integral expression
plt.title(r"$P_{x_0<x<x_1}=\int_{x_0}^{x_1}|\psi(x)|^2\,dx$" + 
          f"\nSelected interval: [{x0:.2f}, {x1:.2f}]",
          loc="left")

# Curve
plt.plot(xs, ys, linewidth=3)

# Shade between x0 and x1 under the curve
mask = (xs >= x0) & (xs <= x1)
plt.fill_between(xs[mask], ys[mask], 0, alpha=0.25)

# Mark the boundaries with vertical dashed lines
plt.axvline(x0, linestyle="--", linewidth=2)
plt.axvline(x1, linestyle="--", linewidth=2)

# Tick marks and labels
plt.xlabel("x")
plt.ylabel(r"$|\psi(x)|^2$")

# Annotate the probability
plt.text(0.02, 0.90, rf"$P_{{{x0:.2f}<x<{x1:.2f}}} = {P:.3f}$",
         transform=plt.gca().transAxes, fontsize=16)

plt.xlim(xs.min(), xs.max())
plt.ylim(bottom=0)
plt.grid(True, alpha=0.3)
plt.tight_layout()

plt.show()
<Figure size 1000x500 with 1 Axes>

Examples of using probabilistic calculations in QM

Example 1: Probability in a region (1D)

Take the normalized wavefunction on [0,1][0,1]: ψ(x)=3x\psi(x)=\sqrt{3}\,x. Then p(x)=ψ(x)2=3x2p(x)=|\psi(x)|^2=3x^2.

P(a<x<b)=ab3x2dx=[x3]ab=b3a3.P(a<x<b)=\int_a^b 3x^2\,dx= \left[x^3\right]_a^b=b^3-a^3.
P(0.3<x<0.6)=0.630.33=0.2160.027=0.189.P(0.3<x<0.6)=0.6^3-0.3^3=0.216-0.027=0.189.

Example 2: 3D slice probability (separable state)

Let ψ(x,y,z)=27xyz\psi(x,y,z)=\sqrt{27}\,xyz on the unit cube [0,1]3[0,1]^3 (this is normalized since ψ2=27x2y2z2|\psi|^2=27x^2y^2z^2 and 01x2dx=13\int_0^1 x^2dx=\tfrac13, so 27(13)3=127(\tfrac13)^3=1).

P=axbx ⁣ ⁣ayby ⁣ ⁣azbz27x2y2z2dzdydx=(bx3ax3)(by3ay3)(bz3az3).P=\int_{a_x}^{b_x}\!\!\int_{a_y}^{b_y}\!\!\int_{a_z}^{b_z}27x^2y^2z^2\,dz\,dy\,dx = \big(b_x^3-a_x^3\big)\big(b_y^3-a_y^3\big)\big(b_z^3-a_z^3\big).

Example 3: Averages and variance from a PDF

For ψ(x)=3x\psi(x)=\sqrt{3}\,x on [0,1][0,1] (so p(x)=3x2p(x)=3x^2):

x=01x3x2dx=301x3dx=34.\langle x\rangle=\int_0^1 x\,3x^2\,dx=3\int_0^1 x^3dx=\tfrac34.
x2=01x23x2dx=301x4dx=35.\langle x^2\rangle=\int_0^1 x^2\,3x^2\,dx=3\int_0^1 x^4dx=\tfrac35.
Var(x)=x2x2=35(34)2=380,σx=3800.1937.\mathrm{Var}(x)=\langle x^2\rangle-\langle x\rangle^2=\tfrac35-\Big(\tfrac34\Big)^2=\tfrac{3}{80},\qquad \sigma_x=\sqrt{\tfrac{3}{80}}\approx 0.1937.

Example 4: Operator expectations (momentum/energy in a box)

Operator rules:

Use the infinite square well on [0,1][0,1] with V(x)=0V(x)=0 inside and ψn(x)=2sin(nπx)\psi_n(x)=\sqrt{2}\sin(n\pi x), which is normalized.

p=01ψn(i)ψndx=0\langle p\rangle=\int_0^1\psi_n^*(-i\hbar)\psi_n'\,dx=0

(the integrand is odd over a full sine half-wave, or integrate by parts with vanishing boundary terms).

p2=01ψn(2)ψndx=(nπ)2.\langle p^2\rangle=\int_0^1\psi_n^*(-\hbar^2)\psi_n''\,dx =(n\pi\hbar)^2.
E=p22m=(nπ)22m.\langle E\rangle=\frac{\langle p^2\rangle}{2m}=\frac{(n\pi\hbar)^2}{2m}.