Inspiration
I have always been interested in programming neural networks and saw this hackathon as a great opportunity to explore how quantum computing can be applied to them.
What it does
This program provides the tools to build an AI model using a quantum neural network (QNN). This program will create a QNN and optimize it using training data. The specific use is very arbitrary and relies on the training data you supply it. The QNN will take in a vector of length of the number of q-bits used and output a vector with the same length. The network will take a vector of q-bit states as its input. This allows you to train the network on quantum data. The network can all be trained on binary data as well; you would just then only use q-bits in the |0> or |1> state. The same input will be run through the QNN 1024 times, and the network will output the frequency of each state. The state with the highest frequency can be interpreted as the QNN's output vector. The program also uses training data to optimize the QNN through a method called gradient descent. After the QNN has been optimized, a file is created storing the values for all the parameters of the circuit.
How I built it
To understand a QNN, you first need to understand a classical neural network. A NN is just a really complicated function that takes in a vector of numerical values and outputs another vector of numerical values. The NN holds many columns of nodes, where a column represents a layer in the NN. The first column is the input vector and the last is the output vector. Columns in the middle are called hidden layers. Each node holds a numerical value and is connected to every other node in the next layer through a weight. Data passes through the network, starting from the input nodes to the output nodes. The value in a node in the next layer is calculated by summing the product of all the previous nodes' values and their corresponding weights. This is repeated for every node in that layer. Because each layer in the network can be represented as a vector, this operation can be interpreted as a vector on matrix multiplication. So what the network is really doing is taking a vector and multiplying it by a matrix for every layer it passes through.
Next, I needed to translate this idea into something that can be implemented on a quantum computer. A vector in a quantum computer can be represented as the states of the q-bits. Because quantum gates can be represented as matrices, you can use a series of parameterized quantum gates to represent a general matrix. For each layer in the QNN 3 parameterized gates that rotate the state around each of the 3 axes by theta (rx, ry, and rz). Then all the qubits are entangled together using a CNOT gate, and the next layer begins. Using this, the program can create a QNN.
The values inside the matrices are initially just random values, making the output useless. In order to train the model, you treat the values within the matrix as parameters and make adjustments to them to minimize an error function. There are many different ways to create an error function, but here is how my program calculates error. First, the input data is run through the QNN 1024 times, and the probabilities of each qubit being read in the |1> state is recorded. Next, the sum over all the qubits of the difference between observed and expected squared is calculated. The number is divided by the number of qubits, and this is the error of a single data point. Finally, the error from all the data points is summed together; this is the total error. This error function has layers*qubits*3 parameters, one for each parameterized gate. Next the the gradient of the error function is calculated. The parameters are changed negatively proportionally to that gradient vector. This is repeated until the error function has reached a local minimum; hopefully, this value is 0.
Challenges we ran into
One issue with QNN is that it does not run on a quantum computer, so the program is very slow because every time you want to pass data through the QNN, you have to simulate the quantum circuit, which takes a relatively long time. This is not really an issue when you just want to use the model, but it is more of a problem when training the model. To calculate the gradient, it takes 2* the number of parameters in circuit simulations because you have to simulate the circuit every time you change a parameter. This makes the training process very slow when not working on a quantum computer.
What's next for Quantum Neural Network
This Quantum Neural Network set up the framework to train a model, but does not provide any specific implementation. I plan to use this QNN and some training data to create a model of something. The nice thing about a QNN is that it can be used to model classical data or quantum data, so there are many different things this QNN could model.
Built With
- python
- qiskit
Log in or sign up for Devpost to join the conversation.