# How to interpret a quantum circuit as a matrix?

If a circuit takes more than one qubit as its input and has quantum gates which take different numbers of qubits as their input, how would we interpret this circuit as a matrix?

Here is a toy example:

## Specific Circuit

The first gate is a Hadamard gate which is normally represented by $$\frac{1}{\sqrt{2}}\begin{bmatrix}1&1\\1&-1\end{bmatrix}$$

Now, since we're only applying it to the first qubit, we use a kronecker product on it (this confused me so much when I was starting out - I had no idea how to scale gates; as you can imagine, it's rather important), so we do $$H\otimes I$$, where $$I$$ is the 2x2 identity matrix. This produces

$$\frac{1}{\sqrt{2}}\begin{bmatrix}1 & 0 & 1 & 0\\0 & 1 & 0 & 1\\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1\end{bmatrix}$$

Next we have a CNOT gate. This is normally represented by

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

This is the right size for two qubits, so we don't need to scale using kronecker products. We then have another hadamard gate, which scales the same was as the first. To find the overall matrix for the circuit, then, we multiply them all together:

$$\frac{1}{\sqrt{2}}\begin{bmatrix}1 & 0 & 1 & 0\\0 & 1 & 0 & 1\\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1\end{bmatrix}\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&0&1\\0&0&1&0\end{bmatrix}\frac{1}{\sqrt{2}}\begin{bmatrix}1 & 0 & 1 & 0\\0 & 1 & 0 & 1\\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1\end{bmatrix}$$

and get

$$\frac{1}{2}\begin{bmatrix}1&1&1&-1\\1&1&-1&1\\1&-1&1&1\\-1&1&1&1\end{bmatrix}$$

(if python multiplied correctly =) We would then multiply this by our original qubit state, and get our result.

## Generalization

So basically, you go through each gate one by one, take the base representation, and scale them appropriately using kronecker products with identity matrices. Then you multiply all the matrices together in the order they are applied. Be sure to do this such that if you wrote out the multiplication, the very first gate is on the far right; as arriopolis points out, this is a common mistake. Matrices are not commutative! If you don't know the base representation of a matrix, check first wikipedia's article on quantum gates which has a lot.

• Maybe it's instructive to add that one should always reverse the order of matrix multiplication. In this particular toy example, it's not necessary as the circuit is symmetric, but in general, one should always put the matrix of the left-most gate in the right-most position of the matrix multiplication. Jun 12 '18 at 10:23
• @arriopolis, good point; I will add that! Jun 12 '18 at 13:20
• Rather than thinking about 'scaling' the gate, from what I understood, the kronecker product by the identity matrix is due to the fact that on the second qubit nothing is applied, but if you consider the circuit as a whole, at the first step it will undergo and H transform on the first qubit and an "I" transform on the second, that are represented at once with H⊗I.
– FSic
Jun 19 '18 at 11:58
• @F.Siciliano that is a good way to think about it as well; for me it's a good way to remind myself of why I'm doing it. Jun 19 '18 at 13:31