Skip to content

🎨 Retain register name - #2014

Open
simon1hofmann wants to merge 5 commits into
mainfrom
codex/1616-retain-quantum-register-name
Open

🎨 Retain register name#2014
simon1hofmann wants to merge 5 commits into
mainfrom
codex/1616-retain-quantum-register-name

Conversation

@simon1hofmann

@simon1hofmann simon1hofmann commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

Description

This PR retains source-level quantum-register names when translating a QuantumComputation to the QC dialect.
The register name is attached to the generated memref.alloc using the new mqt.quantum_register_name string attribute, matching the existing handling of classical-register names.

Fixes #1616.

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.

@simon1hofmann simon1hofmann self-assigned this Aug 6, 2026
@simon1hofmann simon1hofmann added enhancement Improvement of existing feature c++ Anything related to C++ code MLIR Anything related to MLIR labels Aug 6, 2026
@simon1hofmann simon1hofmann added this to the MLIR Support milestone Aug 6, 2026
@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@simon1hofmann, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 9 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 496ffa28-c702-44f5-ac94-263a2ebcd6b1

📥 Commits

Reviewing files that changed from the base of the PR and between cb20786 and 6582458.

📒 Files selected for processing (1)
  • mlir/unittests/Dialect/QC/Translation/test_openqasm3_emission.cpp
📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Quantum register allocations can retain source-level names.
    • Register names are preserved across QC/QCO conversions and generated program metadata.
    • OpenQASM output reuses declared register names.
    • Duplicate non-empty register names are rejected.
  • Bug Fixes

    • Improved translation fidelity for named quantum registers.
  • Tests

    • Added coverage for name preservation, output generation, and duplicate-name validation.
  • Documentation

    • Updated the unreleased changelog with the latest infrastructure contribution and reference.

Walkthrough

The change preserves quantum-register names across QC and QCO allocation, conversion, and translation paths. Named allocations store mqt.quantum_register_name. Duplicate names are rejected, and OpenQASM3 reuses valid stored names.

Changes

Quantum register name propagation

Layer / File(s) Summary
Named allocation contract and storage
mlir/include/mlir/Dialect/Utils/Utils.h, mlir/include/mlir/Dialect/QC/Builder/QCProgramBuilder.h, mlir/include/mlir/Dialect/QCO/Builder/QCOProgramBuilder.h, mlir/lib/Dialect/QC/Builder/QCProgramBuilder.cpp, mlir/lib/Dialect/QCO/Builder/QCOProgramBuilder.cpp
QC and QCO allocation APIs accept optional names. Named allocations store mqt.quantum_register_name. Duplicate non-empty names are rejected.
Translation and conversion propagation
mlir/lib/Conversion/*, mlir/lib/Dialect/QC/Translation/*
Translation passes source names to allocation APIs. QC/QCO conversions copy the name attribute. OpenQASM3 uses valid stored names and generates unique fallback names.
Regression validation and changelog
mlir/unittests/Conversion/*, mlir/unittests/Dialect/QC/*, mlir/unittests/Dialect/QCO/*, CHANGELOG.md
Tests verify name retention, OpenQASM emission, and duplicate-name diagnostics. The changelog records PR #2014.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SourceRegister
  participant Translation
  participant AllocationBuilder
  participant Conversion
  participant OpenQASM3
  SourceRegister->>Translation: provide register name
  Translation->>AllocationBuilder: allocate register with name
  AllocationBuilder->>AllocationBuilder: validate uniqueness
  AllocationBuilder-->>Translation: return allocation with name attribute
  Conversion->>Conversion: copy name attribute
  OpenQASM3->>Conversion: read stored register name
  Conversion-->>OpenQASM3: emit named register
Loading

Possibly related PRs

Suggested reviewers: burgholzer, denialhaag

Poem

A rabbit gives each register a name,
QC and QCO carry the same.
Through conversion, labels stay bright,
OpenQASM prints them right.
Duplicate names? Not in sight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.06% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: retaining quantum-register names.
Description check ✅ Passed The description explains the change, motivation, linked issue, testing, and checklist status; the unchecked AI disclosure item is non-critical.
Linked Issues check ✅ Passed The implementation preserves quantum-register names with the requested memref attribute and adds regression tests for the linked issue [#1616].
Out of Scope Changes check ✅ Passed The changes consistently support quantum-register name preservation across builders, conversions, translations, emission, and tests.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch codex/1616-retain-quantum-register-name

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@simon1hofmann
simon1hofmann marked this pull request as ready for review August 6, 2026 12:01

@burgholzer burgholzer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like a great start.

Just a few quick side comments:

  • does the OpenQASM importer already do this? Feels like it should also preserve the name.
  • is this attribute retained across dialect conversions? Ideally, it would propagate through the QCO conversion and lead to a nice OpenQASM roundtrip. (Maybe this is already handled though)
  • is there anything to do for QIR emission? (Both on classical and quantum registers)
  • is there anything to do for the OpenQASM emission?

@denialhaag denialhaag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a lot for getting this started, @simon1hofmann! 🙂

Below, you can find two things that jumped out to me. In addition to that, I agree with @burgholzer's comment above. I would expect some changes in the OpenQASM translations and the QIR conversions to be necessary. 🤔

Comment thread mlir/include/mlir/Dialect/QC/Builder/QCProgramBuilder.h
Comment thread mlir/lib/Dialect/QC/Builder/QCProgramBuilder.cpp
@simon1hofmann

simon1hofmann commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

This looks like a great start.

Just a few quick side comments:

  • does the OpenQASM importer already do this? Feels like it should also preserve the name.
  • is this attribute retained across dialect conversions? Ideally, it would propagate through the QCO conversion and lead to a nice OpenQASM roundtrip. (Maybe this is already handled though)
  • is there anything to do for QIR emission? (Both on classical and quantum registers)
  • is there anything to do for the OpenQASM emission?

You're right, these should be added as well.

Concerning QIR emission, __quantum__rt__qubit_allocate_array only expects a size parameter (https://www.qir-alliance.org/qir-runner/qir_backend/fn.__quantum__rt__qubit_allocate_array.html). Should we add custom metadata or just emit omit the name?

@denialhaag

Copy link
Copy Markdown
Member

Concerning QIR emission, __quantum__rt__qubit_allocate_array only expects a size parameter (https://www.qir-alliance.org/qir-runner/qir_backend/fn.__quantum__rt__qubit_allocate_array.html). Should we add custom metadata or just emit the name?

I think you are right; register names only make sense for classical registers in the context of output recording. 🤔

@burgholzer

Copy link
Copy Markdown
Member

Concerning QIR emission, __quantum__rt__qubit_allocate_array only expects a size parameter (https://www.qir-alliance.org/qir-runner/qir_backend/fn.__quantum__rt__qubit_allocate_array.html). Should we add custom metadata or just emit the name?

I think you are right; register names only make sense for classical registers in the context of output recording. 🤔

yeah. for QIR emission, quantum registers do not really matter 👍🏼

@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor
❌ Action failed

Review failed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@mlir/unittests/Dialect/QC/Translation/test_openqasm3_emission.cpp`:
- Around line 530-545: Extend ReusesQuantumRegisterNames with an invalid stored
register name, then require strict OpenQASM parsing of the emitted result.
Assert the output does not declare the invalid name while preserving the
existing valid-name assertion.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: ce584dc3-5254-4e6c-aed3-fda634cf5aca

📥 Commits

Reviewing files that changed from the base of the PR and between 52cf1e7 and cb20786.

📒 Files selected for processing (18)
  • CHANGELOG.md
  • mlir/include/mlir/Dialect/QC/Builder/QCProgramBuilder.h
  • mlir/include/mlir/Dialect/QCO/Builder/QCOProgramBuilder.h
  • mlir/include/mlir/Dialect/Utils/Utils.h
  • mlir/lib/Conversion/QCOToQC/QCOToQC.cpp
  • mlir/lib/Conversion/QCToQCO/QCToQCO.cpp
  • mlir/lib/Dialect/QC/Builder/QCProgramBuilder.cpp
  • mlir/lib/Dialect/QC/Translation/OpenQASMToQCEmitter.cpp
  • mlir/lib/Dialect/QC/Translation/TranslateQCToOpenQASM3.cpp
  • mlir/lib/Dialect/QC/Translation/TranslateQuantumComputationToQC.cpp
  • mlir/lib/Dialect/QCO/Builder/QCOProgramBuilder.cpp
  • mlir/unittests/Conversion/QCOToQC/test_qco_to_qc.cpp
  • mlir/unittests/Conversion/QCToQCO/test_qc_to_qco.cpp
  • mlir/unittests/Dialect/QC/IR/test_qc_ir.cpp
  • mlir/unittests/Dialect/QC/Translation/test_openqasm3_emission.cpp
  • mlir/unittests/Dialect/QC/Translation/test_qasm3_translation.cpp
  • mlir/unittests/Dialect/QC/Translation/test_quantum_computation_translation.cpp
  • mlir/unittests/Dialect/QCO/IR/test_qco_ir.cpp

Comment thread mlir/unittests/Dialect/QC/Translation/test_openqasm3_emission.cpp
@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@denialhaag denialhaag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks really clean to me now! I have three more comments, but I'll already now to not blcok the PR further. I'll leave the final say to @burgholzer. 😌

Comment on lines +48 to +50
/// Attribute used to retain a source-level quantum-register name.
inline constexpr llvm::StringLiteral QUANTUM_REGISTER_NAME_ATTR =
"mqt.quantum_register_name";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This applies to quite a few places, but I think we should generally go with qubit register instead of quantum register. 🤔

Suggested change
/// Attribute used to retain a source-level quantum-register name.
inline constexpr llvm::StringLiteral QUANTUM_REGISTER_NAME_ATTR =
"mqt.quantum_register_name";
/// Attribute used to retain a source-level qubit-register name.
inline constexpr llvm::StringLiteral QUBIT_REGISTER_NAME_ATTR =
"mqt.qubit_register_name";

Comment on lines +1384 to +1385
/// Track non-empty source-level quantum register names.
llvm::StringSet<> quantumRegisterNames;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also here, for example.

Suggested change
/// Track non-empty source-level quantum register names.
llvm::StringSet<> quantumRegisterNames;
/// Track non-empty source-level qubit-register names.
llvm::StringSet<> qubitRegisterNames;

Comment on lines 570 to 572
}

)mlir";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
}
)mlir";
}
)mlir";

@denialhaag
denialhaag requested a review from burgholzer August 6, 2026 18:21
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 enhancement Improvement of existing feature MLIR Anything related to MLIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🎨 Retain register name

3 participants