Skip to content

[python] Accept int and NumPy angles in kernel builder gates - #5977

Open
burrows99 wants to merge 1 commit into
NVIDIA:mainfrom
burrows99:fix/builder-numeric-angles
Open

burrows99 wants to merge 1 commit into
NVIDIA:mainfrom
burrows99:fix/builder-numeric-angles

Conversation

@burrows99

Copy link
Copy Markdown
Contributor

Problem

In the kernel builder, a constant rotation angle works only when it is a Python float. An int or a NumPy scalar that is not a float subclass (np.float32, np.int64, ...) is passed through raw and fails while the op is built:

>>> kernel.ry(1, q[0])
ValueError: Operand 0 of operation "quake.ry" must be a Sequence of Values (is not a Value)
>>> kernel.exp_pauli(np.float32(0.5), q, "XX")
AttributeError: 'numpy.float32' object has no attribute 'mlirValue'

get_parameter_value (used by every rotation gate, their controlled forms and u3) only converts isinstance(parameter, float), and exp_pauli has its own copy of the same float-only check. givens_rotation goes through exp_pauli, so it fails with a NumPy angle too, and fermionic_swap with an int angle.

Fix

get_parameter_value now turns any numbers.Real into an f64 constant (float(parameter)), and exp_pauli uses get_parameter_value instead of its own check. QuakeValue angles take the same path as before.

flowchart TD
    A[kernel.ry with angle 1] --> B{Build the quake op}
    B -->|Before| C[ValueError operand 0 is not a Value]
    B -->|After| D[f64 constant angle and the kernel samples]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

Reproducer

cuda-quantum-cu13 0.17.0 from pip (macOS arm64, qpp-cpu), with the installed kernel/kernel_builder.py replaced by main's (16e37c7) for the before run and by this branch's for the after run.

import cudaq
kernel = cudaq.make_kernel()
q = kernel.qalloc(1)
kernel.ry(1, q[0])
print(cudaq.sample(kernel))

On main:

  File ".../cudaq/kernel/kernel_builder.py", line 63, in __generalOperation
    opCtor([], parameters, controls, [target.mlirValue], is_adj=isAdj)
  ...
ValueError: Operand 0 of operation "quake.ry" must be a Sequence of Values (is not a Value)

With this change:

{ 0:765 1:235 }

Edge cases and unchanged behaviour checked

In the running library (shown in the video):

  • kernel.exp_pauli(np.float32(0.5), q, "XX") builds and samples 00 / 11.
  • kernel.givens_rotation(np.int64(1), q[0], q[1]) after x(q[0]) builds and samples 01 / 10.
  • Unchanged: a make_kernel(float) parameter angle and a float literal angle in one kernel still build and sample.

By tests only:

  • test_non_float_angle[1 | np.int64(1) | np.float32(1.0)] (new): ry, u3, exp_pauli and givens_rotation with each angle type give the same state as with 1.0.

Tests

Run against the 0.17.0 wheel with python/cudaq/kernel/kernel_builder.py from main and then from this branch (the rest of the installed package unchanged):

$ python -m pytest -q python/tests/builder/test_kernel_builder.py -k non_float_angle   # main
3 failed, 86 deselected
$ python -m pytest -q python/tests/builder/test_kernel_builder.py -k non_float_angle   # this branch
3 passed, 86 deselected
$ python -m pytest -q python/tests/builder/test_kernel_builder.py   # main: 3 failed, 83 passed, 3 skipped
86 passed, 3 skipped

The other python/tests/builder/ files (test_NoiseModel, test_encoding, test_observe, test_optimizer, test_qalloc_init*, test_qpp_target, test_quake_value, test_resource_metrics, test_sample*, test_state, test_translate, ...) pass the same on main and on this branch. 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 C++ / lit suites; CI is the real check for those.

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

get_parameter_value turned only a Python float into a constant, so
kernel.ry(1, q), kernel.rx(np.float32(0.5), q) or kernel.u3 with NumPy
angles passed the raw number to the op builder and failed with
"Operand 0 of operation quake.ry must be a Sequence of Values".
exp_pauli had its own float-only check and raised AttributeError
('int' object has no attribute 'mlirValue'), which also broke
givens_rotation with a NumPy angle.

Treat any numbers.Real as a constant angle and let exp_pauli use the
same helper.

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 python-lang Anything related to the Python CUDA Quantum language implementation python bridge Involves the python bridge to quake testing Relates to testing kernel builder Relating to the dynamic kernel builder in C++ or Python labels 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

kernel builder Relating to the dynamic kernel builder in C++ or Python python bridge Involves the python bridge to quake python-lang Anything related to the Python CUDA Quantum language implementation testing Relates to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant