Skip to content

[Logical] Reject cudaq.logical.reset in a logical program with a clear error - #5978

Open
burrows99 wants to merge 1 commit into
NVIDIA:mainfrom
burrows99:fix/logical-reset-in-program
Open

burrows99 wants to merge 1 commit into
NVIDIA:mainfrom
burrows99:fix/logical-reset-in-program

Conversation

@burrows99

Copy link
Copy Markdown
Contributor

Fixes #5908

Problem

cudaq.logical.reset inside a function decorated with cudaq.logical.program gives no diagnostic about an unsupported operation. Tracing the program fails deep in the builder:

cudaq.mlir._mlir_libs._site_initialize.<locals>.MLIRError: Unable to parse attribute:
error: "#qlx.action<reset>":1:13: expected qlx::BuiltinAction to be one of: h, s, sdg, x, y, z, t, tdg, cx, cz, ccz, idle, pauli_rotation, ccx

ops/_impl.py's reset calls trace.reset(...) when the trace has one and otherwise falls back to trace.apply_standard("reset", ...). The P0 program builder (UnplacedBuilder) has no reset, and QLX has no reset builtin action, so the fallback builds an attribute that cannot be parsed. The fallback is still needed for gadget bodies, where apply_standard("reset") emits fabric.reset.

Fix

When the active trace is the P0 program builder, cudaq.logical.reset raises:

TypeError: cudaq.logical.reset is not supported in a logical program; it is available in a gadget body or a P3 physical trace

This follows the existing barrier / release checks and the isinstance(trace, UnplacedBuilder) branch in measure. P3 physical traces (trace.reset) and gadget bodies (fabric.reset) take the same paths as before.

flowchart TD
    A[cql.reset in a logical program] --> B{Trace the program}
    B -->|Before| C[MLIRError cannot parse qlx action reset]
    B -->|After| D[TypeError naming where reset is available]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

Reproducer

cudaq-logical 0.2.0 from pip (aarch64 Linux, python:3.12 container), with the installed ops/_impl.py replaced by main's (16e37c7) for the before run and by this branch's for the after run.

import cudaq.logical as cql

@cql.program
def prog() -> tuple[bool]:
    q = cql.allocate(1, state=cql.types.zero)
    return (cql.measure_z(cql.reset(cql.h(q[0]))),)

print(cql.compile(prog))

On main: the MLIRError above. With this change: the TypeError above.

Edge cases and unchanged behaviour checked

In the running library (shown in the video):

  • A reset on the second of two qubits, measured with measure_x, is rejected with the same TypeError.
  • Unchanged: a program without reset (measure_z(h(q[0]))) compiles to the same P0 module.

By tests only:

  • Gadget-body ql.reset (fabric.reset): test_verify_instruments.py 40 passed and test_gadget_verification.py 19 passed, the same on main and this branch.
  • The P3 physical cql.reset path (test_run_physical.py) does not collect against the 0.2.0 wheel (cudaq.num_available_gpus is missing from the installed cudaq); CI is the check for it.

Tests

$ python -m pytest -q test_cudaq_logical.py -k reset_in_a_logical_program   # main
1 failed
$ python -m pytest -q test_cudaq_logical.py -k reset_in_a_logical_program   # this branch
1 passed
$ python -m pytest -q test_cudaq_logical.py   # main: 13 failed, 19 passed; this branch: 12 failed, 20 passed

The 12 other failures are the same tests on main and on this branch (Quake import and build-provenance tests that need native parts newer than the 0.2.0 wheel). yapf --style google --diff (v0.40.2, the pre-commit version) reports no changes. I did not build CUDA-Q from source or run the ctest/lit suites.

AI tools (Claude Code) assisted with this change; the bug was reproduced on main and the fix verified as shown.

…r error

QLX has no reset action, so cudaq.logical.reset in a function decorated
with cudaq.logical.program fell back to apply_standard("reset") and
failed with an MLIRError while parsing #qlx.action<reset>. Raise a
TypeError that names the operation and where it is available (a gadget
body or a P3 physical trace) instead. Gadget and physical traces keep
their existing reset paths.

Signed-off-by: Raunak Burrows <raunakburrows@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda-q logical Related to CUDA-Q Logical. label Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda-q logical Related to CUDA-Q Logical.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cudaq.logical.reset inside a cudaq.logical.program fails with an MLIR attribute parse error

1 participant