netket.driver.AbstractDriver#

class netket.driver.AbstractDriver[source]#

Bases: Pytree

Abstract base class for NetKet variational drivers.

This class must be inherited from in order to create a driver that immediately works with NetKet loggers and callback mechanism.

The run() method executes the driver loop and invokes AbstractCallback hooks at well-defined points during each step. To stop the loop early from a callback, raise StopRun. For a detailed description of the loop structure and the available callback hooks, see The Run Loop and Callback Hooks.

Note

How to implement a new driver

For a concrete example, look at the file netket/driver/vmc.py.

To implement a new optimization driver (with an optax optimizer), subclass AbstractOptimizationDriver instead.

To implement a new dynamics driver (time evolution), subclass AbstractDynamicsDriver instead.

If subclassing AbstractDriver directly, define:

Inheritance
Inheritance diagram of netket.driver.AbstractDriver
__init__(variational_state, minimized_quantity_name='loss')[source]#

Initializes a variational driver.

Parameters:
  • variational_state (VariationalState) – The variational state.

  • minimized_quantity_name (str) – the name of the loss function in the logged data set.

Attributes
state#

Returns the machine that is optimized by this driver.

step_count#

Returns a monotonic integer labelling all the steps performed by this driver. This can be used, for example, to identify the line in a log file.

Methods
compute_loss_and_update()[source]#
Return type:

Any

Performs a step of the optimization driver, returning the PyTree of the gradients that will be optimized.

Concrete drivers must override this method.

Note

When implementing this function on a subclass, you must return the gradient which must match the pytree structure of the parameters of the variational state.

The gradient will then be passed on to the optimizer in order to update the parameters.

Moreover, if you are minimising a loss function you must set the field self._loss_stats with the current value of the loss function.

This will be logged to any logger during optimisation.

Return type:

Any

Returns:

the update for the weights.

estimate(observables, fullsum=False)[source]#

Return MCMC statistics for the expectation value of observables in the current state of the driver.

Parameters:
  • observables – A pytree of operators for which statistics should be computed.

  • fullsum (bool)

Returns:

A pytree of the same structure as the input, containing MCMC statistics for the corresponding operators as leaves.

replace(**kwargs)[source]#

Replace the values of the fields of the object with the values of the keyword arguments. If the object is a dataclass, dataclasses.replace will be used. Otherwise, a new object will be created with the same type as the original object.

Return type:

TypeVar(P, bound= Pytree)

Parameters:
  • self (P)

  • kwargs (Any)

reset()[source]#

Deprecated since version 3.22: Use reset_step() to reset the sampler state at the beginning of a step. Note that the old reset() also reset step_count to 0; this behaviour is no longer supported.

reset_step(hard=False)[source]#

Resets the state of the driver at the beginning of a new step.

This method is called at the beginning of every step in the optimization.

Parameters:
  • hard (bool) – If True, the reset is a hard reset, resulting in a complete resampling even if resample_fraction

  • None. (is not)

run(n_iter, out=(), obs=None, step_size=1, show_progress=True, save_params_every=50, write_every=50, callback=None, timeit=False, _graceful_keyboard_interrupt=True)[source]#

Runs this variational driver, updating the weights of the network stored in this driver for n_iter steps and dumping values of the observables obs in the output logger.

It is possible to control more specifically what quantities are logged, when to stop the optimisation, or to execute arbitrary code at every step by specifying one or more callbacks, which are passed as a list of functions to the keyword argument callback.

Callbacks are functions that follow this signature:

def callback(step, log_data, driver) -> bool:
    ...
    return True/False

If a callback returns True, the optimisation continues, otherwise it is stopped. The log_data is a dictionary that can be modified in-place to change what is logged at every step. For example, this can be used to log additional quantities such as the acceptance rate of a sampler.

Alternatively, AbstractCallback subclasses can be used to hook into more stages of the loop. To stop the optimisation early from any callback hook, raise StopRun: the driver will catch it, finalise all callbacks via their on_run_end method, and return normally without propagating the exception.

Loggers are specified as an iterable passed to the keyword argument out. If only a string is specified, this will create by default a nk.logging.JsonLog. To know about the output format check its documentation. The logger object is also returned at the end of this function so that you can inspect the results without reading the json output.

Parameters:
  • n_iter (int) – the total number of iterations to be performed during this run.

  • out (Iterable[AbstractLog] | None) – A logger object, or an iterable of loggers, to be used to store simulation log and data. If this argument is a string, it will be used as output prefix for the standard JSON logger.

  • obs (dict[str, AbstractObservable] | None) – An iterable containing all observables that should be computed

  • step_size (int) – Every how many steps should observables be logged to disk (default=1)

  • callback (Union[Callable[[int, dict, AbstractDriver], bool], AbstractCallback, None]) – Callable or list of callable callback functions to stop training given a condition

  • show_progress (bool) – If true displays a progress bar (default=True)

  • save_params_every (int) – Every how many steps the parameters of the network should be serialized to disk (ignored if logger is provided)

  • write_every (int) – Every how many steps the json data should be flushed to disk (ignored if logger is provided)

  • timeit (bool) – If True, provide timing information.

  • _graceful_keyboard_interrupt (bool) – (Internal flag, defaults to True) If True, the driver will gracefully handle a KeyboardInterrupt, usually arising from doing ctrl-C, returning the current state of the simulation. If False, the KeyboardInterrupt will be raised as usual. This only has an effect when running in interactive mode.

update_parameters(dp)[source]#

Updates the parameters of the variational state.

Subclasses must override this method. Optimization drivers should apply the optimizer; dynamics drivers should apply the delta directly.

Parameters:

dp – the pytree containing the updates to the parameters.