# Swap Test for vector difference - how are different sized inputs combined?

I'm working on a similar problem of that raised by Aman in Inner product of quantum states

Concerning the use of Swap Test for calculating the difference of two vectors. An example of the original Lloyd formula is given in Quantum machine learning for data scientists.

And I got stuck at the same point, i.e. what mathematically means the inner product between the two qubits $$\psi$$ and $$\phi$$, which have different size. The explanation in the paper (eq. 133) which says

$$\langle \phi|\psi\rangle = \frac{1}{\sqrt{2Z}}(|a||a\rangle - |b||b\rangle)$$

looks incorrect because it equals an inner product (scalar value) with a qubit! Or am I missing something?

The suggestion of composing $$\phi$$ with tensor products of the identity matrices for matching $$\psi$$ and $$\phi$$ sizes looks a math trick because such composition is not an allowed quantum operation (identity matrix I should be supposed to be a qubit, and it's not of course). I share Aman's doubts on the demonstration of (132) i.e. $$|a-b|^2=2Z|\langle\phi|\psi\rangle|^2.$$

• First, two qubits always have the same size; by definition the dimension of a qubit is 2. If you have two quantum registers, of two dimensions and of three dimensions, then it doesn't really make sense to apply the Swap Test to these two registers. You could apply the Swap Test to the dimension-two register and a dimension-two subspace of the dimension-three register, or a dimension-3 extension of the first register and the second register. But to see whether that would be a good idea, you have to tell us what you're trying to accomplish with the Swap Test. – Peter Shor Mar 16 '19 at 14:28
• Hi, Gianni. Welcome to Quantum Computing SE! Please do not post mathematical expressions as screenshots, but use MathJax instead. Review Why are images of text, code and mathematical expressions discouraged?. I've edited it on your behalf this time. – Sanchayan Dutta Mar 16 '19 at 14:39
• @Peter, I'm trying to apply Lloyd's method for deriving the difference between two vectors (a) and (b) using the Swap test, and also reported in the paper I've mentioned. There two qubit registers psi and phi are introduced and the vector difference is said can be calculated as inner product of those two registers (see formula (132) above. – Gianni Mar 16 '19 at 15:59
• The point is that psi and phi have different size (psi is n+1, where n is the size of the two vectors, and phi is size 1), and then it's not clear how you can perform an inner product between them. Those two register are supposed to be the 2 inputs of the swap test (ancilla apart), but their size has to match. You mention the use of a sub-space for the bigger register (psi in my case), or of an extended subspace for the smaller register (phi), but I don't see how to reflect that both mathematically and into circuit element. Could you be more explicit? – Gianni Mar 16 '19 at 15:59
• Finally, the first equation (133) I report relating a scalar (inner product) with a qubit register. How can that be correct? – Gianni Mar 16 '19 at 16:02

The paper you refer is incomplete and not very right on this part. First a minus sign should be present in : $$|\phi\rangle = \frac{1}{\sqrt{Z}} (|a||0\rangle - |b||1\rangle)$$

Secondly, if you look at the original reference of this procedure on a special case of algorithm but it can be generalized, what you swap is actually the ancilla qubit of $$|\psi\rangle$$ with the qubit in state $$|\phi\rangle$$.

I was mislead by this paper before but found out the trick recently. We should point out also that there is a step of estimation of $$Z$$ before using this algorithm.

• Thanks cnada, I've spot the typo of the minus sign already, and I can fix it. What it's still not clear to me is how |ψ⟩ and |ϕ⟩ are used in the swap test circuit. I'd expect to use them as inputs, but I cannot because they doesn't have same size. Vice versa, looking at the relation for calculation the probability for having the ancilla in state |ϕ⟩, the expected result of vector difference squared module can be get considering |ψ⟩ as swap test output. I couldn't find any paper where this part is described explicitly. , – Gianni Mar 16 '19 at 20:02
• Well you input them in the circuit, add an ancilla qubit for the swap test and apply H, then CSWAP with the ancilla of the $\psi$ only with $\phi$, reapply H and measure the swap test ancilla. I did an implementation myself but I will have to see for sharing it. – cnada Mar 16 '19 at 20:25
• Thanks for this clarification. I see a bit of light on the topic.... That's fine with me but the step of applying the CSWAP to the ψ ancilla only. How can you separate that part from the whole ψ, which is in superimposed (possibly entangled) state? – Gianni Mar 16 '19 at 20:33
• This state is just made of an extra qubit I call its ancilla and another register for the vectors. So applying only on its ancilla is possible no matter what kind of entanglement you are doing. I am sure by looking at the effect of each steps on the states would make more sense of what it does but that's the procedure one needs to do. You cannot do the swap test anyway on registers with different size, and as I said, you don't apply operations on the register containing the vectors. – cnada Mar 16 '19 at 21:36
• Got it! @cnada: thanks so much for the hint! I've followed the idea of partial subs-pace swap applied to psi ancilla and to phi, and done a bit of math for checking how that works through SWAP test steps. I've also managed to simulate and validate the results in my python lib for several vector sizes. I will make a short summary with all the math in order to share the result with others (I couldn't find it fully explained anywhere else). – Gianni Mar 17 '19 at 16:50

You are not swapping the first register (one qubit) with the entire second register ($$k$$ qubits), but just with the first qubit of the second register.

What you need to know is what is meant by $$\langle x | y \rangle$$ when $$x$$ is one qubit and $$y$$ is $$k$$ qubits. The resulting state is the $$k-1$$ qubit state you get when you project one qubit (generally the first one, if it isn't specified) onto the vector $$|x\rangle$$.

So for example, suppose $$|x\rangle = \alpha |0\rangle + \beta |1\rangle.$$

and suppose $$|y\rangle = \frac{1}{\sqrt{2}}\left( |00\rangle + |11\rangle\right).$$

When we take the inner product of $$|x\rangle$$ and $$|y\rangle$$, we get $$\begin{eqnarray*} \langle x | y \rangle &=& \frac{1}{\sqrt{2}} \left(\alpha^* \langle 0 | + \beta^* \langle1 | \right) \big( |0\rangle \otimes |0\rangle + |1\rangle \otimes |1\rangle\big) \\ &=& \frac{1}{\sqrt{2}} \left( \alpha^* \langle 0| 0\rangle \otimes |0\rangle + \alpha^* \langle 0| 1\rangle \otimes |1\rangle + \beta^* \langle 1| 0\rangle \otimes | 0\rangle+\beta^* \langle 1| 1\rangle \otimes |1\rangle \right) \\ &=& \frac{1}{\sqrt{2}} \left( \alpha^* |0\rangle +\beta^* | 1\rangle \right) \end{eqnarray*}$$

• Thanks so much for your clarification. In the meanwhile I went to a similar conclusion based on another answer, but you point is also very useful to me for consolidating the underlying math. – Gianni Mar 17 '19 at 16:53

Starting from the last index, just swap two qubits as long as you have them and then measure the ancilla qubit. This Works fine for me!!

• What does this mean exactly? Swapping qubits will never reduce the number of qubits, so if you keep swapping "as long as you have them", you'll be swapping forever. Or do you mean to swap qubits until you get to the first index? That's still not specified precisely, and I also don't think that's how Lloyd's method is supposed to work. (I think there's only one swap involved.) Could you explain in more detail, and maybe work out an example? – Peter Shor Mar 18 '19 at 12:15
• Here is the general algorithm, we know the two registers haven't an equal number of qubits. qr1 = size of n, and qr2 = size of m. And let m > n d = m - n for i = n up to 0{ cswap(ancilla, qr1[i], qr2[i+d]) } measure(ancilla) – Aman Mar 18 '19 at 12:35
• In Lloyd's algorithm, that the OP is asking about, you swap just one qubit. In the general SWAP test, you swap two $k$-bit registers. What you're describing is some kind of generalization of both of them. – Peter Shor Mar 20 '19 at 18:01