# How to apply a Hadamard gate to one qubit in a 2-qubit system?

Edit:

I've pinpointed that the difference between solutions occurs when I apply the $H$, up to there, the solutions match. So, perhaps it is better to phrase my question as: how to apply an $H$ gate to one qubit in a 2-qubit system?

End Edit

I have the following quantum circuit, and I need to calculate the values of the two qubits afterwards.

Now I know the values are:

• $\lvert \psi_1 \rangle = \frac{1}{\sqrt{2}}(\lvert 0 \rangle+\lvert 1 \rangle)$
• $\lvert \psi_2 \rangle = \lvert 0 \rangle$

It's pretty simple to see, and I checked them with a simulator just to be sure.

The trouble I'm getting into is when I try to express this as a two qubit system.

As I understand it should be as follows:

$$\lvert \psi\prime \rangle=M\lvert \psi \rangle=\frac{1}{\sqrt{2}}(\lvert 00 \rangle+\lvert 10 \rangle)$$

Where $\lvert \psi\prime \rangle$ is the state after the circuit is complete, $\lvert \psi \rangle = \lvert 00 \rangle$ is the initial state, and $M$ is the matrix representing the combined effect of all the gates of the circuit.

Now, I calculate $M$ as follows:

$$M=\frac{1}{\sqrt{2}}(I\otimes X)\begin{bmatrix}I&0\\0&X\end{bmatrix}(I\otimes X)(H\otimes I)$$

I wrote the $CNOT$ matrix in block matrix form here, and extracted the $\frac{1}{\sqrt{2}}$ from $H$ to the beginning.

\begin{align}M&=\frac{1}{\sqrt{2}}\begin{bmatrix}X&0\\0&X\end{bmatrix}\begin{bmatrix}I&0\\0&X\end{bmatrix}\begin{bmatrix}X&0\\0&X\end{bmatrix}\begin{bmatrix}I&I\\I&-I\end{bmatrix} \\ &=\frac{1}{\sqrt{2}}\begin{bmatrix}X&0\\0&I\end{bmatrix}\begin{bmatrix}X&0\\0&X\end{bmatrix}\begin{bmatrix}I&I\\I&-I\end{bmatrix} \\ &=\frac{1}{\sqrt{2}}\begin{bmatrix}I&0\\0&X\end{bmatrix}\begin{bmatrix}I&I\\I&-I\end{bmatrix} \\ &=\frac{1}{\sqrt{2}}\begin{bmatrix}I&I\\X&-X\end{bmatrix}\end{align}

Now, plugging $M$ back into the first equation...

\begin{align}\lvert\psi\prime\rangle &=\frac{1}{\sqrt{2}}\begin{bmatrix}I&I\\X&-X\end{bmatrix}\lvert\psi\rangle \\ &=\frac{1}{\sqrt{2}}\begin{bmatrix}1&0&1&0\\0&1&0&1\\0&1&0&-1\\1&0&-1&0\end{bmatrix}\begin{bmatrix}1\\0\\0\\0\end{bmatrix}\\&=\frac{1}{\sqrt{2}}\begin{bmatrix}1\\0\\0\\1\end{bmatrix}\\&=\frac{1}{\sqrt{2}}(\lvert 00\rangle + \lvert 11\rangle)\end{align}

Which is not the correct solution. Where am I at fault?

The problem is in the order of the transformations. The first transformation you apply in the circuit is $I\otimes X$, whereas in the computation of $M$ it is actually the last. Recall that the rightmost operator is the first to act on the state (matrix multiplication is not commutative).
$$M=(H\otimes I)(I\otimes X)CNOT(I\otimes X) = \begin{bmatrix} H & 0 \\ 0 & H \end{bmatrix} \begin{bmatrix} X & 0 \\ 0 & X \end{bmatrix} \begin{bmatrix} I & 0 \\ 0 & X \end{bmatrix} \begin{bmatrix} X & 0 \\ 0 & X \end{bmatrix}= \begin{bmatrix} H & 0 \\ 0 & H \end{bmatrix} \begin{bmatrix} X & 0 \\ 0 & X \end{bmatrix} \begin{bmatrix} X & 0 \\ 0 & I \end{bmatrix}= \begin{bmatrix} I & I \\ I & -I \end{bmatrix} \begin{bmatrix} I & 0 \\ 0 & X \end{bmatrix}= \begin{bmatrix} I & X \\ I & -X \end{bmatrix}$$
Now applying $M$ on $|00\rangle$ will yield the right result.