Quantum Circuit To Compute Any Inner Product

I'm currently reading the paper Classification with Quantum Neural Networks on Near Term Processors

It shows a method to determine the following quantity:

Where U is a unitary operator acting on $$|z,1\rangle$$. The paper states the following:

I am wondering if there is a way to calculate any general inner product like $$\langle z_2, 1|U|z, 1\rangle$$ or just $$\langle a | b \rangle$$ in general? How could I modify the circuit to do so? (or is there a different methodology to calculate the inner product with different vectors?)

• The inner product between two states can be computed using the SWAP test. Is that what you're looking for? Also, some interesting details regarding efficient implementation on NISQ devices can be found in this paper. Jul 6 '20 at 6:35

We can use the SWAP test to determine the inner product of 2 states $$|\phi\rangle$$ and $$|\psi\rangle$$. The circuit is shown below
The state of the system at the beginning of the protocol is $$|0\rangle \otimes |\phi \rangle \otimes |\psi \rangle$$. After the Hadamard gate, the state of the system is $$|+\rangle \otimes |\phi \rangle \otimes |\psi \rangle$$.
The controlled SWAP gate transforms the state into $$\frac{1}{\sqrt {2}}|0\rangle \otimes |\phi \rangle \otimes |\psi \rangle + |1\rangle \otimes |\psi \rangle \otimes |\phi \rangle$$.
The second Hadamard gate results in $$\frac {1}{2}(|0\rangle|\phi\rangle|\psi\rangle +|1\rangle|\phi\rangle|\psi\rangle +|0\rangle|\psi\rangle|\phi\rangle -|1\rangle|\psi\rangle|\phi\rangle ) \\ =\frac {1}{2}|0\rangle (|\phi\rangle|\psi\rangle +|\psi\rangle|\phi\rangle)+ \frac{1}{2}|1\rangle(|\phi\rangle|\psi\rangle -|\psi\rangle|\phi \rangle)$$
The Measurement gate on the first qubit ensures that it's 0 with a probability of $$P(\text{First qubit}=0)=\frac {1}{2}\Big(\langle\phi|\langle\psi| + \langle\psi|\langle\phi|\Big ) \frac {1}{2}\Big (|\phi\rangle|\psi\rangle + |\psi\rangle |\phi\rangle \Big )=\frac {1}{2}+\frac {1}{2}|\langle\psi|\phi\rangle|^{2}$$ when measured.
The downside of this test is that the qubits cannot be recovered to the same state as before. Hence $$|\psi\rangle,|\phi\rangle|$$ must be prepared multiple times independently in order to get a good probability estimate and hence the value of inner product.