What is PennyLane?
PennyLane is Xanadu's open-source quantum ML framework. It treats quantum circuits as differentiable functions, enabling gradient-based optimization directly through quantum computations. You can compute gradients of quantum circuits using parameter-shift rules, adjoint differentiation, or backpropagation — and plug them into PyTorch or JAX training loops. All of this is free and local.
Installation
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 # PyTorchFirst Quantum Circuit
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──┤ ╰ProbsQuantum Machine Learning — Variational Classifier
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}")Using JAX Backend for Speed
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 gradientConnecting to Other Backends
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")💡
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