# Help in understanding the algebra of hadamard test

I am new to quantum computing and was looking through the jupyter notebook of Variational Quantum Linear Solver by Qiskit. I came across the hadamard test and was not understanding how it works. It is written there,

$$\frac{|0\rangle \ + \ |1\rangle}{\sqrt{2}} \ \otimes \ |\psi\rangle \ = \ \frac{|0\rangle \ \otimes \ |\psi\rangle \ + \ |1\rangle \ \otimes \ |\psi\rangle}{\sqrt{2}}$$

Applying our controlled unitary:

$$\frac{|0\rangle \ \otimes \ |\psi\rangle \ + \ |1\rangle \ \otimes \ |\psi\rangle}{\sqrt{2}} \ \rightarrow \ \frac{|0\rangle \ \otimes \ |\psi\rangle \ + \ |1\rangle \ \otimes \ U|\psi\rangle}{\sqrt{2}}$$

Then applying the Hadamard gate to the first qubit:

$$\frac{|0\rangle \ \otimes \ |\psi\rangle \ + \ |1\rangle \ \otimes \ U|\psi\rangle}{\sqrt{2}} \ \rightarrow \ \frac{1}{2} \ \big[ |0\rangle \ \otimes \ |\psi\rangle \ + \ |1\rangle \ \otimes \ |\psi\rangle \ + \ |0\rangle \ \otimes \ U|\psi\rangle \ - \ |1\rangle \ \otimes \ U|\psi\rangle \big]$$

$$\Rightarrow \ \frac{1}{2} |0\rangle \ \otimes \ (\mathbb{I} \ + \ U)|\psi\rangle \ + \ \frac{1}{2} |1\rangle \ \otimes \ (\mathbb{I} \ - \ U)|\psi\rangle$$

How do we get the equation "applying hadamard gate to the first qubit"? Especially the fourth term which is negative.

The Hadamard gate $$H$$ transforms the computational basis as follows: \begin{align}H\lvert 0\rangle &= \lvert + \rangle = \frac{\lvert 0\rangle + \lvert 1\rangle}{\sqrt 2} \\ H\lvert 1\rangle &= \lvert - \rangle = \frac{\lvert 0\rangle \color{red}- \lvert 1\rangle}{\sqrt 2}\end{align} So, to elaborate a little bit on the steps involved in the transformation that's confusing you: applying a Hadamard gate to the first qubit equates to applying $$H\otimes I$$ to the whole system, which gives us the result \begin{align*} (H\otimes I)\bigg(\frac{\lvert 0\rangle\otimes\lvert\psi\rangle + \lvert 1\rangle\otimes U\lvert\psi\rangle}{\sqrt 2}\bigg) &= \frac{H\lvert 0\rangle\otimes\lvert\psi\rangle + H\lvert 1\rangle\otimes U\lvert\psi\rangle}{\sqrt 2} \\ &= \frac{\tfrac{\lvert 0\rangle + \lvert 1\rangle}{\sqrt 2}\otimes \lvert\psi\rangle + \tfrac{\lvert 0\rangle \color{red}- \lvert 1\rangle}{\sqrt 2}\otimes U\lvert\psi\rangle}{\sqrt 2} \\ &= \frac{\lvert 0\rangle\otimes\lvert\psi\rangle + \lvert 1\rangle\otimes\lvert\psi\rangle + \lvert 0\rangle\otimes U\lvert\psi\rangle \color{red}- \lvert 1\rangle\otimes U\lvert\psi\rangle}{2} \end{align*} Does this clear up where the sign of the fourth term comes from?
• Yes, That makes sense. I didn't take into account how Hadamard acts on $|1\rangle$ state. Thank you. Commented Mar 20, 2023 at 5:32