Quanvolutional Neural Networks — Image Classification

Aryaman Sinha
4 min readSep 4, 2021

--

We all have been very much comfortable using Bits as a unit for information in any computational system. But what about the new unit of information, Qubits— the computational unit of quantum computers. As now many have started using the state-of-the-art Neural Nets in industry and making great products for the consumers, seeing this now the researchers are making advancements to a new branch of Neural Networks integrating with Quantum Computing.

Qubit vs Bit

As we know bit is basically the two logical states which classical computers can take. i.e. 0 or 1. Whereas, in quantum computers, the state can be infinite. The qubit can be represented by the linear superposition of the two orthonormal basis vectors denoted by |0⟩ and |1⟩ and pronounced as ket 0 and ket 1, mathematically represented as column vectors [0 1] and [1 0] respectively.

Qubit Logic Gates

We can consider them as simply as some transformation function applied on n-qubit input. Now, this transformation can be understood and logically jot down to some boxes which does some operations and give output in terms of qubit states when qubit states are fed as input. One can think of them as just the logic gates we use in our normal bit system i.e. AND, NOT, NOR, etc. but the complexity of the quantum system gives a ready use of the quantum states and develop complex logic gates. For example, Rotational gates (Rx, Ry, Rz) which do nothing but rotational transformation of the states, but how does it practically look can be easily visualised using Bloch Sphere.

Bloch Sphere representing the qubit state using theta and phi rotational angles just. So nothing but a polar representation of qubit states

To see how these Rotational gates works go to https://www.quantum-inspire.com/kbase/rotation-operators/

As we here see the glimpse of simple rotational gates, there are many other gates one can use and design complex circuits. For more info on gates go to Wikipedia. Let us move forward on how can we use these logic circuits to our advantage and develop Quantum Neural Networks.

Quanvolutional Neural Networks (QNN)

In Henderson et al., authors introduced the concept of using quantum circuits to construct a new layer known as Quanv Layer or Quantum Convolutional Layer, this is basically a pre-processing layer before the conventional CNN. The idea is to take either nxn image slices just like simple iteration done in block processing of any image, now as we have n² input pixel values to the Quanv function, now this function takes each input pixel and first encode this information in qubit state, there are different methods to encode the information using thresholding, or most commonly used is the rotational gate i.e. just apply rotational gate either Rx/Rz/Ry to all the inputs where θ the angle of rotation is pi*{value of pixel info} and thus we get different states in Bloch sphere. The next step is to construct any random quantum circuit which can be of “d” depth, depth is basically the number of layers of gates stacked one after another in one sequence. Now, as a result, we get some n² quantum states at the end. After, this quantum circuit output we feed this to the decoder which can be again changed according to the design of the system. Here, the authors go for the most logical approach, in a real quantum system it is impossible to get the number of the states but here as authors’ QNN doesn’t specifically require an actual quantum system rather they utilize the simulation of it, thus, the simulation systems provide one quality that is we can get the count of the all the states possible at the end of the quantum circuit. IBM’s library Qiskit provides different simulation environments which can be used to achieve this. Below is the code snippet for this quanv operation using Python Library Qiskit by IBM,

import numpy as npimport qiskit as qsk
def circuit(phi): circ = qsk.QuantumCircuit(len(phi)) for i in range(len(phi)): # encoding if phi[i]>=0.5: # threshold of 0.5 circ.rx(np.pi,i) else: circ.rx(0,i) random_circ = random_circuit(len(phi),2,measure=True) circ = circ + random_circ job = qsk.execute(circ,qsk.Aer.get_backend('qasm_simulator'), shots=1024) result = job.result(). #random circuit simulated job results my_out=[] for i in range(len(phi)): # decoding marginalised_results = qsk.result.marginal_counts(result, indices=[i]) marginalised_counts = marginalised_results.get_counts() my_out.append(marginalised_counts.get('1',0)/1024) #counts the number of qubits with 1 state output of all 1024 jobs simulated return np.array(my_out)# Quanvolutional processingdef quanv(img): out_img = np.zeros((img.shape[0]//2,img.shape[1]//2,4)) for i in tqdm(range(0,img.shape[0],2)): for j in range(0,img.shape[1],2): phi = [ img[i,j],img[i,j+1], img[i+1,j],img[i+1,j+1]] # 2x2 image window out_phi = circuit(phi) for c in range(4): out_img[i//2,j//2,c] = out_phi[c]return out_img

This pre-processing step can be used very easily and the resultant feature maps of (N/2, N/2, Cx4) will be generated which can b now used as new input to the conventional CNN and CNN can be trained for any binary/multi-class classification. This methodology doesn’t require any quantum computing power as Qiskit’s simulation-based environment can be used here very easily.

One can also look into the different outputs and simulation results of any quantum circuit using IBM Quantum Composer. Below is the block diagram depicting the QNN process by the authors,

Image Credits: Henderson et al.

To compare with the results, the authors provide the MNIST classification results, using QNN which gives 95% and higher accuracy.

Until Next Time ….

Live Long and Prosper 🖖

--

--