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