PennyLane
PennyLane is a cross-platform Python library for differentiable programming of quantum computers, quantum machine learning, and quantum chemistry. It enables users to build, optimize, and deploy hybrid quantum-classical applications by seamlessly integrating with popular machine learning frameworks like NumPy, PyTorch, TensorFlow, and JAX. The library is under active development, with new versions and features released every few months, aiming to make quantum computing accessible for research and application development.
Warnings
- breaking Maintenance support for NumPy versions less than 2.0 is deprecated as of PennyLane v0.44.x and will be completely dropped in v0.45. Future versions of PennyLane will only work with NumPy >= 2.0.
- breaking Support for Intel MacOS platforms (x86) has been removed as of PennyLane v0.44.x, impacting Macs running on Intel processors. Support for Python 3.10 was deprecated in v0.42 and removed in subsequent releases. Minimum Python version for current PennyLane is 3.11.
- deprecated The `custom_decomps` keyword argument for `qml.device` is deprecated and will be removed in v0.45. New decomposition rules should be defined as quantum functions with registered resources using `qml.decomposition.enable_graph`.
- deprecated Accessing hermiticity via `pennylane.operation.Operator.is_hermitian` is deprecated. Use `is_verified_hermitian` for better reflection of functionality or the `is_hermitian()` function for thorough verification.
- breaking The argument `level=None` is no longer valid for functions like `qml.specs()`, `qml.draw()`, `workflow.get_transform_program()`, `workflow.construct_batch()`, `drawer.draw_mpl()`. It must be replaced with `level='device'` to apply all transforms.
Install
-
pip install pennylane -
pip install pennylane[all]
Imports
- pennylane
import pennylane as qml
Quickstart
import pennylane as qml
from pennylane import numpy as np
# Define a quantum device
dev = qml.device("default.qubit", wires=2)
# Define a QNode (quantum function)
@qml.qnode(dev)
def circuit(phi, theta):
qml.RX(phi[0], wires=0)
qml.RY(phi[1], wires=1)
qml.CNOT(wires=[0, 1])
qml.RX(theta, wires=0)
return qml.expval(qml.PauliZ(0))
# Define parameters with automatic differentiation enabled
phi_params = np.array([0.54, 0.12], requires_grad=True)
theta_param = np.array(0.9, requires_grad=True)
# Execute the circuit
result = circuit(phi_params, theta_param)
print(f"Circuit output: {result}")
# Compute gradients
grad_fn = qml.grad(circuit)
gradients = grad_fn(phi_params, theta_param)
print(f"Gradients: {gradients}")