# Transform matrix into a combination of simple quantum gates

I am trying to transform this matrix into a combination of quantum gates but I cannot find any such functionality on Qiskit or anywhere else. I have tried to use Quirk but I do not understand it.

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

• Hi @poster12345! If any of the 2 answers below answered your question, don't forget to check the "accept answer". – Nelimee May 23 at 14:44
• An implicit subquestion here is "Is there an efficient algorithm to decide whether a given matrix can be represented by a poly-sized quantum circuit?". I'm pretty sure the answer is no. – Jalex Stark May 25 at 17:53

Math:

If you are familiar with tensor products, observe that $$I \otimes X$$ gives the desired matrix. Showing the steps of this computation,

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

To obtain the corresponding circuit, note that this tensor represents two gates applied at the same circuit depth on two qubits. That is, you would have an identity gate applied to the top qubit (equivalently, not applying any gate), and you would have the $$X$$ gate applied on the bottom qubit.

Shown as a circuit diagram:

Qiskit:

Using the following import, you can decompose a two qubit gate to a sequence of single qubit gates, as long as the input gate does not create entanglement.

from qiskit.quantum_info.synthesis import TwoQubitBasisDecomposer


You can find the code for this file here: https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/quantum_info/synthesis/two_qubit_decompose.py (qiskit-terra/qiskit/quantum_info/synthesis/two_qubit_decompose.py).

However, as input to the gate decomposer, you need to create a 2-qubit gate corresponding to the matrix given. I am not sure how to do so, and will update my answer when I find out.

Further note that at the current time, n-qubit gate decomposition is not supported; you can only decompose 2-qubit gates.

This matrix is a tensor product of two simpler matrices, $$I \otimes X$$, which you represent by applying an $$X$$ gate to the second qubit and doing nothing with the first one.