PennyLane とは?
PennyLane は Xanadu のオープンソース量子 ML フレームワークです。量子回路を微分可能な関数として扱い、量子計算を通じて勾配ベースの最適化を直接行えるようにします。パラメータシフト則、随伴微分、またはバックプロパゲーションを用いて量子回路の勾配を計算し、それらを PyTorch や JAX のトレーニングループに組み込むことができます。これらすべてが無料でローカルに利用できます。
PennyLane is the go-to SDK for variational circuits and VQE, the most practical class of hybrid algorithms on today's hardware.
インストール
terminal
# Core with default.qubit (pure NumPy, always free)
pip install pennylane
# Fast C++ simulator (10-100x speedup)
pip install pennylane-lightning
# GPU simulator (requires NVIDIA GPU)
pip install pennylane-lightning-gpu
# For JAX or PyTorch integration
pip install pennylane jax jaxlib # JAX
pip install pennylane torch # PyTorch最初の量子回路
pennylane_basic.py
import pennylane as qml
import numpy as np
# Choose your device (all free, local)
dev = qml.device("default.qubit", wires=2)
# dev = qml.device("lightning.qubit", wires=2) # Faster C++ version
@qml.qnode(dev)
def bell_state():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
return qml.probs(wires=[0, 1])
result = bell_state()
print(result) # [0.5, 0. , 0. , 0.5]
# Draw the circuit
print(qml.draw(bell_state)())
# 0: ──H─╭●──┤ ╭Probs
# 1: ────╰X──┤ ╰Probs量子機械学習 — 変分分類器
qml_classifier.py
import pennylane as qml
import numpy as np
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def variational_circuit(params, x):
# Encode input data
qml.AngleEmbedding(x, wires=[0, 1])
# Variational ansatz
qml.BasicEntanglerLayers(params, wires=[0, 1])
return qml.expval(qml.PauliZ(0))
# Initialize random parameters
params = np.random.uniform(0, np.pi, size=(3, 2))
# Compute gradient with parameter-shift rule (exact!)
grad_fn = qml.grad(variational_circuit)
x_sample = np.array([0.1, 0.2])
gradients = grad_fn(params, x_sample)
print(f"Parameters shape: {params.shape}")
print(f"Gradient shape: {gradients.shape}")
# Training loop
optimizer = qml.AdamOptimizer(stepsize=0.01)
for step in range(100):
params, cost = optimizer.step_and_cost(
lambda p: variational_circuit(p, x_sample), params
)
if step % 20 == 0:
print(f"Step {step}: cost = {cost:.4f}")高速化のための JAX バックエンドの利用
pennylane_jax.py
import pennylane as qml
import jax
import jax.numpy as jnp
dev = qml.device("default.qubit", wires=4)
@qml.qnode(dev, interface="jax")
def circuit(params):
for i in range(4):
qml.RY(params[i], wires=i)
for i in range(3):
qml.CNOT(wires=[i, i+1])
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(3))
# JIT compile the circuit for massive speedup
jit_circuit = jax.jit(circuit)
# Automatic differentiation with JAX
grad_circuit = jax.grad(jit_circuit)
params = jnp.array([0.1, 0.2, 0.3, 0.4])
print(jit_circuit(params)) # Fast JIT-compiled execution
print(grad_circuit(params)) # Automatic gradient他のバックエンドへの接続
pennylane_backends.py
import pennylane as qml
# Local simulators (all free)
qml.device("default.qubit", wires=4) # NumPy
qml.device("lightning.qubit", wires=4) # C++ (fast)
qml.device("lightning.gpu", wires=4) # NVIDIA GPU
# IBM Quantum (free tier — needs account)
# pip install pennylane-qiskit
qml.device("qiskit.ibmq", wires=4, backend="ibm_sherbrooke")
# Amazon Braket
# pip install amazon-braket-pennylane-plugin
qml.device("braket.local.qubit", wires=4) # Free local
qml.device("braket.aws.qubit", wires=4,
device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1")Keep exploring
💡
Also available via HLQuantum
Want to run the same circuit on multiple backends without rewriting your code? HLQuantum abstracts this SDK (and 5 others) behind a single unified API.
python
import hlquantum as hlq
qc = hlq.Circuit(2)
qc.h(0).cx(0, 1).measure_all()
# One line to switch between any backend
result = hlq.run(qc, shots=1024) # auto-detect
result = hlq.run(qc, shots=1024, backend="pennylane") # explicit