jaxrens.sampling.moves.swap

Species-swap move kernel for multi-component nested sampling.

Exchanges the identities of two atoms of different species. Positions and the cell are untouched, so the move explores chemical ordering at fixed composition and fixed geometry — the productive degree of freedom for alloys, where a displacement move has to tunnel through a barrier to achieve what one swap does directly.

Why the pair is drawn by construction

The obvious implementation — draw two atom indices uniformly, then reject the draw if they happen to share a species — applies the species test after the energy call, so every same-species draw costs a full backend evaluation and returns a guaranteed rejection. The wasted fraction is sum_s x_s**2 — 50% for an equimolar binary, and ~97% for a single solute in 63 host atoms. It also caps the achievable acceptance rate at 1 - sum_s x_s**2, which for an equimolar binary is exactly the 0.5 default target_acceptance.

This kernel instead draws a pair that is unlike by construction:

  1. Count the atoms of each species (C reductions over n_atoms).

  2. Draw an unordered species pair (s, t), s != t, with weight n_s * n_t — i.e. proportional to how many unlike pairs that species combination actually contributes.

  3. Draw k_s uniform in [0, n_s) and take the k_s-th atom of species s via a running count plus a binary search; likewise for t.

Every proposal is a real swap, so the acceptance rate reflects the physics rather than the composition.

Uniformity and detailed balance

Step 2 followed by step 3 gives each unlike pair {i, j} probability:

(n_s * n_t / Z) * (1 / n_s) * (1 / n_t) = 1 / Z,
Z = sum_{s<t} n_s * n_t = total number of unlike pairs

— exactly uniform over unlike pairs, independent of species abundance. A swap permutes types but leaves the multiset of types fixed, so Z and the set of unlike pairs are invariant, and the reverse proposal has the same probability as the forward one. The proposal is therefore symmetric and the E < Emax gate alone is a valid NS acceptance test.

(For contrast: selecting the first atom uniformly and the second uniformly among the unlike atoms — the obvious alternative — is still symmetric, but it is not uniform over pairs once three or more species are present, because the pair probability then picks up a 1/(N - n_s) + 1/(N - n_t) factor.)

Cost

C reductions plus two prefix sums over n_atoms, all O(n_atoms) with log depth on GPU. No sort, no permutation, no Gumbel top-k — the selection is negligible beside the single backend call, and it keeps the traced graph small, which matters because this kernel is vmapped over walkers and scanned over MWG sub-steps.

Species scoping

species=(code, ...) restricts the draw to pairs drawn from those type codes, mirroring the scoping in galilean.build_kernel. Registering one scoped swap per species pair gives each pair its own acceptance statistics and its own weight in the MWG mix — useful when one substitution is cheap and another is nearly always rejected.

Note that this kernel does not read state.step_size: a swap has no continuous magnitude to tune. Its acceptance rate is invariant under step-size bisection, so it should be excluded from step-size adaptation rather than bisected fruitlessly.

Single-walker function, designed for shard_map(vmap(vmap(…))) wrapping.

Functions

build_kernel

Build a species-swap kernel.