# Calculate the difference between two vectors?

Let's say we have two vectors a and b, and they have the same size. Is there a quantum circuit to calculate the difference a-b?

To be clear, here's an example:

a = [1.2, 0.7] (the norm is not 1), and b = [1.0, 2.0]

Then, we have

a - b = [0.2, -1.3].

• A related idea. If you want to know the difference quantified by $|\langle \psi |\phi \rangle |^2$, then you can use swap test. Jan 7, 2023 at 1:25
• Welcome to QCSE. @narip s suggestion is probably your best bet - you can get certain relationships between two different ket’s, but it’s not clear what’s meant by the difference of the vectors. For example, what if $|a\rangle=|b\rangle$? What would you expect your circuit to output? Jan 7, 2023 at 4:08
• The problem is updated. I hope it's more precise. It's a fundamental computation, and I don't know whether quantum computing can overperform classical computers at this task. Jan 7, 2023 at 4:33
• There is a smart way to compute the Euclidean distance. Would this be helpful? Jan 7, 2023 at 15:38
• there's no-go theorems about the impossibility of a unitary operation giving as output the superposition of different inputs. See quantumcomputing.stackexchange.com/q/11554/55, physics.stackexchange.com/q/543883/58382, physics.stackexchange.com/q/380268/58382
– glS
Jan 7, 2023 at 15:41

Suppose you have two n-qubit vectors $$|\psi\rangle = U_1|0\rangle^{\otimes n}$$ and $$|\phi\rangle = U_2|0\rangle^{\otimes n}$$. Then you prepare the (n+1)-qubit 0 state, and apply $$H$$ to the first qubit:

$$1/\sqrt{2}(|0\rangle+|1\rangle)|0\rangle^{\otimes n}$$

Using the same qubit as the control, you apply $$U_1$$ if the qubit is in the $$|0\rangle$$ state, and $$U_2$$ if it's in the $$|1\rangle$$ state:

$$1/\sqrt{2} (|0\rangle|\psi\rangle+|1\rangle|\phi\rangle)$$

Then applying $$H$$ to the first qubit again results in:

$$|0\rangle\frac{|\psi\rangle+|\phi\rangle}{2} + |1\rangle\frac{|\psi\rangle-|\phi\rangle}{2}$$

Now you have the vector $$\frac{|\psi\rangle-|\phi\rangle}{2}$$ in the second half of the $$2^{n+1}$$ dimensional quantum state. Furthermore, measuring the first qubit to be in the $$|1\rangle$$ state will have probability:

$$\| \frac{|\psi\rangle-|\phi\rangle}{2}\|^2 = \frac{1}{4} \| |\psi\rangle-|\phi\rangle\|^2$$

• Thanks for your comment! It seems like the output is a number. Is there some way to output a vector? For example, a = [1.2, 0.7] (the norm is not 1), and b = [1.0, 2.0]. Then, we have a - b = [0.2, -1.3]. Jan 7, 2023 at 4:27
• @254po: to get the vector, you have to measure all qubits. If the first one is in state $|1\rangle$, then rest of qubits represents the difference of the input vectors. Of course, the measurement has to be repeated many times to get probability distribution. The probabilities then have to be square rooted to get the amplitudes representing vector members. However, in this way you get only a absolute values of the members. To get signs or imaginary part, complete quantum tomography has to be employed to reconstruct the state. Note that the tomography is exponential complex in number of qubits. Jan 7, 2023 at 8:02
• @Martin Vesely, thanks for your comment. I think it helps me understand better. Jan 7, 2023 at 20:48