Quantum Machine Learning: Hybrid quantum-classical Machine Learning with PyTorch and Qiskit
Quantum Computing is the most hyped-up topic in 2020 after Google has announced Quantum Supremacy with the debut of a newly engineered solution. Quantum Computer has huge power to outperform classical computers in most of the Machine Learning Tasks.
Quantum Computers use Qubits that are similar to the bits in classical computers with additional ability superposition and entanglement. Gigantic amounts of data can be computed with quantum machine learning algorithms, quantum ML increments such abilities shrewdly, by examining quantum states (qubits) and systems. Performance enhancement can be achieved by shifting difficult calculations to a quantum computer or optimize quantum algorithms using classical ML architectures.
What is Quantum Machine Learning?
Quantum Machine Learning is actually built on two concepts
1- Quantum data
2- Hybrid quantum-classical Model
Quantum data is any data source that occurs in a natural or artificial quantum system. This can be data generated by a quantum computer like the samples gathered from the Sycamore processor for Google’s demonstration of quantum supremacy. Quantum data exhibits superposition and entanglement, leading to joint probability distributions that could require an exponential amount of classical computational resources to represent or store. The quantum supremacy experiment showed it is possible to sample from an extremely complex joint probability distribution of 2⁵³ Hilbert space.
2-Hybrid Quantum-Classical Model
The quantum-classical hybrid neural network can be implemented by using a parameterized circuit for hidden layer of our neural net. What is meant by the parameterized quantum circuit? A circuit where the rotation angle of each gate is specified by the classical input vector’s components. The previous Layer’s output is collected and used as an input for our parameterized circuit. Our quantum circuit’s measurement statistics can be collected and used as input for the following layer.
In this article, I will step by step go through how a classical neural network can be partially quantized to create a hybrid quantum-classical neural network. We will code the integration of state of the art open-source platform Pytorch and Qiskit.
Handwritten Digit Recognization Using Hybrid Quantum- Classical Approach
Let’s begin coding for our very first problem and observe how quantum-classical fusion achieves speedup. We will begin our discussion with neural networks and along the way will develop a handwritten digit classifier from scratch. We will be using PyTorch because it is cool! This MNIST classifier will predict between two classes 0 and 1.
To understand the benefit of this hybrid approach one must have knowledge about classical neural networks. Let’s dive into the basic Machine Learning Concept Needed for this topic, For detailed concepts behind the inner intuition I highly recommend you to read other articles related to Machine Learning.
Classical Neural Networks
Neural networks are typically organized in layers, work in a similar way as the human brain. Layers are made up of a number of interconnected ‘nodes’, these nodes are referred to as perceptron. A perceptron is typically a simple, easy-to-compute, and nonlinear function that has one or more inputs to a single real number. Perceptron has an ‘activation function’. Patterns are presented to the network via the ‘input layer’, which communicates to one or more ‘hidden layers’ where the actual processing is done via a system of weighted ‘connections’. The hidden layers then link to an ‘output layer’ where the answer is output as shown in the graphic below
Quantum Neural Network
A quantum neural network (QNN) is used to describe a parameterized quantum computational model that is best executed on a quantum computer. This term is often interchangeable with the parameterized quantum circuit (PQC)
Step#1 — Importing necessary Packages from Qiskit and Pytorch
To get started we’ll need to install Qiskit. If you have Python >= 3.5 and Jupyter installed you can run this locally. Or alternatively, you can run this in Google Colab (if you have a Google account).Before starting we make all the necessary imports
Step#02 — Loading MNIST Dataset
Originally Dataset is available on Yann Lecun’s website. Don’t forget — “Garbage in, garbage out !”, So make sure to Clean the dataset. Using few lines of code in PyTorch we can easily download the cleaned and already prepared data.
Step#03 — Tensor to List
To connect Qiskit and Pytorch object we define a function to convert tensor to a list in Python, such that they can be fed into quantum circuits in Qiskit.
Step#04 — Make Quantum Class With Qiskit
We can conveniently put our Qiskit quantum functions into a class. First, we specify how many trainable quantum parameters and how many shots we wish to use in our quantum circuit. In this example, we will keep it simple and use a 1-qubit circuit with one trainable quantum parameter θ. We hard code the circuit for simplicity and use an RY−rotation by the angle θ to train the output of our circuit. The circuit looks like this:
Step#05— Make Pytorch’s Forward and Backward pass for Neural Network
Now that our quantum circuit is defined, we can create the functions needed for backpropagation using PyTorch. The forward and backward passes contain elements from our Qiskit class. The backward pass directly computes the analytical gradients using the finite difference formula
Step#06—Finally, Train the Model
Finally coming towards the training part using Stochastic gradient descent algorithm. I have trained it till 30 epochs
Our network looks like this
The point of this exercise was to get you thinking about integrating techniques from ML and quantum computing in order to investigate if there is indeed some element of interest — and thanks to PyTorch and Qiskit, this becomes a little bit easier.