quantax.sampler.Samples#

class quantax.sampler.Samples#

The samples generated by the sampler, with 5 attributes. Only spins is positional, the other attributes are keyword-only, e.g. Samples(spins, psi=psi, prob=prob).

This class is mutable and not a PyTree, so it can’t be passed to jitted functions. The attributes missing in the samples are filled in place by the functions computing them, e.g. Oloc.

Warning

A filled psi belongs to the state it was evaluated by. To reuse the samples with another state, or after the parameters of the state are updated, set psi to None or to the new wave function first.

spins:

The spin configurations \(s\).

psi:

The wave function \(\psi(s)\) of the spin configurations. None if not evaluated yet, e.g. when the wave function evaluated by the sampler is not consistent with the forward pass of the state, see sweep.

prob:

The probability \(q_s\) from which the samples are drawn, up to a normalization constant. It is the probability actually used by the sampler, e.g. \(|\psi(s)|^n\) with the reweight factor n and \(\psi\) evaluated by the sampling forward pass. None defaults to \(q_s = |\psi(s)|^2\).

state_internal:

The internal status of samples for the forward pass with reference. See RefModel for details.

r:

The reweighting factor. According to

\[\left< x \right>_p = \frac{\sum_s p_s x_s}{\sum_s p_s} = \frac{\sum_s q_s x_s p_s/q_s}{\sum_s q_s p_s/q_s} = \frac{\left< x p/q \right>_q}{\left< p/q \right>_q},\]

the expectation value with probability distribution p can be computed from samples with a different probability distribution q.

The reweighting factor is defined as

\[r_s = \frac{p_s/q_s}{\left< p/q \right>_q},\]

so that \(\left< x \right>_p = \left< r x \right>_q\).

Usually, \(p_s = |\psi(s)|^2\) is the target probability, and \(q_s\) can be chosen as \(|\psi(s)|^n\) or computed from a helper neural network. In the former case, \(r_s = \frac{|\psi_s|^{2-n}}{\left< |\psi|^{2-n} \right>}\). None stands for the trivial factor \(r_s = 1\). If prob is given, r is computed from psi and prob by update_r.

__init__(spins: Array, *, psi: NDArray | Array | LogArray | None = None, prob: NDArray | Array | LogArray | None = None, state_internal: PyTree = None, r: Array | None = None) → None#
__getitem__(idx) → Samples#

Index every array leaf along the leading (sample) axis, e.g. samples[:10].

update_r() → None#

Update the reweighting factor r in place from the wave function and probability of the samples as

\[r_s = \frac{p_s/q_s}{\left< p/q \right>_q}, \quad p_s = |\psi(s)|^2, \quad q_s = \text{prob}_s.\]

Nothing is changed if prob is None, which keeps a given r.

Raises:

ValueError – If prob is given but psi is None.

block_until_ready() → Samples#

Wait until all arrays of the samples are computed, also called by jax.block_until_ready.