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.
We can now write this in a Vector form
The actual computation is as follows and the result is a scalar.
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.
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.
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
This comes from taking the outer product of our states
Now let us apply the Hadamard Gate (H) to the first qubit in the 0 state
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.
Now let us apply another Hadamard on the left most qubit which gives the following
Expanding this out in terms of our left most qubit, we get the following
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.
With some re-arrangement we can see this becomes
This shows that we can test for orthogonality on and 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 and are the same. Similarly we can look at the probability of return a state 1 on the ancillary.
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 and . If orthogonal, then we expect to see the state 1 with a probability of . Basically we have found a way that we can check using just a few gates the level of overlap of our two original states.
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.
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.