Variational Monte Carlo for many-body FEX
Experimental. Part of
alkahest.fex; names and signatures may change between minor versions. Requires NumPy.
The many-body leaves let FEX fit a ground state
v = log ψ on collocation points with a deterministic residual loss, and
fex.finalize_many_body proves a closed form exactly. This page adds the
Monte Carlo side: sampling |ψ|², local energies, the energy and variance
losses with their gradient estimators, stochastic reconfiguration,
autocorrelation-aware error bars, and the Kato cusp conditions — as exact
symbolic checks, as built-in constraints (cusp-fixed Jastrow factors) and as
penalties.
Every number on this page that comes from sampling is statistical. A VMC
estimate is a mean local energy with a one-sigma error bar, and it carries
status = "statistical". It never certifies anything: an exact solution is
proved by fex.finalize_many_body (a symbolic zero test of Hψ − Eψ at the
fitted N, or the symbolic-N lift), and rigorous energy bounds come from the
validated Rayleigh-quotient module. A zero sample variance of E_L is strong
evidence that ψ is an eigenfunction, not a proof.
Proofs for every particle number N are on the
many-body page.
Trial wavefunctions
A trial wavefunction is handled through v = log|ψ|: its value, ∇v, Δv,
the log-derivatives O_k = ∂v/∂θ_k, and a vector–Jacobian product of the jet
in θ (the hook the variance gradient needs).
| Class | ψ |
Parameters |
|---|---|---|
FexAnsatz(template, e, θ, problem) |
a FEX candidate: v itself for the log form of Problem.many_body, `log |
u |
PadeJastrow(N, D, b=, spins=) |
exp(Σ_{i<j} a_ij r_ij/(1 + b r_ij)), a_ij fixed by the Kato cusp |
log b (and the amplitudes with cusp=False) |
FixedFactor(fn, N, D) / FixedFactor.from_slater_jastrow(ev) |
a parameter-free factor, e.g. a Slater determinant from a compiled Slater–Jastrow evaluator | none |
a * b, ProductAnsatz([...]) |
the product (v adds) |
concatenated |
A FEX candidate is evaluated by the same fused exact-jet evaluator that scores
candidates in a search (Evaluator.jets, value_jacobian, jet_vjp), in
parallel over walkers; nothing is recompiled when the parameters change.
Sampling and the local energy
Sampler moves a batch of walkers with all-particle Metropolis–Hastings
(method="metropolis") or drift–diffusion Langevin moves
(method="langevin": the drift is the quantum force ∇ log|ψ|²/2, capped
per particle as in Umrigar, Nightingale & Runge (1993), with the
Metropolis–Hastings correction, so |ψ|² is sampled exactly). The step
adapts during burn-in towards a target acceptance and is then frozen, so
detailed balance holds while samples are recorded. A proposal where
log|ψ|² is not finite is rejected. The local energy is
E_L = Hψ/ψ = −½(Δv + |∇v|²) + V.
vmc_energy runs burn-in and a recorded chain and returns a VMCEstimate:
the mean of E_L, its error bar from a blocking analysis of the
walker-averaged series (Flyvbjerg & Petersen 1989, with the automatic block
choice of Lee, Booth, Thom & Alavi 2011), the integrated autocorrelation
time, the variance of E_L with its own error bar, the acceptance and the
largest deviation |E_L − Ē| seen.
import numpy as np
from alkahest import fex
rng = np.random.default_rng(0)
# Calogero–Moser, N = 4, λ = 2, ω = 1: E0 = N/2 + λN(N−1)/2 = 14 (Calogero 1971)
prob = fex.Problem.many_body(4, 1, one_body=lambda r: r**2 / 2, pair=lambda r: 2 / r**2)
tpl = fex.Template.many_body("B(P,R2)", 4) # P[log] + R2[id]
exact = fex.FexAnsatz(tpl, [4, 0, 1], [2.0, 0.0, -0.5, 0.0], prob)
est = fex.vmc_energy(exact, prob, walkers=128, steps=50, rng=rng)
print(round(est.energy, 9), est.variance < 1e-20, est.status) # 14.0 True statistical
# A Gaussian trial for the 3-D oscillator: <E> = N·D·(a/2 + 1/(8a))
osc = fex.Problem.many_body(2, 3, one_body=lambda r: r**2 / 2)
trial = fex.FexAnsatz(fex.Template.many_body("R2", 2, 3), [1], [-0.35, 0.0], osc)
est = fex.vmc_energy(trial, osc, walkers=256, steps=100, method="langevin", rng=rng)
print(est) # E = 3.21… ± 0.00… (statistical, 1σ; …)Losses, gradients and optimisers
vmc_optimize(ansatz, problem, loss=..., optimizer=...) alternates sampling
(the walkers are kept between iterations and re-equilibrated briefly) with a
parameter step. The losses and their standard low-variance estimators are
- energy
⟨E_L⟩:∂θ⟨E⟩ = 2⟨(E_L − Ē)(O − Ō)⟩(the⟨∂θ E_L⟩term vanishes in expectation becauseHis Hermitian); - variance
Var(E_L)(Umrigar, Wilson & Wilkins 1988):2⟨(E_L − Ē)∂θE_L⟩ + 2⟨((E_L − Ē)² − σ²)(O − Ō)⟩, the first term through the evaluator's vector–Jacobian product; - any mixture
(1 − w)E + w Var(loss=w).
The optimisers are stochastic reconfiguration (optimizer="sr", the default:
δθ = −η (S + ε diag S + ε)⁻¹ g with S = ⟨(O − Ō)(O − Ō)ᵀ⟩, the natural
gradient in the wavefunction's metric; Sorella 1998, 2001), Adam and plain
SGD. vmc_fit(template, problem, e, θ, factors=[...]) does the same for one
FEX skeleton, optionally times extra factors, and returns the candidate's
e and full θ for finalize_many_body.
# Moshinsky's two coupled oscillators (k = 4): E0 = 3/2 (1 + √(1 + 2k)) = 6
mosh = fex.Problem.many_body(2, 3, one_body=lambda r: r**2 / 2, pair=lambda r: 2 * r**2)
start = fex.FexAnsatz(fex.Template.many_body("B(R2,P2)", 2, 3), [1, 0, 1],
[-0.3, 0.0, -0.8, 0.0], mosh)
fit = fex.vmc_optimize(start, mosh, iterations=30, walkers=256, steps=8,
final_steps=40, rng=rng)
print(fit.estimate) # E = 6.000000 ± 0.000000 …: the exact ψ
print(np.round(fit.ansatz.full_theta, 3)) # [-0.5 0. -0.5 0. ]Searching with VMC
Problem.many_body(..., loss="vmc") turns fex.solve_many_body into
fex.vmc_search: every canonical skeleton of the template is fitted to the
centred log-form loss (the variance of E_L) with Levenberg–Marquardt; the
best are scored by a VMC energy run; the collocation points are then redrawn
from |ψ|² of the best candidate and the fit is repeated (the fixed-sample
variance minimisation of Umrigar, Wilson & Wilkins, resampled every round).
Candidates are ranked by VMC energy — the variational principle — and,
within two combined error bars, by the variance of E_L (the zero-variance
principle). Exact solutions of the log-form equation that are not bound
states (growing at infinity or singular at a coalescence, e.g. Calogero's
λ → 1 − λ branch) are refitted from sign-flipped starts or dropped before
scoring, since their |ψ|² is not a density. The winner goes to
finalize_many_body unchanged.
cm = fex.Problem.many_body(3, 1, one_body=lambda r: r**2 / 2, pair=lambda r: 2 / r**2,
loss="vmc")
tpl3 = fex.Template.many_body("B(P,R2)", 3)
res = fex.solve_many_body(tpl3, cm, rounds=2, walkers=128, steps=30, rng=rng)
print(tpl3.describe(res.e), res.vmc) # (P[log] + R2[id]) E = 7.5 ± 0 …
fin = fex.finalize_many_body(res, cm)
print(fin["energy"], fin["verification"]["status"]) # 15/2 exactly_verifiedKato cusp conditions
Where two charged particles meet, ψ has a cusp: after a spherical average
about the coalescence, ∂_r⟨ψ⟩ = c ψ at r = 0. With
H = −Σ∇²/(2m) + Σ q_i q_j/r_ij in D ≥ 2 dimensions,
c = 2μ q₁q₂/(2l + D − 1) (μ the reduced mass; l = 0 where ψ does not
vanish at the coalescence, l = 1 at the node of an equal-spin pair):
| Coalescence | D = 3 |
D = 2 |
general D |
|---|---|---|---|
electron–nucleus (charge Z) |
−Z |
−2Z |
−2Z/(D − 1) |
| electron–electron, opposite spins | ½ |
1 |
1/(D − 1) |
| electron–electron, equal spins | ¼ |
⅓ |
1/(D + 1) |
Sources: Kato (1957) for D = 3, l = 0; Pack & Byers Brown (1966) for the
equal-spin ¼; Pan & Sahni (2003, eq. 11) for D ≥ 2. cusp_coefficient
returns these (exact rationals); the test-suite also checks, symbolically, that
each one cancels the 1/r term of the pair's radial equation. Without the
cusp, E_L diverges like 1/r at the coalescence: a measure-zero set the
sampler never hits, but it makes the variance of E_L large (and, for the
energy estimator, heavy-tailed).
As exact checks. cusp_conditions(v, coords, (N, D), nuclei=[(pos, Z)], spins=...) places each pair at c ± (r/2)n (the centre and all other
particles stay symbolic, r > 0) or each particle at pos + r n, and takes
lim_{r→0⁺} ∂_r v symbolically along two axes and their opposites (the
average over ±n is the spherical average of the smooth part). satisfied
is an exact identity for every position of the other particles;
violated comes with the exact limit found.
import alkahest as ak
pool = ak.ExprPool()
x = [pool.symbol(f"x{i}") for i in range(6)]
r1 = ak.sqrt(x[0]**2 + x[1]**2 + x[2]**2)
r2 = ak.sqrt(x[3]**2 + x[4]**2 + x[5]**2)
r12 = ak.sqrt((x[0] - x[3])**2 + (x[1] - x[4])**2 + (x[2] - x[5])**2)
v = -2 * (r1 + r2) + r12 / (2 * (1 + r12 / 4)) # Z = 2 Slater–Jastrow
for rec in fex.cusp_conditions(v, x, (2, 3), nuclei=[((0, 0, 0), 2)]):
print(rec["kind"], rec["particles"], rec["actual"], rec["status"])
# pair (0, 1) 1/2 satisfied / nucleus (0,) -2 satisfied / nucleus (1,) -2 satisfied
hyd = -pool.rational(27, 16) * (r1 + r2) # screened hydrogenic
print([r["status"] for r in fex.cusp_conditions(hyd, x, (2, 3), nuclei=[((0, 0, 0), 2)])])
# ['violated', 'violated', 'violated']As constraints. PadeJastrow fixes each pair amplitude to the cusp
coefficient of the pair (from spins), so the electron–electron cusps hold
exactly for every b. For a free FEX candidate, vmc_optimize(..., cusps=[("nucleus", m, pos, c), ("pair", i, j, c)], cusp_weight=w) adds the
penalty w Σ (∂_r v − c)², the directional derivative taken at distance
cusp_h from the coalescence on the walkers' configurations.
Validation
Helium and Hooke's atom have Coulomb cusps; Calogero–Moser and the coupled
oscillators are smooth bosonic warm-ups. Measured with
benchmarks/fex/bench_vmc.py validate (2 000 walkers × 500 recorded
configurations per estimate, three moves apart, after 300 burn-in moves):
| System | Trial | Exact or reference | VMC energy (statistical, 1σ) | Var E_L |
|---|---|---|---|---|
Calogero–Moser, N = 4, λ = 2 (1D) |
exact ψ |
14 (exact) |
14.000000 ± 0 |
4·10⁻³⁰ |
Calogero–Moser, N = 8 |
exact ψ |
60 (exact) |
60.000000 ± 0 |
1·10⁻²⁸ |
Calogero–Moser, N = 4 |
found by loss="vmc" search (2.1 s) |
14 |
14.000000 ± 0; finalize_many_body: exactly_verified, E = 14 |
5·10⁻³⁰ |
Moshinsky, N = 2, k = 4 (3D) |
B(R2,P2), SR from a perturbed start |
6 (exact) |
6.000000 ± 0, weights −½, −½ (exact) |
1·10⁻²⁷ |
coupled oscillators, N = 8, k = 1 |
same | 33 (exact) |
33.000000 ± 0, weights −½, −⅛ (exact) |
2·10⁻²⁷ |
Hooke's atom, k = ¼ |
found by loss="vmc" search over B(U(P),R2) (2.6 s) |
2 (exact) |
2.000000 ± 0 |
2·10⁻³² |
| Hooke's atom | Gaussian × cusp-fixed Padé–Jastrow, SR | 2 |
2.000102 ± 0.000037 |
1.5·10⁻⁴ |
| helium | e^{−27/16 (r₁+r₂)} (cusps violated) |
−2.84765625 (exact for this trial) |
−2.8462 ± 0.0021 |
0.87 |
| helium | e^{−2(r₁+r₂)} (e–n cusp only) |
−2.75 (exact for this trial) |
−2.7497 ± 0.0013 |
1.07 |
| helium | e^{−2(r₁+r₂)} × Padé, both cusps exact, b by SR |
−2.903724… (reference) |
−2.87910 ± 0.00072 |
0.114 |
| helium | same b, e–e amplitude ¼ instead of ½ |
−2.903724… |
−2.8516 ± 0.0013 |
0.198 |
| helium | e^{−27/16 (r₁+r₂)} × Padé (e–n cusp violated) |
−2.903724… |
−2.8362 ± 0.0012 |
0.455 |
| helium | e^{−α(r₁+r₂)} × Padé, α = 1.843, b = 0.355 by SR |
−2.903724… |
−2.89138 ± 0.00043 |
0.134 |
| helium | FEX R[id] + log(P[id]) = e^{−α(r₁+r₂)}(1 + c r₁₂), α = 1.847, c = 0.352 |
−2.903724… |
−2.89128 ± 0.00082 |
0.130 |
The reference is the nonrelativistic ground state −2.903724377034… Eh of
Nakashima & Nakatsuji (J. Chem. Phys. 127, 224104 (2007)). The exact rows
are closed forms (Calogero 1971; Moshinsky 1968 and its N-particle normal
modes; Hooke's atom at k = ¼: Kais, Herschbach & Levine 1989, Taut 1993,
ψ = (1 + r₁₂/2) e^{−(r₁² + r₂²)/4}). The search recovers Hooke's closed
form exactly, log(1 + r₁₂/2) − (r₁² + r₂²)/4, and cusp_conditions proves
its electron–electron cusp; finalize_many_body's grouped zero test of the
log-form residual leaves that one unverified (a rational function of the
square root r₁₂ that the grouping does not reduce), so for Hooke's atom the
exact energy 2 is the literature's, confirmed — statistically — by the
zero variance.
The cusps and the variance. Without the electron–electron cusp the
local energy has a 1/r₁₂ spike: on samples with r₁₂ < 0.1 the variance of
E_L is 48.7 for e^{−2(r₁+r₂)}, 13.9 with the wrong amplitude ¼, and
0.027 with the exact ½; without the electron–nucleus cusp the same happens
at r_m < 0.05 (20.7 at α = 27/16, 0.23 at α = 2 with the Padé factor).
The largest deviation |E_L − Ē| seen drops from about 100 to 0.96 when
both cusps hold. The lowest energies here come from relaxing α below Z
(the energy gains more from the orbital than it loses from the
electron–nucleus spike): a one-body template richer than α r is what can
have both.
Throughput
Measured with benchmarks/fex/bench_vmc.py throughput on a shared 32-core
x86-64 machine (other jobs running; NumPy and the Rust evaluator, no GPU).
Walker moves is all-particle Metropolis moves per second with 1 024 walkers
(each move evaluates v, ∇v, Δv at the proposal). An SR step is one
iteration of vmc_optimize: 512 walkers × 10 recorded configurations (2 moves
apart) plus re-equilibration, local energies, O, the gradient and the SR
solve. In parentheses: the same before this change, when Evaluator.jets
at arbitrary points ran serially and tabulated every operator of the
many-body leaves' table rather than the candidate's own.
| System | N |
walker moves / s | SR step |
|---|---|---|---|
| Calogero–Moser, 1D | 2 | 1.55 M (0.84 M) | 19 ms (32 ms) |
| Calogero–Moser, 1D | 4 | 1.61 M (0.82 M) | 19 ms (44 ms) |
| Calogero–Moser, 1D | 8 | 1.13 M (0.60 M) | 26 ms (69 ms) |
| Calogero–Moser, 1D | 16 | 0.71 M (0.27 M) | 48 ms (129 ms) |
| Calogero–Moser, 1D | 32 | 0.36 M (0.10 M) | 109 ms (339 ms) |
| coupled oscillators, 3D | 2 | 1.44 M (0.91 M) | 19 ms (35 ms) |
| coupled oscillators, 3D | 4 | 1.08 M (0.77 M) | 24 ms (46 ms) |
| coupled oscillators, 3D | 8 | 0.90 M (0.53 M) | 30 ms (86 ms) |
| coupled oscillators, 3D | 16 | 0.48 M (0.20 M) | 64 ms (162 ms) |
| coupled oscillators, 3D | 32 | 0.25 M (0.05 M) | 134 ms (416 ms) |
The cost per move grows like the number of pairs, N(N−1)/2, once N is
large. Fermions go through FixedFactor.from_slater_jastrow and add the
Slater–Jastrow evaluator's own cost per point (O(N³)). The torch evaluator
is not used by the samplers.
Credits
Variational Monte Carlo goes back to McMillan (Phys. Rev. 138, A442 (1965)) and Ceperley, Chester & Kalos (Phys. Rev. B 16, 3081 (1977)); the Metropolis algorithm to Metropolis, Rosenbluth, Rosenbluth, Teller & Teller (1953); the drift–diffusion move to Reynolds, Ceperley, Alder & Lester (J. Chem. Phys. 77, 5593 (1982)) with the drift cap of Umrigar, Nightingale & Runge (J. Chem. Phys. 99, 2865 (1993)); variance minimisation to Umrigar, Wilson & Wilkins (Phys. Rev. Lett. 60, 1719 (1988)); stochastic reconfiguration to Sorella (Phys. Rev. Lett. 80, 4558 (1998); Phys. Rev. B 64, 024512 (2001)); the blocking analysis to Flyvbjerg & Petersen (J. Chem. Phys. 91, 461 (1989)) and Lee, Booth, Thom & Alavi (Phys. Rev. E 83, 066706 (2011)); the cusp conditions to Kato (Commun. Pure Appl. Math. 10, 151 (1957)), Pack & Byers Brown (J. Chem. Phys. 45, 556 (1966)) and Pan & Sahni (J. Chem. Phys. 119, 7083 (2003)). The reviews of Foulkes, Mitas, Needs & Rajagopal (Rev. Mod. Phys. 73, 33 (2001)) and Becca & Sorella (Quantum Monte Carlo Approaches for Correlated Systems, CUP 2017) cover all of it. The Finite Expression Method is Liang & Yang's (see the FEX guide).