# How to calculate inner product of quantum states with other method than swap test? [duplicate]

In connection to this question, I am wondering how to calculate value $$\langle \psi|\phi \rangle$$ for arbitrary quantum states $$|\psi\rangle$$ and $$|\phi\rangle$$.

A swap test is able to return only $$|\langle \psi|\phi \rangle|^2$$ which means that sign of the product is forgotten in case the inner product is real. Moreover, information about real and imaginary parts is lost in case of complex result.

Do you know about any more general method how to calculate the inner product than swap test?

• A method for finding $\langle\psi|\phi\rangle$ would let us observe the global phase which is unobservable. To see this set $|\psi\rangle = |0\rangle$ and $|\phi\rangle=e^{i\theta}|0\rangle$. Therefore, no such method exists. May 28, 2021 at 18:14
• There are methods to estimate the inner product see quantumcomputing.stackexchange.com/q/6339/11793 which shows how to get the real part. You can use a similar trick to estimate the imaginary part of the inner product, thus giving you an estimate of the whole thing. Though you would need to be able to prepare the states and run the circuit over and over again. May 28, 2021 at 18:31
• @Condo That only works if you are given a state that already incorporates $|\psi\rangle$ and $|\phi\rangle$ with a fixed phase relationship. If all you have is two separate states, your quoted method won't work. May 28, 2021 at 18:41
• BTW: It's also interesting to consider how this plays out in the swap test where the measurable quantity is the absolute value of $\langle\psi|\phi\rangle$. Feb 10, 2022 at 0:12
• The existence of global phase is really an artifact of using kets to represent quantum states. In a sense, it is more appropriate to use a tensor product of a ket $|\psi\rangle$ and its adjoint $\langle\psi|$. When we apply some $M$ to $|\psi\rangle$, its adjoint $M^\dagger$ is automatically applied to $\langle\psi|$. This is clear in density matrix formalism where e.g. unitary evolution is $U\rho U^\dagger$ and which avoids the issue of the unobservable global phase. Feb 10, 2022 at 0:13

I doubt that this is possible. Given a state $$|\phi\rangle$$, we have no method for distinguishing it from $$e^{i\varphi}|\phi\rangle$$ for any phase $$\varphi$$. This means that we have no way of distinguishing between $$\langle\psi|\phi\rangle\qquad \mathrm{vs.}\qquad e^{i\varphi}\langle\psi|\phi\rangle.$$ Specifically, we can choose $$\varphi$$ such that $$e^{i\varphi}\langle\psi|\phi\rangle=|\langle\psi|\phi\rangle|.$$ We thus should be precluded from measuring anything other than this absolute value.
To get around this constraint, we would need to set up a superposition of $$|\phi\rangle$$ and $$|\psi\rangle$$ like $$|\Psi\rangle\propto |\psi\rangle+|\phi\rangle.$$ Only then could we get around the global phase problem. I would be interested in seeing a circuit that can take $$|0\rangle|\psi\rangle|\phi\rangle\to|\Psi\rangle|a\rangle|b\rangle$$ for any generalized ancilla $$|0\rangle$$ and final states $$|a\rangle$$ and $$|b\rangle$$. If I had to guess, I would say that it is impossible to perform this transformation with a fixed relative phase in $$|\Psi\rangle$$. We might be happy saying that $$|0\rangle|\psi\rangle|\phi\rangle\to\frac{|\psi\rangle+|\phi\rangle}{\mathcal{N}}|a\rangle|b\rangle$$ for some normalization constant $$\mathcal{N}$$, but then we would need to also be happy with the transformation $$|0\rangle|\psi\rangle e^{i\varphi}|\phi\rangle\to\frac{|\psi\rangle+e^{i\varphi}|\phi\rangle}{\mathcal{N}}|a\rangle|b\rangle,$$ but that is nonsensical for the same deterministic transformation, because the initial states $$|0\rangle|\psi\rangle |\phi\rangle$$ and $$|0\rangle|\psi\rangle e^{i\varphi}|\phi\rangle$$ are indistinguishable.