Quantum Machine Learning, an Introduction

Quantum Machine Learning

The Hello World of QML

We’ll start at the beginning and show the Quantum analog of the some of the typical algorithms. Starting with the perhaps one of the most simple but useful algorithms: The Dot Product. We’ll refresh the classical version before introducing the the Quantum Equivalent.

Dot Product in the classical World

Perhaps one of the most useful operations in mathematics. The dot product or inner product can be useful for understand how similar two vectors are. Crucially important in Machine Learning or in fact many processes. Techniques such as cosine similarity use the dot product to determine how similar two vectors are. Each of the vectors could represent a multitude of things. For example in TFIDF – the vectors can represent documents in which each vector encodes the normalized frequencies of word count, with each element of the Vector a normalized word count. Without cover the specific details, but assuming that we have two vectors, we can use the dot product given in the equation below to highlight whether our vectors are different or similar.

A \Cdot B = |A||B| \Cos(\Theta)

We can now write this in a Vector form

A \Cdot B = \Begin{Bmatrix}A &Amp; B &Amp; C \End{Bmatrix} \Begin{Bmatrix}D \\ E \\ F \End{Bmatrix}

The actual computation is as follows and the result is a scalar.

A \Cdot B = Ad + Be + Cf

You can easily see that if you take the normalized vectors A and B to be equal the result will be 1 but an example might be the following toy example where A and B are normalized, it is easily to show that the result is 0 or that the two vectors are orthogonal.

A \Cdot B = \Begin{Bmatrix}1 &Amp; 0 &Amp; 0\End{Bmatrix} \Begin{Bmatrix}0 \\ 1 \\ 0 \End{Bmatrix} = 0

Translating to Dirac notation which is useful in Quantum Information, the dot product can be written as the row vector and column vector as follows, where A and B are the state vectors.

\Left≪ A | B \Right≫

Another way at looking at these expressions is as an overlap function which measures how much of state A and B overlap. Outputting 1 means that A and B are identical, but output values can range from 0 to 1 with zero meaning there are no overlaps at all. Turning now to the Quantum analogue.

Overlap in the Quantum World

How do we achieve this overlap in the Quantum world? Computing the product with a Quantum Circuit is relatively easy. Using a swap test, we can pick up a squared probability of the overlap.

Where can it be used?

Useful in many routines, when distances need to be examined we can use a swap test. We use a Hadamard function on three qubits. One ancillary qubit and the two qubits of our states. Applying the Hadamard to the ancillary qubit we get the following

\Left | S_{0} \Right≫ = \Left | 0, \Psi, \Phi \Right≫

This comes from taking the outer product of our states

\Left | S_{0} \Right≫ = \Left | 0 \Right≫ \Otimes \Left | \Psi \Right≫ \Otimes \Left | \Phi \Right≫

Now let us apply the Hadamard Gate (H) to the first qubit in the 0 state

\Left | S_{1} \Right≫ = \Frac{1}{\Sqrt{2}} \Left( \Left | 0, \Psi, \Phi \Right≫ + \Left | 1, \Psi, \Phi \Right≫ \Right)

Now we can apply the Swap Gate to the quantum states which is controlled on the ancillary qubit – which is 1 on the right hand side of the equation. This will give us the following state.

\Left | S_{2} \Right≫ = \Frac{1}{\Sqrt{2}} \Left( \Left | 0, \Psi, \Phi \Right≫ + \Left | 1, \Phi, \Psi \Right≫ \Right)

Now let us apply another Hadamard on the left most qubit which gives the following

\Left | S_{3} \Right≫ = \Frac{1}{\Sqrt{2}} \Left( \Left | \Frac{1}{\Sqrt{2}} \Left( \Left | 0 \Right≫ + \Left | 1 \Right≫ \Right), \Psi, \Phi \Right≫ + \Left | \Frac{1}{\Sqrt{2}} \Left( \Left | 0 \Right≫ + \Left | 1 \Right≫ \Right), \Phi, \Psi \Right≫ \Right)

Expanding this out in terms of our left most qubit, we get the following

\Left | S_{3} \Right≫ = \Frac{1}{2} \Left | 0 \Right≫ \Left( \Left | \Psi, \Phi \Right≫ + \Left | \Phi, \Psi \Right≫ \Right) + \Frac{1}{2} \Left | 1 \Right≫\Left( \Left | \Psi, \Phi \Right≫ - \Left | \Phi, \Psi \Right≫ \Right)

Interestingly what we have here is now an expression that looks at the sum of our swapped terms and the difference between these swapped terms. We can immediately see properties of this system. Looking at the first term we have term that will be maximal or twice the original state if equal. Looking at the second term, this will be lowest then the states are the same. Lets compute the probabilities more explicitly.

P(\Left | 0 \Right≫) = \Frac{1}{4}{ \Bigg| \Left( \Left | \Psi, \Phi \Right≫ + \Left | \Phi, \Psi \Right≫ \Right) \Bigg|}^2

With some re-arrangement we can see this becomes

P(\Left | 0 \Right≫) = \Frac{1}{2} + \Frac{1}{2} {\Bigg| \Left≪ \Psi | \Phi \Right≫ \Bigg|}^2

This shows that we can test for orthogonality on \Psi and \Phi if we see the state 0 with a probability of half. If we were to see the state 0 with a probability of one then we can conclude the \Psi and \Phi are the same. Similarly we can look at the probability of return a state 1 on the ancillary.

P(\Left | 1 \Right≫) = \Frac{1}{2} - \Frac{1}{2} {\Bigg| \Left≪ \Psi | \Phi \Right≫ \Bigg|}^2

This is consistent with what we found before, if you measure a 0 with certainty then you know that there is maximal overlap between the states \Psi and \Phi. If orthogonal, then we expect to see the state 1 with a probability of \Frac{1}{2}. Basically we have found a way that we can check using just a few gates the level of overlap of our two original states.

Swap Routines

As was mentioned, we need to examine how we can create a swap process. It requires just two gates, a NOT and one of the favorite gates: the Hadamard. If we have two qubits, we can show the following matrix.

Swap = X \Otimes H = \Begin{Bmatrix} 1 &Amp; 0 &Amp; 0 &Amp; 0 \\ 0 &Amp; 0 &Amp; 1 &Amp; 0 \\ 0 &Amp; 1 &Amp; 0 &Amp; 0 \\ 0 &Amp; 0 &Amp; 0 &Amp; 1 \\ \End{Bmatrix}

Summary

Here we have shown how one the overlap or effectively the inner product or dot product can be created from a relatively simple quantum circuit. These techniques can be used in a variety of different circuits such as Quantum K-means clustering.

For more details, please see one of the Quantum Machine Learning books on your journey towards Quantum Machine Learning from Peter Wittek, and Maria Schuld.