How Do Transformers "Do" Physics? Investigating the Simple Harmonic Oscillator

Subhash Kantamneni, Ziming Liu, Max Tegmark

TL;DR

When transformers learn to predict physical dynamics in-context, do they act as black-box interpolators, or do they learn recognizable algorithms? By defining rigorous correlational and causal probing criteria for internal computational "intermediates," this paper reveals that transformers simulate the undamped simple harmonic oscillator (SHO) using a well-known numerical method: the matrix exponential.

What Problem Does This Paper Address?

Large language models and transformers demonstrate impressive in-context learning capabilities, including predicting trajectories of physical systems. However, understanding their internal "world model" remains a core challenge in mechanistic interpretability.

When humans model physics, we either solve differential equations analytically or apply numerical integration. Do transformers learn closed-form analytical solutions, or do they implement numerical approximations? To answer this, the authors introduce a hypothesis-driven interpretability framework: if a transformer utilizes a specific modeling method $g(X, I)$, its hidden states must encode and causally rely on the method's unique internal intermediate $I$.

Methods

The authors evaluate transformer computations through a four-part evaluation suite developed first on linear regression ($y = wx$, where the intermediate $I = w$) and then applied to the simple harmonic oscillator ($\ddot{x} + 2\gamma \dot{x} + \omega_0^2 x = 0$):

  1. Criterion 1 (Encoding Presence): Hidden states encode $I$. Linear probes and non-linear polynomial "Taylor probes" (via Canonical Correlation Analysis) assess how well hidden states predict $I$.
  2. Criterion 2 (Performance Correlation): In-context learning performance gains correlate with improvements in the representation quality of $I$.
  3. Criterion 3 (Hidden State Explanation — Weak Causal): A "reverse probe" ($I \to \text{Hidden State}$) tests whether significant hidden state variance is dedicated to storing $I$.
  4. Criterion 4 (Intervention — Strong Causal): Intervening on the model’s internal states using reverse-probed representations (e.g., swapping $I \to I'$ or altering physical parameters $\Delta t, \omega_0$) predictably modifies model outputs according to $g(X, I')$.

For the harmonic oscillator state $\begin{bmatrix} x \\ v \end{bmatrix}$, the authors formulated three candidate numerical methods from ODE literature:

  • Linear Multistep Method: Uses past derivative history ($I = A\Delta t$).
  • Taylor Expansion Method: Uses higher-order derivatives ($I = (A\Delta t)^j$).
  • Matrix Exponential Method: Exact state-transition operator for linear time-invariant systems ($I = e^{A\Delta t}$).
Input: {(x₀, v₀), ..., (xₙ, vₙ)} 
               │
               ▼
┌──────────────────────────────┐
│     Transformer Layers       │ ──► Probe for Intermediate I
└──────────────────────────────┘     (e.g., e^{AΔt})
               │
               ▼
Output: (xₙ₊₁, vₙ₊₁)

Datasets

The study evaluated purely synthetic, noiseless physical trajectories:

  • Linear Regression Benchmark: 5,000 sequences of length 65 with $w, X \in [-0.75, 0.75]$, evaluated against out-of-distribution test sets ($0.75 \le |w| \le 1.0$).
  • Undamped SHO ($\gamma = 0$): 5,000 sequences of 65 steps generated over frequencies $\omega_0 \in [\pi/4, 5\pi/4]$ and timesteps $\Delta t \in [0, 2\pi/\omega_0]$. An out-of-distribution test set varied $\omega_0$ outside training bounds.
  • Damped SHO ($\gamma > 0$): 5,000 sequences of 32 steps covering underdamped ($\gamma < \omega_0$) and overdamped ($\gamma > \omega_0$) regimes with $\omega_0 \in [0.25\pi, 1.25\pi]$ and $\Delta t \in [0, 2\pi/(13\omega_0)]$.

Key Insights and Ideas

  • Transformers emulate numerical physics, not closed-form formulas: Rather than memorizing sinusoids or trigonometric identities, transformers converge toward classical state-space update matrices.
  • The Matrix Exponential dominates: While linear multistep and Taylor intermediates showed moderate correlational encoding, synthetic control experiments proved these were mathematical byproducts of expanding $e^{A\Delta t} = \sum_j (A\Delta t)^j / j!$. Only the matrix exponential satisfied all causal intervention criteria.
  • Zero-shot generalization across regimes: Remarkably, transformers trained only on underdamped trajectories generalized in-context to overdamped trajectories. Because underdamped dynamics rely on oscillatory sines/cosines while overdamped dynamics decay purely exponentially, this generalization is strong evidence of a unified underlying state-space algorithm.
  • Larger models are more interpretable: Larger models exhibited a clean phase transition toward linear intermediate encodings. The authors link this to the lottery ticket hypothesis: greater capacity allows models to find cleaner, modular representations.

Key Results

Criterion Linear Multistep Taylor Expansion ($j=3$) Matrix Exponential ($e^{A\Delta t}$)
1. Max Probe $R^2$ 0.66 0.67 0.84
2. Performance Correlation ($R^2$) 0.73 0.74 0.89
3. Hidden State Variance Explained ($R^2$) 0.42 0.53 0.78
4. Intervention Success Rate 0.44 0.44 0.72 (18/25 models)

Summary of evaluations for undamped SHO models across criteria.

  • Causal control: Directly intervening on the hidden states with reverse probes of $e^{A\Delta t}$ enabled predictable steering of predictions in 18 out of 25 model architectures (varying layers $L \in [1, 5]$ and hidden sizes $H \in [2, 32]$).
  • Interventions altering the physical parameters ($\Delta t \to \Delta t'$, $\omega_0 \to \omega_0'$) forced the models to predictably alter trajectory frequencies.

Limitations and Future Work

  • Simplified Architecture: Evaluated models used single-head attention without LayerNorm to maximize interpretability, leaving open how these mechanisms scale to standard multi-head, normalized LLMs.
  • Drop-off on Damped Systems: For underdamped systems with friction ($\gamma > 0$), intermediate encoding quality dropped significantly (Criterion 1 $R^2 = 0.54$, Criterion 3 $R^2 = 0.16$). The authors hypothesize the model may use algorithms outside their hypothesis space or exploit natural amplitude decay toward zero.
  • Noiseless Trajectories: Trajectories were ideal and noiseless.
  • Future Directions: Applying Koopman operator theory to linearize nonlinear dynamical systems into similar state-space matrices, potentially extending this probing framework to complex, nonlinear physics.

Summary updated

BibTeX
@inproceedings{kantamneni2024how,
  author = {Subhash Kantamneni and Ziming Liu and Max Tegmark},
  title = {{How Do Transformers "Do" Physics? Investigating the Simple Harmonic Oscillator}},
  booktitle = {MI @ ICML},
  year = {2024},
  url = {https://openreview.net/forum?id=cDvIuGobya},
}
Figure