In our previous tutorials, we explored the Qiskit Programming language where we did projects from Shor's algorithm to quantum random number generators. In this tutorial, we will explore the PennyLane Framework- which is an open source software development kit built upon the idea of differentiable programming. We will do some basic tasks in PennyLane and highlight its future potential for quantum machine learning applications and optimization tasks and variational classifiers.

PennyLane works by integrating machine learning libraries with quantum simulators and hardware, giving users the power to train quantum circuits. Pennylane is written in Python, so if you know Qiskit, it is not that hard to learn about PennyLane. Classical computations,optimization or training of models, are executed using the standard scientific computing or machine learning libraries such as SciPy in Python. PennyLane provides an interface to these libraries and integrates these with quantum simulators to provide an interface between classical and quantum computing.

PennyLane’s uses NumPy to perform computations, but connects with machine learning libraries like PyTorch and Tensorflow for writing programs.PennyLane is built with default simulator devices, and connected with external software and hardware to run quantum circuits like IBM’s Qiskit, or Google’s Cirq.

The main advantage of using PennyLane is to control and manipulate parametrized quantum circuits or variational circuits on quantum devices, and if we use PennyLane we can easy feed these circuits to quantum neural networks. Using PennyLane, we can easily find and operate gradients of quantum circuits, which is essential for the machine learning package to perform backpropagation.

To install PennyLane, you must have a version of Python installed. Then run the following command

Quantum computations are represented as quantum node objects in PennyLane. A quantum node is an initialization of the quantum circuit, and encapsulates the computation to the specific quantum device that executes the circuit. Quantum nodes can be easily created by using the qnode decorator.

QNodes can interact with machine learning libraries such as PyTorch and TensorFlow. This is what allows it to connect the quantum circuit to data structures like TensorFlow tensors and optimizers. By default, QNodes use the NumPy interface.

A quantum circuit is created using the syntax below:

Instead of the qubit, PennyLane uses the term wires to refer to a quantum subsystem. The quantum function returns either a single or a tuple of measured observable values, by applying a measurement function, which in the function abive is the PauliZ operator to the qubit 1

To run and optimize a quantum circuit, we need to specify the quantum device on which to execute the circuit. The device is an instance or object of the Device class. It is instantiated using the device loader as follows

The name of the device is the first argument, wires represent the number of subsystems represented by the device, analytic indicates if the device should calculate expectations and variances analytically and shots indictate how many times the circuit should be evaluated to calculate the expectation values

A quantum function is used to create a quantum node or QNode object, which encapsulates the quantum function and the device used to execute the function

A QNode is initialized as follows:

The QNode is used to compute the result of a quantum circuit and can calculate results classically. For example, we will run the results of the quantum_function by creating a QNode circuit and passing the parameters x and y in the function quantum_function. Then an RZ gate, CNOT and RY will be applied on the circuit.

We will now use the quantum circuit and QNode to build a variational classifier in PennyLane. Variational circuits are circuits that can be trained using supervised learning or labelled data to classify new data.

The Variational Circuit can be implemented using the Parity Function

We will first import the required libraries.

Variational classifiers define “layer” or “block", elementary circuit architecture that gets repeated to build the variational circuit.

Our circuit layer consists of rotations on every qubit, as well as CNOTs that create entanglement. The rotation function is represented by qml.Rot(phi, theta, omega, wires). By applying the qml.Rot() function, an arbitary single qubit rotation is applied

For encoding data inputs into the circuit, we need to encode the inputs into the state of the qubits. The quantum state |𝜓⟩ is prepared using a computational basis state. The Basis State function is used which encodes the input into a quantum state. The input should be a list of zeros and ones. For encoding data inputs into the circuit, we need to encode the inputs into the state of the qubits. The quantum state |𝜓⟩ is prepared using a computational basis state.

Now, the quantum node is prepared by using the weights as parameters.

The variational classifier will return the circuit of the weights+bias, similar to classical machine learning

The cost function is the sum of the loss function and a regularizer. This loss function is used during training to measure the distance between target labels and model prediction using the square loss function

The accuracy can be determined given how many predictions were correct

The accuracy can be determined given how many predictions were correct

We will know build a variational classifier on the Iris dataset. We will do preprocessing to encode the inputs into the amplitudes of a quantum state. Every input x has to be translated into a set of rotation angles

The circuit is coded according to the algorithm in this paper

To learn more about the PennyLane software, visit https://pennylane.ai/qml/