PennyLaneAI / catalyst

A JIT compiler for hybrid quantum programs in PennyLane
https://docs.pennylane.ai/projects/catalyst
Apache License 2.0
101 stars 26 forks source link

OpenQASM generation in MLIR #873

Open dime10 opened 1 week ago

dime10 commented 1 week ago

Context

We would like to run a small experiment of adding an OpenQASM dialect in MLIR. Having a native representation of the OpenQASM standard available in MLIR would be beneficial in several ways, like facilitating the translation of an OpenQASM text file into and out of MLIR, enabling transformations on OpenQASM programs, and more generally improving inter-operability between tools.

Goal

For this experiment we would like to focus on the representation of OpenQASM in MLIR and its translation to the OpenQASM text format.

For the first step, we would like to keep things simple and create a new dialect called "OpenQASM" with only three elements in it:

Let's have a look at a simple OpenQASM program:

    OPENQASM 3.0;
    include "stdgates.inc";

    qubit[2] q;
    ry(0.7) q[0];
    cx q[0], q[1];

We can represent this program in MLIR using the dialect sketched out above, where the IR might look something like this:

module {
    oq.allocate(5)
    oq.ry(0.7) 0
    oq.cnot 0, 1
}

We would then like to be able to traverse the MLIR and print out the program in the OpenQASM format.

Technical Details

A few more details on the dialect and operations which could be helpful:

The MLIR documentation has a few resources about defining new dialects, notably:

It will be easiest to set up the dialect in the existing Catalyst source tree.

It may also be helpful to look at the existing dialect files provided there, although for this implementation the full structure does not need to be replicated. It would be sufficient to have a single TableGen (.td), header (.h), and source (.cpp) file for the dialect components and the translation pass, but other configurations are also fine.

For the translation pass, the method to produce the textual OpenQASM format from the IR can be freely chosen, although one possible approach may simply iterate over the MLIR module and print the equivalent OpenQASM out to console. See also:

The pass could then be registered with the quantum-opt tool and run as quantum-opt --translate-to-openqasm example_ir.mlir.

Installation Help

Refer to the Catalyst installation guide for how to install a source build of the project.