Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 · Getting Started

A Python Toolkit for Quantum Mechanics

Set up the numerical toolkit for the whole course: spatial grids, discretized wavefunctions, numerical integration, derivatives as matrices, and plotting.

Most interesting quantum problems cannot be solved with pen and paper. Fortunately, a wavefunction can be stored as an array of numbers on a grid, and operators become matrices. This lesson builds the small toolkit you will reuse for the rest of the course. Every example runs right here in the browser.

Wavefunctions as arrays

Pick a grid of N equally spaced points x₀, x₁, …, x_{N-1} with spacing dx. A wavefunction ψ(x) becomes an array psi[j] = ψ(xⱼ). Because the ψ values are complex, use dtype=complex.

The norm ∫ |ψ|² dx becomes the sum Σ |ψⱼ|² dx. To normalize, divide by the square root of that sum.

Python A normalized Gaussian on a grid

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The value at the center is close to π^(-1/4) ≈ 0.7511, matching the exact normalization (the grid has no point exactly at 0, so it differs slightly in later digits).

Expectation values from a grid

The average of a function of position is ⟨x⟩ = ∫ x |ψ|² dx, approximated by Σ xⱼ |ψⱼ|² dx. We will use this formula all through the course.

Derivatives as matrices

The kinetic energy involves a second derivative. On a grid, the standard three-point formula is

ψ''(xⱼ) ≈ (ψⱼ₊₁ - 2ψⱼ + ψⱼ₋₁) / dx².

This is a matrix multiplication: a tridiagonal matrix with -2 on the diagonal and 1 on the two neighbors, divided by dx². Boundary points are treated by assuming ψ = 0 outside the grid, which is fine for states that die off before the edges.

Python Second derivative matrix test

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The interior error is about 5e-6: the three-point formula is accurate to order dx². Doubling N reduces the error about fourfold.

Eigenvalue problems

Soon we will meet equations of the form H ψ = E ψ: find numbers E (eigenvalues) and vectors ψ. With H as a matrix, numpy.linalg.eigh solves it. For large grids, use a sparse tridiagonal solver such as scipy.linalg.eigh_tridiagonal, which only needs the diagonal and off-diagonal.

As a warm-up, the eigenvalues of -D2 with zero boundaries are known exactly: kₙ² = (nπ/L)² for a box of length L. Check that your solver finds them:

Python Eigenvalues of the discrete second derivative

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The numeric values agree with (nπ/L)² to about four or five digits. The agreement improves as N grows, and worsens for higher n, because higher states wiggle faster and need more grid points.

Good habits

  1. Check norms. After every step, verify Σ|ψ|² dx ≈ 1.
  2. Test against exact cases. Always run your code on a problem with a known answer first.
  3. Convergence. Double N and see whether results change. If they do, N is too small.
  4. Use scaled units. Set ħ = m = 1 inside code, so numbers are order 1. We will convert back to real units when it matters.

Plotting complex wavefunctions

A complex ψ cannot be drawn as a single line. Common choices: plot Re ψ, Im ψ and |ψ|² together, or color by phase.

Python Plot real part, imaginary part and density

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real and imaginary parts oscillate with wavenumber k₀ = 3 inside a Gaussian envelope, while the density |ψ|² has no oscillation at all. This is a wave packet moving to the right; we meet it again later.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. How do you approximate ∫|ψ|² dx on a grid?

    A Riemann sum: Σ |ψⱼ|² dx.
  2. 2. The three-point second derivative has which matrix structure?

    Each point couples only to its two neighbors, giving a tridiagonal matrix.
  3. 3. Which NumPy routine suits a Hermitian eigenvalue problem?

    eigh is for Hermitian (or real symmetric) matrices and returns real, sorted eigenvalues.
  4. 4. If doubling N changes your result a lot, you should

    A converged numerical answer should barely change when the grid is refined.

Summary

  • Wavefunctions are complex arrays; integrals are sums times dx.
  • The second derivative is a tridiagonal matrix (1, -2, 1)/dx².
  • Use eigh or eigh_tridiagonal to solve H ψ = E ψ; verify with exact cases.
  • Check norms and convergence, and plot Re, Im and |ψ|².

Next: Blackbody Radiation and Planck's Idea