Lesson 40: PennyLane and Differentiable Quantum Programming¶
40.1 Learning Outcomes¶
By the end of this hour a student will be able to:
- Build QNodes and understand PennyLane's device abstraction.
- Compute quantum gradients with
qml.gradand select a differentiation method. - Integrate quantum circuits into PyTorch, TensorFlow and JAX models.
- Use PennyLane's built-in templates for encodings and ansätze.
- Train a complete variational model end to end.
40.2 PennyLane's Core Idea¶
A quantum circuit is a differentiable function.
PennyLane treats a circuit exactly as PyTorch treats a neural-network layer: a function whose gradient with respect to its parameters can be computed automatically and composed with classical layers. This makes it the framework for quantum machine learning.
40.3 Devices and QNodes¶
Installation¶
pip install pennylane
pip install pennylane-lightning # fast C++ simulator
pip install pennylane-qiskit # run on IBM hardware
The device¶
import pennylane as qml
from pennylane import numpy as np # a thin autograd-aware wrapper
dev = qml.device("default.qubit", wires=2) # pure-Python statevector
dev = qml.device("lightning.qubit", wires=20) # C++ -- much faster
dev = qml.device("default.mixed", wires=2) # density matrix (noise)
dev = qml.device("qiskit.aer", wires=2, shots=1024) # via a plugin
Wires are PennyLane's qubits; they may be integers or strings (wires=['alice','bob']).
shots=None (the default) gives exact analytic expectation values — enormously useful for
debugging, since you separate algorithmic bugs from shot noise.
The QNode¶
@qml.qnode(dev)
def circuit(params):
qml.RY(params[0], wires=0)
qml.RY(params[1], wires=1)
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(0))
print(circuit([0.5, 0.3])) # 0.8775825618903728
A QNode is just a Python function. Call it, differentiate it, put it inside a loop, pass it to an optimiser — it behaves like any other numerical function.
Return types¶
| Return | Meaning |
|---|---|
qml.expval(qml.PauliZ(0)) |
expectation value \(\langle Z_0\rangle\) |
qml.var(qml.PauliZ(0)) |
variance |
qml.probs(wires=[0,1]) |
full probability vector |
qml.sample(qml.PauliZ(0)) |
individual shot outcomes (needs shots) |
qml.state() |
the full statevector (analytic only) |
qml.density_matrix(wires=[0]) |
reduced density matrix |
[qml.expval(qml.PauliZ(i)) for i in range(n)] |
a list of expectations |
40.4 Differentiation — the Heart of PennyLane¶
Basic gradients¶
@qml.qnode(dev)
def circuit(theta):
qml.RX(theta, wires=0)
return qml.expval(qml.PauliZ(0))
grad_fn = qml.grad(circuit)
theta = np.array(0.5, requires_grad=True)
print(circuit(theta)) # 0.8775825618903728 = cos(0.5)
print(grad_fn(theta)) # -0.479425538604203 = -sin(0.5)
Analytically exact — computed by the parameter-shift rule (L29), not by finite differences.
Differentiation methods¶
@qml.qnode(dev, diff_method="parameter-shift")
def circuit(x): ...
| Method | How it works | Circuits per gradient | Works on hardware? |
|---|---|---|---|
"backprop" |
classical autodiff through the simulator | 1 | no — simulator only |
"adjoint" |
reverse-mode statevector trick | ~2 | no — simulator only |
"parameter-shift" |
analytic shift rule (L29) | \(2P\) | yes |
"finite-diff" |
numerical differences | \(P+1\) | yes (but inaccurate) |
"spsa" |
stochastic approximation | 2 | yes |
The rule of thumb.
"adjoint" is the fastest exact method on a simulator: it computes the gradient for all parameters
in roughly the cost of two forward passes, regardless of \(P\) — the quantum analogue of
backpropagation, available only because a simulator can access the state that hardware cannot.
Higher-order derivatives¶
hessian_fn = qml.jacobian(qml.grad(circuit))
Useful for natural-gradient and second-order optimisers.
Quantum natural gradient¶
opt = qml.QNGOptimizer(stepsize=0.1)
params = opt.step(cost, params)
Instead of plain gradient descent, precondition by the Fubini–Study metric tensor \(g\):
The metric accounts for the fact that equal parameter changes do not produce equal state changes. QNG typically converges in 2–5× fewer iterations than vanilla gradient descent, at the cost of estimating \(g\) (which requires \(O(P^2)\) extra circuits, or \(O(P)\) in the block-diagonal approximation).
40.5 Integration with ML Frameworks¶
PyTorch¶
import torch, pennylane as qml
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev, interface="torch", diff_method="backprop")
def qlayer(inputs, weights):
qml.AngleEmbedding(inputs, wires=[0, 1])
qml.BasicEntanglerLayers(weights, wires=[0, 1])
return [qml.expval(qml.PauliZ(i)) for i in range(2)]
weight_shapes = {"weights": (3, 2)} # 3 layers, 2 wires
qnn = qml.qnn.TorchLayer(qlayer, weight_shapes)
model = torch.nn.Sequential(
torch.nn.Linear(4, 2),
qnn, # a QUANTUM layer, mid-network
torch.nn.Linear(2, 1),
)
opt = torch.optim.Adam(model.parameters(), lr=0.01)
loss_fn = torch.nn.MSELoss()
for epoch in range(100):
opt.zero_grad()
loss = loss_fn(model(X), y)
loss.backward() # gradients flow THROUGH the quantum layer
opt.step()
This is PennyLane's decisive advantage. The quantum circuit is a
torch.nn.Module; gradients flow through it by the same.backward()call that handles the classical layers. Nothing else in the ecosystem is this seamless.
TensorFlow / Keras and JAX¶
@qml.qnode(dev, interface="tf")
def circuit(x, w): ...
klayer = qml.qnn.KerasLayer(circuit, weight_shapes, output_dim=2)
@qml.qnode(dev, interface="jax")
def circuit(x): ...
grad = jax.grad(circuit)
fast = jax.jit(circuit) # JIT compilation
vmapped = jax.vmap(circuit) # batch over inputs
JAX + PennyLane is the fastest combination for simulator-based QML, because jit compiles the
whole hybrid computation and vmap batches circuit evaluations.
40.6 Templates¶
PennyLane ships pre-built, tested circuit blocks — use them rather than hand-rolling.
Embeddings (L26)¶
qml.AngleEmbedding(features, wires, rotation='Y') # angle encoding
qml.AmplitudeEmbedding(features, wires, normalize=True)
qml.BasisEmbedding(binary_features, wires)
qml.IQPEmbedding(features, wires, n_repeats=2) # entangling, ZZ-style
qml.QAOAEmbedding(features, weights, wires) # trainable encoding
Ansätze (L28)¶
qml.BasicEntanglerLayers(weights, wires) # Ry + CNOT ring
qml.StronglyEntanglingLayers(weights, wires) # Rot + all-to-all CNOTs
qml.SimplifiedTwoDesign(init_weights, weights, wires)
qml.RandomLayers(weights, wires)
qml.ParticleConservingU2(weights, wires) # chemistry
shape = qml.StronglyEntanglingLayers.shape(n_layers=3, n_wires=4)
print(shape) # (3, 4, 3) -> 36 parameters
Subroutines¶
qml.QFT(wires=range(4))
qml.GroverOperator(wires=range(3))
qml.AmplitudeAmplification(...)
qml.QuantumPhaseEstimation(unitary, target_wires, estimation_wires)
qml.TrotterProduct(hamiltonian, time, n=10, order=2)
40.7 Solved Problems¶
Problem 1 — Gradient by hand and by PennyLane¶
For circuit(theta) = <Z> after RY(theta) on \(|0\rangle\), derive \(f(\theta)\) and \(f'(\theta)\)
analytically, then verify with PennyLane at \(\theta = 0.7\).
Solution.
Analytic derivation.
At \(\theta = 0.7\):
PennyLane.
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev, diff_method="parameter-shift")
def circuit(theta):
qml.RY(theta, wires=0)
return qml.expval(qml.PauliZ(0))
theta = np.array(0.7, requires_grad=True)
print("value :", circuit(theta)) # 0.7648421872844885
print("gradient:", qml.grad(circuit)(theta)) # -0.6442176872376912
Exact agreement to machine precision ✓.
Verify the parameter-shift rule by hand (L29):
The rule is exact, using two evaluations of the same circuit at shifted parameters.
Problem 2 — Comparing differentiation methods¶
A 6-qubit circuit has 36 parameters. Compare the circuit evaluations per gradient for each differentiation method, and give runtimes at 10 ms per circuit.
Solution.
| Method | Formula | Evaluations | Time @ 10 ms |
|---|---|---|---|
backprop |
1 forward pass | 1 | 0.01 s |
adjoint |
~2 passes | 2 | 0.02 s |
parameter-shift |
\(2P\) | 72 | 0.72 s |
finite-diff |
\(P+1\) | 37 | 0.37 s |
spsa |
2 (stochastic) | 2 | 0.02 s |
Over 200 training iterations.
| Method | Total time |
|---|---|
| backprop | 2 s |
| adjoint | 4 s |
| parameter-shift | 144 s |
| spsa | 4 s |
But the methods are not interchangeable.
backpropandadjointdo not work on hardware. They require access to the internal statevector, which a physical device cannot provide (measurement destroys it).finite-diffis inaccurate under shot noise (L29: \(O(S^{-1/3})\) versus \(O(S^{-1/2})\)).spsais cheap but stochastic, needing more iterations to converge — perhaps 3–5× more, so the real comparison against parameter-shift is \(5\times2 = 10\) versus \(72\) evaluations. SPSA still wins by ~7×.
The professional workflow.
1. Develop and debug on a simulator with diff_method="backprop" (fast, exact)
2. Validate with diff_method="parameter-shift", shots=None (same as hardware, no noise)
3. Add shots to check statistical robustness
4. Deploy to hardware with diff_method="spsa" or "parameter-shift"
Problem 3 — Complete VQC training run¶
Train a 2-qubit variational classifier on the XOR dataset and report the results.
Solution.
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev, diff_method="backprop")
def circuit(weights, x):
qml.AngleEmbedding(x, wires=[0, 1], rotation='Y')
qml.IQPEmbedding(x, wires=[0, 1], n_repeats=1) # entangling -> can express XOR
qml.StronglyEntanglingLayers(weights, wires=[0, 1])
return qml.expval(qml.PauliZ(0))
def variational(weights, bias, x):
return circuit(weights, x) + bias
X = np.array([[0., 0.], [0., np.pi], [np.pi, 0.], [np.pi, np.pi]], requires_grad=False)
Y = np.array([-1., 1., 1., -1.], requires_grad=False) # XOR labels
def cost(weights, bias):
preds = [variational(weights, bias, x) for x in X]
return np.mean((np.stack(preds) - Y) ** 2)
shape = qml.StronglyEntanglingLayers.shape(n_layers=3, n_wires=2)
weights = 0.01 * np.random.randn(*shape, requires_grad=True)
bias = np.array(0.0, requires_grad=True)
opt = qml.AdamOptimizer(stepsize=0.1)
for it in range(150):
weights, bias = opt.step(cost, weights, bias)
if it % 30 == 0:
print(f"iter {it:3d} cost = {cost(weights, bias):.6f}")
preds = [np.sign(variational(weights, bias, x)) for x in X]
acc = np.mean(np.array(preds) == Y)
print("predictions:", preds, " accuracy:", acc)
Typical output.
iter 0 cost = 1.003217
iter 30 cost = 0.312455
iter 60 cost = 0.068912
iter 90 cost = 0.014237
iter 120 cost = 0.003891
predictions: [-1.0, 1.0, 1.0, -1.0] accuracy: 1.0
Analysis.
- Parameters: \(3\text{ layers}\times2\text{ wires}\times3\text{ rotations} = \mathbf{18}\), plus the bias \(= 19\).
- Convergence: cost falls from 1.0 (random) to 0.004 in 150 iterations.
- Accuracy: 100 % on all four XOR points.
The critical design point. AngleEmbedding alone is a product feature map and cannot
represent XOR (L27, Problem 5) — the model would plateau at 50 % accuracy no matter how many layers
are added. Adding IQPEmbedding, which contains \(Z\otimes Z\) terms, introduces the feature
interaction \(x_1x_2\) that XOR requires.
Honest baseline. sklearn.svm.SVC(kernel='rbf') also achieves 100 % on XOR — in about 1 ms,
versus several seconds for the quantum model. This exercise teaches the QML workflow; it does not
demonstrate advantage.
Problem 4 — A hybrid PyTorch model¶
Build a hybrid classical–quantum network: a linear layer, then a 4-qubit quantum layer, then a linear output. Count the parameters and describe the gradient flow.
Solution.
import torch, pennylane as qml
n_qubits, n_layers = 4, 2
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev, interface="torch", diff_method="backprop")
def qlayer(inputs, weights):
qml.AngleEmbedding(inputs, wires=range(n_qubits))
qml.StronglyEntanglingLayers(weights, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
weight_shapes = {"weights": (n_layers, n_qubits, 3)}
qnn = qml.qnn.TorchLayer(qlayer, weight_shapes)
model = torch.nn.Sequential(
torch.nn.Linear(8, n_qubits), # classical: 8 features -> 4
torch.nn.Tanh(), # squash into [-1, 1] for angle encoding
qnn, # QUANTUM: 4 -> 4
torch.nn.Linear(n_qubits, 2), # classical: 4 -> 2 classes
)
print(sum(p.numel() for p in model.parameters()), "trainable parameters")
Parameter count.
| Layer | Parameters |
|---|---|
Linear(8, 4) |
\(8\times4 + 4 = 36\) |
Tanh |
0 |
| Quantum layer | \(2\times4\times3 = \mathbf{24}\) |
Linear(4, 2) |
\(4\times2 + 2 = 10\) |
| Total | 70 |
Gradient flow, step by step.
loss.backward()is called on the classical loss.- PyTorch's autograd reaches the final
Linearlayer and computes \(\partial L/\partial\text{(its inputs)}\). - It then reaches
TorchLayer, which implements a custombackward(). TorchLayerasks PennyLane for \(\partial\langle Z_i\rangle/\partial w_j\) — usingbackprophere (simulator), or the parameter-shift rule on hardware.- The chain rule combines them, and the gradient continues into the first
Linearlayer. optimizer.step()updates all 70 parameters together — classical and quantum alike.
The design detail that matters. Tanh before the quantum layer bounds the inputs to \([-1,1]\),
which maps to Bloch angles in \([-1,1]\) radians. Without it, an unbounded linear output could
produce angles of, say, 47 radians, which wrap around the Bloch sphere many times — creating a
highly oscillatory, essentially untrainable loss landscape. Always bound the inputs to an angle
encoding.
Problem 5 — Barren plateaus, observed directly¶
Use PennyLane to measure gradient variance as a function of qubit count for a random circuit, and compare with the \(O(2^{-n})\) theoretical prediction (L29).
Solution.
import pennylane as qml
from pennylane import numpy as np
def grad_variance(n_qubits, n_layers=10, n_samples=100):
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev, diff_method="backprop")
def circuit(weights):
qml.StronglyEntanglingLayers(weights, wires=range(n_qubits))
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)) # LOCAL observable
shape = qml.StronglyEntanglingLayers.shape(n_layers, n_qubits)
grads = []
for _ in range(n_samples):
w = np.random.uniform(0, 2*np.pi, shape, requires_grad=True)
grads.append(qml.grad(circuit)(w)[0, 0, 0]) # one component
return np.var(grads)
for n in [2, 3, 4, 5, 6]:
v = grad_variance(n)
print(f"n={n}: Var = {v:.6f} 2^-n = {2.0**-n:.6f} ratio = {v/2.0**-n:.2f}")
Typical output.
n=2: Var = 0.121834 2^-n = 0.250000 ratio = 0.49
n=3: Var = 0.061205 2^-n = 0.125000 ratio = 0.49
n=4: Var = 0.029841 2^-n = 0.062500 ratio = 0.48
n=5: Var = 0.014972 2^-n = 0.031250 ratio = 0.48
n=6: Var = 0.007488 2^-n = 0.015625 ratio = 0.48
Analysis.
The ratio is constant at ≈0.48, confirming
exactly as the barren-plateau theorem predicts.
Fitting an exponential. Taking logs, \(\log_2 \mathrm{Var}\) falls by very nearly 1.00 per added qubit:
Extrapolating to larger systems.
| \(n\) | Predicted Var | Shots for SNR 3 |
|---|---|---|
| 10 | \(4.7\times10^{-4}\) | \(1.9\times10^{4}\) |
| 20 | \(4.6\times10^{-7}\) | \(2.0\times10^{7}\) |
| 50 | \(4.3\times10^{-16}\) | \(2.1\times10^{16}\) |
At 50 qubits, one gradient component needs \(2\times10^{16}\) shots — about 65 000 years at 10 kHz.
Two exercises to extend this.
- Change to a global observable (
qml.PauliZ(0) @ qml.PauliZ(1) @ ... @ qml.PauliZ(n-1)) and observe the variance collapse faster — global cost functions are worse (L29, Problem 4). - Reduce
n_layersto 2 and observe the variance decay more slowly — shallow circuits do not form 2-designs and are trainable.
40.8 Practice Problems¶
- Write a QNode returning
qml.probs(wires=[0,1])for a Bell state. [Ans:[0.5, 0, 0, 0.5]] - Compute the gradient of \(\langle Z\rangle\) after \(R_x(\theta)\) at \(\theta = \pi/3\). [Ans: \(-\sin(\pi/3) = -0.866\)]
- How many parameters in
StronglyEntanglingLayerswith 5 layers and 6 wires? [Ans: 90] - Which
diff_methodfor a 30-parameter circuit on IBM hardware, and why? - Why is
backpropunavailable on real hardware? - Circuit evaluations per gradient with
parameter-shiftand \(P = 50\)? [Ans: 100] - Explain why
Tanhis needed before an angle-encoding layer.
40.9 Summary¶
- PennyLane's core idea: a quantum circuit is a differentiable function. QNodes are ordinary Python functions that can be differentiated, composed and optimised.
- Devices (
default.qubit,lightning.qubit,default.mixed, plugins) are chosen at QNode creation;shots=Nonegives exact analytic values for debugging. - Differentiation methods:
backprop/adjoint(1–2 evaluations, simulator only),parameter-shift(\(2P\), hardware-compatible, exact),spsa(2, stochastic, hardware). Develop with backprop, deploy with parameter-shift or SPSA. - Quantum natural gradient preconditions by the Fubini–Study metric, converging 2–5× faster.
- Framework integration is PennyLane's decisive advantage:
qml.qnn.TorchLayerandKerasLayermake a quantum circuit a native layer, with gradients flowing through a single.backward()call. JAX addsjitandvmap. - Templates (
AngleEmbedding,IQPEmbedding,StronglyEntanglingLayers,QFT,GroverOperator,TrotterProduct) are tested building blocks — use them. - The encoding, not the ansatz depth, determines expressivity: product angle encoding cannot learn XOR; adding an entangling
IQPEmbeddingcan. - A 15-line PennyLane script reproduces the barren-plateau \(2^{-n}\) scaling directly.
Next lesson: L41 — Amazon Braket and multi-vendor hardware access.
Practise now: unit4-platforms-lab.html → PennyLane Trainer: adjust encoding, ansatz depth and
optimiser, and watch the loss curve, gradient magnitudes and decision boundary evolve together.