I am wondering if it is possible to generalize the Hadamard test for computing $\text{Re} \langle \phi | U | \psi \rangle$ (different states for left and right operands).


1 Answer 1


Yes, it is possible to adapt the Hadamard test to compute the real and imaginary parts of expectation values of the form $\langle \phi | U | \psi \rangle$, as shown in the figure below, where $|\psi \rangle \langle 0|$ corresponds to a unitary operation that prepares the state $|\psi \rangle$ starting from the fiducial state $|0\rangle$ and, as usual, the filled (empty) circle denotes a controlled-operation where the nontrivial action on the target-qubit(s) is triggered when the control-qubit is in state $|1\rangle$ ($|0\rangle$). As in the standard version of the Hadamard test, upon setting $b = 0$ ($b = 1$), the real (imaginary) part is given by $2P_0-1$, where $P_0$ is the probability of measuring the ancillary qubit in state $|0\rangle$. This is valid for an arbitrary number of qubits $n$ in the main register.

enter image description here

P.S.: As @gIS noted in a comment below before I made this update, this circuit does not compute $\textrm{Re}\langle\phi|U|\psi\rangle$ in the same sense that the SWAP test computes $|\langle\phi|\psi\rangle|^2$ or the original Hadamard test computes the real and imaginary parts of $\langle\psi|U|\psi\rangle$. Indeed, this adaptation of the Hadamard test does not take $|\psi\rangle$ and $|\phi\rangle$ as inputs, but rather encodes them via unitaries within the circuit itself. In fact, there cannot be a circuit taking $|\psi\rangle$ and $|\phi\rangle$ and giving as output the real part of $\langle\phi|U|\psi\rangle$, as that quantity would depend on the unphysical global phases of the ket states.

  • $\begingroup$ Thanks! What is the gate $S^\dagger$? The $b$ parameter is litterally an exponent providing the identity for $b=0$ and $S^\dagger$ for $b=1$, right? $\endgroup$
    – francler
    Jan 25 at 16:00
  • $\begingroup$ $S$ usually represents the square root of the Pauli $Z$ operator. Its unitary matrix is $\begin{equation}\begin{pmatrix}1 & 0 \\ 0 & i \end{pmatrix}\end{equation}$ Therefore $S^\dagger$ is the conjugate of this $S$ gate namely $\begin{equation} S^\dagger = \begin{pmatrix}1 & 0 \\ 0 & -i \end{pmatrix}\end{equation}$ $\endgroup$
    – Callum
    Jan 25 at 16:51
  • $\begingroup$ You use $S^\dagger$ for the imaginary part. If you want the real part use $(S^\dagger)^{0} = I$. $\endgroup$
    – Callum
    Jan 25 at 18:13
  • $\begingroup$ Thanks! Concerning the controlled operation with the empty circle, can this be achieved by putting an X on the ancilla and then the standard $|1\rangle$ controlled gate right after? Apart from this, do you have any reference about that test? $\endgroup$
    – francler
    Jan 26 at 7:56
  • $\begingroup$ The gate with the empty circle is a $|0\rangle$ controlled gate. Yes you could use $X$ gates on either side of the gate on the control qubit of a standard "filled circle" gate. Some quantum libraries like qiskit and pytket support $|0\rangle$ controlled gates directly. $\endgroup$
    – Callum
    Jan 26 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.