# How to compute the gate matrix for an operation on qubits not next to each other

I have a quantum circuit with 4 input qubits, A, B, C, and D. A is at the top, D is at the bottom.

If I wanted to do a CNOT between B and C and leave A and D alone, I know the gate matrix for this would be computed as $$I \otimes CNOT \otimes I$$ where $$I$$ is the 2x2 identity matrix, $$CNOT$$ is the controlled not gate matrix, and $$\otimes$$ is tensor product.

But how do I compute the gate matrix if I want to CNOT A and D and leave B and C alone?

I've looked at this question and it seems close, but the MBQC terminology seems to be confusing everyone (including me).

I will give an example of three qubits because I am lazy to writing out things for four qubits, but you should get the idea.

Assume you have three qubits, qubits $$A, B$$ and $$C$$. You perform a $$\text{CNOT}$$ from qubit $$A$$ to $$C$$. So what is the matrix representation of $$\text{CNOT}_{A \to C}$$?

### Step 1:

Write out what happens to all computational basis states under this operation.

\begin{align} |000\rangle \to |000\rangle \\ |001\rangle \to |001\rangle \\ |100\rangle \to |101\rangle \\ |101\rangle \to |100\rangle \\ \end{align}

\begin{align} |010\rangle \to |010\rangle \\ |011\rangle \to |011\rangle \\ |110\rangle \to |111\rangle \\ |111\rangle \to |110\rangle \\ \end{align}

### Step 2:

Take the sum over the outer product between all these initial and final states, and that will give you your required matrix, wherein the outer product, bra part is the initial state, and the ket part is the final state after the action of your operation.

\begin{align} \text{CNOT}_{A \to C} &= |000\rangle \langle 000| +|001\rangle \langle 001| +|101\rangle \langle 100| +|100\rangle \langle 101|\\ &+ |010\rangle \langle 010| +|011\rangle \langle 011| +|111\rangle \langle 110| +|110\rangle \langle 111| \,.\\ \end{align}

### General case:

If you have a quantum operation $$M$$ which maps a set of orthogonal states $$\{ |i\rangle \}$$ to another set of orthogonal states $$\{ |j\rangle \}$$, then the matrix representation of $$M$$ can be written as follows $$M = \sum_{i,j} |j\rangle \langle i|\,.$$

### Why this works?

Let's take an example so things will be clear. What should happen to, say state $$|101\rangle$$ under the action of $$\text{CNOT}_{A \to C}$$? It should go to $$|100\rangle\,.$$ So let's see -

\begin{align} \text{CNOT}_{A \to C} |101\rangle &= \bigg(|000\rangle \langle 000| +|001\rangle \langle 001| +|101\rangle \langle 100| +|100\rangle \langle 101|\\ &+ |010\rangle \langle 010| +|011\rangle \langle 011| +|111\rangle \langle 110| +|110\rangle \langle 111|\bigg)|101\rangle\,. \end{align}

Here, you can see that all terms in the summation except the term with $$|101\rangle$$ as bra part will go to zero since they are all orthogonal states. The only term which survives is

$$\text{CNOT}_{A \to C} |101\rangle = |100\rangle \underbrace{ \langle 101|101\rangle}_{= 1}\,.$$

Hence, you get $$\text{CNOT}_{A \to C} |101\rangle = |100\rangle\,.$$

Since we start with a set of orthogonal initial states, for a given initial state, the only term in the summation which would be non-zero would be the one which as the bra part of that initial state. The bra and ket of the initial state will go to 1, and you will always be left with the appropriate final state.

#### How to do this process efficiently? A short-cut

For your case, you will need a $$16 \times 16$$ matrix. Each column from left to right corresponds to the basis states $$0,1,\cdots 15$$ in binary. Just replace the appropriate column to the resultant state under your $$\text{CNOT}_{A \to D}$$ operation—no need to perform 16 outer products and then do the addition.

Example- $$|1001\rangle$$ should go to $$|1000\rangle$$. So place the $$9^{\text{th}}$$ coloumn$$^1$$ in your matrix $$\big( (1001)_2 = (9)_{10 } \big)\,,$$ with state vector for the resultant state $$|1000\rangle\,.$$

This should save a lot of computation, whenever your initial states are computational basis states.

$$^1$$: Here, I am labeling coloumns of $$16 \times 16$$ matrix from $$0$$ to $$15$$.

• Nice answer!!!!! Dec 22, 2023 at 12:25
• @Saul_better Thank you. Dec 22, 2023 at 22:16