4.3 KiB
PennyLane is a cross-platform Python library for programming quantum computers. Its differentiable programming paradigm enables the execution and training of quantum programs on various backends.
PennyLane connects quantum computing with powerful machine learning frameworks like NumPy’s autograd, JAX, PyTorch, and TensorFlow, making them quantum-aware.
Its central job is to manage the execution of quantum computations, including the evaluation of circuits and the computation of their gradients. This information is forwarded to the classical framework, creating seamless quantum-classical pipelines for applications. PennyLane’s design principle states that circuits can be run on various kinds of simulators or hardware devices without making any changes – the complex job of optimising communication with the devices, compiling circuits to suit the backend, and choosing the best gradient strategies is taken care of.
The library comes with default simulator devices, but is well-integrated with external software and hardware to run quantum circuits—such as IBM’s Qiskit, or Google’s Cirq, Rigetti’s Forest, or Xanadu’s Strawberry Fields. You can find a jupyter lab instance of all examples here.
Creating a quantum circuit
import pennylane as qml
def my_quantum_function(x, y):
qml.RZ(x, wires=0)
qml.CNOT(wires=[0,1])
qml.RY(y, wires=1)
return qml.expval(qml.PauliZ(1))
Quantum functions are a restricted subset of Python functions, adhering to the following constraints:
- The quantum function accepts classical inputs, and consists of quantum operators or sequences of operators called Templates, using one instruction per line.
- The function can contain classical flow control structures such as
for
loops orif
statements. - The quantum function must always return either a single or a tuple of measured observable values, by applying a measurement function to a qubit observable or continuous-value observable.
Defining a Device
To run—and later optimize—a quantum circuit, one needs to first specify a computational device.
The device is an instance of the Device
class, and can represent either a simulator or hardware device. They can be instantiated using the device
loader.
dev = qml.device('default.qubit', wires=2, shots=1000)
PennyLane offers some basic devices such as the 'default.qubit'
, 'default.mixed'
, lightning.qubit
, and 'default.gaussian'
simulators; additional devices can be installed as plugins (see available plugins for more details).
Note: that the choice of a device significantly determines the speed of your computation, as well as the available options that can be passed to the device loader.
For example, check out the 'lightning.qubit'
plugin, which is a fast state-vector simulator supporting GPUs.
Example: qml.FlipSign
basis_state = [1, 0]
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def circuit():
for wire in list(range(2)):
qml.Hadamard(wires=wire)
qml.FlipSign(basis_state, wires=list(range(2)))
return qml.state()
The result being:
circuit()
tensor([ 0.5+0.j, 0.5+0.j, -0.5+0.j, 0.5+0.j], requires_grad=True)
Examples
- PyTorch & PennyLane implementation of small molecules