Skip to content

Latest commit

 

History

History
162 lines (128 loc) · 7.24 KB

sampling.md

File metadata and controls

162 lines (128 loc) · 7.24 KB

Sampling NQS with Metropolis–Hastings

Eloc in NQS shows how to compute the local energy $E_\text{loc}(s)$ of one spin configuration. This note covers where those configurations come from: a Markov chain Monte Carlo sampler that draws $s$ from $|\psi_M(s)|^2$. The reference implementation is sampling.py; the notation is the same as in README.md and nqs_helpers.py.

Why sample?

The variational energy $\langle E \rangle$ is an average of the local energy over the Born distribution of the trial state (see Neural Quantum States):

$$ \langle E \rangle = \frac{\langle \psi_M | \hat H | \psi_M \rangle}{\langle \psi_M | \psi_M \rangle} = \sum_s p(s), E_\text{loc}(s), \qquad p(s) = \frac{|\psi_M(s)|^2}{\sum_{s'} |\psi_M(s')|^2}. $$

The sum runs over $2^N$ configurations, and the normalization $\sum_{s'} |\psi_M(s')|^2$ cannot be computed either. If we can draw samples $s^{(1)}, \dots, s^{(N_S)} \sim p(s)$, $\langle E \rangle$ becomes a sample mean,

$$ \langle E \rangle \approx \frac{1}{N_S} \sum_{k=1}^{N_S} E_\text{loc}\big(s^{(k)}\big). $$

Metropolis–Hastings

Metropolis–Hastings builds a Markov chain $s^{(0)} \to s^{(1)} \to \dots$ whose stationary distribution is $p(s)$, using only ratios of $p$, so the unknown normalization cancels. From the current state $s$:

  1. propose a new state $s'$,
  2. accept it with probability

$$ A(s \to s') = \min!\left(1, \frac{|\psi_M(s')|^2}{|\psi_M(s)|^2}\right) = \min!\left(1,; e^{,2\left[\log\psi(s') - \log\psi(s)\right]}\right), $$

  1. 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 $\log\psi$ directly, and working in logs avoids overflow. This simple acceptance ratio assumes a symmetric proposal — $s \to s'$ is proposed exactly as often as $s' \to s$ — which is true for the move below.

Staying in the zero-magnetization sector

The Heisenberg Hamiltonian conserves the total magnetization $m = \sum_i s_i$, so its eigenstates each live in one sector of fixed $m$. For an even number of spins the ground state lies in the zero-magnetization sector $m = 0$, which is the standard choice and the default everywhere in this folder. So we only need to sample that sector:

  • 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 $m = 0$: it conserves any $m$. To sample another sector (e.g. the lowest state with $m = 2$, for a spin gap), only the starting state changes: $(N + m)/2$ spins up and the rest down, via sample(W, b, n_samples, magnetization=m). $m$ must have the parity of $N$.

$i$ and $j$ can be any two sites, not only nearest neighbours. Note that this is the same two-spin flip as an off-diagonal term in $E_\text{loc}$ — there it is restricted to a bond, here it is not.

The lookup table, again

Evaluating $\log\psi(s')$ from scratch costs $O(N \cdot M)$. As in the Eloc calculation, flipping two spins changes only two terms of the pre-activation:

$$ \theta'_h = \theta_h - 2 W_{ih} s_i - 2 W_{jh} s_j, \qquad \log\psi(s') = \gamma \sum_{h=1}^{M} \log\big(2\cosh\theta'_h\big), $$

which is $O(M)$. In the code this is literally the same function, delta_theta_pair_flip in nqs_helpers.py. The difference with Eloc is that the sampler keeps the result: when a move is accepted, $\theta \leftarrow \theta'$ and $\log\psi(s) \leftarrow \log\psi(s')$, so $\theta$ is computed from scratch only once, at the start of the chain.

Sweeps, thermalization and samples

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_thermalization sweeps 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.

C independent chains

sampling.py runs one chain. Production samplers run $C$ independent chains in parallel, each with its own random stream, its own starting state and its own thermalization, and split the sample budget over them: with $N_S$ total samples each chain contributes $N_S / C$. Per chain the loop is exactly the single-chain algorithm. In the CUDA MH sampler (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 $O(M)$ sum over hidden units:

// 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 $m$, so they sample the same distribution. The CUDA sampler likewise selects the sector only through the initial state.

Algorithm for sampling a single chain

  1. Draw a random state $s$ with $N/2$ spins up and $N/2$ down ($m = 0$; for another sector, $(N + m)/2$ up).
  2. 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)$.
  3. Repeat n_thermalization + n_samples sweeps; one sweep is $N$ times:
    1. Pick a random up spin $i$ and a random down spin $j$.
    2. $\theta'h = \theta_h - 2 W{ih} s_i - 2 W_{jh} s_j$.
    3. $\log\psi(s') = \gamma \sum_h \log\big(2\cosh\theta'_h\big)$.
    4. Draw $u \sim U(0,1)$; if $u &lt; \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')$.
  4. After each of the last n_samples sweeps, store $s$.

As for Eloc, we use $\gamma = 0.5$.

Contents

  • sampling.py — mh_step, sweep, sample(W, b, n_samples, magnetization=0); a demo with python sampling.py that samples the pre-trained 16×16 RBM and estimates $\langle E \rangle$ per spin with eloc_lookup_table.
  • sampling_cuda.py — the same demo, but $E_\text{loc}$ of the samples comes from the CUDA kernel cuda/eloc_batched.cu (build with make -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$).