# What's the matrix representation of the CSWAP?

I don't know how to represent the matrix format of the CSWAP gate in the circuit: Despite reviewing some material about CSWAP from Qiskit CSWAP, I am still unable to understand the concept. I am seeking to obtain the 16x16 matrix format in order to calculate the quantum state vector after the CSWAP.

In addition, it can be noted that q2 and q3 are fully entangled (I am unsure if this information is relevant).

Any help would be greatly appreciated, thank you!

To get the $$CSWAP$$ matrix representation in Qiskit, you can use the Operator class defined in the qiskit.quantum_info module:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.visualization import array_to_latex

qc = QuantumCircuit(4)
qc.cswap(0, 1, 3)

array_to_latex(Operator(qc), max_size=16)


If you want to do all the actual maths, you should start from the following: $$CSWAP_{0 \rightarrow 1, 3} = I \otimes I \otimes I \otimes |0 \rangle \langle 0| + SWAP_{1, 3} \otimes |1 \rangle \langle 1|$$ The formula above basically means: "if qubit $$q_0$$ (right-most) is in state $$|0\rangle$$, don't do anything; if it is in state $$|1\rangle$$, swap qubit $$q_1$$ and $$q_3$$". The $$SWAP_{i,j}$$ gate can then be decomposed in 3 controlled-not operations as $$CX_{i,j} \cdot CX_{j,i} \cdot CX_{i,j}$$. So, in this specific case, you have to compute: $$CX_{1, 3} = I \otimes I \otimes |0 \rangle \langle 0| + X \otimes I \otimes |1 \rangle \langle 1|$$ $$CX_{3, 1} = |0 \rangle \langle 0| \otimes I \otimes I + |1 \rangle \langle 1| \otimes I \otimes X$$

Finally, if you put all together and perform the calculations, you will get the same 16x16 unitary matrix returned by Qiskit.

• Thank you so much!!! It helps me a lot. ^_^ By the way, does Qiskit really do the matrix manipulations when it simulates a quantum computer on a classical computer? Or it just makes some logical judgments to perform the calculations? For example, CNOT|11>=|10>, can be done easily by logical judgment instead of matrix manipulation. Feb 11 at 10:48
• Honestly, I'm not sure how Qiskit actually computes that matrix but yes, probably some kind of optimization happens under the hood (I don't know the details). If you are curious about that, I suggest you to open a new question here on QCSE and maybe Qiskit experts can help to better understand. Feb 11 at 11:08
• Thank you, and have a nice day! Feb 11 at 11:46

2-qubit gates can be simulated effectively, much faster than potentially large matrix multiplies. They can be applied basically with linear complexity over the size of the full state (Python example here).

A Swap(x, y) gate can be implemented as:

  Cnot(x, y)
Cnot(y, x)
Cnot(x, y)


Correspondingly, a controlled Swap gate would be:

  CCnot(x, y)
CCnot(y, x)
CCnot(x, y)


So the final question is - how to implement this CCnot gate with two qubit gates? The answer is - with the so-called Sleater-Weinfurter construction: • Thank you Hundt! Feb 11 at 23:59
• Actually, it is sufficient to use $(I\otimes CNOT[x,y])CCNOT[y,x](I\otimes CNOT[x,y])$. If there is $|0\rangle$ on control qubit of CCNOT, two CNOT gates cancel each other. If there is $|1\rangle$ on control qubit, qubits $x$ and $y$ are swaped. Feb 12 at 14:13
• Right, smart :-) Feb 12 at 15:38