✨ Implement getInputQubits()/getOutputQubits()#1474
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
92f0867 to
9cf79c3
Compare
📝 WalkthroughSummary by CodeRabbit
WalkthroughAdds bulk accessor methods Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as Caller
participant CtrlOp as CtrlOp
participant Targets as Targets (operands)
participant Body as BodyUnitary
participant Results as Results
Note over Caller,Results: Old flow (before this change)
Caller->>CtrlOp: getInputQubit(i)
CtrlOp->>Body: getInputQubit(i) -- delegate to body unitary
Body-->>CtrlOp: Operand (resolved)
CtrlOp-->>Caller: Operand
Note over Caller,Results: New flow (after this change)
Caller->>CtrlOp: getInputQubits() / getInputQubit(i)
CtrlOp->>Targets: read getTargetsIn() (first T operands)
Targets-->>CtrlOp: OperandRange
CtrlOp-->>Caller: OperandRange
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
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: 3
🤖 Fix all issues with AI agents
In `@mlir/include/mlir/Dialect/QCO/IR/QCODialect.h`:
- Around line 83-87: The assertion in getInputQubits() is off-by-one and wrongly
requires operands.size() to be greater than T; change the check to allow
equality so take_front(T) is valid when operands.size() == T. Update the assert
in ValueRange getInputQubits() (method getInputQubits of the QCODialect/op
class) from asserting T < operands.size() to asserting T <= operands.size() so
it correctly permits zero-parameter cases.
In `@mlir/lib/Dialect/QCO/IR/Modifiers/CtrlOp.cpp`:
- Around line 190-198: CtrlOp::getOutputQubits returns targets then controls,
which contradicts getOutputQubit(i) that returns controls first; swap the concat
arguments so the returned ValueRange is controls followed by targets (i.e. call
llvm::concat<Value> with ValueRange{getControlsOut()} then
ValueRange{getTargetsOut()}) to match per‑qubit accessor ordering and keep
CtrlOp::getOutputQubits consistent with getOutputQubit.
- Around line 169-177: getInputQubits() currently concatenates targets then
controls (using llvm::concat<Value>(ValueRange{getTargetsIn()},
ValueRange{getControlsIn()}), producing a different ordering than
getInputQubit(i) which returns controls first. Fix getInputQubits() to
concatenate controls then targets (i.e., use llvm::concat with
ValueRange{getControlsIn()} followed by ValueRange{getTargetsIn()}) so that
getInputQubits()[i] matches getInputQubit(i); keep using the same ValueRange
wrapping to avoid changing storage semantics.
This change will hopefully shorten debug times for optimization patterns that introduce new gates. Especially when inserting gates with constant parameters, caution is advised when working with circuits containing controlled gates because the pattern will also be applied to the operation inside the control body - potentially leading to undesired operations in the control body. This assertion should make this easier to spot and debug.
147f2d5 to
8baf49c
Compare
Signed-off-by: burgholzer <burgholzer@me.com>
burgholzer
left a comment
There was a problem hiding this comment.
Okay. After spending some more time on this, I found a way cleaner solution that should also be the most performant one.
There is no need to perform any kind of iterator concatenation here.
@taminob You may want to add this PR to the changelog list.
Once CI is green, this is ready to go from my side.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@mlir/include/mlir/Dialect/QCO/IR/QCOInterfaces.td`:
- Around line 105-118: The documentation for the bulk range accessors lacks an
ordering contract; update the docstrings for InterfaceMethod entries
getInputQubits and getOutputQubits (and the singular getOutputQubit) to state
explicitly that the ranges are ordered the same way as the per-element accessors
(e.g., getInputQubit(i) / getOutputQubit(i)), so callers can rely on consistent
index-based correspondence between the singular getters and the returned
OperandRange/ResultRange.
Thanks for looking into this - I feel slightly stupid for not seeing this simple solution...
I will rebase the branch and add it to the changelog 👍 |
|
@taminob didn't you want to add it to the changelog too? ;-) |
Description
Introduce
getInputQubits()andgetOutputQubits()which is especially required for a smooth interaction withCtrlOpthroughUnitaryOpInterface.Checklist: