## Calculating Dot Product of Two States

3

I've been reading Peter Wittek's Quantum Machine Learning. In chapter 10.2 of this book, the author explains how we can calculate the dot product of two states:

To evaluate the dot product of two training instances, we need to do the following (Lloyd et al., 2013a):
● Generate two states, $$|\psi\rangle$$ and $$|\phi\rangle$$, with an ancilla variable;
● Estimate the parameter Z = $$\|x_i\|^2$$ + $$\|x_j\|^2$$, the sum of the squared norms of the two instances;
● Perform a projective measurement on the ancilla alone, comparing the two states.

The author further goes on explaining the process:

Z times the probability of the success of the measurement yields the square of the Euclidean distance between the two training instances: $$\|x_i - x_j\|^2$$. We calculate the dot product in the linear kernel as $$x_i^Tx_j = \frac{Z-\|x_i-x_j\|^2}{2}.$$

I am not sure what does 'success of measurement' means in this context. From my understanding, we first need to estimate Z (which is done while preparing $$|\psi \rangle$$ and $$|\phi \rangle$$) and then using the prepared states, along with an ancilla, we measure the distance(?) between the two training instances by using a swap test on the above-prepared state. Just for reference, the states are: $$|\psi \rangle = \frac{|0\rangle|x_i\rangle + |1\rangle|x_j\rangle}{\sqrt 2}$$ $$|\phi \rangle = \frac{\|x_i\| |0\rangle - \|x_j\| |1\rangle}{Z}.$$

In the swap test, you usually measure the ancilla qubit in the computational basis and the result would be something like '0101111011101100....'. The probability of the success of the measurement, I think (though I didn't carefully check it), should be the probability of getting 0 (or +1 from +1/-1 if you like). For the relationship between the probability and the estimate of the 'dot product', maybe Michael Walter's lecture (https://staff.fnwi.uva.nl/m.walter/physics491/lecture10.pdf, starting from 10.2) well explained it or you can check it by yourself using only linear algebra.

– raycosine – 2021-01-05T08:11:06.423

is this the swap test, or another algorithm?

– glS – 2021-01-05T11:27:24.413

@glS we only use a swap test in the last part of the algorithm to determine whether or not they are equivalent. – IE Irodov – 2021-01-05T12:51:42.887

it's a bit hard to say without having the book at hand, but the extract you quote mentions doing a projective measurement on the ancilla. I assume the "success of measurement" is meant as the event in which the ancilla is measured in some state (presumably $|1\rangle$?). Does that make sense in the context? – glS – 2021-01-05T15:37:28.920

@gIS The sub-section is available in the link that I have provided, I would have added that here but that would have made the post unnecessarily long. From what I can understand from the book and source provided in the comments is that we use swap test to confirm if they are equivalent or not. If they are not equivalent, the result that we will get is somehow correlated to the distance between the points. If they are exactly equal we will get |0\rangle with 100% probability, otherwise, we'll get an expected value somewhere in between. – IE Irodov – 2021-01-06T16:33:02.647

well from your description I'd say you use the CSwap circuit (likely in the book), whose output has the form $\frac{1}{2}|0\rangle(\psi\rangle+|\phi\rangle)+(...)$, and then the probability of the first ancilla being in the state $|0\rangle$ is $\frac14||\psi\rangle+|\phi\rangle|^2$ while the prob of it being $|1\rangle$ is $\frac14||\psi\rangle-|\phi\rangle|^2$, which is the distance you are interested in – glS – 2021-01-06T18:56:46.943