Implementing a Hybrid Quantum-Classical Neural Network by Utilizing a
  Variational Quantum Circuit for Detection of Dementia
Magnetic resonance imaging (MRI) is a common technique to scan brains for
strokes, tumors, and other abnormalities that cause forms of dementia. However,
correctly diagnosing forms of dementia from MRIs is difficult, as nearly 1 in 3
patients with Alzheimer's were misdiagnosed in 2019, an issue neural networks
can rectify. Quantum computing applications This proposed novel neural network
architecture uses a fully-connected (FC) layer, which reduces the number of
features to obtain an expectation value by implementing a variational quantum
circuit (VQC). The VQC created in this study utilizes a layer of Hadamard
gates, Rotation-Y gates that are parameterized by tanh(intensity) * (pi/2) of a
pixel, controlled-not (CNOT) gates, and measurement operators to obtain the
expected values. This study found that the proposed hybrid quantum-classical
convolutional neural network (QCCNN) provided 97.5% and 95.1% testing and
validation accuracies, respectively, which was considerably higher than the
classical neural network (CNN) testing and validation accuracies of 91.5% and
89.2%. Additionally, using a testing set of 100 normal and 100 dementia MRI
images, the QCCNN detected normal and demented images correctly 95% and 98% of
the time, compared to the CNN accuracies of 89% and 91%. With hospitals like
Massachusetts General Hospital beginning to adopt machine learning applications
for biomedical image detection, this proposed architecture would approve
accuracies and potentially save more lives. Furthermore, the proposed
architecture is generally flexible, and can be used for transfer-learning
tasks, saving time and resources.
Ryan Kim
Implementing a Hybrid Quantum-Classical Neural Network by Utilizing a Variational Quantum Circuit for Detection of Dementia

###### Abstract

Magnetic resonance imaging (MRI) is a common technique to scan brains for strokes, tumors, and other abnormalities that cause forms of dementia. However, correctly diagnosing forms of dementia from MRIs is difficult, as nearly 1 in 3 patients with Alzheimer's were misdiagnosed in 2019, an issue neural networks can rectify. Quantum computing applications This proposed novel neural network architecture uses a fully-connected (FC) layer, which reduces the number of features to obtain an expectation value by implementing a variational quantum circuit (VQC). The VQC created in this study utilizes a layer of Hadamard gates, Rotation-Y gates that are parameterized by tanh(intensity) \({}^{\circ}\) (\(\pi\)/2) of a pixel, controlled-not (CNOT) gates, and measurement operators to obtain the expected values. This study found that the proposed hybrid quantum-classical convolutional neural network (QCCNN) provided 97.5% and 95.1% testing and validation accuracies, respectively, which was considerably higher than the classical neural network (CNN) testing and validation accuracies of 91.5% and 89.2%. Additionally, using a testing set of 100 normal and 100 dementia MRI images, the QCCNN detected normal and deemed images correctly 95% and 98% of the time, compared to the CNN accuracies of 89% and 91%. With hospitals like Massachusetts General Hospital beginning to adopt machine learning applications for biomedical image detection, this proposed architecture would approve accuracies and potentially save more lives. Furthermore, the proposed architecture is generally flexible, and can be used for transfer-learning tasks, saving time and resources.

 quantum, machine learning, dementia, variational quantum circuit

## I Introduction

With the field of quantum physics growing rapidly, more and more applications of quantum computing are entering the computer science field [1]. Quantum computing offers several benefits over classical computing, which include faster computations, a broader variety of problems to solve, and better performance for machine learning tasks [2]. The properties of encoding information through superposition and quantum entanglement are not present in classical computing and could provide new ways to solve specific tasks if harnessed correctly. My research attempts to enhance existing machine learning methods by applying a variational quantum circuit layer to a pre-existing network, in hopes of improving the accuracy of the network. This novel hybrid quantum-classical neural network was tested utilizing an MRI dataset of brains with and without dementia, which holds relevance to this day. Dementia is a growing issue in the world, as over 55 million people in the world suffer from it [3]. Dementia is the deterioration of cognitive function and can be caused by a variety of factors, such as head trauma, alcohol, Parkinson's, Alzheimer's, and more. Magnetic resonance imaging (MRI) is one of the most effective ways of diagnosing dementia, but identifying whether a patient has a certain condition is difficult because the images are complex and require subjective deduction [4][5]. One of the new ways to detect a certain condition through MRI images is to use neural networks, which can be much more accurate and objective than humans [6].

## II Materials and Methods

### _Description of Dataset_

The image dataset used in this study was produced and published publicly by Sarvesh Dubey on Kaggle [7]. The dataset consists of both commented and non-demented MRI images of brains separated into four different classes representing different severities of dementia: not demented, mildly demented, moderately demented, and very demented. Overall, the total number of images was approximately 6400.

Since this study focuses on the binary classification between demented and non-demented images, I chose to use the 3199 non-demented and 2239 very demented images for my dataset.

### _Preprocessing of Dataset_

The two pools of images were separated into different folders, and labeled 1 through the total number of images in the folder. By keeping the labels consistent within the folder, it becomes easy to iterate over the dataset to use for the model.

Additionally, images were resized to 250 x 250 pixels and the dataset array was converted into a Pytorch tensor.

### _Classical Neural Network_

A classical neural network consists of many different layers, used to extract features with mathematical functions. In deep-learning, feed-forward neural networks tend to be one of the most widely used models for object detection. A generalized layer of one feed-forward neural network can be represented by [8]:

\[L_{i}=x_{i}\to y_{i}=\psi(Wx_{i}+b) \tag{1}\]
where \(L_{i}\) is the specified layer, \(W\) is the weights, \(x_{i}\) is the input feature vector, \(y_{i}\) is the output feature vector, b is the bias, and \(\psi\) is a nonlinear activation function. The biases and weights are the values that will be optimized during the training process.

The selected model used for feature extraction was the ResNet18, which is a sequential convolutional neural network, that includes 18 layers in total. ResNet18 consists of convolution, batch normalization, ReLU, and max pooling layers.

### _Variational Quantum Circuit_

A variational quantum circuit takes advantage of certain quantum properties such as superposition, entanglement, and quantum gates to encode and manipulate data. Superposition is fundamental in quantum mechanics, as it a system is at all possible states, with each state having a corresponding probability, until a measurement occurs. Superposition of a general 1/2 state vector in the z-basis can be represented by:

\[\left|\psi\right\rangle=a\left|+\right\rangle+b\left|-\right\rangle \tag{2}\]

where \(\psi\) is the given state, + is the spin-up z-basis vector, - is the spin-down z-basis vector, a is the corresponding probability amplitude of +, and b is the corresponding probability amplitude of -.

Entanglement is another foundational principle of quantum mechanics, which states that once measuring a state of one particle of an entangled pair, the state of the other particle is known, no matter how far the particles are from each other. A simple representation of an entangled system in a Stern-Gerlach experiment is:

\[\left|\psi\right\rangle=\frac{1}{\sqrt{2}}(\left|+\right\rangle_{1}\left|- \right\rangle_{2}+\left|-\right\rangle_{1}\left|+\right\rangle_{2}) \tag{3}\]

where \(+_{1}\) and \(-_{1}\) represent basis state vectors of the first particle and \(+_{2}\) and \(-_{2}\) represent basis state vectors of the second particle.

In quantum computing, there are gates that perform operations on qubits, which store information. The most fundamental gate is the Hadamard (H) gate, which brings a single qubit into a superposition state. Another important gate is the controlled-not (CNOT) gate, which checks if the control qubit has the state \(\left|1\right\rangle\) and then flips the target qubit from \(\left|0\right\rangle\) to \(\left|1\right\rangle\) and vice versa. The final gate used in this study is the rotation-y (RY) gate, which rotates a given qubit state a given number of radians around the complex y-axis.

In general, variational quantum circuits consist of three major layers: embedding, variational, and measurement layer. A visualization of a variational quantum circuit is represented in Figure 1.

The embedding layer \(E\) will map a vector from the classical space to the quantum Hilbert space, and is parameterized by \(\theta\), which can both be defined as

\[E:x\rightarrow\left|x\right\rangle=E(x)\left|0\right\rangle \tag{4}\]

\[\theta_{i}=tanh(I_{x,y})\ast\frac{\pi}{2} \tag{5}\]

where \(I\) is the intensity of a pixel specified at (\(x\), \(y\)) of a given convoluted input image. A variational layer, \(L\), can be defined as [8]:

\[L:\left|x\right\rangle\rightarrow\left|y\right\rangle=U\left|w\right\rangle \left|x\right\rangle \tag{6}\]

where \(\left|x\right\rangle\) is the input state, \(\left|y\right\rangle\) is the output state, \(w\) is a vector of classical variational parameters, and U is a unitary gate matrix that represents the matrix product of the H, CNOT, and RY gates.

The concatenation of these variational layers L of depth n can be expressed as:

\[Q=L_{n}\circ\cdots L_{2}\circ L_{1} \tag{7}\]

Fig. 1: Variational quantum circuit with embedding layer, variational layer, and measurement layer reducing 512 features to 4 features
The measurement layer takes information from the four qubits and computes the expectation value, producing a classical output vector of dimension 4. It can be expressed as:

\[M:\left|x\right\rangle\to y=\left\langle x\right|\hat{y}\left|x\right\rangle \tag{8}\]

Overall, the variational quantum circuit can be expressed as:

\[V=M\circ Q\circ E \tag{9}\]

### _Creating a Hybrid Quantum-Classical Neural Network_

The ResNet-18 neural network model was used the extract features from the input images, and the variational quantum circuit significantly reduces the number of features, which catalyzes the network towards classification. According to Figure 2, 512 features are extracted from the initial ResNet-18 average pooling layer and then fed into the variational quantum circuit.

After the variational quantum circuit computes the given quantum logic gates and applies a z-measurement on each qubit, the four dimensional vector is sent to the classical fully-connected (FC) layer, to which it is converted into two values: an expectation value for a fermented brain and an expectation value for a non-demented brain.

Representing the quantum-classical neural network mathematically as \(QCNN\):

\[QCNN=L_{4\xrightarrow{}2}\cdot Q_{512\xrightarrow{}4}\cdot L_{512} \tag{10}\]

where \(L_{512}\) represents the ResNet-18 Layers that extract 512 features, \(Q_{512\xrightarrow{}4}\) is the variational quantum circuit that reduces the 512 features to 4, and \(L_{4\xrightarrow{}2}\) is the fully-connected layer.

### _Defining Hyperparameters_

In machine learning, hyperparameters define the learning process before training the model. Hyperparameters cannot be changed during the training process of the neural network model [9].

The first hyperparameter to be defined as the number of epochs. Epochs are defined as the number of times the whole dataset is passed through the model. In this study, the number of epochs is set to 20 to ensure that the accuracies and losses are able to converge.

The next hyperparameter to be defined as the learning rate. In machine learning, the learning rate defines how much the model changes its weights with respect to the loss gradient function. Generally, the higher the learning rate, the faster the loss function converges and the lower the learning rate, the more intricately and accurately the loss function converges [10].

Another hyperparameter to be defined is the batch size, which is the number of images to be sent through the model in one iteration. The more images used in a batch, the slower the model is trained [11]. However, like the learning rate, time is exchanged for potentially higher accuracies and lower losses. In this study, the chosen batch size was 32 images.

The layers of a neural network are made up of neurons, which can be activated, from which information can be sent to the next layer. The activation function, which determines whether or not a neuron activates, is another important hyperparameter. In this study, the chosen nonlinear activation function is the rectified linear unit (ReLU), which can be defined as [8]:

\[ReLU(x)=max(0,x) \tag{11}\]

The loss function is another important hyperparameter that quantifies how far the model prediction is from the actual value. The chosen loss function for this study is the cross-entropy loss function for binary classification, which can be expressed as [8]:

\[CL=-\sum_{n=1}^{2}y_{n}log(P_{n}) \tag{12}\]

where \(y_{n}\) is the output value computed by the model and \(P\) is the overall probability of the \(n\)th class.

The last hyperparameter is the optimizer, which is an algorithm to minimize the loss function. The selected optimizer for this study is the Adam optimizer [12], which can compute large amounts of data effectively.

All of the hyperparameters are represented in Table I.

\begin{table}
\begin{tabular}{c c c c c} \multicolumn{5}{c}{Hyperparameters for the hybrid and classical models} \\ \hline Epochs & Learning Rate & Loss Function & Optimizer & Batch Size & Activation Function \\
**20** & \(10^{-4}\) & **Cross-Entropy** & **Adam** & **32** & **RELu** \\ \hline \end{tabular}
\end{table} TABLE I:

Fig. 2: Architecture of the Hybrid Quantum-Classical Neural Network
### _Performance Metrics_

To compare the ResNet-18 and the proposed model, the accuracy, recall, precision, and f1-score metrics are used:

\[accuracy=\frac{TP+TN}{TP+TN+FP+FN} \tag{13}\]

\[recall=\frac{TP}{TP+FN} \tag{14}\]

\[precision=\frac{TP}{TP+FP} \tag{15}\]

\[F_{1}=\frac{2*recall*precision}{recall+precision} \tag{16}\]

where \(TP\) represents the true positives, which are data values correctly given a positive label. \(TN\) represents the true negatives, which are data values correctly given a negative label. \(FP\) represents the false positives, which are data values incorrectly given a positive label. \(FN\) represents the false negatives, which are data values incorrectly given a negative label.

For calculating the testing metrics, the same 100 unused non-dementemented and 100 unused demetned brain MRIs were used. Conditions were kept the same to prevent confounding variables to influence the comparison of performance metrics between the two models.

## III Results And Discussion

### _Training_

During the training phase, the accuracy and loss graphs for the classical and hybrid neural networks are shown in Figures 3 and 4.

In Figure 3, the hybrid model consistently has a higher accuracy than the classical model, with the difference of epochs 1-5.

After both models reached convergence, the training accuracy was approximately 0.976 for the hybrid model and 0.915 for the classical model. The corresponding convergence training losses were 0.148 and 0.316 for the hybrid and classical models, respectively.

### _Validation_

In Figure 5, the hybrid model once again consistently has a higher accuracy than the classical model, with the difference of

Fig. 4: Training loss with respect to epoch number for both hybrid and classical models

Fig. 5: Validation accuracy with respect to epoch number for both hybrid and classical models

Fig. 3: Training accuracy with respect to epoch number for both hybrid and classical models

Fig. 6: Validation loss with respect to epoch number for both hybrid and classical models
accuracy during convergence being approximately 0.06. Both models drastically improve in accuracy between epochs 1-4. In Figure 6, the hybrid model consistently has less loss than the classical model, with the difference of loss during convergence being approximately 0.18. Both models initially start with large values of loss and then drastically decrease in loss between epochs 1-5, but less drastically than the training losses.

After both models reached convergence, the training accuracy was approximately 0.976 for the hybrid model and 0.915 for the classical model. The corresponding convergence training losses were 0.148 and 0.316 for the hybrid and classical models, respectively.

### _Testing_

For the hybrid model testing results shown in Figure 7, out of the 100 normal images tested, 95 were correctly classified as normal and 5 were incorrectly classified as demented. Additionally, out of the 100 demented images tested, 98 were correctly classified as demented and 2 were incorrectly classified as normal. In Figure 8, which included the classical model testing results, 89 normal images were correctly classified as normal and 11 normal images were incorrectly classified as demented. The classical model also correctly classified 91 demented images and incorrectly classified 9 demented images as normal.

As shown in Table II, the hybrid model performed better in every metric calculated: accuracy, loss, recall, precision and F1-score. Since it has a higher accuracy and F1-score, it is better at correctly predicting the right label for an image across both classes, demented and non-demented. Since the hybrid model has a better recall and precision than the classical model, it is both better at predicting correct labels out of the true positives and has a better accuracy for predicting positive labels out of the dataset.

## IV Conclusion

This study found that by implementing a variational quantum circuit layer to the ResNet-18 neural network, the training accuracy, validation accuracy, training loss, validation loss, testing accuracy, recall, precision, and F1-score is improved significantly. The proposed hybrid model provides better predictions than the original ResNet-18 model due to its inherent quantum nature, which displays another example of quantum supremacy, where a quantum application provides significant speedups or benefits over a classical one [13]. This framework of a hybrid model can be applied to other binary classification systems, as ResNet-18 is generally flexible and the variational quantum circuit can reduce a large number of parameters. This hybrid model has potential to provide more accurate results for dementia from MRI diagnosis and improve patient outcomes. In the future, more layers of the proposed model can be parameterized by a quantum circuit to improve accuracy and the model can be trained on other datasets for binary classification tasks.

## V Acknowledgment

This research was done independently by the author, who is affiliated with the Thomas Jefferson High School for Science and Technology. No funding or mentoring was associated with this research.

Fig. 8: Confusion matrix for demented and non-demented labels for the classical model

Fig. 7: Confusion matrix for demented and non-demented labels for the hybrid model