K-Dense-AI/scientific-agent-skills / skills/pennylane
pennylane
VerifiedBuilds and differentiates PennyLane quantum circuits, hybrid PyTorch or JAX models, molecular VQE and QAOA workflows. Use for variational quantum algorithms, quantum machine learning, simulator validation, and moving validated circuits to provider plugins. For hardware-specific compilation use qiskit or cirq; for open-system dynamics use qutip.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 7.0 KB |
| references/advanced_features.md | markdown | 7.7 KB |
| references/devices_backends.md | markdown | 8.9 KB |
| references/getting_started.md | markdown | 4.0 KB |
| references/optimization.md | markdown | 6.7 KB |
| references/quantum_chemistry.md | markdown | 7.5 KB |
| references/quantum_circuits.md | markdown | 4.9 KB |
| references/quantum_ml.md | markdown | 6.5 KB |
references/optimization.md
View on GitHub# Optimization and objective conventions
Targets PennyLane 0.45.1. Each block is self-contained and exercised numerically.
## Gradients before optimization
```python
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=1)
def quantum_function(x):
qml.RY(x, 0)
return qml.expval(qml.Z(0))
x = np.array(0.4, requires_grad=True)
for method in ["backprop", "parameter-shift", "adjoint", "finite-diff"]:
circuit = qml.QNode(quantum_function, dev, diff_method=method)
assert np.allclose(qml.grad(circuit)(x), -np.sin(x), atol=1e-6)
second_order = qml.QNode(quantum_function, dev, diff_method="parameter-shift", max_diff=2)
assert np.allclose(qml.jacobian(qml.grad(second_order))(x), -np.cos(x))
```
Backprop/adjoint require supporting simulators and compatible measurements;
finite-shot gradients typically use parameter-shift or stochastic estimates.
A parameter-shift gradient's circuit count depends on generators, decomposition,
measurements and batching; do not assume exactly two circuits for every parameter.
`max_diff=2` enables the higher-order derivative path above.
Use the optimizer from the chosen interface. PennyLane's
`GradientDescentOptimizer`, `AdamOptimizer`, `MomentumOptimizer`,
`NesterovMomentumOptimizer`, `AdagradOptimizer` and `RMSPropOptimizer` work with
Autograd arrays. Torch uses `torch.optim`; JAX uses its own updates or Optax.
`step_and_cost` gives the **old** cost and **new** parameters. Recompute final cost.
## SPSA and quantum natural gradient
```python
import pennylane as qml
from pennylane import numpy as np
np.random.seed(19)
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev)
def cost(x):
qml.RY(x[0], 0)
return qml.expval(qml.Z(0))
x = np.array([0.5], requires_grad=True)
spsa = qml.SPSAOptimizer(maxiter=80, a=0.3, c=0.1)
for _ in range(80):
x = spsa.step(cost, x)
assert cost(x) < cost(np.array([0.5], requires_grad=True))
qng = qml.QNGOptimizer(stepsize=0.1, approx="block-diag", lam=0.01)
y = np.array([0.5], requires_grad=True)
y = qng.step(cost, y)
assert cost(y) < cost(np.array([0.5], requires_grad=True))
```
SPSA has `step`/`step_and_cost`, not `minimize`. Its optimizer estimates gradients
itself; setting the QNode to `diff_method='spsa'` as well is unnecessary here.
QNG needs a compatible QNode/metric; regularization handles near-singularity but
changes the update. Full `qml.metric_tensor` can need an auxiliary wire;
block-diagonal approximations have different cost/geometry.
`QNSPSAOptimizer` is a stochastic natural-gradient approximation, not quantum
analytic descent. `RotosolveOptimizer` needs per-argument frequency information
(`nums_frequency` or `spectra`) and is not a universal drop-in optimizer.
## MaxCut: minimize negative cut size
```python
import pennylane as qml
from pennylane import numpy as np
import networkx as nx
graph = nx.cycle_graph(3)
cost_h, mixer_h = qml.qaoa.maxcut(graph)
dev = qml.device("default.qubit", wires=list(graph.nodes))
@qml.qnode(dev)
def qaoa_energy(params):
for wire in graph.nodes:
qml.Hadamard(wire)
for gamma, beta in params:
qml.qaoa.cost_layer(gamma, cost_h)
qml.qaoa.mixer_layer(beta, mixer_h)
return qml.expval(cost_h)
params = np.array([[0.3, 0.2]], requires_grad=True)
initial_energy = float(qaoa_energy(params))
opt = qml.AdamOptimizer(stepsize=0.1)
for _ in range(80):
params = opt.step(qaoa_energy, params)
final_energy = float(qaoa_energy(params))
assert final_energy < initial_energy
assert final_energy >= -2.0 - 1e-8
assert -final_energy > 1.99
```
`maxcut(graph)` has no `constrained` keyword. Its Hamiltonian is
`sum((Zi Zj - I)/2)`, so each cut edge contributes **-1**. Negating this objective
before minimizing favors the wrong answer. The measured mean cut score is not
the best sampled bit string; evaluate each bit string with the classical graph
objective, record sample frequencies, and compare with exact small instances.
Node labels determine wires. Weighted or constrained problems require a matching
cost construction; do not assume unweighted MaxCut reads edge weights.
## QUBO: include linear terms, offset and both matrix triangles
For the declared convention `f(x) = x.T @ Q @ x`, with binary `x`, substitute
`x_i=(I-Z_i)/2`. A symmetric matrix includes off-diagonal contributions twice.
Keep the constant to reproduce absolute objectives.
```python
import itertools
import pennylane as qml
import numpy as np
def qubo_hamiltonian(Q):
Q = np.asarray(Q, dtype=float)
if Q.ndim != 2 or Q.shape[0] != Q.shape[1] or not len(Q) or not np.isfinite(Q).all():
raise ValueError("Q must be a nonempty finite square real matrix")
n = len(Q)
offset = np.trace(Q) / 2
linear = -np.diag(Q).copy() / 2
coeffs, observables = [], []
for i in range(n):
for j in range(i + 1, n):
pair = Q[i, j] + Q[j, i]
offset += pair / 4
linear[i] -= pair / 4
linear[j] -= pair / 4
coeffs.append(pair / 4)
observables.append(qml.Z(i) @ qml.Z(j))
coeffs.extend(linear)
observables.extend(qml.Z(i) for i in range(n))
coeffs.append(offset)
observables.append(qml.I(0))
return qml.Hamiltonian(coeffs, observables)
Q = np.array([[1.0, -2.0], [-2.0, 1.0]])
H = qubo_hamiltonian(Q)
expected = [np.asarray(x) @ Q @ np.asarray(x) for x in itertools.product([0, 1], repeat=2)]
assert np.allclose(np.diag(qml.matrix(H, wire_order=[0, 1])), expected)
```
Use this diagonal `H` with `qml.qaoa.cost_layer`, an X mixer and the same minimization
convention. For a polynomial written with each `i<j` term only once, convert it
to the stated matrix convention first. Enumerate small problems before training.
## Training and scientific diagnostics
Keep optimizer state between batches/epochs; recreating Adam resets its moments.
Compute validation loss on held-out data and retain the best parameters. Monitor
finite loss, gradient norm, objective improvement and several seeded restarts.
For barren-plateau studies, estimate each parameter's gradient distribution across
independent initializations and system sizes; variance across different parameters
at one initialization plus a universal threshold is not evidence of exponential
concentration. Compare analytic and finite-shot gradients to separate shot noise.
## Sources
- [Gradient interfaces](https://docs.pennylane.ai/en/stable/introduction/interfaces.html)
- [SPSAOptimizer](https://docs.pennylane.ai/en/stable/code/api/pennylane.SPSAOptimizer.html)
- [QNGOptimizer](https://docs.pennylane.ai/en/stable/code/api/pennylane.QNGOptimizer.html)
- [RotosolveOptimizer](https://docs.pennylane.ai/en/stable/code/api/pennylane.RotosolveOptimizer.html)
- [MaxCut](https://docs.pennylane.ai/en/stable/code/api/pennylane.qaoa.cost.maxcut.html)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |