1
$\begingroup$

I am having hard time figuring out how the CX (controlled-NOT) gate is represented in the matrix representation.

I understood that tensor product and the identity matrix are the keys, and I understood how the matrix representation works for single-qubit matrices. For example, if we have a circuit with a quantum register q composed of 3 qubits, the operation X q[1] has the matrix representation 1 $$I_2 \otimes X \otimes I_2 = \begin{pmatrix} 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&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}.$$

Obviously, I am aware of the matrix representation of the CX gate: $$\begin{pmatrix} 1&0&0&0\\ 0&1&0&0\\ 0&0&0&1\\ 0&0&1&0\\ \end{pmatrix}$$

Taking back our quantum register q, applying CX to the second (control) and third register (target) (CX q[1], q[2]) gives us the matrix representation 2 $$I_2 \otimes CX = \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}$$

The problem comes when we try to apply the CX gate to other pairs of qubits:

  1. I suspect the matrix representation of CX q[2], q[1] (applying a CX gate with the third qubit of the system as control and the second qubit of the system as target) to be $I_2 \otimes CX^r$ where $$CX^r = \begin{pmatrix} 0&1&0&0\\ 1&0&0&0\\ 0&0&1&0\\ 0&0&0&1\\ \end{pmatrix}$$ but I am not sure.

  2. I really don't know how CX q[0], q[2] (applying a CX gate with the first qubit of the system as control and the third qubit of the system as target) would be represented.

To summarise, my question is "how does the CX (or more generally multi-qubit gates) is represented as a matrix when there are more than 2 qubits in the system?".

1 Computed with Python and numpy:

import numpy as np
X = np.array([[0, 1], [1, 0]])
ID2 = np.identity(2)
print(np.kron(np.kron(ID2, X), X))

2 Computed with Python and numpy:

import numpy as np
CX = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
ID2 = np.identity(2)
print(np.kron(ID2, CX))
$\endgroup$

marked as duplicate by Niel de Beaudrap, Community Oct 24 '18 at 9:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ can you spell out in words what you mean by CX q[0], q[2] and the likes? I'm not familiar with Q# notation (or whatever notation that is) $\endgroup$ – glS Oct 24 '18 at 9:03
  • 1
    $\begingroup$ I edited my question! Tell me if it is not enough. $\endgroup$ – Nelimee Oct 24 '18 at 9:06
  • $\begingroup$ You can conjugate by the appropriate swaps. Figure out what permutation you need to get the indices as you already had them and break that into swaps. $\endgroup$ – AHusain Oct 24 '18 at 9:14
  • $\begingroup$ Most of the answer to your question can be found on this related question: "How to derive the CNOT matrix for a 3-qbit system where the control & target qbits are not adjacent?" $\endgroup$ – Niel de Beaudrap Oct 24 '18 at 9:29
  • 1
    $\begingroup$ @Nelimee: No problem, though I do also describe how this extends to many qubits --- I just don't write any examples of them. $\endgroup$ – Niel de Beaudrap Oct 24 '18 at 9:55
1
$\begingroup$

An easy way is to get it via braket notation.

Consider the general case of $n$ qubits. The representation of a CNOT between the last two qubits (last qubit being the target) is easily seen to be: $$\newcommand{\ketbra}[1]{\lvert#1\rangle\!\langle#1\rvert} CX_{n-1\to n}= I_{2^{n-2}}\otimes\big(\ketbra0\otimes I_2+\ketbra1\otimes X\big),$$ that is, in matrix notation, the resulting of tensoring the identity over the first $n-2$ qubits with the usual CNOT matrix.

What if the CNOT acts instead between the $i$-th and $j$-th qubits? The braket representation does not change significantly, we just need to change the order of the terms so that it becomes a bit uglier$^1$: \begin{align} CX_{i\to j} &= I_{2^{i-1}}\otimes\ketbra0\otimes I_{2^{j-i-1}}\otimes I\otimes I_{2^{n-j}} \\ &+ I_{2^{i-1}}\otimes\ketbra1\otimes I_{2^{j-i-1}}\otimes X\otimes I_{2^{n-j}}. \end{align} To build up the typical matrix representation you then just compute the Kronecker product of these terms.

For example, if $n=3$, the control is the third qubit and the target is the first second qubit, you have $$CX_{3\to 2}=I\otimes I\otimes\ketbra0+I\otimes X\otimes \ketbra1.$$


$^1$ Note that the way I've written it, this representation only works for $i<j$, but I'm sure you can easily see what it becomes when $i>j$.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.