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}$$

  • 1
    $\begingroup$ Hi @poster12345! If any of the 2 answers below answered your question, don't forget to check the "accept answer". $\endgroup$ – Adrien Suau May 23 '19 at 14:44
  • $\begingroup$ 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. $\endgroup$ – Jalex Stark May 25 '19 at 17:53


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:

enter image description here


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.

  • $\begingroup$ Checking in from the future, are n-qubit gate decomposition now supported in qiskit? If not, do you have suggestions on how to do this for an arbitrary n-qubit unitary gate? $\endgroup$ – thespaceman Oct 6 '20 at 16:44

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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