Commit 12c9308
[python] Bind cudaq.measure_handle, rewire AST bridge, add tests
Python-side counterpart to PR 3b's C++ frontend rewire, per spec
cudaq-spec/proposals/measure_handle.bs (Python API; Operational
Semantics; IR Representation).
Bindings and host stubs
- python/runtime/mlir/py_register_dialects.cpp: bind
`!cc.measure_handle` so the AST bridge can construct, check, and
emit the type from Python.
- python/cudaq/kernel_types.py: `cudaq.measure_handle` kernel-type
stub (mirrors `qubit` / `qvector` / `qview`); host-scope
construction raises a device-only RuntimeError.
- python/cudaq/__init__.py: re-export `cudaq.measure_handle` and add
a host stub for `cudaq.to_bools(handles)` raising the same
device-only error -- kernel-side calls are intercepted by the AST
bridge.
- python/cudaq/kernel/utils.py: `containsMeasureHandle(ty)`,
mirroring `cudaq::cc::containsMeasureHandle` from PR 3b
(`lib/Optimizer/Dialect/CC/CCTypes.cpp`); used by the bridge's
boundary check.
AST bridge (`PyASTBridge` in `python/cudaq/kernel/ast_bridge.py`)
- mz / mx / my emit `quake.{mz,mx,my}` producing
`!cc.measure_handle` (scalar) or `!cc.stdvec<!cc.measure_handle>`
(vector). Discriminate insertion is deferred to coercion sites.
- `__discriminateIfMeasureHandle` inserts `quake.discriminate` at
every spec-listed coercion site: arithmetic-to-bool (if / while /
not / and / or), `changeOperandToType` (i1 + i8 + stdvec<i1>),
Compare (==/!=), explicit `bool(...)`, IfExp test, Assert test;
surfaces the `discriminating an unbound measure_handle`
diagnostic for the default-constructed pattern.
- `cudaq.measure_handle()` -> `cc.UndefOp(!cc.measure_handle)` so
the unbound-handle diagnostic has a recognizable source.
- `cudaq.to_bools(handles)` -> vectorized `quake.discriminate` on
`!cc.stdvec<!cc.measure_handle>`; legacy
`cudaq.to_integer(mz(qvec))` is rejected with a targeted
diagnostic matching PR 3b's C++ rejection.
- Boundary check at entry-point creation: walk the kernel signature
with `containsMeasureHandle` and reject any parameter / return
position transitively containing a `measure_handle`. Diagnostic
matches PR 3b's spec-canonical wording.
Tests
- python/tests/kernel/test_measure_handle.py: new spec-coverage
suite (host-scope rejections, scalar/vector emission shape, every
bool-coercion site, `to_bools` lowering, `to_integer(to_bools(...))`
composition, unbound-handle diagnostic, boundary diagnostic).
- python/tests/mlir/{bug_1775,bug_1777,bug_1875,call_qpu}.py:
type-rename and structural CHECK refresh for the new bridge IR
shape.
- python/tests/kernel/test_{assignments,kernel_features,run_kernel,
to_integer,kernel_shift_operators}.py: pytest migrations off
the implicit `mz`-returns-bool assumption.
Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Pradnya Khalate <pkhalate@nvidia.com>1 parent e11d97d commit 12c9308
15 files changed
Lines changed: 725 additions & 76 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| |||
319 | 319 | | |
320 | 320 | | |
321 | 321 | | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
322 | 333 | | |
323 | 334 | | |
324 | 335 | | |
| |||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
44 | 61 | | |
45 | 62 | | |
46 | 63 | | |
| |||
383 | 400 | | |
384 | 401 | | |
385 | 402 | | |
| 403 | + | |
| 404 | + | |
386 | 405 | | |
387 | 406 | | |
388 | 407 | | |
| |||
675 | 694 | | |
676 | 695 | | |
677 | 696 | | |
| 697 | + | |
| 698 | + | |
678 | 699 | | |
679 | 700 | | |
680 | 701 | | |
| |||
754 | 775 | | |
755 | 776 | | |
756 | 777 | | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
757 | 781 | | |
758 | 782 | | |
759 | 783 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
38 | 61 | | |
39 | 62 | | |
40 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
203 | 215 | | |
204 | 216 | | |
205 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1150 | 1150 | | |
1151 | 1151 | | |
1152 | 1152 | | |
1153 | | - | |
1154 | | - | |
1155 | | - | |
1156 | | - | |
1157 | | - | |
1158 | | - | |
1159 | | - | |
1160 | | - | |
1161 | | - | |
1162 | | - | |
1163 | | - | |
1164 | | - | |
1165 | | - | |
1166 | | - | |
1167 | | - | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
1168 | 1168 | | |
1169 | | - | |
1170 | | - | |
1171 | | - | |
| 1169 | + | |
1172 | 1170 | | |
1173 | 1171 | | |
1174 | 1172 | | |
| |||
1391 | 1389 | | |
1392 | 1390 | | |
1393 | 1391 | | |
1394 | | - | |
| 1392 | + | |
| 1393 | + | |
1395 | 1394 | | |
1396 | 1395 | | |
1397 | 1396 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2727 | 2727 | | |
2728 | 2728 | | |
2729 | 2729 | | |
2730 | | - | |
| 2730 | + | |
2731 | 2731 | | |
2732 | 2732 | | |
2733 | 2733 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
| 134 | + | |
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
152 | | - | |
| 152 | + | |
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
| |||
0 commit comments