Quantum Machine Learning: Hybrid quantum-classical Machine Learning with PyTorch and Qiskit
By: Maham Shafiq
Tags: Neural Networks, Quantum Computing, Quantum Machine Learning, Pytorch, Machine Learning

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.

Prerequisites

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.

So Dataset is finally loaded

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.

References:

1- https://www.tensorflow.org/quantum/concepts

2- https://qiskit.org/textbook/ch-machine-learning/machine-learning-qiskit-pytorch.html