Skip to content

Rebuild select_swap as composed kernels; promise the composition contract (cudaq >= 0.16) - #52

Open
wsttiger wants to merge 2 commits into
NVIDIA:mainfrom
wsttiger:features/primitives_016_refactor
Open

wsttiger wants to merge 2 commits into
NVIDIA:mainfrom
wsttiger:features/primitives_016_refactor

Conversation

@wsttiger

Copy link
Copy Markdown
Collaborator

Summary

Modernizes the primitives subpackage for the composition freedom CUDA-Q
0.16 unlocks: cudaq.control now propagates through kernels that call
other kernels, so factory-minted kernels no longer have to be flattened
to survive control-variant generation.

Two commits:

  1. Rebuild the select_swap QROM as composed kernels. The old
    construction spliced the walk, swap-network, and copy into one flat
    interpreter tape, smuggling the block registers through the walk's
    "ladder" view (the source of repeated reviewer confusion on Add unary-iteration and QROM primitives #40).
    It is now a parent kernel composing three real sub-kernels — walk,
    route, copy — with honest register names. The rebase machinery and
    the _OP_CX_LADDER_TARGET opcode are deleted end-to-end. Public
    signature and num_ladder are unchanged (the alias-sampling PR
    depends on them); the existing exhaustive QROM tests pass unmodified.

  2. Promise composition as a supported surface. A documented
    contract in the package docstring — minted kernels are building
    blocks callable from user kernels, and cudaq.control over the whole
    composition yields the controlled operation — pinned by
    tests/python/test_primitives_composition.py (plain composition on
    every supported CUDA-Q; controlled composition three-way vs the
    builtin controlled walk and an analytic reference, capability-probed
    so it skips pre-0.16). This is the consumption model future
    SELECT-style constructions (QROMChain, THC) build on.

Requires cudaq >= 0.16 for the controlled-composition path (probed and
skipped on 0.15); the plain construction and all existing behavior work
on both. Stacks naturally after the arithmetic/alias PRs (#47/#48).

Validation

Existing QROM tests pass unmodified; the new composition tests pass on
released cudaq 0.15.1 and on the 0.16.0 wheel (40/40 on the QROM +
composition files). Off-π/4 rotation lowering is unaffected.

…sition)

Workstream B of PLAN_primitives_016_refactor.md. CUDA-Q 0.16 fixed
cudaq.control propagation through kernels that call other kernels, so
the select_swap lookup no longer needs to be one flat spliced tape:

- W is now the plain unary_iteration_kernels mint over the block index,
  used directly — its target view IS the block registers. The rebase
  loop (shifting the walk's address operands onto the high bits and
  rewriting the leaf body X into ladder-to-ladder CNOTs) is deleted
  along with the _ADDRESS_OPERANDS table.
- S is a small dedicated routing kernel over (low address bits, blocks)
  applying the binary Fredkin network; its inverse is the same mint
  over the reversed swap list.
- A minted parent kernel keeps the public (address, ladder, output)
  signature and num_ladder contract, carves the documented sub-views
  with qview slices, and composes W S C S_inv W (C is the b copy CNOTs,
  inline).

Verified before restructuring: qview slicing (including on qview
parameters) and cudaq.estimate_resources counting across sub-kernel
calls both work on 0.15.1 and both 0.16 builds, so the resource tests
keep pinning toffoli_count == the compiled ccx count of the composition.

Deleted _OP_CX_LADDER_TARGET end to end (opcode, dispatch arms in all
four interpreter variants, describe() template, interpreter-test spec):
its only user was the spliced construction's copy stage. The
'flatness is load-bearing' claim in the walk module docstring now states
the honest boundary: required through CUDA-Q 0.15, an inspectability/
factory-time-body design choice from 0.16.

Support boundary: the composed lookup runs uncontrolled on 0.15.1
(kernels calling kernels was always legal; full QROM suite green), but
external cudaq.control over the composition needs 0.16 — the new test
pinning the controlled composition against the dense reference probes
the capability and skips honestly on older CUDA-Q. Full primitives set:
152 passed on both 0.16 builds (release tip and with the #5280 fix).

Signed-off-by: Scott Thornton <wsttiger@gmail.com>
Workstreams C and E of the 0.16 refactor plan: the package docstring
now states the composition contract — minted kernels are building
blocks to call from user kernels, and (CUDA-Q >= 0.16) cudaq.control
over the whole composition yields the controlled operation, with
controlled=True remaining the cheaper folded-into-the-walk option
everywhere. Pinned by tests/python/test_primitives_composition.py:
plain composition (walk then hand-written inverse inside a user kernel,
valid on every supported CUDA-Q) and controlled composition against
both the builtin controlled walk and the analytic state on a superposed
control, capability-probed so pre-0.16 runs skip with an honest reason.
The docstring truth sweep found no further stale flatness claims beyond
those already rewritten.

Signed-off-by: Scott Thornton <wsttiger@gmail.com>
@wsttiger wsttiger added the enhancement New feature or request label Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant