Skip to content

✨ Support classical subroutines and array references - #2816

Open
simon1hofmann wants to merge 4 commits into
mainfrom
codex/openqasm-scalar-subroutines
Open

simon1hofmann wants to merge 4 commits into
mainfrom
codex/openqasm-scalar-subroutines

Conversation

@simon1hofmann

@simon1hofmann simon1hofmann commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

Description

Fixes #2593. Based on main; array-valued outputs (#2592) remain separate.

  • Add nonrecursive classical def functions with by-value scalar parameters, local variables, and an optional scalar result. Mutable bit vectors use independent local storage.
  • Borrow readonly/mutable arrays with fixed shapes or fixed-rank runtime dimensions (#dim), including strided and rank-reduced views. Passing arguments does not copy their elements; writes reach caller storage.
  • Check types, widths, ranks, shapes, initialization, and mutable aliasing. All selected elements must be initialized on entry; unresolved mutable overlap is rejected. References cannot escape.
  • Preserve supported calls through QC/QCO and OpenQASM export. QIR uses existing inlining; jeff inlines borrowed helpers and retains scalar helpers. CBit inlining and constant folding before dead-value cleanup preserve backend semantics and quantum linearity.

Only final top-level returns are supported. Early/nested returns, recursion, externs, runtime scalar angle signatures, quantum statements/parameters, local arrays, and mutable-global capture are diagnosed. Angle array elements are supported. Empty views require statically known retained extents. Existing runtime bounds/shape preconditions and backend storage limits remain; no new host-result ABI or dependencies.

Review also fixed repeated evaluation in values[next(counter)] += value, strengthened the backend stride test to inspect caller storage, and removed redundant test checks and plan text.

Validation

501 focused tests passed: 214 OpenQASM target, 239 translation, 11 compiler pipeline, 28 jeff round-trip, and 9 DD/memref/call tests. These cover borrowing, initialization/alias rejection, scalar-copy isolation, empty/strided views, single-evaluation compound indices, QIR JIT, and serialized jeff execution. The compound-index regression failed before the fix and passes after it.

Whole-file C++ lint passed for all 15 changed sources against origin/main; repository lint passed. Additional frontend-header checks found only the existing redundant indices{} initializer in Frontend.h, unchanged from main. No full suite, benchmarks, or automation were run. CI is not being monitored.

Codex assisted with implementation, tests, review, and this description. Human review is still required.

Checklist

  • The pull request only contains commits that are focused and relevant to this change.
  • I have added appropriate tests that cover the new/changed functionality.
  • I have updated the documentation to reflect these changes.
  • The changes follow the project's style guidelines and introduce no new warnings.
  • The changes are fully tested and pass the CI checks.
  • I have reviewed my own code changes.

If PR contains AI-assisted content:

  • Any agent that created, edited, or submitted GitHub content was explicitly authorized for that scope, as required by our AI Usage Guidelines.
  • Every agent-authored or agent-edited public text body begins with the visible disclosure 🤖 *AI text below* 🤖 (titles are exempt).
  • I have disclosed AI assistance in the PR description.
  • I confirm that I have personally reviewed and understood all AI-generated content, and accept full responsibility for it.

@simon1hofmann simon1hofmann self-assigned this Oct 23, 2026
@simon1hofmann simon1hofmann added this to the v4.2.0 - SC26 milestone Oct 23, 2026
@simon1hofmann simon1hofmann added feature New feature or request MLIR Anything related to MLIR OpenQASM Anything related to OpenQASM c++ Anything related to C++ code labels Oct 23, 2026
@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

@simon1hofmann simon1hofmann changed the title ✨ Support classical scalar subroutines ✨ Support classical subroutines and array references Oct 23, 2026
Base automatically changed from codex/array-pipeline-cleanup to main October 23, 2026 20:54
@mergify mergify Bot added the conflict label Oct 23, 2026
🤖 *AI text below* 🤖

Add by-value scalar calls and final returns through the OpenQASM frontend,
QC/QCO, and OpenQASM export using ordinary functions. Preserve bit-copy
isolation and reject unsupported signatures and control flow explicitly.

Keep array references, general returns, and new backend ABIs for follow-ups.

Assisted-by: OpenAI Codex
🤖 *AI text below* 🤖

Borrow typed static or rank-only array views without argument copies. Check initialization, mutability, shape and aliasing; preserve mutations through OpenQASM round trips, QIR and jeff lowering.

Keep array outputs and the host-result ABI out of scope.

Assisted-by: OpenAI Codex
@simon1hofmann
simon1hofmann force-pushed the codex/openqasm-scalar-subroutines branch from 3bd2f75 to 05b3865 Compare October 23, 2026 20:57
@mergify mergify Bot removed the conflict label Oct 23, 2026
Reuse the typed and emitted lvalue in compound assignments so calls in
the index do not run twice. Preserve initialization and conversion checks.

Strengthen the borrowed-view backend oracle and remove redundant checks
and completed-plan repetition.

Assisted-by: OpenAI Codex
@mergify mergify Bot added the conflict label Oct 23, 2026
🤖 *AI text below* 🤖

Preserve borrowed-array inlining and one-time lvalues alongside upstream bulk initializers and wide-bit casts. Propagate declaration failures when exporting helpers.

Fold promoted call operands before canonical cleanup and dead-value removal. Keep independent specialized-unitary and explicitly seeded custom-pipeline references, and preserve narrow signed initializer values.

Validation: 1197 native tests passed with two /dev/full platform skips, 483 focused Python tests passed, and repository lint passed. Whole-file C++ lint remains blocked by unavailable clang-tidy 23.

Assisted-by: OpenAI Codex
@mergify mergify Bot removed the conflict label Oct 25, 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

c++ Anything related to C++ code feature New feature or request MLIR Anything related to MLIR OpenQASM Anything related to OpenQASM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

✨ Support classical subroutines and array-reference parameters

1 participant