-
-
Notifications
You must be signed in to change notification settings - Fork 49
✨ Add non-linear control flow conversions between QC and QCO dialects #1396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 36 commits
9070fdf
4916ea3
ff54901
e968570
27909af
b2ab37c
0fc6bc4
7c323c6
f62fbcd
500431e
31ee7a1
183ffa7
69269f7
b38a9ce
738daae
4e3306e
e58701f
b3d10be
92b556e
660e191
4f0a6c7
597f838
7ad53d3
614f2f1
6ada604
7358d3b
15a8dca
010892b
5a42a8d
ad9acd2
537f19f
bc278b9
59edb8e
b1e77ec
abe1eef
981c6e0
5333f26
a8d71fa
dce760f
67ecea7
7ebca1c
338b7ee
7fb4191
6fed246
30cea55
7596d58
b52fa7b
18487bc
b4da26f
d45f448
dbe805d
c4a1d35
9866d2a
b36f5ab
e268089
ee30cd5
4ea6c50
7c03d28
7933d3b
fb8b255
f81ea76
beb6ed6
65b2cbe
5abab32
ce52deb
551ff31
fd4671b
6334c98
5cab58b
1ecbb48
ae3b6f8
7a32e1f
f2963b2
cafd34e
372a231
470dfa9
fb2e3cc
e6ff89a
8c5612d
e1a8512
ab8f74d
9145b43
7ef1f7f
3938420
ba0c481
d14aecd
86429c5
aee2bca
686ebe3
aa2a70e
7950efe
b480801
b320196
ec8936b
4a70032
ca52bf3
6473046
9be2e7c
ffb2313
8a411cf
fa1a938
a82c3d4
ef77113
f24a768
2cb2766
46851b9
d6c6b94
d093ee7
aa6a890
a6c3424
415b399
41e3c58
2f3731a
b9d3e03
7c4fe40
e712c98
6690227
35a41ec
a62b91c
1ad3e77
0d00c92
254edb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -868,6 +868,208 @@ class QCProgramBuilder final : public OpBuilder { | |
| */ | ||
| QCProgramBuilder& dealloc(Value qubit); | ||
|
|
||
| //===--------------------------------------------------------------------===// | ||
| // SCF operations | ||
| //===--------------------------------------------------------------------===// | ||
|
|
||
| /** | ||
| * @brief Constructs a scf.for operation without iter args | ||
| * | ||
| * @param lowerbound Lowerbound of the loop | ||
| * @param upperbound Upperbound of the loop | ||
| * @param step Stepsize of the loop | ||
| * @param body Function that builds the body of the for operation | ||
| * @return Reference to this builder for method chaining | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.scfFor(lb, ub, step, [&](auto& b) { b.x(q0); }); | ||
| * ``` | ||
| * ```mlir | ||
| * scf.for %iv = %lb to %ub step %step { | ||
| * qc.x %q0 : !qc.qubit | ||
| * } | ||
| * ``` | ||
| */ | ||
| QCProgramBuilder& scfFor(Value lowerbound, Value upperbound, Value step, | ||
li-mingbao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const std::function<void(OpBuilder&)>& body); | ||
li-mingbao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @brief Constructs a scf.while operation without return values | ||
| * | ||
| * @param beforeBody Function that builds the before body of the while | ||
| * operation | ||
| * @param afterBody Function that builds the after body of the while operation | ||
| * @return Reference to this builder for method chaining | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.scfWhile([&](auto& b) { | ||
| * b.h(q0); | ||
| * auto res = b.measure(q0) | ||
| * b.condition(res) | ||
| * }, [&](auto& b) { | ||
| * b.x(q0); | ||
| * b.yield() | ||
| * }); | ||
| * ``` | ||
| * ```mlir | ||
| * scf.while : () -> () { | ||
| * qc.h %q0 : !qc.qubit | ||
| * %res = qc.measure %q0 : !qc.qubit -> i1 | ||
| * scf.condition(%tres) | ||
| * } do { | ||
| * qc.x %q0 : !qc.qubit | ||
| * scf.yield | ||
| * } | ||
li-mingbao marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+940
to
+965
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to show both, a while as well as a doWhile loop here? |
||
| * ``` | ||
| */ | ||
| QCProgramBuilder& scfWhile(const std::function<void(OpBuilder&)>& beforeBody, | ||
| const std::function<void(OpBuilder&)>& afterBody); | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * @brief Constructs a scf.if operation without return values | ||
| * | ||
| * @param condition Condition for the if operation | ||
| * @param thenBody Function that builds the then body of the if | ||
| * operation | ||
| * @param elseBody Function that builds the else body of the if operation | ||
| * @return Reference to this builder for method chaining | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.scf.if(condition, [&](auto& b) { | ||
| * b.h(q0); | ||
| * }, [&](auto& b) { | ||
| * b.x(q0); | ||
| * }); | ||
| * ``` | ||
| * ```mlir | ||
| * scf.if %condition { | ||
| * qc.h %q0 : !qc.qubit | ||
| * } else { | ||
| * qc.x %q0 : !qc.qubit | ||
| * } | ||
| * ``` | ||
| */ | ||
|
Comment on lines
+980
to
+995
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given how it is very common in quantum to only have a |
||
| QCProgramBuilder& | ||
| scfIf(Value condition, const std::function<void(OpBuilder&)>& thenBody, | ||
| const std::function<void(OpBuilder&)>& elseBody = nullptr); | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @brief Constructs a scf.condition operation without any additional Values | ||
li-mingbao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param condition Condition for condition operation | ||
| * @return Reference to this builder for method chaining | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.condition(condition); | ||
| * ``` | ||
| * ```mlir | ||
| * scf.condition(%condition) | ||
| * ``` | ||
| */ | ||
| QCProgramBuilder& scfCondition(Value condition); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| //===--------------------------------------------------------------------===// | ||
| // Func operations | ||
| //===--------------------------------------------------------------------===// | ||
|
|
||
| /** | ||
| * @brief Constructs a func.return operation without return values | ||
| * | ||
| * @return Reference to this builder for method chaining | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.funcReturn(); | ||
| * ``` | ||
| * ```mlir | ||
| * func.return | ||
| * ``` | ||
| */ | ||
| QCProgramBuilder& funcReturn(); | ||
|
|
||
| /** | ||
| * @brief Constructs a func.call operation without return values | ||
| * | ||
| * @param name Name of the function that is called | ||
| * @param operands ValueRange of the used operands | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.funcCall("test", {q0}); | ||
| * ``` | ||
| * ```mlir | ||
| * func.call @test(%q0) : (!qco.qubit) -> () | ||
| * ``` | ||
| */ | ||
| QCProgramBuilder& funcCall(StringRef name, ValueRange operands); | ||
li-mingbao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @brief Constructs a func.func operation with return values | ||
| * | ||
| * @param name Name of the function that is called | ||
| * @param argTypes TypeRange of the arguments | ||
| * @param body Body of the function | ||
| * @return Reference to this builder for method chaining | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.funcFunc("test", argTypes, [&](OpBuilder& b, | ||
| * ValueRange args) { | ||
| * b.h(args[0]); | ||
| * b.funcReturn(); | ||
| * }) | ||
| * ``` | ||
| * ```mlir | ||
| * func.func @test(%arg0 : !qco.qubit) { | ||
| * qc.h %arg0 : !qc.qubit | ||
| * func.return | ||
| * } | ||
| * ``` | ||
| */ | ||
| QCProgramBuilder& | ||
| funcFunc(StringRef name, TypeRange argTypes, | ||
| const std::function<void(OpBuilder&, ValueRange)>& body); | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
li-mingbao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| //===--------------------------------------------------------------------===// | ||
| // Arith operations | ||
| //===--------------------------------------------------------------------===// | ||
|
|
||
| /** | ||
| * @brief Constructs a arith.constant of type Index with a given value | ||
| * | ||
| * @param index Value of the constant operation | ||
| * @return Result of the constant operation | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.arithConstantIndex(4); | ||
| * ``` | ||
| * ```mlir | ||
| * arith.constant 4 : index | ||
| * ``` | ||
| */ | ||
| Value arithConstantIndex(int index); | ||
|
|
||
| /** | ||
| * @brief Constructs a arith.constant of type i1 with a given bool value | ||
| * | ||
| * @param b Bool value of the constant operation | ||
| * @return Result of the constant operation | ||
| * | ||
| * @par Example: | ||
| * ```c++ | ||
| * builder.arithConstantBool(true); | ||
| * ``` | ||
| * ```mlir | ||
| * arith.constant 1 : i1 | ||
| * ``` | ||
| */ | ||
| Value arithConstantBool(bool b); | ||
li-mingbao marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| //===--------------------------------------------------------------------===// | ||
| // Finalization | ||
| //===--------------------------------------------------------------------===// | ||
|
|
||
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 repeated application of a gate to the same qubit isn't particularly interesting. Can we extend this example to demonstrate the use of the loop variable? For example to apply a Hadamard gate to all qubits, which is really common in quantum algorithms.