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](https://numpy.org/)’s [autograd](https://github.com/HIPS/autograd), [JAX](https://github.com/google/jax), [PyTorch](https://pytorch.org/), and [TensorFlow](https://www.tensorflow.org/), 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](https://pennylane.ai/plugins.html) 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](https://pennylane.xanadu.ai/lab). --- #### 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](https://docs.pennylane.ai/en/stable/introduction/operations.html) or sequences of operators called [Templates](https://docs.pennylane.ai/en/stable/introduction/templates.html), using one instruction per line. - The function can contain classical flow control structures such as `for` loops or `if` statements. - The quantum function must always return either a single or a tuple of _measured observable values_, by applying a [measurement function](https://docs.pennylane.ai/en/stable/introduction/measurements.html) to a [qubit observable](https://docs.pennylane.ai/en/stable/introduction/operations.html#intro-ref-ops-qobs) or [continuous-value observable](https://docs.pennylane.ai/en/stable/introduction/operations.html#intro-ref-ops-cvobs). --- ## [Defining a Device](https://docs.pennylane.ai/en/stable/introduction/circuits.html#defining-a-device "Permalink to this headline") To run—and later optimize—a quantum circuit, one needs to first specify a _computational device_. The device is an instance of the [`Device`](https://docs.pennylane.ai/en/stable/code/api/pennylane.Device.html "pennylane.Device") class, and can represent either a simulator or hardware device. They can be instantiated using the [`device`](https://docs.pennylane.ai/en/stable/code/api/pennylane.device.html "pennylane.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](https://pennylane.ai/plugins.html) 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](https://github.com/PennyLaneAI/pennylane-lightning), 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 ](https://github.com/jundeli/quantum-gan)