Examples¶
This section describes complete, runnable examples demonstrating various capabilities of schr. All examples are available in the examples/ directory and include animated visualizations of quantum phenomena.
1D Examples¶
Free Particle Dispersion¶
File: examples/free_particle_1d.py
Physics:
A free particle is described by the time-dependent Schrödinger equation with zero potential:
For a Gaussian wavepacket with initial width \(\sigma_0\) and momentum \(p_0\):
The wavepacket spreads in time due to quantum dispersion. The width evolves as:
The center of the wavepacket moves with group velocity \(v_g = p_0/m\), while the dispersion causes the uncertainty \(\Delta x\) to increase over time, satisfying the time-energy uncertainty relation.
Visualization:
The example generates an animated movie showing the probability density \(|\psi(x,t)|^2\) evolving in time, along with a plot comparing the numerical width evolution with the analytical prediction.
Quantum Tunneling¶
File: examples/tunneling_1d.py
Physics:
Quantum tunneling allows particles to penetrate classically forbidden regions. Consider a rectangular barrier:
For a particle with energy \(E < V_0\), the wavefunction inside the barrier (\(E < V\)) decays exponentially:
The transmission coefficient through a rectangular barrier of width \(a\) is:
For \(\kappa a \gg 1\), this simplifies to \(T \approx 16\dfrac{E}{V_0}\left(1 - \dfrac{E}{V_0}\right) e^{-2\kappa a}\).
The wavefunction exhibits interference between incident, reflected, and transmitted components, producing characteristic oscillations near the barrier edges.
Visualization:
The example creates an animated movie showing a Gaussian wavepacket encountering the potential barrier. The animation clearly shows the wavepacket splitting into reflected and transmitted parts, with the transmitted amplitude significantly smaller than classically expected.
Key Observable: Transmission and reflection coefficients compared with analytical predictions.
2D Examples¶
Double-Slit Experiment¶
File: examples/double_slit.py
Physics:
The double-slit experiment demonstrates wave-particle duality. A plane wave or wavepacket passes through two slits separated by distance \(d\), creating an interference pattern on a distant screen.
The interference pattern arises from the superposition:
where \(\psi_1\) and \(\psi_2\) are contributions from each slit. The probability density \(|\psi|^2\) exhibits fringes with spacing:
where \(\lambda = 2\pi\hbar/p\) is the de Broglie wavelength, \(L\) is the distance to the screen, and \(p\) is the particle momentum.
Visualization:
An animated 2D visualization shows the wavefunction propagating through the double slit and forming interference fringes. Absorbing boundary conditions prevent reflections from domain edges.
Key Observable: Fringe spacing and visibility of interference pattern.
python examples/double_slit.py
Quantum Vortex¶
File: examples/quantum_vortex_2d.py
Physics:
A quantum vortex is a topological defect in a complex wavefunction where the phase winds by \(2\pi m\) around a singularity (\(m\) is the winding number). The wavefunction takes the form:
where \(f(r) \sim r^{|m|}\) near the core to ensure single-valuedness. In a harmonic trap \(V(r) = \frac{1}{2}m\omega^2 r^2\), the vortex state exhibits:
Phase structure: The phase arg(\(\psi\)) winds \(m\) times around the vortex core
Density depletion: \(|\psi|^2 \to 0\) at the core (\(r \to 0\))
Angular momentum: The state carries angular momentum \(L_z = m\hbar\) per particle
The probability current circulates around the vortex:
Visualization:
The example generates two animations:
Probability density \(|\psi|^2\) showing the vortex core
Phase structure revealing the \(2\pi m\) winding
Key Observable: Angular momentum expectation value \(\langle L_z \rangle\) and phase winding number.
QED Examples¶
Photon Field Basics¶
File: examples/photon_field.py
Physics:
A quantized photon field is described in Fock space using creation (\(a^\dagger\)) and annihilation (\(a\)) operators satisfying:
These operators act on number states \(|n\rangle\):
The number operator \(\hat{n} = a^\dagger a\) counts photons:
The Hamiltonian for a single mode is:
Demonstration:
The example verifies:
Commutation relations
Action of operators on Fock states
Photon number expectation values
Key Observable: Verification of \([a, a^\dagger] = 1\) and proper normalization.
Jaynes-Cummings Dynamics¶
File: examples/jaynes_cummings.py
Physics:
The Jaynes-Cummings model describes a two-level atom coupled to a single photon mode. The Hamiltonian (in the rotating wave approximation) is:
where:
\(\omega_a\) is the atomic transition frequency
\(\omega_c\) is the cavity mode frequency
\(g\) is the atom-field coupling strength
\(\hat{\sigma}_z = |e\rangle\langle e| - |g\rangle\langle g|\) is the Pauli z operator
\(\hat{\sigma}_+ = |e\rangle\langle g|\) raises the atom to the excited state
\(\hat{\sigma}_- = |g\rangle\langle e|\) lowers the atom to the ground state
For the resonant case (\(\omega_a = \omega_c\)) starting from \(|e,0\rangle\) (excited atom, vacuum field), the system undergoes Rabi oscillations:
where \(\Omega_R = 2g\) is the Rabi frequency. Energy is exchanged periodically between atom and field:
At \(t = 0\): atom excited, zero photons
At \(t = \pi/\Omega_R\): atom in ground state, one photon
At \(t = 2\pi/\Omega_R\): back to initial state
Visualization:
An animated plot showing the time evolution of:
Excited state probability \(P_e(t)\)
Average photon number \(\langle n \rangle(t)\)
Both oscillate with the Rabi frequency, demonstrating coherent energy exchange.
Key Observable: Rabi frequency \(\Omega_R\) extracted from oscillation period.
Running Examples
All examples can be run directly with uv:
# Ensure dependencies are synced
uv sync
# Navigate to examples directory
cd examples
# Run 1D examples
uv run python free_particle_1d.py
uv run python tunneling_1d.py
# Run 2D examples
uv run python double_slit.py
uv run python quantum_vortex_2d.py
# Run QED examples
uv run python photon_field.py
uv run python jaynes_cummings.py
# Run benchmarks
uv run python benchmark_gpu.py
Modifying Examples¶
All examples are designed to be easily modified. Key parameters to adjust:
Grid Parameters:
nx, ny: Number of grid points (powers of 2 recommended)x_min, x_max: Spatial extent
Physical Parameters:
mass: Particle mass (default: 1.0 = electron mass)hbar: Reduced Planck constant (default: 1.0)Potential parameters:
V0,omega, etc.
Numerical Parameters:
dt: Time step (reduce for better accuracy)num_steps: Number of evolution steps
Visualization:
save_every: Frame saving frequencydpi: Image resolutioncmap: Colormap (try ‘viridis’, ‘plasma’, ‘hot’, etc.)
Next Steps¶
Adapt examples for your research problem
Combine multiple techniques (e.g., tunneling + time-dependent potential)
Explore parameter space systematically
Validate against analytical solutions when available
For more details, see:
src for detailed API documentation
Theoretical Background for mathematical foundations