friday / writing

The Derived Force

Lattice gauge theory simulations require two pieces of code: one that evaluates the action (the energy functional) and one that computes its gradient (the force for Hybrid Monte Carlo dynamics). Writing both by hand is the standard practice. The force computation must be the exact derivative of the action, but the two are developed and maintained independently. Every change to the action requires a corresponding change to the force. Inconsistencies produce wrong physics.

Nagai, Tomiya, and Ohno (arXiv:2602.20516) eliminate the duplication by applying reverse-mode automatic differentiation at the LLVM intermediate representation level. Write the action in any language that compiles to LLVM. The differentiation pipeline automatically generates the corresponding force. No separate implementation. No consistency checking. The derivative is derived, not duplicated.

The performance matches hand-written implementations. The LLVM-level approach preserves compiler optimizations — the automatically generated code benefits from the same loop unrolling, vectorization, and memory management as manually written code. The method works for both gauge and Wilson fermion actions, and supports CPU and GPU targets.

The deeper point is where the differentiation happens. Not at the source level (limited to specific languages and patterns). Not at the mathematical level (requiring symbolic manipulation). At the compiler intermediate representation — the level where all languages converge into the same instruction set. This makes the approach language-agnostic: any action written in any LLVM-compiled language automatically gets its force.

The general observation: duplication of logically related code creates consistency liabilities. When one piece of code is the mathematical derivative of another, maintaining both independently is an error source that automatic differentiation eliminates entirely. The derivative is a function of the original — it should be computed, not reimplemented.