Skip to content

✨ Preserve reusable gates across OpenQASM import and export - #2338

Merged
burgholzer merged 1 commit into
mainfrom
codex/openqasm-reusable-gates
Sep 6, 2026
Merged

✨ Preserve reusable gates across OpenQASM import and export#2338
burgholzer merged 1 commit into
mainfrom
codex/openqasm-reusable-gates

Conversation

@burgholzer

@burgholzer burgholzer commented Sep 3, 2026

Copy link
Copy Markdown
Member

🤖 AI text below 🤖

Description

Preserve OpenQASM gate definitions as reusable QC functions and emit supported helpers as dependency-ordered OpenQASM gate declarations. Straight-line gates use the unitary-function contract; structured gate bodies remain private generic functions.

This PR is stacked on #2340. MLIR call-graph and SCC traversal provides callee-first export order, while the OpenQASM semantic analyzer rejects recursion before emission. Gate-expression export retains integer-to-float promotion, so expressions such as a converted loop index divided by two preserve floating-point semantics.

The exporter retains the current classical-register safeguards. Gate bodies cannot use mutable classical state; generic OpenQASM subroutines and a classical-reference call ABI are outside this change.

Codex implemented and tested the changes with independent OpenQASM/MLIR specialist review. Regressions cover a 100-function program, numeric loop expressions, strict reparsing, and QC ↔ QCO round trips.

Integrated-stack validation: release build; all 3,916 registered CTest cases with one expected skip; all 294 Qiskit translation tests; whole-file C++ lint and repository lint. Stub generation passed without tracked changes. Hosted CI is separate evidence and has not been claimed as passed.

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.
  • I have added entries to the changelog for any noteworthy additions, changes, fixes, or removals.
  • I have added migration instructions to the upgrade guide (if needed).
  • 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.

@burgholzer burgholzer added c++ Anything related to C++ code Core Anything related to the Core library and IR feature New feature or request MLIR Anything related to MLIR OpenQASM Anything related to OpenQASM labels Sep 3, 2026
@burgholzer
burgholzer force-pushed the codex/openqasm-reusable-gates branch from a09bfa7 to 73cf929 Compare September 3, 2026 03:52
@burgholzer
burgholzer changed the base branch from codex/qco-function-model to codex/qir-reusable-functions September 3, 2026 03:52
@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.20370% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../Dialect/QC/Translation/TranslateQCToOpenQASM3.cpp 90.6% 15 Missing ⚠️
...lib/Dialect/QC/Translation/OpenQASMToQCEmitter.cpp 90.6% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@burgholzer
burgholzer force-pushed the codex/qir-reusable-functions branch from 7debc7d to 763fafc Compare September 3, 2026 05:07
@burgholzer
burgholzer force-pushed the codex/openqasm-reusable-gates branch from 73cf929 to e784ed2 Compare September 3, 2026 05:07
@burgholzer
burgholzer force-pushed the codex/openqasm-reusable-gates branch from e784ed2 to 86f99a1 Compare September 3, 2026 23:12
@burgholzer
burgholzer force-pushed the codex/qir-reusable-functions branch 2 times, most recently from 6b1d409 to 01dce46 Compare September 4, 2026 21:59
@burgholzer
burgholzer force-pushed the codex/openqasm-reusable-gates branch from 86f99a1 to e16931c Compare September 4, 2026 21:59
@burgholzer
burgholzer force-pushed the codex/qir-reusable-functions branch from 01dce46 to d0936e5 Compare September 5, 2026 13:09
@burgholzer
burgholzer force-pushed the codex/openqasm-reusable-gates branch from e16931c to 6bc8251 Compare September 5, 2026 13:09
@burgholzer
burgholzer force-pushed the codex/qir-reusable-functions branch from d0936e5 to 0d4f103 Compare September 5, 2026 15:00
@burgholzer
burgholzer force-pushed the codex/openqasm-reusable-gates branch from 6bc8251 to 8f02f58 Compare September 5, 2026 15:00
@burgholzer
burgholzer force-pushed the codex/qir-reusable-functions branch from 0d4f103 to a05c17f Compare September 6, 2026 10:39
@mergify mergify Bot added the conflict label Sep 6, 2026
@burgholzer burgholzer added this to the v4.0.0 - IEEE Quantum Week milestone Sep 6, 2026
@burgholzer burgholzer self-assigned this Sep 6, 2026
Base automatically changed from codex/qir-reusable-functions to main September 6, 2026 11:12
Keep custom gate definitions as private QC functions and applications as calls across import and export.

Use source-order semantics and standard MLIR call-graph traversal to keep deep nonrecursive programs compact without a duplicate recursive validator.

Assisted-by: Codex
@burgholzer
burgholzer force-pushed the codex/openqasm-reusable-gates branch from 8f02f58 to 87ebcfa Compare September 6, 2026 11:17
@mergify mergify Bot removed the conflict label Sep 6, 2026

@burgholzer burgholzer left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This one also looks clean.
I placed a couple of restrictions on the supported functions that we may want to lift in a follow up, but I still think this is fine to go in as-is for now as it genuinely expands the scope of constructs we can support.

Functions with structured control flow are currently never marked as unitary even though we could likely allow some subset of control flow while still calling an operation unitary (for loops in particular could be easily supported).
In addition, the handling around a gate called main is maybe not fully ideal. We may be able to simplify the code a little bit by making stronger assumptions about the potential names for custom gates/functions.

@burgholzer
burgholzer enabled auto-merge (squash) September 6, 2026 11:38
@burgholzer
burgholzer merged commit 2c2c0dd into main Sep 6, 2026
26 checks passed
@burgholzer
burgholzer deleted the codex/openqasm-reusable-gates branch September 6, 2026 11:56
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 Core Anything related to the Core library and IR 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.

1 participant