pennylane
它能做什么
PennyLane 可以帮你构建和优化量子电路。它能把量子计算和人工智能模型(比如 PyTorch 或 JAX)结合起来,用来解决复杂问题,例如计算分子能量或验证量子电路。
适用场景
- ✓当你想优化带有可调参数的量子电路时。
- ✓当你想搭建一个同时包含量子计算和经典(AI)计算的模型时。
- ✓当你需要计算分子能量或解决量子优化问题时。
技能链接
https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/pennylane/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“帮我创建一个量子电路,用来计算氢分子的能量,并用 VQE 算法对它进行优化。”
每周 5 个新技能,1 分钟读完。
工作原理
PennyLane 让你先定义量子电路,然后借助机器学习工具调整这些电路的参数。你可以在电脑上模拟运行这个电路;如果做了相应配置,也可以把它发送到真实的量子硬件上运行。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
PennyLane
When to use
Use PennyLane to optimize parameterized circuits, build hybrid quantum-classical models, estimate molecular energies, or compare a validated circuit across devices. This skill targets stable PennyLane 0.45.1. The local examples use small synthetic systems; successful optimization does not establish quantum advantage, molecular accuracy, or hardware fidelity.
Installation
Create a dedicated environment; provider plugins and compiler dependencies should be resolved separately from unrelated scientific packages:
uv venv --python 3.13 .venv-pennylane
uv pip install --python .venv-pennylane/bin/python "pennylane==0.45.1"
PennyLane 0.45 requires NumPy 2+. Core installation includes Lightning. For ML, use the tested JAX 0.7.1/JAXlib 0.7.1 pair or PyTorch; do not assume the latest JAX is supported by this PennyLane release. See device setup for optional plugin versions and their verification limits.
Workflow
- Fix the problem convention: feature ordering and label encoding for ML; Hamiltonian sign for optimization; units, charge, multiplicity, basis, active space and mapping for chemistry.
- Build a small analytic
default.qubitcircuit. Keep the quantum function separate from the QNode when reusing it on another backend. - Check a known value and a gradient against an analytic or finite-difference
result before training. Use trainable
pennylane.numpyarrays for Autograd, framework-native tensors for Torch/JAX. - Optimize while recording objective, gradient norm, seeds, ansatz shape and
package versions.
step_and_costreturns the cost before its update; evaluate the objective again for final reporting. - Validate independently: held-out examples and classical baselines for ML; particle number and a sector-appropriate classical reference for VQE; enumerated small instances for QAOA.
- Introduce finite shots/noise, report uncertainty, inspect decomposed resources, then select a current accessible hardware backend and execution budget. Plugin portability does not guarantee identical gates, measurements or gradients.
Quick start: value, gradient and optimization
This self-contained example is executed by the skill's tests.
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev, interface="autograd", diff_method="backprop")
def energy(theta):
qml.RY(theta, wires=0)
return qml.expval(qml.Z(0))
theta = np.array(0.3, requires_grad=True)
assert np.allclose(energy(theta), np.cos(theta))
assert np.allclose(qml.grad(energy)(theta), -np.sin(theta))
opt = qml.GradientDescentOptimizer(stepsize=0.2)
for _ in range(80):
theta = opt.step(energy, theta)
final_energy = float(energy(theta))
assert final_energy < -0.999
print(f"[OK] final expectation = {final_energy:.6f}")
Load the relevant reference
- Getting started: QNodes, shots, random streams, trainability, batched parameters and simulator selection.
- Quantum circuits: controls, measurement feedback, wire ordering, QFT, transforms and current resource access.
- Quantum ML: executed TorchLayer and JAX training, stable classifier loss, feature scaling and evaluation.
- Quantum chemistry: H2 UCCSD, units, particle sector checks, dipoles, active spaces, geometry and excited-state caveats.
- Devices: simulators, provider contracts, credentials, current target discovery and source-only integration boundaries.
- Optimization: gradient checks, SPSA, QNG, MaxCut sign and exact QUBO-to-Ising conversion.
- Advanced features: templates, noise, parametrized Hamiltonian evolution, Catalyst and error-correction examples.
Failure checks
- Samples/counts require finite shots; states require a supporting simulator.
Use
qml.set_shotson the QNode rather than mutating device shots. - Mid-circuit measurement values are symbolic. Use
qml.cond, not Pythonif m. qml.specsin 0.45.1 returnsCircuitSpecs; access.resources, not obsolete top-level dictionary keys. Record the transform level and account for split tapes.qml.qaoa.maxcutproduces negative cut size. Minimize it directly.
…
常见问题
什么是量子电路?
它是一串指令(量子门),用来操作量子比特(qubit)来完成计算。你可以把它理解成量子计算机的程序。
什么是变分量子算法(VQE/QAOA)?
这类方法会使用一个带有可调参数的量子电路,再配合经典计算机,去寻找问题的最优解,比如分子的最低能量,或者某个优化问题的解。
我可以在自己的电脑上运行吗?
可以。PennyLane 能在你的电脑上模拟量子电路。如果想在真实的量子硬件上运行,你需要先开通某个服务商的访问权限。
详情
作者的原始描述
Builds 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.
热度
- GitHub 星标
- 51k
- 排名
- #3525
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …