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

  • 2
    $\begingroup$ A related idea. If you want to know the difference quantified by $|\langle \psi |\phi \rangle |^2$, then you can use swap test. $\endgroup$
    – narip
    Commented Jan 7, 2023 at 1:25
  • 1
    $\begingroup$ 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? $\endgroup$ Commented Jan 7, 2023 at 4:08
  • $\begingroup$ 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. $\endgroup$
    – 254po
    Commented Jan 7, 2023 at 4:33
  • 1
    $\begingroup$ There is a smart way to compute the Euclidean distance. Would this be helpful? $\endgroup$
    – rhundt
    Commented Jan 7, 2023 at 15:38
  • 1
    $\begingroup$ 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 $\endgroup$
    – glS
    Commented Jan 7, 2023 at 15:41

1 Answer 1


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

  • 1
    $\begingroup$ 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]. $\endgroup$
    – 254po
    Commented Jan 7, 2023 at 4:27
  • 3
    $\begingroup$ @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. $\endgroup$ Commented Jan 7, 2023 at 8:02
  • 1
    $\begingroup$ @Martin Vesely, thanks for your comment. I think it helps me understand better. $\endgroup$
    – 254po
    Commented Jan 7, 2023 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.