qml.MomentumQNGOptimizerQJIT¶
- class MomentumQNGOptimizerQJIT(stepsize=0.01, momentum=0.9, approx='block-diag', lam=0)[source]¶
Bases:
QNGOptimizerQJIT
Optax-like and
jax.jit
/qml.qjit
-compatible implementation of theMomentumQNGOptimizer
, a generalized Quantum Natural Gradient (QNG) optimizer considering a discrete-time Langevin equation with QNG force.For more theoretical details, see the
MomentumQNGOptimizer
documentation.Note
Please be aware of the following:
As with
MomentumQNGOptimizer
,MomentumQNGOptimizerQJIT
supports a single QNode to encode the objective function.MomentumQNGOptimizerQJIT
does not support any QNode with multiple arguments. A potential workaround would be to combine all parameters into a single objective function argument.MomentumQNGOptimizerQJIT
does not work correctly if there is any classical processing in the QNode circuit (e.g.,2 * theta
as a gate parameter).
- Parameters:
stepsize (float) – the stepsize hyperparameter (default value: 0.01).
momentum (float) – the momentum coefficient hyperparameter (default value: 0.9).
approx (str) –
approximation method for the metric tensor (default value: “block-diag”).
If
None
, the full metric tensor is computedIf
"block-diag"
, the block-diagonal approximation is computed, reducing the number of evaluated circuits significantlyIf
"diag"
, the diagonal approximation is computed, slightly reducing the classical overhead but not the quantum resources (compared to"block-diag"
)
lam (float) – metric tensor regularization to be applied at each optimization step (default value: 0).
Example:
Consider a hybrid workflow to optimize an objective function defined by a quantum circuit. To make the entire workflow faster, the update step and the whole optimization can be just-in-time compiled using the
qjit()
decorator:import pennylane as qml import jax.numpy as jnp dev = qml.device("lightning.qubit", wires=2) @qml.qnode(dev) def circuit(params): qml.RX(params[0], wires=0) qml.RY(params[1], wires=1) return qml.expval(qml.Z(0) + qml.X(1)) opt = qml.MomentumQNGOptimizerQJIT(stepsize=0.1, momentum=0.2) @qml.qjit def update_step_qjit(i, args): params, state = args return opt.step(circuit, params, state) @qml.qjit def optimization_qjit(params, iters): state = opt.init(params) args = (params, state) params, state = qml.for_loop(iters)(update_step_qjit)(args) return params
>>> params = jnp.array([0.1, 0.2]) >>> iters = 1000 >>> optimization_qjit(params=params, iters=iters) Array([ 3.14159265, -1.57079633], dtype=float64)
Make sure you are using the
lightning.qubit
device along withqml.qjit
.Methods
init
(params)Return the initial state of the optimizer.
step
(qnode, params, state, **kwargs)Update the QNode parameters and the optimizer's state for a single optimization step.
step_and_cost
(qnode, params, state, **kwargs)Update the QNode parameters and the optimizer's state for a single optimization step and return the corresponding objective function value prior to the step.
- init(params)[source]¶
Return the initial state of the optimizer. This state is always initialized as an array of zeros with the same shape and type of the given array of parameters.
- Parameters:
params (array) – QNode parameters
- Returns:
initial state of the optimizer
- Return type:
array
- step(qnode, params, state, **kwargs)¶
Update the QNode parameters and the optimizer’s state for a single optimization step.
- Parameters:
qnode (QNode) – QNode objective function to be optimized
params (array) – QNode parameters to be updated
state – current state of the optimizer
**kwargs – variable-length keyword arguments for the QNode
- Returns:
(new parameters values, new optimizer’s state)
- Return type:
tuple
- step_and_cost(qnode, params, state, **kwargs)¶
Update the QNode parameters and the optimizer’s state for a single optimization step and return the corresponding objective function value prior to the step.
- Parameters:
qnode (QNode) – QNode objective function to be optimized
params (array) – QNode parameters to be updated
state – current state of the optimizer
**kwargs – variable-length keyword arguments for the QNode
- Returns:
(new parameters values, new optimizer’s state, objective function value)
- Return type:
tuple