Skip to content

Commit 4029d09

Browse files
🎨 Use custom builder classes in ctrl methods (#1402)
## Description This PR improves the signature of the `QCProgramBuilder::ctrl` and `QCOProgramBuilder::ctrl` methods. ## Checklist: - [x] The pull request only contains commits that are focused and relevant to this change. - [x] ~I have added appropriate tests that cover the new/changed functionality.~ - [x] ~I have updated the documentation to reflect these changes.~ - [x] ~I have added entries to the changelog for any noteworthy additions, changes, fixes, or removals.~ - [x] ~I have added migration instructions to the upgrade guide (if needed).~ - [x] The changes follow the project's style guidelines and introduce no new warnings. - [x] The changes are fully tested and pass the CI checks. - [x] I have reviewed my own code changes. --------- Signed-off-by: burgholzer <burgholzer@me.com> Co-authored-by: burgholzer <burgholzer@me.com>
1 parent 233b974 commit 4029d09

File tree

10 files changed

+121
-141
lines changed

10 files changed

+121
-141
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This project adheres to [Semantic Versioning], with the exception that minor rel
1111

1212
### Added
1313

14-
- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264]) ([**@burgholzer**], [**@denialhaag**])
14+
- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264], [#1402]) ([**@burgholzer**], [**@denialhaag**])
1515
- ✨ Return device handle from `add_dynamic_device_library` for direct backend creation ([#1381]) ([**@marcelwa**])
1616
- ✨ Add IQM JSON support for job submission in Qiskit-QDMI Backend ([#1375], [#1382]) ([**@marcelwa**], [**@burgholzer**])
1717
- ✨ Add authentication support for QDMI sessions with token, username/password, auth file, auth URL, and project ID parameters ([#1355]) ([**@marcelwa**])
@@ -286,6 +286,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool
286286

287287
<!-- PR links -->
288288

289+
[#1402]: https://github.com/munich-quantum-toolkit/core/pull/1402
289290
[#1385]: https://github.com/munich-quantum-toolkit/core/pull/1385
290291
[#1384]: https://github.com/munich-quantum-toolkit/core/pull/1384
291292
[#1383]: https://github.com/munich-quantum-toolkit/core/pull/1383

mlir/include/mlir/Dialect/QC/Builder/QCProgramBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ class QCProgramBuilder final : public OpBuilder {
833833
*
834834
* @par Example:
835835
* ```c++
836-
* builder.ctrl(q0, [&](auto& b) { b.x(q1); });
836+
* builder.ctrl(q0, [&] { builder.x(q1); });
837837
* ```
838838
* ```mlir
839839
* qc.ctrl(%q0) {
@@ -842,7 +842,7 @@ class QCProgramBuilder final : public OpBuilder {
842842
* ```
843843
*/
844844
QCProgramBuilder& ctrl(ValueRange controls,
845-
const std::function<void(OpBuilder&)>& body);
845+
const std::function<void()>& body);
846846

847847
//===--------------------------------------------------------------------===//
848848
// Deallocation

mlir/include/mlir/Dialect/QC/IR/QCOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ def CtrlOp : QCOp<"ctrl",
966966

967967
let builders = [
968968
OpBuilder<(ins "ValueRange":$controls, "UnitaryOpInterface":$bodyUnitary)>,
969-
OpBuilder<(ins "ValueRange":$controls, "const std::function<void(OpBuilder &)>&":$bodyBuilder)>
969+
OpBuilder<(ins "ValueRange":$controls, "const std::function<void()>&":$bodyBuilder)>
970970
];
971971

972972
let hasCanonicalizer = 1;

mlir/include/mlir/Dialect/QCO/Builder/QCOProgramBuilder.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,10 +987,11 @@ class QCOProgramBuilder final : public OpBuilder {
987987
*
988988
* @par Example:
989989
* ```c++
990-
* {controls_out, targets_out} = builder.ctrl(q0_in, q1_in, [&](auto& b) {
991-
* auto q1_res = b.x(q1_in);
992-
* return {q1_res};
993-
* });
990+
* {controls_out, targets_out} =
991+
* builder.ctrl(q0_in, q1_in, [&](ValueRange targets) {
992+
* auto q1_res = builder.x(targets[0]);
993+
* return {q1_res};
994+
* });
994995
* ```
995996
* ```mlir
996997
* %controls_out, %targets_out = qco.ctrl(%q0_in) %q1_in {
@@ -1001,7 +1002,7 @@ class QCOProgramBuilder final : public OpBuilder {
10011002
*/
10021003
std::pair<ValueRange, ValueRange>
10031004
ctrl(ValueRange controls, ValueRange targets,
1004-
const std::function<ValueRange(OpBuilder&, ValueRange)>& body);
1005+
const std::function<ValueRange(ValueRange)>& body);
10051006

10061007
//===--------------------------------------------------------------------===//
10071008
// Deallocation

mlir/include/mlir/Dialect/QCO/IR/QCOOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ def CtrlOp : QCOOp<"ctrl", traits =
10991099
build($_builder, $_state, controls.getTypes(), targets.getTypes(), controls, targets);
11001100
}]>,
11011101
OpBuilder<(ins "ValueRange":$controls, "ValueRange":$targets, "UnitaryOpInterface":$bodyUnitary)>,
1102-
OpBuilder<(ins "ValueRange":$controls, "ValueRange":$targets, "const std::function<ValueRange(OpBuilder &, ValueRange)>&":$bodyBuilder)>
1102+
OpBuilder<(ins "ValueRange":$controls, "ValueRange":$targets, "const std::function<ValueRange(ValueRange)>&":$bodyBuilder)>
11031103
];
11041104

11051105
let hasCanonicalizer = 1;

mlir/lib/Dialect/QC/Builder/QCProgramBuilder.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ QCProgramBuilder& QCProgramBuilder::reset(Value qubit) {
166166
const std::variant<double, Value>&(PARAM), ValueRange controls) { \
167167
checkFinalized(); \
168168
CtrlOp::create(*this, loc, controls, \
169-
[&](OpBuilder& b) { OP_CLASS::create(b, loc, PARAM); }); \
169+
[&] { OP_CLASS::create(*this, loc, PARAM); }); \
170170
return *this; \
171171
}
172172

@@ -191,7 +191,7 @@ DEFINE_ZERO_TARGET_ONE_PARAMETER(GPhaseOp, gphase, theta)
191191
Value target) { \
192192
checkFinalized(); \
193193
CtrlOp::create(*this, loc, controls, \
194-
[&](OpBuilder& b) { OP_CLASS::create(b, loc, target); }); \
194+
[&] { OP_CLASS::create(*this, loc, target); }); \
195195
return *this; \
196196
}
197197

@@ -228,9 +228,8 @@ DEFINE_ONE_TARGET_ZERO_PARAMETER(SXdgOp, sxdg)
228228
const std::variant<double, Value>&(PARAM), ValueRange controls, \
229229
Value target) { \
230230
checkFinalized(); \
231-
CtrlOp::create(*this, loc, controls, [&](OpBuilder& b) { \
232-
OP_CLASS::create(b, loc, target, PARAM); \
233-
}); \
231+
CtrlOp::create(*this, loc, controls, \
232+
[&] { OP_CLASS::create(*this, loc, target, PARAM); }); \
234233
return *this; \
235234
}
236235

@@ -263,8 +262,8 @@ DEFINE_ONE_TARGET_ONE_PARAMETER(POp, p, theta)
263262
const std::variant<double, Value>&(PARAM2), ValueRange controls, \
264263
Value target) { \
265264
checkFinalized(); \
266-
CtrlOp::create(*this, loc, controls, [&](OpBuilder& b) { \
267-
OP_CLASS::create(b, loc, target, PARAM1, PARAM2); \
265+
CtrlOp::create(*this, loc, controls, [&] { \
266+
OP_CLASS::create(*this, loc, target, PARAM1, PARAM2); \
268267
}); \
269268
return *this; \
270269
}
@@ -300,8 +299,8 @@ DEFINE_ONE_TARGET_TWO_PARAMETER(U2Op, u2, phi, lambda)
300299
const std::variant<double, Value>&(PARAM3), ValueRange controls, \
301300
Value target) { \
302301
checkFinalized(); \
303-
CtrlOp::create(*this, loc, controls, [&](OpBuilder& b) { \
304-
OP_CLASS::create(b, loc, target, PARAM1, PARAM2, PARAM3); \
302+
CtrlOp::create(*this, loc, controls, [&] { \
303+
OP_CLASS::create(*this, loc, target, PARAM1, PARAM2, PARAM3); \
305304
}); \
306305
return *this; \
307306
}
@@ -326,9 +325,8 @@ DEFINE_ONE_TARGET_THREE_PARAMETER(UOp, u, theta, phi, lambda)
326325
QCProgramBuilder& QCProgramBuilder::mc##OP_NAME( \
327326
ValueRange controls, Value qubit0, Value qubit1) { \
328327
checkFinalized(); \
329-
CtrlOp::create(*this, loc, controls, [&](OpBuilder& b) { \
330-
OP_CLASS::create(b, loc, qubit0, qubit1); \
331-
}); \
328+
CtrlOp::create(*this, loc, controls, \
329+
[&] { OP_CLASS::create(*this, loc, qubit0, qubit1); }); \
332330
return *this; \
333331
}
334332

@@ -358,8 +356,8 @@ DEFINE_TWO_TARGET_ZERO_PARAMETER(ECROp, ecr)
358356
const std::variant<double, Value>&(PARAM), ValueRange controls, \
359357
Value qubit0, Value qubit1) { \
360358
checkFinalized(); \
361-
CtrlOp::create(*this, loc, controls, [&](OpBuilder& b) { \
362-
OP_CLASS::create(b, loc, qubit0, qubit1, PARAM); \
359+
CtrlOp::create(*this, loc, controls, [&] { \
360+
OP_CLASS::create(*this, loc, qubit0, qubit1, PARAM); \
363361
}); \
364362
return *this; \
365363
}
@@ -394,8 +392,8 @@ DEFINE_TWO_TARGET_ONE_PARAMETER(RZZOp, rzz, theta)
394392
const std::variant<double, Value>&(PARAM2), ValueRange controls, \
395393
Value qubit0, Value qubit1) { \
396394
checkFinalized(); \
397-
CtrlOp::create(*this, loc, controls, [&](OpBuilder& b) { \
398-
OP_CLASS::create(b, loc, qubit0, qubit1, PARAM1, PARAM2); \
395+
CtrlOp::create(*this, loc, controls, [&] { \
396+
OP_CLASS::create(*this, loc, qubit0, qubit1, PARAM1, PARAM2); \
399397
}); \
400398
return *this; \
401399
}
@@ -417,9 +415,8 @@ QCProgramBuilder& QCProgramBuilder::barrier(ValueRange qubits) {
417415
// Modifiers
418416
//===----------------------------------------------------------------------===//
419417

420-
QCProgramBuilder&
421-
QCProgramBuilder::ctrl(ValueRange controls,
422-
const std::function<void(OpBuilder&)>& body) {
418+
QCProgramBuilder& QCProgramBuilder::ctrl(ValueRange controls,
419+
const std::function<void()>& body) {
423420
checkFinalized();
424421
CtrlOp::create(*this, loc, controls, body);
425422
return *this;

mlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ struct CtrlInlineGPhase final : OpRewritePattern<CtrlOp> {
9797
SmallVector<Value> newControls(op.getControls());
9898
const auto newTarget = newControls.back();
9999
newControls.pop_back();
100-
CtrlOp::create(rewriter, op.getLoc(), newControls, [&](OpBuilder& b) {
101-
POp::create(b, op.getLoc(), newTarget, gPhaseOp.getTheta());
100+
CtrlOp::create(rewriter, op.getLoc(), newControls, [&] {
101+
POp::create(rewriter, op.getLoc(), newTarget, gPhaseOp.getTheta());
102102
});
103103
rewriter.eraseOp(op);
104104

@@ -161,14 +161,14 @@ void CtrlOp::build(OpBuilder& odsBuilder, OperationState& odsState,
161161

162162
void CtrlOp::build(OpBuilder& odsBuilder, OperationState& odsState,
163163
ValueRange controls,
164-
const std::function<void(OpBuilder&)>& bodyBuilder) {
164+
const std::function<void()>& bodyBuilder) {
165165
const OpBuilder::InsertionGuard guard(odsBuilder);
166166
odsState.addOperands(controls);
167167
auto* region = odsState.addRegion();
168168
auto& block = region->emplaceBlock();
169169

170170
odsBuilder.setInsertionPointToStart(&block);
171-
bodyBuilder(odsBuilder);
171+
bodyBuilder();
172172
odsBuilder.create<YieldOp>(odsState.location);
173173
}
174174

0 commit comments

Comments
 (0)