-
Notifications
You must be signed in to change notification settings - Fork 42
✨ Add MLIR pass for merging rotation gates #1019
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?
Conversation
So far, this PR adds a pass that can merge the following gates:
I'm not sure about how to merge the remaining gates mentioned in #898. I have the feeling that they are fundamentally different and cannot be merged as easily because the respective gates cannot be decomposed into something that commutes. I also still have to figure out why the tests are failing on Windows. I already texted you privately, but if you have any input, I would be very interested, @burgholzer, @DRovara, and @ystade. :) |
I did some more thinking on this topic and I might have an idea: Rotations by Euler Angles are known to be difficult to compose from multiple rotations. That's why quaternions (https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation) are often used by game engines to represent rotations. I think we might be able to do the same thing here: first translate the three Euler angles to quaternions, then we combine the multiple rotations into one single rotation and then we translate back into Euler angles. If you want we can also chat about this in person tomorrow, @denialhaag. |
Have a look at the current Qiskit code in that regard. I remember that they are doing something very similar in the code since recently. Might serve as some good inspiration. The rules for The two qubit gates are a bit more special. In general, merging might not be particularly easy here. But cancellation is fairly easy to decide as also indicated by the code linked in the issue description. Maybe those gates should be included as special rules there. Just my two cents here though. |
@DRovara, yes, an in-person chat would be nice! I'll be in the office in a bit! :) |
@burgholzer, thanks for the ideas! I'll have a look at the Qiskit implementation! |
For reference, the Qiskit pass is implemented here. |
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.
Thanks a lot for the effort! I made some comments with suggestions, feel free to look into them.
One more thing though: As far as I see, this will only work if the parameters are passed as operands and won't work if the parameters are static attributes. This is okay for now, but we should probably keep this in mind and at the very least open a follow-up issue for that.
mlir/lib/Dialect/MQTOpt/Transforms/MergeRotationGatesPattern.cpp
Outdated
Show resolved
Hide resolved
Thank you for the review! Regarding your second point, I think I am still missing some context (and knowledge) to understand. 😬 |
My commits from today add support for canceling all gates mentioned in the issue. 👍
|
I'm fine with making that a separate step. In that case, let's not forget to create the corresponding issue if we don't plan to tackle that situation immediately. |
Yeah. In the interest of closing out issues and merging features fast, I'd also argue that we should create a follow-up issue for that, which summarizes the discussions we had in this PR, and move this PR along so that it can be merged rather soon. |
Just for context, the idea was
However, since this PR works with dynamic operands, we can't really know if Once we have that, |
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 gave the PR another look and I just have some small comments left. I hope they should be addressable without too much effort.
Especially regarding the test cases, I think it is good to set up a good standard early.
Thanks a lot!
if (type == "gphase" || type == "rx" || type == "ry" || type == "rz" || | ||
type == "rxx" || type == "ryy" || type == "rzz" || type == "rzx") { |
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.
Can we use the MERGEABLE_GATES
constant for this check instead?
// ----- | ||
// This test checks that consecutive rx gates are merged correctly. | ||
|
||
module { |
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 think it would be best if we still did some smaller clean-up with the CHECK strings here.
While writing the MQTOpt dialect features
tests, we decided for the standard of collecting all CHECK
strings at the start of the test function body. Maybe we should do that here, too, rather than interleaving the checks.
Also, we had some discussions on what parts of the code actually warrant CHECK
strings and decided that only those functions relevant to the tested feature do. That means that something like the register allocations/deallocations or qubit extractions/insertions don't really require CHECK strings.
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.
Done. 👍
} | ||
|
||
// ----- | ||
// This test checks that incompatible multi-qubit gates are not merged. |
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 believe there might still be some scenarios that are worthy of test cases. Here are some ideas:
rx(a) q1; rx(b) q2
should not be merged as they are different qubits- similarly, two
rxx
gates that only share one qubit should not be merged - maybe we can even test something like
rzz q1, q2; rzz q2, q1
which should not be merged based on the code (but could in theory also be merged, just so that we have a documentation of that behaviour in the tests) - it would also be interesting to test how merging works if the angle parameters are not constants, but e.g. arguments to the test function. In that situation, there would probably be an actual "add" operation to add up the angles rather than just the single, already added up constant that is generated by MLIR's internal optimizations.
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.
Done. Let me know what you think! :)
Description
This PR adds a pass for merging simple rotation gates.
Fixes #898
Checklist:
I have updated the documentation to reflect these changes.I have added migration instructions to the upgrade guide (if needed).