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.$$


Posted 2019-03-16T11:46:32.233

Reputation: 199

1First, 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 – 2019-03-16T14:28:10.253

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 [edit]ed it on your behalf this time.

– Sanchayan Dutta – 2019-03-16T14:39:41.907

@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 – 2019-03-16T15:59:17.760

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 – 2019-03-16T15:59:40.380

Finally, the first equation (133) I report relating a scalar (inner product) with a qubit register. How can that be correct? – Gianni – 2019-03-16T16:02:05.397

@GianniCasonato To also notify a previous commenter, mention their user name with a @ symbol: @peter or @PeterShor will both work. Review the formatting help page. I've edited your first comment. And please go through the MathJax tutorial. Terms like "phi" and "psi" are not very readable when written in words. Please use MathJax to write them as $\phi$ ($\phi$) and $\psi$ ($\psi$) instead. Thanks!

– Sanchayan Dutta – 2019-03-17T07:59:57.550



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.


Posted 2019-03-16T11:46:32.233

Reputation: 4 213

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 – 2019-03-16T20:02:29.320

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 – 2019-03-16T20:25:32.883

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 – 2019-03-16T20:33:38.750

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 – 2019-03-16T21:36:22.003

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 – 2019-03-17T16:50:36.953

@GianniCasonato Glad you got it finally. If interested, I can help you with the summary. All the best! – cnada – 2019-03-17T17:11:50.053

@cnada please tell me the things I need to know to figure it out the estimation of Z? – Aman – 2019-03-18T08:23:17.427

@Aman Why not submit another question ? – cnada – 2019-03-18T09:39:36.187


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*}

Peter Shor

Posted 2019-03-16T11:46:32.233

Reputation: 309

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 – 2019-03-17T16:53:00.080


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!!


Posted 2019-03-16T11:46:32.233

Reputation: 463

2What 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 – 2019-03-18T12:15:45.083

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 – 2019-03-18T12:35:11.243

1In 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 – 2019-03-20T18:01:31.973