-
Notifications
You must be signed in to change notification settings - Fork 174
Qubit Allocation Analysis Pass #1098
Qubit Allocation Analysis Pass #1098
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
| @@ -0,0 +1 @@ | |||
| # {ExpandStaticAllocation} Specification | |||
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.
Just curious, what is this md for? Is it a placeholder for future expansion or does it provide some auto-documentation functionality with LLVM?
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've added a SPECIFICATION.md to each of the passes. The initial thought was that each of the pass should have a specification such that it is clear what it does without the need to read the code. However, this may be revised in one of the future PRs.
| @@ -0,0 +1,9 @@ | |||
| # Qubit Allocation Analysis | |||
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 specification.md is filled in, but the other two are not. Maybe include some placeholder text in the other two files indicating that they will be written in the future?
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 a good suggestion. I will add a placeholder text to the template in one of the coming PRs.
Qubit Allocation Analysis
Dependencies
This PR depends on #1092.
Purpose
The purpose of this pass is to analyse the code for qubit allocations and identify
the allocation dependency. This helps subsequent transfomation passes expand the code
to, for instance, eliminate loops and classical logic. This is desirable as the control
logic for some quantum computing systems may be limited and one may therefore wish
to reduce its complexity as much as possible at compile time.
Quick start
The following depnds on:
Running following command
will first build the pass, then build the QIR using Q# following by removing the noise using
optwith optimisation level 1. Finally, it will execute the analysis pass and should provide you with information about qubit allocation in the Q# program defined inConstSizeArray/ConstSizeArray.qs.Detailed run
From the Passes root (two levels up from this directory), make a new build
% mkdir Debug % cd Debug % cmake ..and then compile the
QubitAllocationAnalysis:Next return
examples/QubitAllocationAnalysisand enter the directoryConstSizeArrayto build the QIR:or execute the commands manually,
% dotnet build ConstSizeArray.csproj % opt -S qir/ConstSizeArray.ll -O1 > ../analysis-example.ll % make cleanReturning to
examples/QubitAllocationAnalysis, the pass can now be ran by executing:% opt -load-pass-plugin ../../Debug/libs/libQubitAllocationAnalysis.dylib --passes="print<qubit-allocation-analysis>" -disable-output analysis-example.llExample cases
Below we will consider a few different examples. You can run them by updating the code in
ConstSizeArray/ConstSizeArray.qsand executingmake runfrom theexamples/QubitAllocationAnalysisfolder subsequently. You will need to deleteanalysis-example.llbetween runs.Trivially constant
This is the simplest example we can think of:
The corresponding QIR is:
Running the pass procudes following output:
Dependency case
In some cases, a qubit array will be compile time constant in size if the function arguments
provided are compile-time constants. One example of this is:
The corresponding QIR is
The analyser returns following output:
Summary case
Finally, we do a summary case that demonstrates some of the more elaborate cases:
We will omit the QIR in the documenation as it is a long. The output of the anaysis is:
Tests
Finally, this PR introduces Lit tests for QIR analysis and transformations:
These are located in
tests/.Qubit Static Expansion
Purpose
A complementary pass to the one described above, is the expansion pass. Based on the analysis performed by the first pass, this pass attempts to change qubit allocations to static allocations (i.e. allocations with known size at compile time). This is the first step towards compiling for targets that does not have loop support.
Quick Start
The dependencies are described in above. To run this example, execute
in the
examples/QubitAllocationAnalysis. If ran with following Q# code :the original QIR becomes
After running the pass, the new QIR is
We see that the original code is expanded with two new functions where the constants are added inside the function body to replace the function arguments. This, in turn, allows us to do constant folding and eventually loop unrolling for the logic. The original function is preserved, albeit being dead code. In a future update, we may choose to discard the original functoin.