# How to calculate the state overlap of two quantum circuits?

Given two parametrized circuits, is there a way of calculating the state overlap of the two of them? I have seen that with Qiskit you can do it through this class: https://qiskit.org/documentation/stubs/qiskit.algorithms.state_fidelities.BaseStateFidelity.html#qiskit.algorithms.state_fidelities.BaseStateFidelity

However, I need to calculate the following inner product: $$\langle\psi(x)|\phi(y)\rangle$$, instead of $$|\langle\psi(x)|\phi(y)\rangle|^2$$. Is there any way to calculate it without it being squared?

Thank you in advance for the help!

• Not straightforwardly: when you measure something, it needs to be a real number! The trick, often, is to get something like $|\langle\psi(x)|\phi(y)\rangle|^2$ and then stick some unitary in the middle (will depend on what your states are) and measure again. That usually gives you enough information to be able to work backwards to the quantity you want. Jun 23 at 13:23

Given two unitaries such that $$U_1|0\rangle = |\psi(x)\rangle$$ and $$U_2|0\rangle = |\phi(y)\rangle$$, do the unitary $$U_t = U_2 U_1^{\dagger}$$ on $$|\phi(y)\rangle$$ controlled by a Hadamard transformed control qubit, perform an extra Hadamard on the control qubit, and then measure the control qubit. After performing this measurement multiple times, if a $$|0\rangle$$ result is treated as a 1 and a $$|1\rangle$$ as -1, the expected value will be the real part of the inner product. Change the circuit to perform a $$S^{-1}$$ on the controlled qubit just before the controlled unitary (so the state of the control qubit is $$|0\rangle - i|1\rangle$$ prior to entanglement) and the measurement's expected value will be the imaginary part of the inner product. Doing both measurements enough times to estimate the expected values will provide the complete inner product.
This works because $$\langle \phi(y) |U_t|\phi(y)\rangle = \langle\psi(x)|\phi(y)\rangle$$ and these real and imaginary circuits will respectively leave the system in the state $$\frac{1}{2}(|0\rangle \otimes(I + U_t)|\phi(y)\rangle + |1\rangle\otimes(I - U_t)|\phi(y)\rangle)$$ and $$\frac{1}{2}(|0\rangle\otimes(I - i U_t)|\phi(y)\rangle + |1\rangle\otimes(I + i U_t)|\phi(y)\rangle)$$. The probably of measuring $$|0\rangle$$ and $$|1\rangle$$ in each respective case are $$\frac{1}{4}(2 + \langle\psi(x)|\phi(y)\rangle + \langle\phi(y)|\psi(x)\rangle)$$ and $$\frac{1}{4}(2 - \langle\psi(x)|\phi(y)\rangle - \langle\phi(y)|\psi(x)\rangle)$$ for real case and $$\frac{1}{4}(2 - i\langle\psi(x)|\phi(y)\rangle + i\langle\phi(y)|\psi(x)\rangle)$$ and $$\frac{1}{4}(2 + i\langle\psi(x)|\phi(y)\rangle - i\langle\phi(y)|\psi(x)\rangle)$$ for imaginary. Subtracting the second from the first in each case, the results are $$\textit{Re}(\langle\psi(x)|\phi(y)\rangle)$$ and $$\textit{Im}(\langle\psi(x)|\phi(y)\rangle)$$, and the expected value if converted to -1 and 1 is equivalent to this probability subtraction.