harshangrjn / QuantumCircuitOpt.jl

A Julia/JuMP Package for Optimal Quantum Circuit Design
Other
56 stars 15 forks source link

Margolus implementation #46

Open fschulz21 opened 2 years ago

fschulz21 commented 2 years ago

I am trying to implement the Margolus gate (simplified Toffoli with a single sign change) using this package. Here is an implementation in Figure (2) which does the job, but I am not able to reproduce this circuit or find a better one if there exists one such implementation. Is this something possible using this package? I presume Qiskit may implement this, while there may not be guarantees on the quality of the circuit.

harshangrjn commented 2 years ago

@fschulz21 It is possible to decompose the 3-qubit Margolus gate using the QCOpt package. However, since Margolus was not part of the package, I just added support for it, which you should be able to find in the PR named more_3q_gates. Below is the code which implements decomposition for the Margolus gate. Do note that you will need to input a mixed-integer programming (MIP) solver. Here, the code invokes Gurobi solver, which is quite efficient for QCOpt's formulations.

import QuantumCircuitOpt as QCOpt
using JuMP
using Gurobi

function decompose_margolus()
     return Dict{String, Any}(
                 "num_qubits" => 3,
                 "maximum_depth" => 7,
                 "elementary_gates" => ["RY_3", "CNot_1_2", "CNot_2_3", "CNot_1_3", "Identity"],
                 "target_gate" => QCOpt.MargolusGate(),
                 "objective" => "minimize_depth",
                 "RY_discretization" => -π:π/4:π,
                 )
end

mip_optimizer = JuMP.optimizer_with_attributes(Gurobi.Optimizer, "Presolve" => 1)
results = QCOpt.run_QCModel(decompose_margolus(), mip_optimizer)

The output of executing the above code is below. Looks like the optimal circuit, as shown below, is RY_3(-45.0) * CNot_2_3 * RY_3(-45.0) * CNot_1_3 * RY_3(45.0) * CNot_2_3 * RY_3(45.0), where RY_3 represents the Rotation gate about the Y axis on the 3rd qubit and CNot_c_t represents controlled-X gates with respective control (c) and target (t) qubits. This indeed matches with the circuit you showed in the paper.

=============================================================================
Quantum Circuit Model Data

  Number of qubits: 3
  Total number of elementary gates (after presolve): 12
  Maximum depth of decomposition: 7
  Input elementary gates: ["RY_3", "CNot_1_2", "CNot_2_3", "CNot_1_3", "Identity"]
    RY discretization: [-180.0, -135.0, -90.0, -45.0, 0.0, 45.0, 90.0, 135.0, 180.0]
  Type of decomposition: exact
  MIP optimizer: Gurobi

Optimal Circuit Decomposition

  RY_3(-45.0) * CNot_2_3 * RY_3(-45.0) * CNot_1_3 * RY_3(45.0) * CNot_2_3 * RY_3(45.0) = Target gate
  Minimum optimal depth: 7
  Optimizer run time: 28.97 sec.
=============================================================================
fschulz21 commented 2 years ago

This is very nice. Thanks for the help. Look forward to explore the package for many other quantum gates.

I see that the MIP solver you mentioned is a commercial one, for which I do not have a license. Is there an open-source option to execute the above code?

harshangrjn commented 2 years ago

MIP Solvers like Cbc and GLPK can be used to solve. However, the run times are generally very slow with these solvers, especially for larger circuit decompositions.