# QNGOptimizer¶

Module: pennylane

class QNGOptimizer(stepsize=0.01, diag_approx=False)[source]

Optimizer with adaptive learning rate, via calculation of the diagonal or block-diagonal approximation to the Fubini-Study metric tensor. A quantum generalization of natural gradient descent.

The QNG optimizer uses a step- and parameter-dependent learning rate, with the learning rate dependent on the pseudo-inverse of the Fubini-Study metric tensor $$g$$:

$x^{(t+1)} = x^{(t)} - \eta g(f(x^{(t)}))^{-1} \nabla f(x^{(t)}),$

where $$f(x^{(t)}) = \langle 0 | U(x^{(t)})^\dagger \hat{B} U(x^{(t)}) | 0 \rangle$$ is an expectation value of some observable measured on the variational quantum circuit $$U(x^{(t)})$$.

Consider a quantum node represented by the variational quantum circuit

$U(\mathbf{\theta}) = W(\theta_{i+1}, \dots, \theta_{N})X(\theta_{i}) V(\theta_1, \dots, \theta_{i-1}),$

where all parametrized gates can be written of the form $$X(\theta_{i}) = e^{i\theta_i K_i}$$. That is, the gate $$K_i$$ is the generator of the parametrized operation $$X(\theta_i)$$ corresponding to the $$i$$-th parameter.

For each parametric layer $$\ell$$ in the variational quantum circuit containing $$n$$ parameters, the $$n\times n$$ block-diagonal submatrix of the Fubini-Study tensor $$g_{ij}^{(\ell)}$$ is calculated directly on the quantum device in a single evaluation:

$g_{ij}^{(\ell)} = \langle \psi_\ell | K_i K_j | \psi_\ell \rangle - \langle \psi_\ell | K_i | \psi_\ell\rangle \langle \psi_\ell |K_j | \psi_\ell\rangle$

where $$|\psi_\ell\rangle = V(\theta_1, \dots, \theta_{i-1})|0\rangle$$ (that is, $$|\psi_\ell\rangle$$ is the quantum state prior to the application of parameterized layer $$\ell$$).

Combining the quantum natural gradient optimizer with the analytic parameter-shift rule to optimize a variational circuit with $$d$$ parameters and $$L$$ layers, a total of $$2d+L$$ quantum evaluations are required per optimization step.

For more details, see:

James Stokes, Josh Izaac, Nathan Killoran, Giuseppe Carleo. “Quantum Natural Gradient.” arXiv:1909.02108, 2019.

Note

The QNG optimizer only supports single QNodes as objective functions.

In particular:

• For hybrid classical-quantum models, the “mixed geometry” of the model makes it unclear which metric should be used for which parameter. For example, parameters of quantum nodes are better suited to one metric (such as the QNG), whereas others (e.g., parameters of classical nodes) are likely better suited to another metric.
• For multi-QNode models, we don’t know what geometry is appropriate if a parameter is shared amongst several QNodes.

See the quantum natural gradient example for more details on Fubini-Study metric tensor and this optimization class.

Parameters: stepsize (float) – the user-defined hyperparameter $$\eta$$ diag_approx (bool) – If True, forces a diagonal approximation where the calculated metric tensor only contains diagonal elements $$G_{ii}$$. In some cases, this may reduce the time taken per optimization step. tol (float) – tolerance used when finding the inverse of the quantum gradient tensor
step(qnode, x, recompute_tensor=True)[source]

Update x with one step of the optimizer.

Parameters: qnode (QNode) – the QNode for optimization x (array) – NumPy array containing the current values of the variables to be updated recompute_tensor (bool) – Whether or not the metric tensor should be recomputed. If not, the metric tensor from the previous optimization step is used. the new variable values $$x^{(t+1)}$$ array
apply_grad(grad, x)[source]

Update the variables x to take a single optimization step. Flattens and unflattens the inputs to maintain nested iterables as the parameters of the optimization.

Parameters: grad (array) – The gradient of the objective function at point $$x^{(t)}$$: $$\nabla f(x^{(t)})$$ x (array) – the current value of the variables $$x^{(t)}$$ the new values $$x^{(t+1)}$$ array