Skip to content

[Logical] Let cudaq.logical.measure trace in a logical program - #5935

Merged
sacpis merged 5 commits into
NVIDIA:mainfrom
burrows99:fix/logical-measure-in-program
Nov 23, 2026
Merged

sacpis merged 5 commits into
NVIDIA:mainfrom
burrows99:fix/logical-measure-in-program

Conversation

@burrows99

Copy link
Copy Markdown
Contributor

Fixes #5907

Problem

Inside a function decorated with cudaq.logical.program, the generic cudaq.logical.measure always fails while the program is traced:

TypeError: UnplacedBuilder.measure() got multiple values for argument 'basis'

ops/_impl.py forwards every measure call in the P3 physical form, measure(value, *, basis=, destructive=, record=). The P0 program builder's measure takes (basis, value), so the qubit lands in basis and the basis= keyword collides with it. measure_z and measure_x work because they already call the P0 form.

Fix

In cudaq.logical.measure, when the active trace is the P0 program builder, call its measure(basis, value) the way measure_z / measure_x do:

  • a destructive Z or X measurement (basis="z" by default, case-insensitive) now traces and compiles;
  • destructive=False and record= raise a TypeError saying they are available only in a P3 physical trace (the P0 measurement consumes the qubit and records nothing);
  • any other basis raises a ValueError, matching the P3 builder's 'x' or 'z' check.

P3 physical traces take the same path as before.

flowchart TD
    A[cql.measure in a logical program] --> B{Trace the program}
    B -->|Before| C[TypeError multiple values for basis]
    B -->|After| D[qlx.measure Z in the P0 module]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

Reproducer

cudaq-logical 0.2.0 from pip (aarch64 Linux, python:3.12 container); its ops/_impl.py, programs/builder.py and compiler/ sources are identical to main at 52aa88a. The after run swaps in this branch's ops/_impl.py.

import cudaq.logical as cql

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

print(cql.compile(prog))

On main:

  File ".../cudaq/logical/ops/_impl.py", line 85, in measure
    return require_trace("measure").measure(value,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: UnplacedBuilder.measure() got multiple values for argument 'basis'

With this change:

module attributes {qlx.facets = [], qlx.ir_version = "0.4-draft", qlx.model_version = "0.3.10-proposed", qlx.profiles = ["p0"], qlx.stages = ["p0"]} {
  qlx.program @prog : () -> i1 attributes {qlx.profile = "p0", qlx.stage = "p0"} {
    %0 = qlx.prepare "zero" {allocation = 0 : i64, value_index = 0 : i64} : !qlx.logical_qubit
    %1 = qlx.measure <Z> %0 : !qlx.logical_qubit -> i1
    qlx.return %1 : i1
  }
}

Edge cases and unchanged behaviour checked

In the running library (shown in the video):

  • cql.measure(q[0], basis="x") compiles to qlx.measure <X>.
  • cql.measure(q[0], destructive=False) raises TypeError: cudaq.logical.measure(destructive=False) and record= are available only in a P3 physical trace.
  • Unchanged: cql.measure_z and cql.measure_x in one program compile to qlx.measure <Z> and qlx.measure <X>.

By tests only:

  • basis="Z" / "X" (upper case), record="m0" (TypeError) and basis="y" (ValueError): new tests in test_cudaq_logical.py.
  • The P3 physical cql.measure path (destructive, non-destructive, basis=arch.Basis.X, records): test_run_physical.py, unchanged and passing.

Tests

Run in the container above, with the installed package's Python sources replaced by this branch's (only the native _bin / _quake parts come from the 0.2.0 wheel):

$ python -m pytest -q test_cudaq_logical.py -k generic_measure   # main
7 failed, 23 deselected
$ python -m pytest -q test_cudaq_logical.py -k generic_measure   # this branch
7 passed, 23 deselected
$ python -m pytest -q test_cudaq_logical.py test_run_physical.py test_pbc_api.py test_experiments.py test_linearity.py test_estimate.py test_to_pbc.py
test_cudaq_logical.py   30 passed
test_run_physical.py    43 passed, 2 skipped
test_pbc_api.py         16 passed
test_experiments.py     6 passed
test_linearity.py       14 passed
test_estimate.py        41 passed
test_to_pbc.py          1 skipped

After merging current main (513016c) into the branch, rerun the same way (main's newer Python sources over the 0.2.0 wheel's native parts):

$ python -m pytest -q test_cudaq_logical.py -k generic_measure
7 passed, 23 deselected
$ python -m pytest -q test_cudaq_logical.py   # main: 11 failed, 12 passed; this branch: 11 failed, 19 passed
$ python -m pytest -q test_pbc_api.py test_experiments.py test_linearity.py test_estimate.py
16 + 6 + 14 + 41 passed

The 11 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), and test_run_physical.py no longer collects in this environment (cudaq.num_available_gpus is missing from the installed cudaq). CI with a source build is the real check for those.

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.

Inside a function decorated with cudaq.logical.program, cql.measure(q)
raised "UnplacedBuilder.measure() got multiple values for argument
'basis'": the generic op forwarded the P3 physical signature
(value, *, basis, destructive, record) to the P0 builder, whose measure
takes (basis, value).

Route a logical-program trace to the P0 builder's measure, as measure_z
and measure_x do, for a destructive Z or X measurement. destructive=False
and record= raise a TypeError that names the P3 physical trace, and any
other basis raises a ValueError. P3 physical traces are unchanged.

Fixes NVIDIA#5907

Signed-off-by: Raunak Burrows <raunakburrows@gmail.com>
…-in-program

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

copy-pr-bot Bot commented Nov 18, 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 18, 2026

@sacpis sacpis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @burrows99. Left few nit comments.

@sacpis

sacpis commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

/ok to test 3e118b2

Command Bot: Processing...

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

CI Summary (push) — ✅ passed

Run #45232685584 · ✅ 8 · ⏩ 11 · ❌ 0 · ⛔ 0

Top-level jobs (19)
Job Result
binaries ⏩ skipped
build_and_test ✅ success
changes ✅ success
config_devdeps ✅ success
config_source_build ⏩ skipped
config_wheeldeps ✅ success
devdeps ✅ success
docker_image ⏩ skipped
documentation ✅ success
edem_wheels ⏩ skipped
gen_code_coverage ⏩ skipped
metadata ✅ success
python_devel_wheel ⏩ skipped
python_metapackages ⏩ skipped
python_wheels ⏩ skipped
python_wheels_cu12_validation ⏩ skipped
python_wheels_cu13 ⏩ skipped
source_build ⏩ skipped
wheeldeps ✅ success
⏩ Skipped jobs (11) — intentionally skipped on PR builds; run on merge_group / workflow_dispatch
Job
binaries
config_source_build
docker_image
edem_wheels
gen_code_coverage
python_devel_wheel
python_metapackages
python_wheels_cu12_validation
python_wheels_cu13
python_wheels
source_build
All sub-jobs (48) — every matrix leg, with links
Job Status Link
Build and test (amd64, gcc12, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Python) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view
CI Summary ❔ in_progress view
Check for stable CUDA-Q changes ✅ success view
Configure build (devdeps) ✅ success view
Configure build (source_build) ⏩ skipped view
Configure build (wheeldeps) ✅ success view
Create CUDA 12 Python wheels ⏩ skipped view
Create CUDA 13 Python wheels ⏩ skipped view
Create CUDA Quantum installer ⏩ skipped view
Create Docker images ⏩ skipped view
Create Python metapackages ⏩ skipped view
Create cudaq-edem wheels (${{ matrix.platform }}) ⏩ skipped view
Create shared Python development wheel ⏩ skipped view
Documentation / Build and check docs ✅ success view
Gen code coverage ⏩ skipped view
Load dependencies (amd64, gcc12) / Caching ✅ success view
Load dependencies (amd64, gcc12) / Finalize ✅ success view
Load dependencies (amd64, gcc12) / Metadata ✅ success view
Load dependencies (amd64, llvm) / Caching ✅ success view
Load dependencies (amd64, llvm) / Finalize ✅ success view
Load dependencies (amd64, llvm) / Metadata ✅ success view
Load dependencies (arm64, gcc12) / Caching ✅ success view
Load dependencies (arm64, gcc12) / Finalize ✅ success view
Load dependencies (arm64, gcc12) / Metadata ✅ success view
Load dependencies (arm64, llvm) / Caching ✅ success view
Load dependencies (arm64, llvm) / Finalize ✅ success view
Load dependencies (arm64, llvm) / Metadata ✅ success view
Load source build cache ⏩ skipped view
Load wheel dependencies (amd64, 12.6) / Caching ✅ success view
Load wheel dependencies (amd64, 12.6) / Finalize ✅ success view
Load wheel dependencies (amd64, 12.6) / Metadata ✅ success view
Load wheel dependencies (amd64, 13.0) / Caching ✅ success view
Load wheel dependencies (amd64, 13.0) / Finalize ✅ success view
Load wheel dependencies (amd64, 13.0) / Metadata ✅ success view
Load wheel dependencies (arm64, 12.6) / Caching ✅ success view
Load wheel dependencies (arm64, 12.6) / Finalize ✅ success view
Load wheel dependencies (arm64, 12.6) / Metadata ✅ success view
Load wheel dependencies (arm64, 13.0) / Caching ✅ success view
Load wheel dependencies (arm64, 13.0) / Finalize ✅ success view
Load wheel dependencies (arm64, 13.0) / Metadata ✅ success view
Prepare cache clean-up ❔ in_progress view
Retrieve PR info ✅ success view
Validate CUDA 12 Python wheels ⏩ skipped view
✅ Required checks (7/7) — declared in .github/required-checks.yml for push
Required check Status Link
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Python) ✅ success view
Documentation / Build and check docs ✅ success view

burrows99 and others added 2 commits November 23, 2026 17:34
…rors

Address review: the P3-only error now names whichever of
destructive=False and record= was passed, and the basis error reports
the value as given rather than its lower-cased form.

Signed-off-by: burrows99 <raunakburrows@gmail.com>
@sacpis

sacpis commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

/ok to test ad2a38f

Command Bot: Processing...

@sacpis
sacpis enabled auto-merge November 23, 2026 17:54
@sacpis
sacpis added this pull request to the merge queue Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

CI Summary (merge_group) — ✅ passed

Run #45249671344 · ✅ 5 · ⏩ 14 · ❌ 0 · ⛔ 0

⚡ Fast merge queue — only the llvm dev-environment build+test legs ran.
Wheels, metapackages, installers, Docker images, and the gcc12 legs were skipped.
Set the repository variable MERGE_QUEUE_FAST to false to restore the full matrix.

Top-level jobs (19)
Job Result
binaries ⏩ skipped
build_and_test ✅ success
changes ✅ success
config_devdeps ✅ success
config_source_build ⏩ skipped
config_wheeldeps ⏩ skipped
devdeps ✅ success
docker_image ⏩ skipped
documentation ⏩ skipped
edem_wheels ⏩ skipped
gen_code_coverage ⏩ skipped
metadata ✅ success
python_devel_wheel ⏩ skipped
python_metapackages ⏩ skipped
python_wheels ⏩ skipped
python_wheels_cu12_validation ⏩ skipped
python_wheels_cu13 ⏩ skipped
source_build ⏩ skipped
wheeldeps ⏩ skipped
⏩ Skipped jobs (14) — intentionally skipped on PR builds; run on merge_group / workflow_dispatch
Job
binaries
config_source_build
config_wheeldeps
docker_image
documentation
edem_wheels
gen_code_coverage
python_devel_wheel
python_metapackages
python_wheels_cu12_validation
python_wheels_cu13
python_wheels
source_build
wheeldeps
All sub-jobs (29) — every matrix leg, with links
Job Status Link
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view
CI Summary ❔ in_progress view
Check for stable CUDA-Q changes ✅ success view
Configure build (devdeps) ✅ success view
Configure build (source_build) ⏩ skipped view
Configure build (wheeldeps) ⏩ skipped view
Create CUDA 12 Python wheels ⏩ skipped view
Create CUDA 13 Python wheels ⏩ skipped view
Create CUDA Quantum installer ⏩ skipped view
Create Docker images ⏩ skipped view
Create Python metapackages ⏩ skipped view
Create cudaq-edem wheels (${{ matrix.platform }}) ⏩ skipped view
Create shared Python development wheel ⏩ skipped view
Documentation ⏩ skipped view
Gen code coverage ⏩ skipped view
Load dependencies (amd64, llvm) / Caching ✅ success view
Load dependencies (amd64, llvm) / Finalize ✅ success view
Load dependencies (amd64, llvm) / Metadata ✅ success view
Load dependencies (arm64, llvm) / Caching ✅ success view
Load dependencies (arm64, llvm) / Finalize ✅ success view
Load dependencies (arm64, llvm) / Metadata ✅ success view
Load source build cache ⏩ skipped view
Load wheel dependencies ⏩ skipped view
Prepare cache clean-up ❔ in_progress view
Retrieve PR info ✅ success view
Validate CUDA 12 Python wheels ⏩ skipped view
✅ Required checks (4/4) — declared in .github/required-checks.yml for merge_group_fast
Required check Status Link
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view

Merged via the queue into NVIDIA:main with commit abbe600 Nov 23, 2026
198 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 23, 2026
@burrows99
burrows99 deleted the fix/logical-measure-in-program branch November 24, 2026 00:15
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.measure raises TypeError for every call inside a cudaq.logical.program

2 participants