# netket.experimental.sampler.MetropolisPtSamplerÂ¶

class netket.experimental.sampler.MetropolisPtSampler(*args, __precompute_cached_properties=False, __skip_preprocess=False, **kwargs)Â¶

Metropolis-Hastings with Parallel Tempering sampler.

This sampler samples an Hilbert space, producing samples off a specific dtype. The samples are generated according to a transition rule that must be specified.

Inheritance
__init__(*args, __precompute_cached_properties=False, __skip_preprocess=False, **kwargs)Â¶

MetropolisSampler is a generic Metropolis-Hastings sampler using a transition rule to perform moves in the Markov Chain. The transition kernel is used to generate a proposed state $$s^\prime$$, starting from the current state $$s$$. The move is accepted with probability

$A(s\rightarrow s^\prime) = \mathrm{min}\left (1,\frac{P(s^\prime)}{P(s)} e^{L(s,s^\prime)} \right),$

where the probability being sampled from is $$P(s)=|M(s)|^p. Here :math:$$ is a user-provided function (the machine), $$p$$ is also user-provided with default value $$p=2$$, and $$L(s,s^\prime)$$ is a suitable correcting factor computed by the transition kernel.

Parameters
• hilbert â€“ The hilbert space to sample

• rule â€“ A MetropolisRule to generate random transitions from a given state as well as uniform random states.

• n_chains â€“ The number of Markov Chain to be run in parallel on a single process.

• n_sweeps â€“ The number of exchanges that compose a single sweep. If None, sweep_size is equal to the number of degrees of freedom being sampled (the size of the input vector s to the machine).

• n_chains â€“ The number of batches of the states to sample (default = 8)

• machine_pow â€“ The power to which the machine should be exponentiated to generate the pdf (default = 2).

• dtype â€“ The dtype of the statees sampled (default = np.float32).

Attributes
is_exactÂ¶

Returns True if the sampler is exact.

The sampler is exact if all the samples are exactly distributed according to the chosen power of the variational state, and there is no correlation among them.

Return type

bool

machine_pow: int = 2Â¶
n_batchesÂ¶
n_chainsÂ¶

The total number of chains across all MPI ranks.

If you are not using MPI, this is equal to n_chains_per_rank

Return type

int

n_chains_per_rank: int = NoneÂ¶
n_replicas: int = 32Â¶
n_sweeps: int = NoneÂ¶
reset_chains: bool = FalseÂ¶
rule: netket.sampler.MetropolisRule = NoneÂ¶
Methods
init_state(machine, parameters, seed=None)Â¶

Creates the structure holding the state of the sampler.

If you want reproducible samples, you should specify seed, otherwise the state will be initialised randomly.

If running across several MPI processes, all sampler_states are guaranteed to be in a different (but deterministic) state. This is achieved by first reducing (summing) the seed provided to every MPI rank, then generating n_rank seeds starting from the reduced one, and every rank is initialized with one of those seeds.

The resulting state is guaranteed to be a frozen python dataclass (in particular, a flaxâ€™s dataclass), and it can be serialized using Flax serialization methods.

Parameters
• machine (Union[Callable, Module]) â€“ a Flax module or callable with the forward pass of the log-pdf.

• parameters (Any) â€“ The PyTree of parameters of the model.

• seed (Union[int, Any, None]) â€“ An optional seed or jax PRNGKey. If not specified, a random seed will be used.

Return type

SamplerState

Returns

The structure holding the state of the sampler. In general you should not expect it to be in a valid state, and should reset it before use.

log_pdf(model)Â¶

Returns a closure with the log_pdf function encoded by this sampler.

Note: the result is returned as an HashablePartial so that the closure does not trigger recompilation.

Parameters

model (Union[Callable, Module]) â€“ The machine, or apply_fun

Return type

Callable

Returns

the log probability density function

Returns a new object replacing the specified fields with new values.

reset(machine, parameters, state=None)Â¶

Resets the state of the sampler. To be used every time the parameters are changed.

Parameters
• machine (Union[Callable, Module]) â€“ a Flax module or callable with the forward pass of the log-pdf.

• parameters (Any) â€“ The PyTree of parameters of the model.

• state (Optional[SamplerState]) â€“ The current state of the sampler. If itâ€™s not provided, it will be constructed by calling sampler.init_state(machine, parameters) with a random seed.

Return type

SamplerState

Returns

A valid sampler state.

sample(machine, parameters, *, state=None, chain_length=1)Â¶

Samples chain_length elements along the chains.

Parameters
• sampler â€“ The Monte Carlo sampler.

• machine (Union[Callable, Module]) â€“ The model or callable to sample from (if itâ€™s a function it should have the signature f(parameters, Ïƒ) -> jnp.ndarray).

• parameters (Any) â€“ The PyTree of parameters of the model.

• state (Optional[SamplerState]) â€“ current state of the sampler. If None, then initialises it.

• chain_length (int) â€“ (default=1), the length of the chains.

Returns

The next batch of samples. state: The new state of the sampler

Return type

Ïƒ

sample_next(machine, parameters, state=None)Â¶

Samples the next state in the markov chain.

Parameters
• machine (Union[Callable, Module]) â€“ a Flax module or callable apply function with the forward pass of the log-pdf.

• parameters (Any) â€“ The PyTree of parameters of the model.

• state (Optional[SamplerState]) â€“ The current state of the sampler. If itâ€™s not provided, it will be constructed by calling sampler.reset(machine, parameters) with a random seed.

Returns

The new state of the sampler Ïƒ: The next batch of samples.

Return type

state