-
Notifications
You must be signed in to change notification settings - Fork 174
Adding new custom simulator example #1120
Conversation
Goal: Implement an example simulator that is easy to understand and maps more or less directly to the theory. Based on the Eigen C++ linear algebra library. Untested, guide to come.
Move each simulator to its own subdirectory.
…imulator implementation
| void __quantum__rt__callable_make_adjoint(%Callable*) | ||
| ``` | ||
|
|
||
| Some are directly implemented by the Runtime, such as `__quantum__rt__message` or `__quantum__rt__string_create`, while others will require a call to the backend system, such as `__quantum__rt__qubit_allocate`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessarily the case that __quantum_rt_message is handled directly by the Runtime and not the backend?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is how the runtime is currently written if you look at bridge-rt.ll and Output.cpp. I suppose the backend may provide an output stream, but the Runtime does not call any interface methods implemented by backends like it does for qubit allocation for example.
|
|
||
| - the driver `IRuntimeDriver` : this is critical for the Runtime to operate, implementing qubit allocation and other backend-specific functionality | ||
| - the gate set `IQuantumGateSet` : only used by those backends that want to provide the Q# instruction set | ||
| - the diagnostics `IDiagnostics` : optional but helpful component to provide debug information of the backend state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diagnostics pipeline need not provide state information, though I agree that is the most common usage in simulators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there other information that could be provided via the diagnostics interface that I can mention here?
|
|
||
| A trace simulator is a quick way to provide a backend to the QIR Runtime system. | ||
| It's function is to print out all quantum instructions it receives. | ||
| While this can useful for debugging, it is also a simple way to connect QIR to hardware backends that do not support control flow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that application, it's worth noting that the output from trace simulation isn't a representation of the entire quantum program, but a trajectory through the program.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true, which is why I specify this applies to "straight-line" programs with no control flow.
|
Thank you Chris (@cgranade) for providing feedback on this! :) |
|
Apologies to for the continued delay... I'm working on this review and should have it ready tomorrow! |
| { | ||
| UpdateState(GetQubitIdx(q), /*remove=*/true); // ρ' = tr_i[|Ψ⟩〈Ψ|] | ||
| this->numActiveQubits--; | ||
| this->computeRegister.erase(this->computeRegister.begin() + GetQubitIdx(q)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing to note about this implementation for qubit management is that it assumes a given qubit is never released before a qubit that was allocated after it (qubits are strictly First-In-Last-Out ordered). That is true for Q# due to enforced qubit scoping, but may not be true for QIR generally when generated from arbitrary languages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be worth calling this out in the description above along with the note that newly allocated qubits are appended to the end of the register.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think First-In-Last-Out holds of the given implementation. The compute register is a tightly packed array of qubits, and new qubits are always appended to the back, but a qubit can be removed from an arbitrary position in the register. When updating the state vector upon deallocation, the partial trace can also be applied for any qubit index in the register.
| ```shell | ||
| mkdir build | ||
| sudo apt update && sudo apt install -y mono-complete | ||
| curl https://dist.nuget.org/win-x86-commandline/latest/nuget.exe --output build/nuget |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The steps to download nuget and setup via mono can actually be simplified since mono-based nuget is it's own package. See for example:
| apt-get -y install --no-install-recommends mono-complete nuget && \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem I had with getting nuget from apt was that it downloads an incredibly old version (2.8.7.0) with which I couldn't execute the rest of the commands.
swernli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the sample looks good. Thanks for working on this! I've left a few comments regarding places that could use a little clarification, and the nuget package on Linux can simplify the setup instructions, but otherwise it should be ready to merge.
* Optimization example: revert PR #1100 (#1125) * Use stdin/stdout instead of TCP/IP. (#1118) * Use stdin/stdout instead of TCP/IP. * Update package-lock. Co-authored-by: Ricardo Espinoza <[email protected]> * Add QIR JIT compilation from Python example (#1124) * Add QIR JIT compilation example in Python * Add README to JIT example and update for Windows * Small fix to JIT example README * Fix NuGet instructions on Linux * More fixes for Linux Co-authored-by: bettinaheim <[email protected]> Co-authored-by: Stefan J. Wernli <[email protected]> * Update Using and Borrowing Syntax (#1099) * Adding new custom simulator example (#1120) * Initial implementation of a simple simulator for QIR. Goal: Implement an example simulator that is easy to understand and maps more or less directly to the theory. Based on the Eigen C++ linear algebra library. Untested, guide to come. * Small tweaks to QIR simulator example * Restructured version of the Simulator example to separate out components. * First portion of QIR Runtime simulator guide * Add second simulator type: trace simulator Move each simulator to its own subdirectory. * Update Simulation example readme with simulator structure and trace simulator implementation * Remove version 1 of the simulation example * Split out TraceSimulator from top-level README and add run instructions * Move headers and libraries into subfolders * Remove version number from example folder name * Add explanation of the full state simulator sample * Remove duplicate information from top-level simulation sample README * Update structure of top-level simulation sample README * Small fixes to top-level simulator readme * Use and update relative links within repo * Small formatting changes to the sample state simulator README * Add more details on including the Eigen library * Apply suggestions from code review Co-authored-by: Chris Granade <[email protected]> * Add copyright header to source files * Use unicode angle brackets for quantum states * Small fixes to top-level README of simulation example * Get QIR Runtime dependencies from NuGet package instead * Add Linux instructions to sample simulators * Better formatting of the Windows/Linux separation * Apply suggestions from code review. * Add notice about Clang requirement to compile sample * Update examples/QIR/Simulation/TraceSimulator/README.md * Update examples/QIR/Simulation/StateSimulator/README.md Co-authored-by: Chris Granade <[email protected]> Co-authored-by: Stefan J. Wernli <[email protected]> * Fixing memory leak in generated QIR (#1129) * Update Unit Syntax (#1103) * Update Array Syntax (#1135) * Update readme links to runtime repo (#1137) Co-authored-by: dime10 <[email protected]> Co-authored-by: Chris Granade <[email protected]> Co-authored-by: Ricardo Espinoza <[email protected]> Co-authored-by: bettinaheim <[email protected]> Co-authored-by: Stefan J. Wernli <[email protected]> Co-authored-by: Scott Carda <[email protected]>
* Optimization example: revert PR #1100 (#1125) * Use stdin/stdout instead of TCP/IP. (#1118) * Use stdin/stdout instead of TCP/IP. * Update package-lock. Co-authored-by: Ricardo Espinoza <[email protected]> * Add QIR JIT compilation from Python example (#1124) * Add QIR JIT compilation example in Python * Add README to JIT example and update for Windows * Small fix to JIT example README * Fix NuGet instructions on Linux * More fixes for Linux Co-authored-by: bettinaheim <[email protected]> Co-authored-by: Stefan J. Wernli <[email protected]> * Update Using and Borrowing Syntax (#1099) * Adding new custom simulator example (#1120) * Initial implementation of a simple simulator for QIR. Goal: Implement an example simulator that is easy to understand and maps more or less directly to the theory. Based on the Eigen C++ linear algebra library. Untested, guide to come. * Small tweaks to QIR simulator example * Restructured version of the Simulator example to separate out components. * First portion of QIR Runtime simulator guide * Add second simulator type: trace simulator Move each simulator to its own subdirectory. * Update Simulation example readme with simulator structure and trace simulator implementation * Remove version 1 of the simulation example * Split out TraceSimulator from top-level README and add run instructions * Move headers and libraries into subfolders * Remove version number from example folder name * Add explanation of the full state simulator sample * Remove duplicate information from top-level simulation sample README * Update structure of top-level simulation sample README * Small fixes to top-level simulator readme * Use and update relative links within repo * Small formatting changes to the sample state simulator README * Add more details on including the Eigen library * Apply suggestions from code review Co-authored-by: Chris Granade <[email protected]> * Add copyright header to source files * Use unicode angle brackets for quantum states * Small fixes to top-level README of simulation example * Get QIR Runtime dependencies from NuGet package instead * Add Linux instructions to sample simulators * Better formatting of the Windows/Linux separation * Apply suggestions from code review. * Add notice about Clang requirement to compile sample * Update examples/QIR/Simulation/TraceSimulator/README.md * Update examples/QIR/Simulation/StateSimulator/README.md Co-authored-by: Chris Granade <[email protected]> Co-authored-by: Stefan J. Wernli <[email protected]> * Fixing memory leak in generated QIR (#1129) * Creating module based configuration * Adding configuration modules * More refactoring * Deprecating RuleSet profile * Refactoring * Finishing dump config * Preparing for draft PR * Cleaning * More refactoring * Preparing error logging * Fixing CI * Adding commandline tests * Refactor * Fixing issues * Minor changes * Style fixing * Update Unit Syntax (#1103) * Renaming pass * More refactor * Finalising component registration * Formatting style * Minor refactor * Fixed CI * More tests * Removing example QIR * Update Array Syntax (#1135) * Update readme links to runtime repo (#1137) * Updating test * Updating Phi tests * CI update * Removing garbage * Updating according to requests * Removing garbage * Updating config binder Co-authored-by: dime10 <[email protected]> Co-authored-by: Chris Granade <[email protected]> Co-authored-by: Ricardo Espinoza <[email protected]> Co-authored-by: bettinaheim <[email protected]> Co-authored-by: Stefan J. Wernli <[email protected]> Co-authored-by: Scott Carda <[email protected]>
Adds a new example demonstrating how to write a custom simulator and connect it to the QIR Runtime.
Ideally I'd use a different method of including some of the headers defined in qsharp-runtime/src/Qir/Runtime/public needed to compile the simulator samples (currently just copied over).Preview sample here.