Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Replace C++ QIR Runtime with Rust QIR stdlib #1087

Merged
merged 58 commits into from
Sep 16, 2022
Merged

Conversation

swernli
Copy link
Collaborator

@swernli swernli commented Sep 14, 2022

This change removes the C++ implementation for the QIR Runtime and replaces it with a Rust implementation of a QIR stdlib that matches the classical functionality exposed.

Previously, the C++ QIR Runtime was compiled into three dynamic libraries, Microsoft.Quantum.Qir.Runtime, Microsoft.Quantum.Qir.QSharp.Foundation, and Microsoft.Quantum.Qir.QSharp.Core, and the "core" library would load the simulator dll at runtime. With this change, these dynamic libraries are no longer shipped, and we instead build a single static library, qir_stdlib, that is linked into the simulator dynamic library, Microsoft.Quantum.Simulator.Runtime. This makes the full state simulator a "QIR Backend" which means it exposes the classical and quantum functions necessary to link in and execute a QIR program.

To help orient reviews of this rather large change, here are some key landmarks in the code:

  • src/Qir/Runtime The existing C++ code is removed in favor of a single stdlib folder that implements the classical functions described in the QIR spec as a static library written in Rust. This includes unit tests for those functions.
  • src/Qir/Tools The generation logic for a main.cpp to run a given QIR program is updated to remove the support for the C++ specific runtime elements (namely QIR Execution Context and IRuntimeDriver references) in favor of just an invocation of the entry point function. The QIR libraries are removed in favor of linking directly against the full state simulator as a QIR backend.
  • src/Qir/Tests The C++ scenario tests that compile against QIR generated from Q# are adapted to the new library implementation, requiring the removal of any tests dependent on the internal C++ representations. Build infrastructure is updated to link against the full state simulator.
  • src/Simulation/Native The full state simulator is adapted to statically link the qir_stdlib as a dependency and re-export the QIR classical library support. New QIR-compliant C-API is added to allow QIR programs to link directly against the quantum operations supported by the simulator without the need of an additional shim library.
  • src/Simulation/qdk_sim_rs Cleanup to some Rust patterns to make clippy happy with the repo (not specific to the C++ -> Rust QIR transition, just minor general linter fixes).

NOTE: e2e builds will fail until a corresponding update is made to the e2e build script logic in order to build the QIR stdlib before the simulator to satisfy dependencies.

@kuzminrobin
Copy link
Contributor

You may also want to update the .github/CODEOWNERS (some paths get deleted).

PauliId PauliArgCli;
PauliArgCli = PauliId::PauliId_I;
char PauliArgCli;
PauliArgCli = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Pauli values turn into the "magic numbers". E.g. it stops being evident that in green L37 and L20 the same value is used.
Consider creating some common C header with Pauli values enum, other common stuff, and including that header wherever needed.

src/Qir/Runtime/stdlib/src/lib.rs Outdated Show resolved Hide resolved
src/Qir/Runtime/stdlib/src/arrays.rs Outdated Show resolved Hide resolved
@@ -0,0 +1,504 @@
// Copyright (c) Microsoft Corporation.
Copy link
Contributor

@kuzminrobin kuzminrobin Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Nit)
If this file is a result of renaming/moving of some other file, then consider renaming/moving with git mv .. to preserve the history and minimize the diff (and only see the diff for the lines that changed).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Git rename detection doesn't use git mv. It works the same if you git rm and git add two files with similar contents. The rename is detected at diff time and has a configurable similarity threshold (default 50%); see git diff --find-renames[=<n>]. So if more than 50% of the file changed, it won't be seen as a rename by default, even if you used git mv.

Copy link
Contributor

@kuzminrobin kuzminrobin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm done reviewing. Approving with suggestions.
I recommend more approvals.

@swernli
Copy link
Collaborator Author

swernli commented Sep 16, 2022

Merging with passed e2e builds (see https://github.com/microsoft/qdk/pull/288). Thanks everyone for the review!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants