mm.training.Optimizer¶
- class mrmustard.training.Optimizer(symplectic_lr=0.1, unitary_lr=0.1, orthogonal_lr=0.1, euclidean_lr=0.001)[source]¶
Bases:
object
An optimizer for any parametrized object: it can optimize euclidean, orthogonal and symplectic parameters.
Note
In the future it will also include a compiler, so that it will be possible to simplify the circuit/detector/gate/etc before the optimization and also compile other types of structures like error correcting codes and encoders/decoders.
Methods
apply_gradients
(trainable_params, grads)Apply gradients to variables.
compute_loss_and_gradients
(cost_fn, parameters)Uses the backend to compute the loss and gradients of the parameters given a cost function.
minimize
(cost_fn, by_optimizing[, ...])Minimizes the given cost function by optimizing circuits and/or detectors.
should_stop
(max_steps)Returns
True
if the optimization should stop (either because the loss is stable or because the maximum number of steps is reached).- apply_gradients(trainable_params, grads)[source]¶
Apply gradients to variables.
This method group parameters by variable type (euclidean, symplectic, orthogonal) and applies the corresponding update method for each variable type. Update methods are registered on
parameter_update
module.
- static compute_loss_and_gradients(cost_fn, parameters)[source]¶
Uses the backend to compute the loss and gradients of the parameters given a cost function.
This functions is a wrapper around the backend optimizer to extract tensors from parameters and correctly compute the loss and gradients. Results of the calculation are associated back again with the given parameters.
- Parameters:
cost_fn (Callable with no args) – The cost function.
parameters (List[Variable]) – The variables to optimize.
- Returns:
The loss and the gradients.
- Return type:
tuple(Tensor, List[Tensor])
- minimize(cost_fn, by_optimizing, max_steps=1000, callbacks=None)[source]¶
Minimizes the given cost function by optimizing circuits and/or detectors.
- Parameters:
cost_fn (Callable) – a function that will be executed in a differentiable context in order to compute gradients as needed
by_optimizing (list of circuits and/or detectors and/or gates) – a list of elements that contain the parameters to optimize
max_steps (int) – the minimization keeps going until the loss is stable or max_steps are reached (if
max_steps=0
it will only stop when the loss is stable)callbacks (
Callback
, Callable, or List/Dict of them) – callback functions that will be executed at each step of the optimization after backprop but before gradient gets applied. It takes as arguments the optimizer itself, training step (int), the cost value, the cost function, and the trainable parameters (values & grads) dict. The optional returned dict for each step is stored in self.callback_history which is a callback-name-keyed dict with each value a list of such callback result dicts. Learn more about how to use callbacks to have finer control of the optimization process in thecallbacks
module.