Commit 32a24b7
Fix
The `cphase` OpenQASM compatibility gate fails with type errors because
its Q# signature has the control qubit before the angle parameter, while
the compiler always emits classical args before qubit args (as with
every other gate).
```
type error: expected Qubit, found Angle
type error: expected Angle, found Qubit
```
- Reordered `cphase` parameters from `(ctrl : Qubit, lambda : Angle,
qubit : Qubit)` to `(lambda : Angle, ctrl : Qubit, qubit : Qubit)` in
`Std.OpenQASM.Intrinsic`, consistent with `cp`, `crx`, `cry`, `crz`, and
`cu`
- Body unchanged — it references params by name
- Added comprehensive test `all_stdgates_inc_gates_can_be_called` that
calls every gate from OpenQASM's `stdgates.inc` in a single test (25
main gates + 7 backwards compatibility gates: `CX`, `phase`, `cphase`,
`id`, `u1`, `u2`, `u3`)
- Added test `all_stdgates_inc_gates_adjoint_can_be_called` that calls
the adjoint (`inv @`) of every stdgates.inc gate
- Added test `all_stdgates_inc_gates_controlled_can_be_called` that
calls the singly controlled (`ctrl @`) version of every stdgates.inc
gate
- Added Q# compilation verification to all QASM-to-Q# test helpers
(`compile_qasm_to_qsharp`, `compile_qasm_stmt_to_qsharp_with_semantics`,
`compile_qasm_to_qsharp_file`, `compile_qasm_to_qsharp_operation`,
`qsharp_from_qasm_compilation`) so that generated Q# is verified through
the Q# compiler via `compile_ast`. For File mode packages the AST is
verified directly; for Fragments/Operation mode the QASM source is
re-compiled as File mode to produce a complete Q# structure before
verification.
- Fixed `__AngleShl__` → `AngleShl` typo in compiler binary operator
name mapping (the Q# library function is `AngleShl`, matching
`AngleShr`, `AngleAndB`, etc.)
- Fixed `negctrl` functor constraints: `ApplyControlledOnInt` requires
`Adj + Ctl` (it uses a `within/apply` pattern internally), but the
constraint solver only marked `Ctl` for `negctrl`. Split `Ctrl` and
`NegCtrl` handling in `functor_constraints.rs` so that `NegCtrl` sets
both `requires_adj` and `requires_ctl`.
- Fixed `bit` ordered comparison operators (`>`, `>=`, `<`, `<=`): Q#
`Result` type only supports `==` and `!=`. The compiler now wraps both
operands in `ResultAsInt()` calls for ordered comparisons, matching how
`bit[]` comparisons already use `ResultArrayAsIntBE`.
- Fixed `input` variable mutation: OpenQASM `input` variables are
mutable, but Q# operation parameters are immutable. Added a
`collect_assigned_input_symbols` pre-analysis pass that identifies which
input params are reassigned in the program, then generates `mutable a =
a;` shadow copies at the start of the operation body for only those
parameters.
- Fixed `return_from_switch` test: the original QASM input was invalid
(a function returning `bit` with a switch that didn't cover all cases).
Added a `default { return 0; }` branch to make the test input valid.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: minestarks <16928427+minestarks@users.noreply.github.com>
Co-authored-by: idavis <63540+idavis@users.noreply.github.com>cphase gate argument order and fix 4 QASM-to-Q# compiler bugs (#3158)1 parent 21ea7aa commit 32a24b7
9 files changed
Lines changed: 608 additions & 23 deletions
File tree
- library/std/src/Std/OpenQASM
- source/compiler/qsc_openqasm_compiler/src
- tests
- declaration
- expression/binary
- statement
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
225 | | - | |
| 225 | + | |
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
108 | 109 | | |
109 | 110 | | |
110 | 111 | | |
| 112 | + | |
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
| |||
182 | 184 | | |
183 | 185 | | |
184 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
185 | 237 | | |
186 | 238 | | |
187 | 239 | | |
| |||
194 | 246 | | |
195 | 247 | | |
196 | 248 | | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
197 | 253 | | |
198 | 254 | | |
199 | 255 | | |
| |||
385 | 441 | | |
386 | 442 | | |
387 | 443 | | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
388 | 460 | | |
389 | 461 | | |
390 | 462 | | |
| |||
1770 | 1842 | | |
1771 | 1843 | | |
1772 | 1844 | | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
| 1849 | + | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
| 1859 | + | |
1773 | 1860 | | |
1774 | 1861 | | |
1775 | 1862 | | |
| |||
1791 | 1878 | | |
1792 | 1879 | | |
1793 | 1880 | | |
1794 | | - | |
| 1881 | + | |
1795 | 1882 | | |
1796 | 1883 | | |
1797 | 1884 | | |
| |||
Lines changed: 14 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
| |||
231 | 237 | | |
232 | 238 | | |
233 | 239 | | |
234 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
235 | 247 | | |
236 | 248 | | |
237 | 249 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
| |||
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| 180 | + | |
179 | 181 | | |
180 | 182 | | |
181 | 183 | | |
| |||
285 | 287 | | |
286 | 288 | | |
287 | 289 | | |
| 290 | + | |
288 | 291 | | |
289 | 292 | | |
290 | 293 | | |
| |||
293 | 296 | | |
294 | 297 | | |
295 | 298 | | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
| 299 | + | |
302 | 300 | | |
303 | 301 | | |
304 | 302 | | |
305 | 303 | | |
306 | 304 | | |
| 305 | + | |
307 | 306 | | |
308 | 307 | | |
309 | 308 | | |
310 | 309 | | |
311 | 310 | | |
312 | 311 | | |
313 | 312 | | |
314 | | - | |
| 313 | + | |
315 | 314 | | |
316 | 315 | | |
317 | 316 | | |
318 | 317 | | |
319 | 318 | | |
| 319 | + | |
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| |||
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| 363 | + | |
363 | 364 | | |
364 | 365 | | |
365 | 366 | | |
366 | 367 | | |
367 | 368 | | |
368 | 369 | | |
369 | 370 | | |
370 | | - | |
371 | | - | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
372 | 379 | | |
373 | 380 | | |
374 | 381 | | |
| |||
377 | 384 | | |
378 | 385 | | |
379 | 386 | | |
| 387 | + | |
380 | 388 | | |
381 | 389 | | |
382 | 390 | | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
383 | 435 | | |
384 | 436 | | |
385 | 437 | | |
| |||
390 | 442 | | |
391 | 443 | | |
392 | 444 | | |
| 445 | + | |
393 | 446 | | |
394 | 447 | | |
395 | 448 | | |
396 | 449 | | |
397 | 450 | | |
398 | 451 | | |
399 | 452 | | |
400 | | - | |
| 453 | + | |
401 | 454 | | |
402 | 455 | | |
403 | 456 | | |
404 | 457 | | |
| 458 | + | |
405 | 459 | | |
406 | 460 | | |
407 | 461 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
187 | 187 | | |
188 | 188 | | |
189 | 189 | | |
190 | | - | |
| 190 | + | |
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
393 | 393 | | |
394 | 394 | | |
395 | 395 | | |
| 396 | + | |
396 | 397 | | |
397 | 398 | | |
398 | 399 | | |
| |||
406 | 407 | | |
407 | 408 | | |
408 | 409 | | |
| 410 | + | |
| 411 | + | |
409 | 412 | | |
410 | 413 | | |
411 | 414 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
0 commit comments