# Variational Circuits¶

*Section author: Maria Schuld <maria@xanadu.ai>*

Variational circuits (also called *parametrized quantum circuits*) are a family of hybrid quantum-classical algorithms [4]. At the core of the algorithm is a *quantum circuit* \(U\) which depends on a set of circuit parameters \(\theta\), as well as an *objective function* which defines a scalar score for each set of parameters. The goal is to optimise or train the circuit with respect to the objective.

Typically, variational circuits are trained by a classical optimization algorithm that makes queries to the quantum device. The optimization is usually an iterative scheme that finds better candidates for the parameters \(\theta\) in every step, starting with either random or pre-trained initial parameters.

When running a StrawberryFields simulation one can use *numerical* or *automatic* strategies to update the parameters in each step.

## Numerical training¶

Numerical training methods use the quantum circuit as a blackbox. There are basically two subgroups of these methods:

### Derivative-free optimization¶

Derivative-free training methods do not compute gradients to determine the search direction for the parameter update.

Examples of such methods are

- The Nelder-Mead or simplex method
- Evolutionary algorithms
- Particle swarm optimization

Most derivative-free methods treat the objective and thereby the variational circuit as a black-box to query. They are hence easy to use, but not always numerically stable. Especially for large sets of parameters, one might be better of with gradient-based methods.

### Gradient-based optimization¶

Gradient-based training methods take information on the direction of steepest descent in the objective function into account, but at the cost of having to compute derivatives of the objective, and thereby of the variational circuit outputs.

Numerical gradient-based methods compute approximations to the gradients. Usually this is done by the finite-differences method. The value of the objective function is evaluated at two positions close to the current circuit parameters \(\theta\).

More precisely, let \(\mu \in \theta\) be the parameter that we want to update, and let \(C(\mu)\) be the objective function that depends on \(\mu\). The approximation of the gradient is then given by

One problem is that \(\Delta \mu\) has to be as small as possible for good approximations, but still large enough to make the finite difference larger than the error in estimating \(C(\mu)\), which can be quite significant in current quantum technologies.

Note

The finite-difference method can in principle also be used if the simulation is replaced by quantum hardware.

## Automatic training¶

Automatic training is based on automatic differentiation, which is a programming technique that associates each computational operation with a gradient taken with respect to some user-defined variables. Whatever computation is performed, one can automatically compute the gradient of the result. This is very useful for quantum machine learning, because it allows us to retrieve the analytic derivative of a variational circuit output without finding gradients on paper for each circuit architecture, and then implementing them as a training algorithm.

A popular framework that supports automatic differentiation is TensorFlow. The `'tf'`

backend of StrawberryFields is coded in TensorFlow and when a circuit is run with this backend (and in the `run()`

method for the engine we set `evaluation=False`

), the result of the circuit are tensors that have gradients associated with them. The machine learning toolbox uses this to build machine learning models.

Note

Automatic differentiation is a feature of simulations only, and has no equivalent when using real quantum hardware!