qir-alliance / pyqir

PyQIR is a set of APIs for generating, parsing, and evaluating Quantum Intermediate Representation (QIR).
https://qir-alliance.github.io/pyqir
MIT License
54 stars 24 forks source link

Unify parsing and code generation #175

Closed bamarsha closed 1 year ago

bamarsha commented 1 year ago

PyQIR's parsing functionality and code generation functionality are currently totally independent; parsed QIR is read-only and can't be edited and re-emitted, and generated QIR is write-only. This precludes use cases like doing simple transforms or optimizations with PyQIR on QIR that came from another source. It's also more complicated to maintain since there is overlap between the two, and we're using a different Rust LLVM wrapper for each.

We want to combine Parser and Generator into one cohesive thing that can do both. The first step was refactoring Generator to use LLVM objects directly in #157. Next we will add parsing functionality to the LLVM-backed Generator types in #174. Finally, the pyqir-parser package will be retired. We will transition to a single pyqir package that has the same functionality as pyqir-parser, pyqir-generator, and pyqir-evaluator (with the first step in #170).

For users of pyqir-generator and pyqir-evaluator, the impact will be minimal. The main breaking change is to how types for external functions are created, which came with #157. pyqir-parser users will need to adapt to a completely rewritten API, although it will support the same functionality.

A sketch of the proposed API for the combined parsing and generating functionality is here: https://gist.github.com/samarsha/abdfd9fa7b6aee133904935c7b4511a5

The changes to external functions look like this. Before:

from pyqir.generator import SimpleModule, types

m = SimpleModule("foo", 1, 1)
f = m.add_external_function(
    "f",
    types.Function([types.Int(8), types.QUBIT], types.VOID)
)

After:

from pyqir.generator import SimpleModule

m = SimpleModule("foo", 1, 1)
types = m.types
f = m.add_external_function(
    "f",
    types.function(types.void, [types.int(8), types.qubit])
)
LaurentAjdnik commented 1 year ago

Greaaaaaaaaaaaaaaaaat news! 🎉

Thanks so much!