УДК 339.54.012

Analysis of a hybrid quantum-classical convolutional neural network

Хуснутдинов Линар Расимович – аспирант Казанского (Приволжского) федерального университета

Abstract: Article provides an overview of quantum neural networks, as well as a convolutional neural networks. In order to show what limitations and opportunities this approach provides, article will cite several publications on quantum and hybrid generative adversarial networks. Article gives description of the quantum convolutional layers of a neural network and give an example of their use.

Keywords: quantum machine learning, machine learning, quantum neural networks, convolutional neural networks, CNN.

Introduction

Quantum computers are not yet sufficiently developed to solve large tasks of practical importance for the industry – this is especially relevant for neural networks, which in the classical scenario require significant computing resources. That is why the most popular category of hybrid variational algorithms is that train a quantum parametric scheme (QNN) using classical optimization, for example, VQ Eigensolvers and Quantum Approximate Optimization Algorithms.

Applications of quantum neural networks

We will look at two examples that clearly show the difference in constraints when using fully quantum and hybrid quantum-classical networks.

In the first example, a fully quantum generative adversarial network was built, the task of which was to reproduce MNIST, but the whole experiment had one nuance. Obviously, a network of this size is not able to compete with classical networks by tens of thousands of weights, so the end result was a comparison by the number of weights with identical results. A fully quantum network was able to obtain the same results as a classical one, while having 95% fewer parameters.

image001

Figure 1. Scheme of a quantum generative adversarial network from the publication. Source [1].

In the second example, a hybrid quantum-classical generative adversarial network was constructed, the task of which was to reproduce QM-9, a popular dataset of molecules up to 9 atoms, represented by a molecular graph in the form of a 9 by 9 matrix and an atomic vector of 9 elements.

image002

Figure 2. A diagram of a hybrid generative adversarial network from the publication. Source [2].

Qunatum neural network

Architecture

Ideally, this approach would imply that with the help of a classical optimizer we train some parametric scheme on a quantum computer, but in the current realities of NISQ this approach is impossible, so most of the parametric scheme remains on classical computers. In this section, we will show the approach associated with QNN.

In the image of a hybrid network, the procedure is almost identical to classical network training, in which the process of encoding classical data into quantum operators and the process of measuring the quantum state is added in order to transmit already classical information for further calculations on a classical device.

image003

Figure 3. Hybrid neural network training scheme. Source [3].

Anzac

Often in the literature on VQC, especially when it comes to neural networks, such a thing as ansatz is mentioned – in essence, these are pre-prepared sections of a parametric circuit that can be used as composite blocks of a network. If we draw parallels with classical machine learning, then within the PennyLane library these schemes are called templates and can represent, for example, a convolutional layer or embedding, as well as more general elements of a quantum scheme like the preparation of states or permutations between qubits.

Loss function

The loss function works in the same way as in completely classical networks, since optimization takes place on classical hardware. The only thing that is different is the union of quantum and classical gradients. The gradient according to our quantum scheme is obtained by measuring the state, which can vary due to the probabilistic nature of the qubit, so several measurements allow us to approximate the expected gradient using methods like finite differences or parameter-shift, after which it remains only to combine it with the classical gradient.

Network from start to end

Ultimately, we have the following sequence of actions in order to assemble a hybrid neural network:

  • transform data from classical to quantum representations;
  • send this data for calculation on a quantum circuit;
  • to sample and measure the result of a quantum circuit;
  • send the results for calculation on the classical scheme;
  • evaluate the error, calculate gradients and update parameters.

Convolutional variational quantum schemes

Convolutional neural networks

This type of neural networks is aimed at image processing and consists of a sequential set of convolutional layers alternating with other auxiliary layers, for example, with nonlinear transformation functions (activations). The convolutional layer got its name because of the convolution operation, the essence of which is that each fragment of the input image (input) is multiplied by the matrix (3x3 kernel, kernel) of the convolution element by element, and the result is summed up and written to a similar position of the output image (output).

Quantum convolution

The active development of quantum computing creates a request for the construction of a quantum analogue of convolution. Paper [4], describing a similar approach, suggests the following implementation scheme based on variational quantum schemes:

  • the image is divided into blocks of size 2x2;
  • for each block, the input signal is encoded to the quantum state, which is carried out by parameterized rotation of the qubit from the ground state;
  • quantum calculations are performed on input qubits by means of a random quantum circuit;
  • a quantum system is measured to obtain classical quantities;
  • by performing this procedure for all 2x2 regions, a multi-channel image is obtained at the output (four-channel in the example below), which will be used in the following layers of a classical or quantum neural network.

An example of the implementation and operation of such a quantum convolution unit will be demonstrated below.

Preparing the model and data

For the implementation of model different frameworks is used: Library PennyLane is for quantum computing, Tensorflow for training the classifier and Matplotlib for rendering the results.

We set the parameters of the model for training: 4 is the number of epochs, 1 is the number of layers of quantum convolution and 4 its output channels, as well as the number of training – 20 and test examples – 10. We need to use 4 qubits to process each of the 4 pixels of the input block and generate 4 channels.

For demonstration, we use the MNIST dataset, which is designed to predict a digit (from 0 to 9) from its image.

Random quantum chain

A random quantum chain (RandomLayers in PennyLane) is used as a quantum convolution block. The block is implemented by randomly selecting rotation operations for part of the qubits, and paired quantum gates for part of the qubit pairs. At the last stage of block generation, the sequence of operations is randomly shuffled.

In our example, a random quantum circuit processes 4 qubits. Thus, some of the 4 cubes will receive any rotation operations, and some of the 6 pairs of qubits will receive gates. The final sequence of operations will be random.

Implementation of quantum convolution

Next, we create a device that simulates the operation of 4 qubits. It consists of three parts:

  • an encoder that converts input data into quantum states using RY rotations; • random quantum circuit;
  • measurements giving 4 output values.

The quanv function defines a quantum convolution according to the following scheme:

  • splitting the image into blocks of 2x2 pixels;
  • processing of each block by the quantum circuit described above;
  • 4 output values for each block are placed in 4 different channels of one pixel of the output image.

The process described above reduces the resolution of the input image by a factor of 2, which is equivalent to the classical convolution with a 2x2 kernel and a step of 2.

Preparing a dataset

Since the quantum convolutional layer is not trained in our case, it is better to pre-process all the images used from the dataset. The prepared data will be used by a classical neural network to train a classifier model.

Hybrid quantum-classical model

Below we describe the classical part of the neural network, which will be trained to classify 10 digits. A very simple model will be used, based on one fully connected layer and the final application of the softmax function, which outputs the probabilities of the 10 classes presented.

The training will be carried out using the Adam optimizer for the loss function, which uses cross-entropy.

For comparison with the results of a purely classical model, we will train it on the input raw images.

Comparison of results

We will make a comparison based on the test accuracy and the value of the loss function depending on the epoch number.

image004

Figure 4. Comparison of results.

The figures show that there is a strong similarity of the results for two models: hybrid and purely classical. In our small experiments, a rather low accuracy of 50% was achieved (the classical analogue is 40%). However, with an increase in the number of examples used, the accuracy should be significantly higher.

Conclusion

In the coming years, fully quantum neural networks will not be able to solve problems entirely, therefore they will be used as a component of a hybrid quantum-classical solution. As well as for fully quantum networks, hybrid networks allow you to reduce the number of necessary parameters compared to fully classical networks. The training of such networks is almost identical to the training of classical networks, with the exception of a few tricks necessary to work with the parameters of quantum circuits.

Literature

  1. Samuel A. Stein, Betis Baheri, Daniel Chen, Ying Mao, Qiang Guan, Ang Li, Bo Fang, and Shuai Xu, “Qugan: a generative adversarial network through quantum states” 2021 IEEE International Conference on Quantum Computing and Engineering (QCE), 96(4):042113, 2021. URL: https://arxiv.org/abs/2010.09036.
  2. Junde Li, Rasit Topaloglu, and Swaroop Ghosh, “Quantum generative models for small molecule drug discovery”, 2021. URL: https://arxiv.org/abs/2101.03438.
  3. Michael Broughton, Guillaume Verdon, Trevor McCourt, Antonio J. Martinez, Jae Hyeon Yoo, Sergei V. Isakov, Philip Massey, Ramin Halavati, Murphy Yuezhen Niu, Alexander Zlokapa, Evan Peters, Owen Lockwood, Andrea Skolik, Sofiene Jerbi, Vedran Dunjko, Martin Leib, Michael Streif, David Von Dollen, Hongxiang Chen, Shuxiang Cao, Roeland Wiersema, Hsin-Yuan Huang, Jarrod R. McClean, Ryan Babbush, Sergio Boixo, Dave Bacon, Alan K. Ho, Hartmut Neven, and Masoud Mohseni, “Tensorflow quantum: a software framework for quantum machine learning”, 2021. URL: https://arxiv.org/abs/2003.02989.
  4. Maxwell Henderson, Samriddhi Shakya, Shashindra Pradhan, Tristan Cook, “Quanvolutional Neural Networks: Powering Image Recognition with Quantum Circuits” Science, 355(6325):602–606, 2019. URL: https://arxiv.org/abs/1904.04767.

Интересная статья? Поделись ей с другими: