microsoft / qsharp-compiler

Q# compiler, command line tool, and Q# language server
https://docs.microsoft.com/quantum
MIT License
682 stars 170 forks source link

Some questions about Q# compilation #1051

Closed QuXing9 closed 2 years ago

QuXing9 commented 3 years ago

Description

Hello! I am very interested in the Q# quantum language, It's really an amazing work. And I am a beginner in the Q#, there are three questions that confuse me a lot. Can you help me answer them? Thanks very much!

Looking forward to your reply! Thanks very much!

Environment

bettinaheim commented 3 years ago

@QuXing9 Thank you for the great questions! I'll do my best to answer:

We are currently working on setting up convenient tooling to do that, and also fully integrating it into the QDK such that simply building a project will generate the bitcode, link it with the runtime libraries to build the executable and execute it. I am happy to give more updates as we go. :) To create a main, we current use a separate entry point generator that creates a C++ entry point. We are working on packaging a dotnet command line tool that given the path to either a Q# dll or in the future also just directly the path to the QIR bitcode builds an executable. That work is still on a feature branch, however.

In the meantime, we have set up a project for the UnitaryHack that allows to generate and execute .ll files that you could leverage to start experimenting with it. Here are the issues that give instructions for how to easily do that: https://github.com/microsoft/qsharp-compiler/issues/1028 and https://github.com/microsoft/qsharp-compiler/issues/1030. Let me know if that gets you started, or what exactly you were hoping to do and I am happy to assist.

Yes, by and large your understand is correct. The Q# compiler compiles the project to QIR, and the clang linker links that QIR with the compiled runtime libraries to create an executable. The QIR in particular contains declarations without an implementations for a) Q# callables that are declared as intrinsic (the QIR declarations start with quantumqis), and b) the used runtime functions that provide basic functionality (such as function for e.g. array creation) as defined in the QIR specs (the QIR declarations start with quantumrt). These declarations are expected to have an implementation in the runtime libraries that are linked. The QIR runtime package that we link contains compiled native libraries that implement the functions b). It also contains the implementation for a) for the intrinsic Q# callables that are used by the Q# standard libraries (the ones in the QSharp Core library).

The whole idea of this setup is that the quantum instruction set is fully configurable; say you would like to run Q# code on a particular hardware backend that implements a certain set of quantumqis__ functions. One can then use or write a Q# library that defines these functions as intrinsic and define this as the "target package". I realize this doesn't fully explain how this works, but I wanted to share the idea behind all this - hopefully we will be able to set up some nice docs on that in the future. In the meantime, let me know if you have some concrete ideas you would like to experiment with.

The simulator is very similar to the runtime libraries in that it provides the implementation for the quantumqis__ functions and is linked with the QIR and other runtime libraries to create the executable.

Does that help? Let me know if you have more questions, or if you had anything specific in mind that you wanted to do, and we'll do our best to help. :)

QuXing9 commented 3 years ago

@bettinaheim Thank you for your detailed answers! I will have a try as soon as possible!

QuXing9 commented 3 years ago

Hi! @bettinaheim Thanks for your detailed explanation, I successfully executed the QIR code on the Windows system using the Clang compiler. At the same time, I met two new problems, I want to ask you for advice.

Looking forward to your reply. Thanks in advance!

QuXing9 commented 2 years ago

Problem solved, thanks!