# What is a Hadamard test?

What is a Hadamard test? I have seen this term at many places in video lectures and on various weblinks. A detailed answer on this would be a great help. This is what Wikipedia says, but I really could not understand anything.

Edit
I have some knowledge of quantum computing. I know about different gates, quantum states, etc. I saw this term while trying to understand the Pennylane VQLS tutorial. I am greatly struggling with quantum computing because in my study of quantum computing, suddenly some term comes up that I have not seen in general tutorials or video lectures available online.

• What is your background? How familiar are you with the theory, mathematics, and physics of quantum computing? And what specific parts do you not understand from Wikipedia? Editing these into your question will help people answer your question better. Commented Oct 9, 2023 at 18:14
• Thank you @FDGod for the response. I have edited the question.
– Manu
Commented Oct 9, 2023 at 19:10
• "I really could not understand anything [in the Wikipedia page]" doesn't make for a good question. For example, why isn't the first sentence in the wiki page a proper answer to the question? I can see that the page can be hard to parse, but if you don't spell out at least some of the confusion you're actually having people can't know what you already do or don't understand about the topic
– glS
Commented Oct 9, 2023 at 19:22

In many quantum algorithms, we need to compute the expectation value $$\langle\psi|U|\psi\rangle$$, where $$U$$ is an $$n$$-qubit unitary operator and $$|\psi\rangle$$ is an $$n$$-qubit state. The Hadamard test is a quantum algorithmic primitive used to compute this expectation value using one ancilla qubit and some controlled operations[1],[2].

And since $$U$$ is generally not Hermitian, the expectation value can be a complex number. The Hadamard test is able to measure the real and imaginary parts in two separate measurements.

In Hadamard test we use the following circuit for estimating the real part of $$\langle\psi|U|\psi\rangle$$

You can easily verify that the probability of measuring the ancilla qubit to be in state $$|0\rangle$$ equals $$\frac{1}{2}(1+\mathrm{Re}[\langle\psi|U|\psi\rangle])$$

Similarly, we use the following circuit for estimating the imaginary part

where $$S$$ is the phase gate. $$S = \begin{pmatrix}1 & 0 \\ 0 & i \end{pmatrix}$$ The probability of measuring the ancilla qubit to be in state $$|0\rangle$$ equals $$\frac{1}{2}(1+\mathrm{Im}[\langle\psi|U|\psi\rangle])$$

To compute the expectation value with absolute error $$\epsilon$$ we need to repeat this procedure $$\mathcal{O}(1/\epsilon^2)$$ times.

Hadamard test is part of many algorithms. For example:

• If $$|\psi\rangle$$ equals the basis state $$j$$, Hadamard test can be use to get the diagonal element $$u_{jj}$$ for the matrix $$U$$. This technique used in Aharonov–Jones–Landau algorithm to compute the Jones polynomial of the plat closure of a braid[3].
• We can use Hadamard test to compute the trace of the matrix $$U$$ by choosing $$|\psi\rangle$$ to be the maximally mixed state. This technique used by Shor and Jordan to compute the Jones polynomial of the trace closure of a braid in the DQC1 model of computation[4].
• If $$|\psi\rangle=|\phi_1\rangle|\phi_2\rangle$$, and $$U$$ is the $$n$$-qubit SWAP gate, the probability of measuring the ancilla qubit to be in state |0⟩ equals $$\frac{1}{2} + \frac{1}{2}|\langle\phi_1|\phi_2\rangle|^2$$. So, we can use this circuit to estimate the overlap of two the quantum states $$|\phi_1\rangle$$ and $$|\phi_2\rangle$$. This is known as the swap test. It is commonly used in quantum machine learning[5].

• Hadamard test is also used to approximate tensor network contraction[6], to extract information about the gradient of the objective function in variational algorithms[7], ...etc.

[1] Childs. Lecture Notes on Quantum Algorithms.

[3] Aharonov, Jones and Landau. A Polynomial Quantum Algorithm for Approximating the Jones Polynomial

[4] Shor and Jordan. Estimating Jones polynomials is a complete problem for one clean qubit

[6] Arad and Landau. Quantum computation and the evaluation of tensor networks.

• Thank you @Egretta.Thula for this great answer. Can you explain me a few more things like what is Hermitian? What are the probable values of U? What I understand from your response is that if I have state psi and a matrix U, and if I calculate <psi|U|psi> by doing some kind of matrix multiplication, then the value 1/2(1+ Re<psi|U|psi> ) will be equal to the probability computed by doing the measurements for some particular number of shots(say 1000) by the measurment gate?
– Manu
Commented Oct 10, 2023 at 18:58
• If my understanding is correct, please explain what is the application of this?
– Manu
Commented Oct 10, 2023 at 18:59
• U can be any unitary. However, for practical reason we should consider only unitary matrices that can be implemented in polynomial size quantum circuit. For Hermitian matrices see here. If $U$ is Hermitian, the quantity $\langle\psi|U|\psi\rangle$ is real. Commented Oct 12, 2023 at 17:16
• I updated my answer to include some applications of Hadamard test. Commented Oct 12, 2023 at 17:16
• Thank you @Egretta.Thula for great help.
– Manu
Commented Oct 12, 2023 at 17:24

Like others said, not sure what answer you are expecting. Hadamard gate is a single qubit quantum gate. In simple terms, this gate maps the initial state of |0> to an equal superposition of $$|0\rangle$$ and $$|1\rangle$$. It maps $$|1\rangle$$ also to an equal superposition of $$|0\rangle$$ and $$|1\rangle$$ but with a different relative phase. $$H|0\rangle$$ and $$H|1\rangle$$ both have magnitude $$1$$ based on the formula and are orthogonal to each other. Based on that criteria, you run the test, and it will make more sense now, hopefully.

• Hi and welcome to Quantum Computing SE. You described Hadamarda gate, however, Hadamard test is a method how to produce random variable with possible outputs -1 and 1 and expected value direct d by a quantum state and a unitary gate. See details here: en.m.wikipedia.org/wiki/Hadamard_test_(quantum_computation) Commented Oct 10, 2023 at 6:25
• Thank You for the feedback, I thought giving an overview about Hadamard gate to OP might help them understand the test better. Commented Oct 10, 2023 at 14:42