Search code examples
linear-algebraquantum-computingcirq

Decompose a Quantum Circuit in Cirq


I am working on Cirq and need to perform certain unitary operations on qubits. For that, I am using the MatrixGate() function in Cirq. Unlike Qiskit, I could not find any function like decompose or transpile to simplify the Unitary operation into basic U3 and CNOT gates.

For instance, if I want to act the following Unitary Operator,

Unitary Operator

To do this I use this code in Qiskit. Looking for something equivalent in Cirq.

qc=QuantumCircuit(2)
qc.unitary(U,[0,1])
qc=transpile(qc,basis_gates=['cx','u3'])
qc.draw(output='mpl')

Unitary Gate

After using Transpile function in Qiskit

Transpiled Unitary Gate

I even tried to work up the Cosine-Sine Decomposition Algorithm which Qiskit uses to decompose these Unitary Operations. As mentioned in the paper, Quantum Circuits for Isometries, but they do not trivially yield the required decomposition. Please help by either suggesting :

  1. Some code in Cirq to decompose circuits or
  2. A workaround to export Qiksit circuits to Cirq or
  3. A simpler algorithm to decompose Unitary Operations.

Solution

  • An example of such a method is cirq.two_qubit_matrix_to_operations. It uses the kak decomposition (cartan decomposition) to determine how to translate a unitary matrix into a series of operations with minimal number of CZ gates.

    import cirq
    
    desired_matrix = cirq.testing.random_unitary(dim=4)
    
    synthesized_operations = cirq.two_qubit_matrix_to_operations(
        cirq.LineQubit(0),
        cirq.LineQubit(1),
        desired_matrix,
        allow_partial_czs=False,
    )
    circuit = cirq.Circuit(synthesized_operations)
    
    synthesized_matrix = cirq.unitary(circuit)
    
    cirq.testing.assert_allclose_up_to_global_phase(
        desired_matrix,
        synthesized_matrix,
        atol=1e-4
    )
    
    print(desired_matrix.round(3))
    print(circuit)
    

    Prints (for example):

    [[ 0.234-0.169j -0.81 +0.038j -0.327+0.138j -0.364-0.029j]
     [-0.503-0.407j  0.221-0.206j  0.063+0.144j -0.629-0.264j]
     [ 0.271+0.338j  0.337-0.128j -0.343+0.731j -0.165+0.052j]
     [ 0.504+0.236j  0.222+0.269j  0.244-0.371j -0.608-0.043j]]
    0: ───PhX(-0.283)^0.631───@───PhX(0.673)^0.5────@───PhX(-0.375)^0.5───@───PhX(0.827)^0.147───Z^-0.269───
                              │                     │                     │
    1: ───PhX(0.508)^0.338────@───PhX(0.65)^(5/6)───@───PhX(0.65)^0.995───@───PhX(0.302)^0.512───Z^-0.516───