Paper commentary
PennyLane: Automatic differentiation of hybrid quantum-classical computations
PennyLane lets gradients flow through hybrid quantum-classical computations
- Problem Before PennyLane, no single tool optimized hybrid quantum-classical models across quantum devices and machine learning libraries. Most tools were built for one side or the other.
- Method PennyLane makes quantum nodes differentiable, so gradients flow through whole hybrid computations. It picks the gradient method the device supports, such as backpropagation or parameter-shift rules.
- Results The authors report that adjoint differentiation on lightning.qubit cuts time-to-solution compared to other simulators and gradient methods.
Why variational circuits sit outside classical training
Variational circuits are parameter-dependent quantum circuits that a classical computer tunes to optimize an objective. Much of this work is numerical, since the heuristic algorithms are rarely analysed rigorously. Some frameworks were built with variational circuits in mind, but that was not the norm when PennyLane launched in 2018. No tool treated all quantum devices on the same footing while also plugging into machine learning tooling.
So the authors set out to extend libraries such as Autograd, TensorFlow, PyTorch and JAX to handle quantum modules.
Quantum nodes inside a differentiable graph
The core idea is to treat each quantum circuit as a node in a directed acyclic graph, alongside classical numerical nodes. A cost is computed by passing values along this graph, and gradients are accumulated by traversing it with automatic differentiation rules. This means a gradient can reach a variable that sits inside a quantum circuit, which is what gradient descent needs.
Classical nodes can run any numerical computation, as long as it is built from differentiable functions. A quantum node, by contrast, is a function that runs on a device and returns expectation values. That device can be real quantum hardware or a classical simulator.
The circuit inside each quantum node has three stages. It starts from the zero state, applies gates whose parameters come from inputs or trainable variables, and then measures observables. Inputs are encoded as gate parameters that are not trained, while variables are the parameters the optimizer adjusts. Each output is \( f_i(x;\theta)=\langle \hat B_i\rangle=\langle 0|U(x;\theta)^\dagger \hat B_i U(x;\theta)|0\rangle \), the expectation of an observable in the final state.
On hardware, these expectation values are estimated by averaging measurements over R runs of the circuit. Simulators can instead compute the exact value, and this is the default when shots=None. The estimate is unbiased, and its spread depends on the variance of the observable and on the number of shots R.
Picking a gradient method per quantum node
A quantum node's derivative can come from several sources, and PennyLane chooses among them with heuristics. The aim is the most accurate and efficient method that both the circuit and the device support. The first choice is whatever gradient method the device itself provides. If the device computes exact expectations and supports backpropagation, backpropagation comes next.
On hardware, or when shots are used, parameter-shift rules are the next best choice.
Finite differences are the last resort, used when the circuit doesn't allow analytic hardware gradients. The order keeps exact or device-supplied derivatives ahead of approximations. For example, a simulator that supports the adjoint method gets that method first, since device-provided methods come first.
Parameter-shift rules give exact derivatives
For analytic derivatives, PennyLane runs the circuit several times at shifted parameter values and forms a linear combination of the results. This works for single-parameter qubit gates whose Hermitian generator has an equidistant frequency spectrum, which covers the common qubit rotation gates. It also covers continuous-variable circuits built from Gaussian operations.
The rule reads \( \partial_\mu f(\mu)=\sum_{i=1}^{r} c_i\, f(\mu+s_i) \). Here r counts the unique differences in the eigenvalue spectrum of the gate, s_i are the shift values, and c_i are the coefficients. Unlike finite-difference formulas, the shifts are finite, so the result is exact rather than an approximation.
The shifts and coefficients are not fixed, so they can be chosen and then computed as needed. PennyLane picks shifts that are equidistant with respect to the gate's period, which the authors say minimizes variance. The result is still built from measured expectation values, so device noise and statistical error carry through.
Finite differences as the fallback
Numerical methods need only black-box evaluations of the node's output near the current value. The forward method uses \( \partial_\mu f(\mu)\approx \frac{f(\mu+\Delta\mu)-f(\mu)}{\Delta\mu} \). The choice of \( \Delta\mu \) is a trade-off, and it matters more on noisy hardware.
Numerical derivatives approximate the true derivative even when the node returns exact expectations. They are used only when the circuit doesn't permit analytic hardware gradients.
Backpropagation and device-provided derivatives
A simulator written with TensorFlow, PyTorch or JAX can use their backpropagation to compute derivatives. The intermediate values needed for the derivative are stored during the forward circuit evaluation and then reused. This may save time compared with the analytic method on simulators, since it adds only constant overhead.
A device with its own custom gradient formula can expose that formula directly, and PennyLane can use it. Higher-order derivatives come from applying the chain and product rules repeatedly to the gradient transforms. Repeated terms are merged using the gate periods, which avoids redundant device evaluations.
Optimizing a single qubit rotation
The task rotates one qubit with RX and then RY so that it moves from |0> toward |1>. After the rotations the state is \( |\psi\rangle = R_y(\varphi_2)R_x(\varphi_1)|0\rangle \), and the measured output is \( f(\varphi_1,\varphi_2)=\cos(\varphi_1)\cos(\varphi_2) \).
The code declares a one-wire default.qubit device and a quantum node that applies RX and RY to that wire. The node returns the expectation value of PauliZ on that wire, and the optimizer uses a step size of 0.25. A loop calls step_and_cost for 30 iterations and prints the cost at each step. The excerpt does not report the values that this loop prints.
A quantum function must apply operations and return measurement statistics, and several observables are returned together as a tuple. The decorator can take an interface keyword, so the QNode can accept, for example, Torch or TensorFlow tensors. Creating the QNode by calling qml.QNode directly, without the decorator, is an alternative.
Built-in simulators and when adjoint differentiation helps
PennyLane ships five simulator devices, which differ mainly in backend and in the circuit size they suit. default.qubit is a statevector simulator with NumPy, TensorFlow, PyTorch and JAX backends that supports end-to-end backpropagation. Because backpropagation carries memory overhead, default.qubit works best for 0-20 qubits.
lightning.qubit is a C++ statevector simulator that supports adjoint differentiation. It is aimed at quantum nodes with 20 or more qubits. Its adjoint implementation runs in parallel over the requested observables, which matters when a workload evaluates many observables.
lightning.gpu offloads gate calls to NVIDIA's cuQuantum SDK and is presented as an extension of lightning.qubit. The authors report that adjoint differentiation on lightning.qubit cuts time-to-solution compared to other simulators and gradient methods. The excerpt gives no timing numbers or benchmark circuits for this comparison.
A device integration test checks that a device applies and measures operations correctly. Gradient formulas don't need separate device testing, because PennyLane defines and tests them. The example run is pl-device-test against default.qubit with 10000 shots, skipping unsupported operations.
Limitations
Analytic derivatives are limited by device noise and by the statistical imprecision of averaging measurements. This limits accuracy only, since analytic derivatives avoid numerical issues. Finite differences differ here, because they approximate the derivative even when expectations are exact.
Further derivative extensions, such as stochastic parameter-shift rules and a Hadamard test-based approach, are described. They are not currently implemented in PennyLane. The speed claim for lightning.qubit has no timing numbers in the excerpt.
Paper info
- Authors
- Bergholm, Ville, Izaac, Josh, Schuld, Maria, Gogolin, Christian, Ahmed, Shahnawaz, Ajith, Vishnu, Alam, M. Sohaib, Alonso-Linaje, Guillermo, AkashNarayanan, B., Asadi, Ali, Arrazola, Juan Miguel, Azad, Utkarsh, Banning, Sam, Blank, Carsten, Bromley, Thomas R, Cordier, Benjamin A., Ceroni, Jack, Delgado, Alain, Di Matteo, Olivia, Dusko, Amintor, Garg, Tanya, Guala, Diego, Hayes, Anthony, Hill, Ryan, Ijaz, Aroosa, Isacsson, Theodor, Ittah, David, Jahangiri, Soran, Jain, Prateek, Jiang, Edward, Khandelwal, Ankit, Kottmann, Korbinian, Lang, Robert A., Lee, Christina, Loke, Thomas, Lowe, Angus, McKiernan, Keri, Meyer, Johannes Jakob, Montañez-Barrera, J. A., Moyard, Romain, Niu, Zeyue, O'Riordan, Lee James, Oud, Steven, Panigrahi, Ashish, Park, Chae-Yeun, Polatajko, Daniel, Quesada, Nicolás, Roberts, Chase, Sá, Nahum, Schoch, Isidor, Shi, Borun, Shu, Shuli, Sim, Sukin, Singh, Arshpreet, Strandberg, Ingrid, Soni, Jay, Száva, Antal, Thabet, Slimane, Vargas-Hernández, Rodrigo A., Vincent, Trevor, Vitucci, Nicola, Weber, Maurice, Wierichs, David, Wiersema, Roeland, Willmann, Moritz, Wong, Vincent, Zhang, Shaoming, Killoran, Nathan
- Venue
- arXiv · Preprint
- First published
- 2018-11-12
- Added
- 2026-11-12
- Updated
- 2022-7-29
- Version
- v4
- arXiv
- 1811.04968
- Category
- quant-ph · cs.ET · cs.LG · physics.comp-ph
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Bergholm, Ville, Izaac, Josh, Schuld, Maria, Gogolin, Christian, Ahmed, Shahnawaz, Ajith, Vishnu, Alam, M. Sohaib, Alonso-Linaje, Guillermo, AkashNarayanan, B., Asadi, Ali, Arrazola, Juan Miguel, Azad, Utkarsh, Banning, Sam, Blank, Carsten, Bromley, Thomas R, Cordier, Benjamin A., Ceroni, Jack, Delgado, Alain, Di Matteo, Olivia, Dusko, Amintor et al. (2018). PennyLane: Automatic differentiation of hybrid quantum-classical computations. arXiv:1811.04968v4. https://arxiv.org/abs/1811.04968v4
BibTeX
@misc{ville2018pennylane,
title = {{PennyLane: Automatic differentiation of hybrid quantum-classical computations}},
author = {Bergholm, Ville and Izaac, Josh and Schuld, Maria and Gogolin, Christian and Ahmed, Shahnawaz and Ajith, Vishnu and Alam, M. Sohaib and Alonso-Linaje, Guillermo and AkashNarayanan, B. and Asadi, Ali and Arrazola, Juan Miguel and Azad, Utkarsh and Banning, Sam and Blank, Carsten and Bromley, Thomas R and Cordier, Benjamin A. and Ceroni, Jack and Delgado, Alain and Di Matteo, Olivia and Dusko, Amintor and Garg, Tanya and Guala, Diego and Hayes, Anthony and Hill, Ryan and Ijaz, Aroosa and Isacsson, Theodor and Ittah, David and Jahangiri, Soran and Jain, Prateek and Jiang, Edward and Khandelwal, Ankit and Kottmann, Korbinian and Lang, Robert A. and Lee, Christina and Loke, Thomas and Lowe, Angus and McKiernan, Keri and Meyer, Johannes Jakob and Montañez-Barrera, J. A. and Moyard, Romain and Niu, Zeyue and O'Riordan, Lee James and Oud, Steven and Panigrahi, Ashish and Park, Chae-Yeun and Polatajko, Daniel and Quesada, Nicolás and Roberts, Chase and Sá, Nahum and Schoch, Isidor and Shi, Borun and Shu, Shuli and Sim, Sukin and Singh, Arshpreet and Strandberg, Ingrid and Soni, Jay and Száva, Antal and Thabet, Slimane and Vargas-Hernández, Rodrigo A. and Vincent, Trevor and Vitucci, Nicola and Weber, Maurice and Wierichs, David and Wiersema, Roeland and Willmann, Moritz and Wong, Vincent and Zhang, Shaoming and Killoran, Nathan},
year = {2018},
eprint = {1811.04968},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1811.04968v4}
}