# How to represent a CNOT gate operating on three-qubit states as a matrix? [duplicate]

I am wondering how to represent these kinds of circuits as a matrix. Is there any formula for doing this?

Yes, all quantum circuits can be view as a $$2^n \times 2^n$$ unitary matrix as $$n$$ is the number of of qubits.

### Long way:

For your circuits, first note the circuit identity:

Now, using the circuit on the right, you can see that it can be written as

$$U = \big(CNOT \otimes I\big)\cdot\big(I \otimes CNOT\big)\cdot \big(CNOT \otimes I\big)\cdot\big(I \otimes CNOT\big)$$ and recall that $$CNOT \otimes I = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} \otimes \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\ \end{pmatrix}$$

and similarly

$$I\otimes CNOT = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \otimes \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ \end{pmatrix}$$

From here you can just do usual multiplication to find that $$U$$, you should end up with something like:

$$U = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ \end{pmatrix}$$

If you use qiskit , you can extract this operator pretty easily, you can do it as follows:

from qiskit import QuantumCircuit
import qiskit.quantum_info as qi

circuit = QuantumCircuit(3)
circuit.cx(2,0)
op = qi.Operator(circuit)
print(np.real(op) ) #since the matrix has no complex coeff, this makes it easier to look at

array([[1., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 1., 0.]])


note that in qiskit I had to reversed the operation of CNOTS, using the bottom qubit as the control and the top qubit as the target. This is because qiskit uses little endian convention so things go backward... look here for more info about how that effects the way we look at thing.

### Alternative, shorter way:

@gls commented brought up a different way to do this, and it's easier than thinking than thinking of decompose the gate like we did above.

First, note that CNOT gate can be written as: $$CNOT_{12} = |0\rangle \langle 0| \otimes I + |1 \rangle \langle 1| \otimes X$$ This is the definition of the CNOT gate. This is assuming that the control and target qubit are next to each other. But if there is an intermediate qubit then we can do it as: $$CNOT_{13} = |0\rangle \langle 0| \otimes I \otimes I + |1 \rangle \langle 1| \otimes I \otimes X$$

Here note that $$|0\rangle \langle 0 | = \begin{pmatrix}1 \\ 0 \end{pmatrix} \begin{pmatrix} 1 & 0 \end{pmatrix} = \begin{pmatrix}1 & 0 \\ 0 & 0 \end{pmatrix} \hspace{1 cm} |1\rangle \langle 1 | = \begin{pmatrix}0 \\ 1\end{pmatrix} \begin{pmatrix} 0 & 1 \end{pmatrix} = \begin{pmatrix}0 & 0 \\ 0 & 1 \end{pmatrix}$$

And so you can see that $$CNOT_{13}$$ will give you the same result as $$U$$ above.

### Regarding to the second circuit:

Now, then for your second circuit, first note that when the CNOT is reversed, that is, instead of $$CNOT_{12}$$ you are looking at $$CNOT_{2,1}$$ you can write it as:

$$CNOT_{2,1} = I \otimes |0\rangle \langle 0 | + X \otimes |1\rangle \langle 1 | = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0 \end{pmatrix}$$

Then from here, we have that

$$CNOT_{3,1} = I \otimes I \otimes |0\rangle \langle 0 | + X \otimes I \otimes |1\rangle \langle 1 |$$

Doing this leads you to the matrix:

$$CNOT_{3,1} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ \end{pmatrix}$$

Again, if you want to use qiskit to generate this matrix, make sure to remember to define your circuit as circuit.cx(0,2) as it is doing the circuit in reverse.

• isn't it easier to just directly write the matrix corresponding to $|0\rangle\!\langle 0|\otimes I \otimes I+|1\rangle\!\langle 1|\otimes I \otimes X$ without passing through that particular matrix decomposition?
– glS
May 22 at 16:17
• @glS absolutely! I just thought to write it out this way so it is easier to follow... but now that you mentioned this, it feel like I should start with that.... lol May 22 at 16:21
• Or even simpler, write out $I\otimes CNOT = CNOT \oplus CNOT$ and swap rows and columns of the first two qubits May 25 at 15:35