The Hofstadter butterfly is a famous fractal pattern discovered by physicist Douglas Hofstadter in 1976. It appears when studying electrons on a two-dimensional lattice under a perpendicular magnetic field.
This system can be modeled using the Harper Hamiltonian, which describes electrons hopping between lattice sites under an applied magnetic flux. When the magnetic flux per plaquette is a rational number alpha = p/q
(with p
and q
coprime), the spectrum splits into q subbands, forming a rich, self-similar pattern resembling a butterfly.
This fractal spectrum connects to:
- Bloch electrons in a magnetic field
- Quasicrystals
- Quantum Hall effect
- Topological phases of matter
This project reproduces the Hofstadter butterfly using a one-dimensional chain of q sites with a quasiperiodic potential controlled by alpha
.
The Hamiltonian is based on the Harper model (originally 2D) and its 1D quasiperiodic version, known as the Aubry-André model. In modern literature, this 1D formulation is often referred to as the Aubry-André-Harper (AAH) model.
For a system with q sites, the Hamiltonian matrix is defined as:
Periodic boundary conditions are applied: (H_{0,q-1} = H_{q-1,0} = 1)
Key points:
- The system is 1D, representing a chain of
q
sites. 2 * cos(2 * pi * alpha * n)
is the on-site quasiperiodic potential.- Off-diagonal terms
1
represent nearest-neighbor hopping. - Periodic boundary conditions:
H[0, q-1] = H[q-1, 0] = 1
. - Eigenvalues of this matrix are the allowed energy levels.
Plotting eigenvalues versus alpha
in [0,1]
produces the Hofstadter butterfly, reproducing the same fractal pattern as the original 2D Harper model.
This repository provides a parallelized Python implementation using the 1D Harper/Aubry-André-Harper Hamiltonian.
Steps:
-
Construct the Hamiltonian
- For a given
q
andalpha = p/q
, build the Hamiltonian matrix. - Use NumPy for fast vectorized operations.
- For a given
-
Filter coprime pairs
- Only consider
p
andq
coprime, to avoid redundant spectra.
- Only consider
-
Compute all eigenvalues
- For denominators
q <= max_q
, compute the full set of eigenvalues.
- For denominators
-
Parallelization
- Distribute computations using
joblib
.
- Distribute computations using
-
Visualization
- Scatter plot of eigenvalues (
alpha
vs. energy). - KDE density heatmap to show regions of high density of states.
- Scatter plot of eigenvalues (
- Constructs the q × q Harper/Aubry-André-Harper Hamiltonian for flux
alpha = p/q
. - Returns sorted eigenvalues.
- Uses NumPy vectorized operations for efficiency.
-
Computes eigenvalues for a single pair
(p, q)
ifgcd(p, q) = 1
. -
Returns:
alphas
: array ofalpha = p/q
repeated for each eigenvalueenergies
: array of corresponding eigenvalues
-
Ensures only coprime pairs are considered, avoiding duplicates.
- Computes eigenvalues for all valid numerators
p
for a given denominatorq
. - Aggregates results into flattened arrays of
alphas
andenergies
.
-
Generates the full Hofstadter spectrum for all
alpha = p/q
with denominatorsq <= max_q
. -
Uses joblib to parallelize computations across CPU cores.
-
Returns:
alphas
: all magnetic flux valuesenergies
: corresponding eigenvalues
-
Plots the Hofstadter butterfly spectrum.
-
Two visualization layers:
- KDE density heatmap (background)
- Scatter overlay of actual eigenvalues, colored by energy
-
Supports configurable figure size, DPI, color maps, and KDE bandwidth.
-
Holds plotting and computation parameters:
max_q
– maximum denominator for fluxesgrid_res
– resolution of KDE heatmapbw_method
– KDE bandwidthfigsize
,dpi
– plot size and resolutioncmap_kde
,cmap_scatter
– color maps for background and scatter overlay
The output combines two layers:
-
KDE Density Heatmap (background)
- Represents density of states in the
(alpha, E)
plane. - Bright regions = high density (energy bands), dark regions = gaps.
- Represents density of states in the
-
Scatter Overlay (points)
- Each point
(alpha, E)
corresponds to an actual eigenvalue. - Color encodes energy
E
. - Ensures both raw spectrum and smoothed density are visible.
- Each point
Note: KDE heatmap uses Gaussian kernel density estimation; it highlights density, not exact pixel values.
pip install numpy matplotlib joblib scipy
python hofstadter.py
max_q
: max denominator for rational fluxes (higher = more detail).grid_res
: resolution of KDE heatmap.
- Parallelization: CPU cores used via
joblib
. - Vectorization: NumPy used for matrix operations.
- Memory-efficient concatenation: avoids Python list overhead.
This allows max_q
values up to 150 or more without excessive runtime.
With just a few lines of Python, you can generate your own Hofstadter butterflies, visualize the density of states, and explore the fractal spectrum of quantum systems under magnetic fields. This project demonstrates the deep connections between quantum mechanics, topology, and fractal geometry in a fully accessible 1D model.
- D. R. Hofstadter, “Energy levels and wave functions of Bloch electrons in rational and irrational magnetic fields”, Phys. Rev. B 14, 2239 (1976).
- M. Kohmoto, B. Sutherland, and C. Tang, “Critical wave functions and a Cantor-set spectrum of a one-dimensional quasicrystal model”, Phys. Rev. B 35, 1020 (1987).