# Trying to understand this measurement of a simple quantum circuit

It's a newbie question, I know. But I was just wondering if someone could help me understand why this simple circuit results with the measurement shown. I've just didn't get the last step. It's not supposed that the last Hadamard gate would cancel the first one on q? Why I still got results like 011 or 111? You can think of the circuit operation as follows. Remembering that for Hadamard matrix: $$H \times H = \mathbb{I}$$, your circuit looks like the following: $$H \otimes \mathbb{I} \otimes \mathbb{I}(CCNOT(H|0\rangle \otimes H|0\rangle \otimes |0\rangle ))$$ Let's focus on the inner-most part first: $$H|0\rangle \otimes H|0\rangle \otimes |0\rangle \\ =\frac{1}{\sqrt{2}}(|0\rangle + |1\rangle) \otimes \frac{1}{\sqrt{2}}(|0\rangle + |1\rangle) \otimes |0\rangle \\ = \frac{1}{2}(|000\rangle + |010\rangle + |100\rangle + |110\rangle).$$ Now, the action of $$CCNOT$$ gate is that, if the first two qubits are $$1$$, then the third qubit would be flipped. But notice that in the above state, only the last term has two $$1$$'s in it, i.e. the $$|110\rangle$$ term. So, the action of $$CCNOT$$ on the above state would be: $$CCNOT(\frac{1}{2}(|000\rangle + |010\rangle + |100\rangle + |110\rangle)) \\ = \frac{1}{2}(|000\rangle + |010\rangle + |100\rangle + |111\rangle).$$ Now we apply the Hadamard matrix on the first qubit only: $$H \otimes \mathbb{I} \otimes \mathbb{I}(\frac{1}{2}(|000\rangle + |010\rangle + |100\rangle + |111\rangle)) \\ = \frac{1}{2}(\frac{1}{\sqrt{2}}(|0\rangle + |1\rangle)|00\rangle + \frac{1}{\sqrt{2}}(|0\rangle + |1\rangle)|10\rangle + \frac{1}{\sqrt{2}}(|0\rangle - |1\rangle)|00\rangle + \frac{1}{\sqrt{2}}(|0\rangle - |1\rangle)|11\rangle)).$$ Simplify it! Would be a good exercise if nothing else. :) You would see that the resulting state has the terms that you have shown in the second picture.