# How to calculate CNOT gate for three-qubit (1-control, 2-target) states as a matrix? How can I calculate CNOT gate with 1 control and 2 target qubits as matrix?

To understand how to construct the $$CNOT$$ gate with 1 control and 2 target qubits as a matrix, we'll first look at how the regular 2-qubit $$CNOT$$ gate is constructed and then use the same technique to construct the gate in question. If you're just interested in the answer feel free to scroll to the bottom and look at the final matrix :)

Here is the $$CNOT$$ matrix:

$$\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ \end{bmatrix}$$

The way to think about this matrix is that each row corresponds to a basis state input, and each column corresponds to a basis state output i.e.

Let's look at the example $$|10\rangle$$. We know that $$CNOT|10\rangle=|11\rangle$$. If we look at the CNOT matrix above, we see that for the input row $$|10\rangle$$ we have the output $$0 \times |00\rangle + 0 \times |01\rangle + 0 \times |10\rangle + 1 \times |11\rangle = |11\rangle$$

We can also explicitly compute this using the matrix form: $$CNOT|10\rangle = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix} \begin{bmatrix} 0 \\ 0 \\ 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix} = |11\rangle$$

Just to give one more slightly more complicated example, we can use the input state $$\frac{|00\rangle + |11\rangle}{\sqrt{2}}$$. Again looking at the inputs and outputs of the matrix above, we see that

\begin{align} CNOT|00\rangle &= 1 \times |00\rangle + 0 \times |01\rangle + 0 \times |10\rangle + 0 \times |11\rangle = |00\rangle \\ CNOT|11\rangle &= 0 \times |00\rangle + 0 \times |01\rangle + 1 \times |10\rangle + 0 \times |11\rangle = |10\rangle \\ \Rightarrow CNOT\frac{|00\rangle + |11\rangle}{\sqrt{2}} &= \frac{CNOT|00\rangle + CNOT|11\rangle}{\sqrt{2}} = \frac{|00\rangle + |10\rangle}{\sqrt{2}} \end{align}

And you can again verify this using the explicit matrix calculation. So now that we've seen that you can construct the $$CNOT$$ (and actually any) matrix by taking the rows as inputs and columns as outputs, we can just do the same thing for the matrix that you originally asked about! Below I have taken the case where the most significant bit is the control bit, but you can construct the matrix with for whichever situation you want :)

Notice that you can view $$CNOT_{1,2}$$ as a tensor product $$X_2 I_3$$ controlled by qubit 1: $$CNOT_{1,2} \otimes I_3 = C_1(X_2 I_3)$$ The same applies to $$CNOT_{1,3}$$. You can then get the matrix for the controlled $$X_2 X_3$$ gate by multiplying the two $$CNOT$$ matrices: $$C_1(X_2 X_3) = C_1(X_2 I_3) C_1(I_2 X_3)$$ Here, the control qubit is 1, but if you want your qubits in arbitrary order like in circuit you have above, refer to this answer.