Quantum Neural Networks (QNNs) with MATLAB
Discover how to implement quantum neural networks (QNNs) using MATLAB Support Package for Quantum Computing. This demonstration shows you how to train a hybrid quantum-classical neural network for a classification problem that is nonlinearly separable. Learn how to combine quantum computing and AI technologies within the MATLAB® ecosystem to tackle complex data problems efficiently.
Highlights:
- Understand the basics of QNNs and their applications.
- Learn how to set up and train a QNN using MATLAB.
- Explore the integration of quantum layers into classical neural networks.
- See how quantum computing can accelerate neural network training and performance.
Published: 8 Jan 2025
Welcome to our exciting journey to learn about the Quantum Neural Networks. Today, we will explore how you can combine quantum computing and AI technologies using MATLAB. By the end of this video, you will understand what Quantum Neural Networks are, how they work, and why should we use a Quantum Neural Network. Plus, we will guide you on how to implement them in MATLAB. Let's dive in.
If you would like to follow along with this demo, you will need quantum computing support package installed. To install the support package, visit our webpage on quantum computing with MATLAB, and click on this button and have it installed on your system. Then visit our example page on solving the XOR problem using Quantum Neural Network, also linked in the description below. Click on this copy command, open MATLAB, and paste it in the command window. Click, Enter, and you will see how quantum computing-enabled network trains and performs.
This example is built in a MATLAB live script, providing detailed explanations between code blocks. We will focus on the quantum computing aspect. So if you are new to neural network design, check out the online course linked in the description for more on network architecture. Let us begin with the data and task at hand. Below are the lines of code that generate random xy coordinate pairs for our example. We will label each point based on the coordinates using this specified condition.
For instance, if a point meets this condition, we will label it yellow. Otherwise, it will be labeled blue. Here is what the generated data looks like. Before we jump on Quantum Neural Network, let us see how quantum computing accelerates the neural networks. The generated data set we just saw uses mapping function similar to the exclusive OR function. Solving an XOR problem for two-dimensional data is straightforward.
In general, AI is beneficial for problems with many features and complex relationships between features and labels, which are not easily captured by simple rules. Therefore, the purpose of this simple neural network example is not to use quantum computing on an otherwise unsolvable problem. Instead, the goal is to practice encoding classical data onto qubits, and integrating a quantum layer into a classical neural network.
In this example, we want to explore the potential of quantum computing in addressing complex data problems. For instance, when a data set has more features, classical training becomes slow and memory intensive. In our example, the input size is 2. But what if we had 100 features? In that case, a single layer of a neural network would require 12,800 operations. However, mapping 100 features onto 10 quantum bits, or qubits, would greatly reduce the number of operations needed, thanks to qubit superposition and entanglement, allowing for efficient computations.
To learn more about these fundamentals, please refer to our documentation on introduction to quantum computing linked in the description. Let us look at the neural network architecture to see where the Quantum Neural Network fits in. Setting up the network structure in MATLAB is straightforward. First, generate or import your data into MATLAB.
Next, define your network layers using just a few lines of code. You can then set the training options according to your requirements, and proceed to train and test your model on a test data set. The key difference in this setup is the inclusion of the parameterized quantum circuit layer. This PQC layer is what transforms your classical neural network into a Quantum Neural Network, leveraging the principles of quantum mechanics to enhance computational efficiency and performance.
Before we learn what a Quantum Neural Network does, let us first see how the network functions without the quantum layer. This is straightforward. Go to line 22 and replace it with these two new lines. This adds a hidden layer, followed by an activation layer. Now, we are back to a simple neural network. You can learn about various MATLAB deep learning layers linked in the description below.
Now, let us understand the Quantum Neural Networks. Let us explore the custom parameterized quantum circuit, or PQC layer, which serves as the quantum layer of the network. And we also refer it as the Quantum Neural Network. If you are curious about building custom deep learning layers, our detailed documentation is linked below. I will explain how your data transform through the PQC layer, which is part of the neural network. This PQC layer is a quantum circuit that we will examine. It learns from a data set to predict whether a given xy pair belongs to the blue class, or the yellow class.
Here is the quantum circuit for the PQC quantum layer. The main elements of the quantum circuit includes two input quantum bits, or qubits, initialized to state 0 for a known starting state for all computation, and three quantum gates. The first two parameterized rotational RX gates acts on the individual qubits, which encodes the input data to the network. The third gate is a controlled NOT gate, which acts as the entanglement block of the Quantum Neural Network, processing the data nonlinearly for this classification task.
For more on quantum gates, see the documentation linked below. The final component is measuring the second qubit specific to this problem, which relies on the understanding of the circuit. From our XOR data set, we have real numbers for x and y, which we want to encode on the qubits. But before that, let me introduce you to two scalar learnable parameters A and B.
These parameters are adjusted during the iterative training process across the entire network, including this quantum layer. To encode the data and learnable parameters into the quantum layer, we multiply A with x as an input rotational angle of the RX gate of the top qubit, and B with y to the rotational angle of the RX gate to the bottom qubit. Applying these rotational angles to the gates and implementing the circuit is straightforward, and is shown in the lines of code below.
The highlighted boxes are functionalities utilized from the MATLAB support package for quantum computing. The next operation on this circuit is to perform a measurement on the second qubit. This quantity gives us the difference in counts of the qubit being in the state 0, and the 1 state. For this quantum circuit, the measured quantity expectation Z has a predicted form of cos theta 1 times cos theta 2 based on the states of the qubits. You then use the conditional expectation of Z equal to 0 to determine the classification boundaries of the XOR problem.
So a single measurement on this quantum circuit allows us to label a pair of xy numbers as yellow or blue. And that is basically what we wanted to achieve. With this setup, your trained neural network with quantum layer is now ready. You can observe the training performance where the accuracy increases, and the loss function decreases over successive iterations of the data set.
The results from the test data sets show larger values on the diagonal entries of the confusion matrix, indicating accurate predictions for the corresponding classes. Small off-diagonal values indicate minor misclassifications between the corresponding classes.
In this demonstration, we have demonstrated how to train a network that integrates both quantum and classical elements. This approach allows us to enhance our models effectively. To scale your model, create circuits with additional qubits to incorporate more features. Experiment with various entanglement blocks and quantum gates within the circuit layer to develop more complex and nonlinear models.
Finally, measure the output of the quantum network based on the specific problem you are addressing. Now, you understand more about Quantum Neural Networks, why they are used, and how they work. Give it a try yourself and enjoy the quantum world with MATLAB. If you have any questions about this demo, or quantum computing, please leave a comment on this video. Please give it a try. Explore quantum computing with MATLAB.