Path: blob/main/notebooks/ch-demos/variational-quantum-regression.ipynb
3855 views
Variational Quantum Regression
ParseError: KaTeX parse error: \newcommand{\braket} attempting to redefine \braket; use \renewcommandIntroduction
Here we create a protocol for linear regression which can exploit the properties of a quantum computer. For this problem, we assume that we have two data sets, x and y, where x is the independent data and y is the dependent data. There are N data points in each data set. We first want to fit this data to the following equation:
and then we will include higher powers of x. First, we will theoretically explore this proposed algorithm, and then we will tweak the code slightly so that it can be run on a real quantum computer. This algorithm has no known advantage over the most widely-used classical algorithm (Least Squares Method), but does nicely demonstrate the different elements of variational quantum algorithms.
Variational Quantum Computing
Variational quantum computing exploits the advantages of both classical computing and quantum computing. In a very general sense, we propose an initial solution to a problem, called an ansatz. In our case our ansatz will be an ansatz parametrised by a and b. We then prepare our qubits (the quantum equivalent of bits on a normal computer) and test how good the ansatz is, using the quantum computer. Testing the ansatz equates to minimising a cost function. We feed the result of this cost function back to the classical computer, and use some classical optimisers to improve on our ansatz, i.e. our initial guesses for a and b. We repeat this process until the ansatz is good enough within some tolerance. 
Translate to Quantum Domain
We now need to explore how we will translate the data set, y, onto a quantum computer. Let us think of y as a length N vector. The easiest way to encode this data set onto a quantum computer is by initialising qubits in the state , where
and is a normalisation factor.
Now we propose a trial solution, or ansatz, which is parametrised by a and b, as follows:
where is again a normalisation factor.
Due to the definition of the tensor product and the fact that the general statevector of a single qubit is a vector of length 2, qubits can encode length- vectors.
Cost Function
Our proposed cost function, which we wish to minimise is equal to
This computes the normalised fidelity (similarity) of and . We see that if and are equal, our cost function will equal 0, otherwise it will be greater than 0. Thus, we need to compute this cost function with our quantum hardware, and couple it with classical minimising algorithms.
Computing Inner Products on a Quantum Computer
It is clear we now need a quantum algorithm for computing inner products. Let us go through the theory of computing the inner product here, which will be translated to quantum hardware in a couple of sections.
Firstly, assume we have a state:
where we want to find the inner product, . Applying a Hadamard gate on the first qubit, we find:
This means that the probability to measure the first qubit as in the computational basis equals:
This follows because:
After a simple rearrangement, we see that
It follows from a similar logic that if we apply a phase rotation on our initial state:
then the probability of the same measurement:
We can then combine both probabilities to find the true . For this work, we assume that our states are fully real, and so just need the first measurement.
Code Implementation - Theoretical Approach
It should be noted here that qiskit orders its qubits with the last qubit corresponding to the left of the tensor product. For this run through, we are computing the inner product of length-8 vectors. Thus, we require 4 qubits () to encode the state:
Finally, in order to measure the probability of measuring the bottom (leftmost) qubit as in the computational basis, we can find the exact theoretical value by finding the resultant statevector and summing up the amplitude squared of the first entries (i.e. half of them). On a real quantum computer, we would just have to perform the actual measurement many times over, and compute the probability that way. We will show the theoretical approach in practice first.
Now, let's draw the required diagram for theoretically computing the inner product of any two states. Note that the only difference between this circuit diagram and the real, practical diagram for actually running on a quantum computer is that we do not measure the left-most qubit in the computational basis. Again, note that the left-most qubit corresponds to the bottom qubit.
Now let's build a function around this circuit, so that we can theoretically compute the inner product between any two normalised vectors.
Now, let's build a function to compute the cost function associated with any choice of a and b. We have set up x and y such that the correct parameters are (a,b) = (1,0).
Now putting everything together and using a classical optimiser from the scipy library, we get the full code.
Code Implementation - Practical Approach
In order to modify the above slightly so that it can be run on a real quantum computer, we simply have to modify the inner_prod function. Instead of theoretically extracting the probabilility of measuring a 0 on the leftmost qubit in the computational basis, we must actually measure this qubit a number of times and calculate the probability from these samples. Our new circuit can be created as follows, which is identical to the theoretical circuit, but we just add a measurement, and hence need a classical bit.
Now, we can build a new inner_prod function around this circuit, using a different simulator from qiskit.
Our cost function calculation is the same as before, but we now just use this new method for computing the inner product, so the full code can be run as follows.
Extending to Higher Order Fits
We can also extend to fitting to quadratic, cubic, and higher order polynomials. The code remains relatively unchanged, but will update the cost function slightly. We can of course use either the theoretical or practical method for computing the inner products in the following cost function. We are now fitting to an n-order polynomial:
Acknowledgements
I would like to thank Dr. Lee O'Riordan for his supervision and guidance on this work. The work was mainly inspired by work presented in the research paper "Variational Quantum Linear Solver: A Hybrid Algorithm for Linear Systems", written by Carlos Bravo-Prieto, Ryan LaRose, M. Cerezo, Yiğit Subaşı, Lukasz Cincio, and Patrick J. Coles, which is available at this link. I would also like to thank the Irish Centre for High End Computing for allowing me to access the national HPC infrastructure, Kay.