Search code examples
quantum-computingqiskit

How to decompose a N x N unitary matrix into 2- or 1-qubit operators?


Given an N x N unitary operator M, I would like to build a circuit that does the same operation as M by explicitly inputting the gates myself (let's say into the IBMQ composer). I heard that 2-qubit operators could be decomposed using a Qiskit built-in function, however I was wondering if such a thing existed for a general case.

More concretely, given a N x N unitary operator M, I would like to decompose it to something of the form

M_1 x M_2 x M_3 x ... x M_n

where "x" represents the tensor product and M_i is either a 2- or 1-qubit unitary operator. Is there a way to do this programatically, or can it be done by hand on paper in an algorithmic way?

Thank you in advance!


Solution

  • If you want to implement custom unitary, there is a way to do it using Operator function, like this (example for 4x4 unitary matrix):

    from qiskit import QuantumRegister, QuantumCircuit
    from qiskit.quantum_info.operators import Operator
    
    q =  QuantumRegister(2,"qreg")
    qc = QuantumCircuit(q)
    
    customUnitary = Operator([
        [1, 0, 0, 0],
        [0, 0, 0, 1],
        [0, 0, 1, 0],
        [0, 1, 0, 0]
    ])
    qc.unitary(customUnitary, [q[0], q[1]], label='custom')
    qc.draw(output='mpl')
    

    But if your purpose is to decompose it to 1 or 2-qubit operators, the problem is more complex since there can be multiple ways to decompose the same unitary.

    I think the best you can do is to use Qiskit transpiler and define set of gates you want to use:

    from qiskit.compiler import transpile
    
    newCircuit = transpile(qc, basis_gates=['ry', 'rx', 'cx'], optimization_level = 3)
    newCircuit.draw(output='mpl')