Eloc in NQS shows how to compute the local energy
sampling.py; the
notation is the same as in README.md and nqs_helpers.py.
The variational energy
The sum runs over
Metropolis–Hastings builds a Markov chain
- propose a new state
$s'$ , - accept it with probability
- otherwise stay at
$s$ (the chain repeats the current state).
The second form holds for a real wavefunction and is the one the code uses:
the RBM gives us
The Heisenberg Hamiltonian conserves the total magnetization
-
start from a random state with
$N/2$ spins up and$N/2$ down; -
propose exchanging one up spin
$i$ with one down spin$j$ , i.e. flip both. This keeps$m = 0$ exactly and is symmetric.
Nothing in the move depends on sample(W, b, n_samples, magnetization=m).
Evaluating
which is delta_theta_pair_flip in nqs_helpers.py. The difference with Eloc is that
the sampler keeps the result: when a move is accepted,
Successive states in the chain are strongly correlated (a single exchange changes only two spins), so we do not keep every state:
- a sweep is
$N$ proposals, on average one per spin; - the first
n_thermalizationsweeps are discarded, so the chain forgets its random starting point and reaches$p(s)$ ; - after that, the state at the end of every sweep is kept as a sample.
Units differ between the project's samplers: the GPU sampler's sweep = 1
equals one sweep as defined here; the CPU (UltraFast.jl) sampler's sweep
counts single proposals, so a full sweep is sweep = N.
sampling.py runs one chain. Production samplers run ultrafast_intel_interop/samplers/cuda/mh-gpu/src/mh-gpu-sampler.cu, outside
this folder) each chain is one thread block, and the threads of a block share
the
// one CUDA block per chain; threads split the hidden units h
for (int sweep_idx = 0; sweep_idx < n_thermalization + n_samples_per_chain; ++sweep_idx) {
for (int step = 0; step < nspins; ++step) {
// thread 0: pick i in the first half, j in the second half
// (the move is skipped when s[i] == s[j])
// all threads: partial sum of log(2 cosh(theta_h - 2 W_ih s_i - 2 W_jh s_j))
// block reduction -> logpsi_prime
// thread 0: accept if log(u) < 2 * (logpsi_prime - logpsi)
// all threads: on accept, theta_h -= 2 W_ih s_i + 2 W_jh s_j; flip s[i], s[j]
}
if (sweep_idx >= n_thermalization) { /* write s, theta, logpsi as a sample */ }
}The CUDA proposal (one site from each half, skip if parallel) differs from the
Python one (one up spin, one down spin) but both are symmetric and conserve
- Draw a random state
$s$ with$N/2$ spins up and$N/2$ down ($m = 0$ ; for another sector,$(N + m)/2$ up). - Compute
$\theta_h = b_h + \sum_i W_{ih} s_i$ and$\log\psi(s) = \gamma \sum_h \log\big(2\cosh\theta_h\big)$ . - Repeat
n_thermalization + n_samplessweeps; one sweep is$N$ times:- Pick a random up spin
$i$ and a random down spin$j$ . - $\theta'h = \theta_h - 2 W{ih} s_i - 2 W_{jh} s_j$.
-
$\log\psi(s') = \gamma \sum_h \log\big(2\cosh\theta'_h\big)$ . - Draw
$u \sim U(0,1)$ ; if$u < \exp\big(2[\log\psi(s') - \log\psi(s)]\big)$ : flip$s_i$ and$s_j$ , set$\theta \leftarrow \theta'$ and$\log\psi(s) \leftarrow \log\psi(s')$ .
- Pick a random up spin
- After each of the last
n_samplessweeps, store$s$ .
As for Eloc, we use
-
sampling.py—mh_step,sweep,sample(W, b, n_samples, magnetization=0); a demo withpython sampling.pythat samples the pre-trained 16×16 RBM and estimates$\langle E \rangle$ per spin witheloc_lookup_table. -
sampling_cuda.py— the same demo, but$E_\text{loc}$ of the samples comes from the CUDA kernelcuda/eloc_batched.cu(build withmake -C cuda; needs a GPU to run). -
tests/test_sampling.py— sampled states stay in their magnetization sector ($m = 0$ and others), are reproducible for a fixed seed, and for$N = 6$ match the exact distribution$|\psi(s)|^2 / Z$ over all states of the sector (20 for$m = 0$ , 15 for$m = 2$ ).