# How to transform a quantum gate into its matrix representation?

I have a question about how to transform a quantum gate into the matrix representation. For example, we all know the 2-qubit gate--CNOT, can be written like $$\begin{pmatrix} 1& 0& 0& 0 \\ 0& 1& 0& 0 \\ 0& 0& 0& 1 \\ 0& 0& 1& 0 \\ \end{pmatrix}$$ and its image is shown on the lefe side of the following picture:

However, I don't konw how to transform the 3-qubit gate into matrix representation. It doesn't look like to directly utilize tensor product technique, and therefore I want to know how to transform them? Probabily in $$C_1T_3Q_3$$ or in $$C_2T_5Q_6$$ these more general situation, where $$C_k$$ stands for the k-th control qubit, $$Q_j$$ stands for the j-th target qubit, and $$Q_i$$ means that there are i qubits in total.

One fairly straightforward method is to enumerate all the computational basis states in order: 000, 001, 010, 011, 100, 101, 110, 111. You can label each row and column in order. So, for example, the bottom-left matrix element has a column label 000 and row label 111. For each such matrix element, (say, row: a, column: b), you act your gate $$U$$ on the basis state $$|a\rangle$$ and ask what its amplitude is for producing $$|b\rangle$$. That's the matrix element: $$U_{a,b}=\langle b|U|a\rangle$$.

Remember that if you ever find an element has absolute value 1, all other elements in that row and column are 0. So, for the toffoli gate, I might look at $$U|000\rangle$$. This tells me what the first column is: 1 in the top left element (the 000 row) and 0 everywhere else.

• OK, I see... This brute force method can actually solve this problem. But I wonder if there is a more effective way to solve this problem (although it can be solved by the computer through solving the equations). Commented Nov 4, 2022 at 8:47
• That really depends on your initial specification of the gate. If you're only thinking about controlled-not gates and acting them on different pairs of qubits, there are of course much easier ways. For example, for your three-qubit circuit: $|0\rangle\langle 0|\otimes I\otimes I+|1\rangle\langle 1|\otimes I\otimes X$. Commented Nov 4, 2022 at 9:03