✨ Pull GPhaseOps out of multi-operation ctrl modifiers - #2013
✨ Pull GPhaseOps out of multi-operation ctrl modifiers#2013simon1hofmann wants to merge 4 commits into
GPhaseOps out of multi-operation ctrl modifiers#2013Conversation
GPhaseOps out of multi-operation ctrl modifiers
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@coderabbitai full review |
✅ Action performedFull review finished. |
📝 WalkthroughSummary by CodeRabbit
WalkthroughQC and QCO now pull eligible global phases from multi-operation control modifiers and materialize equivalent controlled phase operations. New QC and QCO programs test single- and multi-control equivalence cases. ChangesControlled global-phase canonicalization
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant CtrlOp
participant PullGPhaseOutOfCtrl
participant createControlledPhase
CtrlOp->>PullGPhaseOutOfCtrl: inspect multi-operation control body
PullGPhaseOutOfCtrl->>createControlledPhase: materialize eligible controlled phases
createControlledPhase->>CtrlOp: insert phases and remove original global phases
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@mlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cpp`:
- Around line 38-42: Remove the top-level const qualifiers from the by-value
parameters of both createControlledPhase helpers:
mlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cpp lines 38-42 and
mlir/lib/Dialect/QCO/IR/Modifiers/CtrlOp.cpp lines 45-49. Update controlledLoc,
phaseLoc, controls, and theta to use unqualified Location, ValueRange, and Value
types while leaving behavior unchanged.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: dc5bb5b1-a036-47ef-84e1-e61c0060d3f8
📒 Files selected for processing (9)
CHANGELOG.mdmlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cppmlir/lib/Dialect/QCO/IR/Modifiers/CtrlOp.cppmlir/unittests/Dialect/QC/IR/test_qc_ir.cppmlir/unittests/Dialect/QCO/IR/test_qco_ir.cppmlir/unittests/programs/qc_programs.cppmlir/unittests/programs/qc_programs.hmlir/unittests/programs/qco_programs.cppmlir/unittests/programs/qco_programs.h
| static void createControlledPhase(PatternRewriter& rewriter, | ||
| const Location controlledLoc, | ||
| const Location phaseLoc, | ||
| const ValueRange controls, | ||
| const Value theta) { |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
Drop the top-level const on by-value MLIR core types in both createControlledPhase helpers. The QCO helper mirrors the QC helper, so both signatures carry the same const Location, const ValueRange, and const Value parameters. These types are passed by value, so the qualifier gives callers no guarantee and departs from the project convention.
mlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cpp#L38-L42: change the parameters toLocation controlledLoc,Location phaseLoc,ValueRange controls, andValue theta.mlir/lib/Dialect/QCO/IR/Modifiers/CtrlOp.cpp#L45-L49: apply the same parameter change to the QCO helper.
Based on learnings: "In MLIR code under any mlir/ directory, avoid using const qualifiers on core MLIR types in function parameters/signatures (e.g., Value, Type, Attribute, Operation*, Block*, Region*, etc.)."
📍 Affects 2 files
mlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cpp#L38-L42(this comment)mlir/lib/Dialect/QCO/IR/Modifiers/CtrlOp.cpp#L45-L49
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@mlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cpp` around lines 38 - 42, Remove the
top-level const qualifiers from the by-value parameters of both
createControlledPhase helpers: mlir/lib/Dialect/QC/IR/Modifiers/CtrlOp.cpp lines
38-42 and mlir/lib/Dialect/QCO/IR/Modifiers/CtrlOp.cpp lines 45-49. Update
controlledLoc, phaseLoc, controls, and theta to use unqualified Location,
ValueRange, and Value types while leaving behavior unchanged.
Source: Learnings
burgholzer
left a comment
There was a problem hiding this comment.
Just two comments here while I have some time to kill
| /** | ||
| * @brief Materialize a global phase controlled by @p controls. | ||
| * @return The updated control qubits in their original order. | ||
| */ | ||
| static SmallVector<Value> createControlledPhase(PatternRewriter& rewriter, | ||
| const Location controlledLoc, | ||
| const Location phaseLoc, | ||
| const ValueRange controls, | ||
| const Value theta) { | ||
| assert(!controls.empty()); | ||
| if (controls.size() == 1) { | ||
| return {POp::create(rewriter, controlledLoc, controls.front(), theta) | ||
| .getOutputQubit(0)}; | ||
| } | ||
|
|
||
| auto controlledPhase = CtrlOp::create( | ||
| rewriter, controlledLoc, controls.drop_back(), controls.back(), | ||
| [&](Value target) -> Value { | ||
| return POp::create(rewriter, phaseLoc, target, theta).getOutputQubit(0); | ||
| }); | ||
| return SmallVector<Value>(controlledPhase.getOutputQubits()); | ||
| } |
There was a problem hiding this comment.
This could potentially be optimized a little bit by splitting it into the one-control case (which could simply return a Value instead of a Vector), and the rest.
Also holds for the QC case.
| b.gphase(0.123); | ||
| b.x(targets[0]); | ||
| b.gphase(0.456); |
There was a problem hiding this comment.
Slightly confused here: shouldn't these global phases be combined by the new logic and then the result would only be a single cp gate?
I suppose we do not yet support merging of gates across modifiers (such as the cp's here)..
🤖 AI text below 🤖
Description
Add canonicalization patterns that pull
GPhaseOps out of multi-operationctrlmodifiers in the QC and QCO dialects.A controlled global phase is materialized as:
POpon the control for a single-control modifier;CtrlOpapplyingPOpto the final control for a multi-control modifier.The existing single-operation and new multi-operation canonicalizations share the same controlled-phase construction helper. The QCO implementation also threads the updated control-qubit SSA values through the remaining modifier.
Global phases whose angle is defined inside the modifier body are conservatively left unchanged to avoid violating SSA dominance.
Fixes #1759
AI assistance was used to inspect the existing canonicalizations, implement and refactor the QC and QCO patterns, add regression tests, and run validation.
Checklist
If PR contains AI-assisted content:
🤖 *AI text below* 🤖(titles are exempt).