Assuming I am doing statevector simulations, I need to compute an inner product of the type
$$ X_b = \langle\psi | I_0^{\otimes (n-1)} \otimes X | \psi \rangle, $$
where $\psi$ is a generic input state (also $| \textbf{0} \rangle$) with real amplitudes, $n$ is the size of the computational basis with $N_q = log_2 n$ qubits, $I_0$ is the following matrix
$$ I_0 = \begin{bmatrix} 1& 0 \\ 0 & 0 \end{bmatrix}, $$
so that $I_0^{\otimes (n-1)}$ is all zero except for the leftmost upper entry (which is 1), and $X$ the Pauli matrix
$$ H = \begin{bmatrix} 0& 1 \\ 1 & 0 \end{bmatrix}. $$
As you see, $I_0^{\otimes (n-1)} \otimes X$ is not unitary, but I found out on Github a code to actually compute $X_b$ (https://github.com/ToyotaCRDL/VQAPoisson). It does the following: first it considers the input state as it is. Then, it simply computes the difference between the first amplitude and the second one - with no ancilla and no additional gates. In Qiskit code:
# create your circuit for \psi...
sv = self.qinstance.execute(qc).get_statevector(qc)
X_b = np.real(sv[0]*sv[0].conjugate()) - np.real(sv[1]*sv[1].conjugate())
So, my question is: why? If I directly compute $X_b$ with algebra, i obtain $2\psi_1\psi_2$, which is different from $\psi_1^2 - \psi_2^2$. The second question is: is it possible to modify the Hadamard test so that I can compute $X_b$?