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
57 stars 24 forks source link

Sample for using pyqir_generator to compile a subset of Python to QIR #100

Closed bettinaheim closed 2 years ago

bettinaheim commented 2 years ago

Filing an issue to capture a question/request that has come up in discussions:

This repo currently contains an example that shows how to use the pyqir_generator to generate QIR for a mock language. Conceptually, a program is written in a language, the language specific compiler is then used to create the AST representing that program, and usually tools for traversing that AST (and AST walker) and perform custom logic on each AST node are available as part of the compiler infrastructure. The idea is then to use such a walker to invoke the pyqir_generator on each node to emit QIR representing the program. While this is shown for a custom language in one of the examples, there also exist tools that create an AST for a Python program. The request hence was to add an example to show how such a tool could be used to compile parts of Python itself to QIR.

LaurentAjdnik commented 2 years ago

Hi @bettinaheim!

there also exist tools that create an AST for a Python program

Using the built-in ast Python library probably is the way to go. Here's a nice tutorial.

From here, any Python source file can be converted into LLVM IR, with a little bit (= a ton) of work. PyLLVM attempted to do this: "A compiler from a subset of Python to LLVM-IR".

invoke the pyqir_generator on each node to emit QIR compile parts of Python itself to QIR

The current mock language specializes in quantum circuit description, which makes it ready for conversion into QIR through pygir-generator.

Do we agree that our Python sample should mostly be code for generating a circuit, with no other complex Python code around?

For instance a few lines of Qiskit that would be parsed through the ast library and sent to pyqir-generator for QIR output (hence not using the Qiskit library itself at all)?

LaurentAjdnik commented 2 years ago

Quick and dirty proof of concept on https://github.com/LaurentAjdnik/QWOQ2QIR (Qiskit without Qiskit to QIR 😁).

bettinaheim commented 2 years ago

This proof of concept is great, and exactly along the lines I was thinking. Yes, I agree that the main caveat to this approach is that Python is a huge language, and I would not generally recommend aiming to compile all of Python to QIR. There are actually several projects that have worked on a Python -> LLVM compiler, in particular I believe related to a similar desire in the machine learning community to on one hand allow people to write domain specific code in Python and on the other hand leverage LLVM for performance benefits. I would hence agree that the goal here should be to illustrate what can be done and focus on what is most important for quantum programs (i.e. define a subset of python that is compiled to QIR rather than aiming for a general purpose tool).

@LaurentAjdnik Would you consider contributing a sample to this repository based on your work?