Commit 0def538
authored
🐛 Refactor unitary matrix calculations in ROp, UOp, XXMinusYYOp, and XXPlusYYOp (#1807)
## Description
- Fix `getUnitaryMatrix()` for `ROp`, `UOp`, `XXMinusYYOp`, and
`XXPlusYYOp` when rotation angles produce negative `sin(θ/2)` or
`cos(θ/2)` scalars.
- Replace `std::polar(magnitude, phase)` with `magnitude *
std::exp(i·phase)`, which is well-defined for any real magnitude.
## Problem
`std::polar(ρ, θ)` requires a **non-negative** `ρ` ([LWG
2459](https://cplusplus.github.io/LWG/issue2459)). For negative rotation
angles, gates like `R(θ, φ)` use `sin(θ/2)` as a matrix entry scale,
which can be negative (e.g. `θ = -π/2` → `sin(θ/2) < 0`).
On libc++, `std::polar(negative, φ)` returns `NaN`, breaking unitary
extraction for otherwise valid gates.
## Fix
Build phased entries as ordinary complex multiplication:
```cpp
const auto m01 = std::sin(halfTheta) * std::exp(1i * phase);
```
instead of:
```cpp
std::polar(s, phase); // invalid when s < 0
```
std::polar(1.0, φ) remains appropriate for unit-magnitude global phases.
## Checklist
<!---
This checklist serves as a reminder of a couple of things that ensure
your pull request will be merged swiftly.
-->
- [x] The pull request only contains commits that are focused and
relevant to this change.
- [ ] I have added appropriate tests that cover the new/changed
functionality.
- [ ] 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.
- [ ] 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.
**If PR contains AI-assisted content:**
- [ ] I have disclosed the use of AI tools in the PR description as per
our [AI Usage
Guidelines](https://github.com/munich-quantum-toolkit/core/blob/main/docs/ai_usage.md).
- [ ] AI-assisted commits include an `Assisted-by: [Model Name] via
[Tool Name]` footer.
- [ ] I confirm that I have personally reviewed and understood all
AI-generated content, and accept full responsibility for it.1 parent 4065349 commit 0def538
5 files changed
Lines changed: 25 additions & 17 deletions
File tree
- mlir/lib/Dialect/QCO/IR/Operations/StandardGates
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| |||
598 | 598 | | |
599 | 599 | | |
600 | 600 | | |
| 601 | + | |
601 | 602 | | |
602 | 603 | | |
603 | 604 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
124 | 127 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
142 | 144 | | |
143 | 145 | | |
144 | 146 | | |
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
85 | | - | |
| 85 | + | |
| 86 | + | |
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
| |||
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
85 | | - | |
| 85 | + | |
| 86 | + | |
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
| |||
0 commit comments