Skip to content

[docs] Correct Python docstrings and snippets that contradict the code - #5182

Open
udsy19 wants to merge 3 commits into
NVIDIA:mainfrom
udsy19:docs/api-reference-corrections
Open

[docs] Correct Python docstrings and snippets that contradict the code#5182
udsy19 wants to merge 3 commits into
NVIDIA:mainfrom
udsy19:docs/api-reference-corrections

Conversation

@udsy19

@udsy19 udsy19 commented Aug 18, 2026

Copy link
Copy Markdown

Fixes #5181.

Nine places in the Python documentation state something the code does not do.
Each was verified against an installed cudaq 0.15.1 package before and after
the change. In every case the code is correct and the documentation is wrong,
so this PR changes only comments, docstrings and whitespace — no executable
line changes
.

One of them fails quietly, which is why this is worth a PR rather than a nit:

The SampleResult iteration example in the specification prints wrong data
silently.
algorithmic_primitives.rst uses for bits, count in counts:.
SampleResult iterates over bit-string keys, like a dict, so for the
two-qubit bell kernel the example demonstrates:

Observed: 0, 0
Observed: 1, 1

Python unpacked '00' into two characters; the real counts never appear, and
the output looks plausible next to the C++ tab's Observed: 00, 514. With
counts.items():

Observed: 00, 479
Observed: 11, 521

At any other qubit count the documented form raises
ValueError: too many values to unpack (expected 2).

The rest

  • Two .. code-block:: python blocks in algorithmic_primitives.rst are not
    Python: one uses the C++ scope operator (cudaq::observe), the other splits
    an expression across two lines without parentheses. Both are SyntaxError.
    After this change, ast.parse succeeds on all nine Python code blocks in
    that file; before it, these two failed.
  • Kernel.mz / mx / my document register_name; the parameter is
    regName. The documented name raises TypeError.
  • cudaq.operators.custom.define documents op_id (it is id) and
    instantiate documents operator_id (it is op_id). Both raise TypeError.
  • cudaq.translate says its result is "the circuit, without measurement
    operations". It is not: format="openqasm2" on a kernel containing mz(q)
    emits creg/measure, format="qir" emits __quantum__qis__mz calls, and
    the docstring's own example output shows those calls. The sentence is copied
    from cudaq.draw, where it is correct.
  • SampleResult.__getitem__ documents a float return and returns int — as
    the nanobind-generated signature two lines above in the same __doc__
    already says, and as the sibling count() documents correctly.
  • QuakeValue.slice documents start; the parameter is startIdx.
  • The nine QuakeValue arithmetic dunders document
    Raises: RuntimeError: if the underlying QuakeValue type is not a float.
    No such error is raised: integer operands emit
    arith.muli/addi/subi/divsi, and __neg__ promotes to arith.negf.
    Integer support predates these Raises: lines, so the claim was never
    accurate; replaced with a note on the accepted operand types.
  • cudaq.get_unitary's docstring code block wraps two lines in literal
    backticks and under-indents the trailing print, so it renders with the
    backticks visible and fails with SyntaxError if copied. Fixed; the block now
    extracts and runs, printing the Bell unitary.

One thing that is not a doc fix

Commit d7fabc860 adds cx and qvector to
.github/pre-commit/spelling_allowlist.txt. The literal backticks in the
get_unitary example were load-bearing: the python matrix in
spellcheck_config.yml skips docstring content between inline backticks, so
removing them exposes those two words. Both are CUDA-Q vocabulary and qvec was
already in the list. That is the only hunk outside docstrings and .rst.

It shares a commit with the unitary.py docstring fix rather than standing
alone, so dropping the commit would also drop one of the nine corrections. If
you would rather solve the spelling side another way, say so and I will split
the commit and drop only the allowlist hunk.

Overlaps I checked

Verification

cudaq 0.15.1 (aca5853a7), Python 3.13, macOS 26.5.1 arm64. Every claim above was
executed, both before and after.

pre-commit run --all-files --hook-stage pre-push was run on upstream/main
and on this branch. Both give the same result: 13 hooks pass, and
Check links in Markdown files fails on a pre-existing dead CI-badge URL in
README.md, a file this branch does not touch. pyspelling passes.

The Sphinx build was not run locally — conf.py needs autodoc against a full
source build. As a substitute I parsed both edited .rst files with docutils
before and after (identical diagnostics, nothing new), ast.parsed every
.. code-block:: python in the edited files, and extracted and executed the
two blocks that were re-indented.


Scope note: an earlier draft of this branch also re-indented the apply_noise example in python_api.rst. That change was dropped because open PR #2969 already replaces that block with a literalinclude. This PR deliberately leaves it alone.

udsy19 added 3 commits August 18, 2026 11:43
All three are transliteration slips from the adjacent C++ tab.

1. ``for bits, count in counts:`` (the "fine-grained access to the bits and
   counts" example under ``cudaq::sample``). ``SampleResult`` iterates over
   bit-string keys, like a ``dict``, so this unpacks each bit-string into
   characters instead of yielding pairs. For the two-qubit ``bell`` kernel the
   example demonstrates, it silently prints ``Observed: 0, 0`` and
   ``Observed: 1, 1`` rather than the actual counts; at any other width it
   raises ``ValueError: too many values to unpack (expected 2)``. Use
   ``counts.items()``, which is the form already used elsewhere in the docs.

2. ``result = cudaq::observe(...)`` inside a ``.. code-block:: python`` — the
   C++ scope-resolution operator. ``SyntaxError`` as written.

3. The Hamiltonian assignment is split across two lines with no parentheses or
   continuation, which C++ allows and Python does not. ``SyntaxError`` as
   written. Wrapping the right-hand side in parentheses matches how the same
   Hamiltonian is written in ``examples.rst``.

After this change every ``.. code-block:: python`` in the file parses with
``ast.parse``; before it, these two did not.

Signed-off-by: Udaya Tejas <udayatejas2004@gmail.com>
Each of these is rendered into the published Python API reference through
``docs/sphinx/api/languages/python_api.rst``. All were checked against the
installed package.

* ``Kernel.mz`` / ``mx`` / ``my`` document the optional register argument as
  ``register_name``; the parameter is ``regName``. Passing the documented name
  raises ``TypeError: PyKernel.mz() got an unexpected keyword argument
  'register_name'``.

* ``cudaq.operators.custom.define`` documents its first argument as ``op_id``;
  it is ``id``. ``cudaq.operators.custom.instantiate`` documents its first
  argument as ``operator_id``; it is ``op_id``. Both documented names raise
  ``TypeError``.

* ``cudaq.translate`` describes its return value as a drawing of the execution
  path "without measurement operations". It returns the kernel translated to
  the requested format, measurements included: ``format="openqasm2"`` on a
  kernel containing ``mz(q)`` emits ``creg``/``measure`` lines, and the
  docstring's own QIR example output shows ``__quantum__qis__mz`` calls. The
  wording matches ``cudaq.draw``, where it is correct.

* ``SampleResult.__getitem__`` documents a ``float`` return. It returns
  ``int`` -- as the nanobind-generated signature two lines above in the same
  ``__doc__`` already states, and as the sibling ``count()`` correctly
  documents.

* ``QuakeValue.slice`` documents its first argument as ``start``; it is
  ``startIdx``.

* The nine ``QuakeValue`` arithmetic dunders document
  ``Raises: RuntimeError: if the underlying QuakeValue type is not a float``.
  No such error is raised. ``__checkTypesAndCreateQuakeValue`` has emitted
  integer arithmetic since before these docstrings were added, and ``__neg__``
  promotes an integer operand to floating point rather than failing. Replaced
  with a note describing the accepted operand types.

Comments and docstrings only; no functional change.

Signed-off-by: Udaya Tejas <udayatejas2004@gmail.com>
The ``.. code-block:: python`` in ``cudaq.get_unitary`` wraps two of its four
kernel lines in single backticks and under-indents the trailing ``print(U)``.
The backticks render literally and make the block invalid Python: transcribed
verbatim it fails with ``SyntaxError`` on the first backticked line. With them
removed and ``print(U)`` aligned, the example runs and prints the Bell
unitary.

The backticks were load-bearing for the spell checker: the ``python`` matrix in
``.github/pre-commit/spellcheck_config.yml`` skips docstring content between
inline backticks, and ``cx`` and ``qvector`` are not in the allowlist. Both are
CUDA-Q vocabulary (``qvec`` is already listed), so they are added there
instead.

Signed-off-by: Udaya Tejas <udayatejas2004@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 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 documentation Improvements or additions to documentation python-lang Anything related to the Python CUDA Quantum language implementation python bridge Involves the python bridge to quake kernel builder Relating to the dynamic kernel builder in C++ or Python labels Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python API docs and docstrings contradict the code in several places

1 participant