# A CNOT between two Hadamard gates: why does the CNOT changed the output of the second Hadamard gate?

Applying the Hadamard gate twice in a row, it restores the original input:

https://algassert.com/quirk#circuit={%22cols%22:[[%22H%22],[%22H%22]]}

However, if a CNOT control is added between the two Hadamard gates, the output of the second Hadamard gate changes:

https://algassert.com/quirk#circuit={%22cols%22:[[%22H%22],[%22%E2%80%A2%22,%22X%22],[%22Chance%22],[%22H%22]]}

I can't understand the behavior of the second Hadamard gate: the input has remaining $$\frac{|0\rangle+|1\rangle}{\sqrt{2}}$$ and the gate does not seem to have any effect.

This is because the CNOT gate created an an entangled state and the system after the CNOT gate can't be written individually. That is, you can't stay that your first qubit is in the state $$\dfrac{|0\rangle + |1\rangle}{\sqrt{2}}$$ anymore.

That is consider the circuit:

Here $$q_0$$ and $$q_1$$ both start in the state $$|0\rangle$$. So you start with the initial state $$|\psi\rangle_{init} = |0\rangle \otimes |0\rangle = |00\rangle$$.

1. By first applying the Hadamard gate on the first qubit put the system in the state $$|\psi \rangle_0 = \dfrac{|0\rangle + |1\rangle}{\sqrt{2}} \otimes |0\rangle = \dfrac{|00\rangle + |10\rangle}{\sqrt{2}}$$. Here you can say that your first qubit $$q_0$$ is in the state $$|q_0 \rangle = \dfrac{|0\rangle + |1\rangle}{\sqrt{2}}$$ and your second qubit $$q_1$$ is in the state $$|q_1\rangle = |0\rangle$$.

1. Then, applying the CNOT gate here with the controlled-qubit being $$q_0$$ and target qubit being $$q_1$$ puts the state in $$|\psi \rangle_1 = \dfrac{|00\rangle + |11\rangle}{\sqrt{2}}$$. Note that you can no longer can write $$|\psi \rangle_1 = |q_0 \rangle \otimes |q_1\rangle$$. That is, you can't say that the first qubit is in the state $$|q_0\rangle = \dfrac{|0\rangle + |1\rangle}{\sqrt{2}}$$ and the second qubit in some state $$|q_1\rangle$$. They are entangled to one another.

1. Lastly, if you apply the Hadamard gate to the first qubit, which is applying the operation $$H \otimes I = \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1\\ 1 &-1 \end{pmatrix} \otimes \begin{pmatrix} 1 & 0\\ 0 & 1 \end{pmatrix}$$ to the state $$|\psi \rangle = \dfrac{|00\rangle + |11\rangle}{\sqrt{2}} = \dfrac{1}{\sqrt{2}} \begin{pmatrix} 1\\ 0 \\ 0 \\1 \end{pmatrix}$$ we will get the final state $$|\psi\rangle_{final} = \dfrac{|00\rangle + |01\rangle + |10\rangle - |11\rangle}{2} = \dfrac{1}{2} \begin{pmatrix} 1\\ 1 \\ 1 \\-1 \end{pmatrix}$$

This is the reason why you see that you have $$50\%$$ probability of measuring the first qubit $$q_0$$ in the state $$|0\rangle$$ and $$50\%$$ probability of measuring it in the state $$|1\rangle$$.