Skip to content

✨ Add Qubit Reuse Pass - #1923

Open
DRovara wants to merge 87 commits into
mainfrom
mlir/qubit-reuse
Open

✨ Add Qubit Reuse Pass#1923
DRovara wants to merge 87 commits into
mainfrom
mlir/qubit-reuse

Conversation

@DRovara

@DRovara DRovara commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Description

Part 3 out of 3 of the Qubit Reuse Implementation

This PR implements the actual qubit reuse pattern.

How To Use

mqt-cc now provides options to run qubit reuse.

mqt-cc --pass-pipeline='builtin.module(reuse-qubits-full)' input.qasm

Runs the greedy qubit reuse pass on the given program.

mqt-cc --pass-pipeline='builtin.module(reuse-qubits-full)' input.qasm

Performs "advanced" qubit reuse beyond just reset and reuse, where measurement lifting and classical control replacement is employed to further facilitate qubit reuse.

Further Details

Using a greedy approach, we search for qubits that do not interact and try to reuse resources by introducing reset operations. The measurement lifting and classical control replacement passes can also be used to increase the applicability of the pattern.

This is one of 3 PRs that will, in total, constitute qubit reuse:

  1. Measurement Lifitng
  2. Replacing Classical Controls
  3. Reuse Qubits

AI Notice: Gemini 3.1 and Claude Sonnet 4.6 were used for the generation of some code.

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:

  • I have disclosed the use of AI tools in the PR description as per our AI Usage Guidelines.
  • AI-assisted commits include an Assisted-by: [Model Name] via [Tool Name] footer.
  • I confirm that I have personally reviewed and understood all AI-generated content, and accept full responsibility for it.

DRovara and others added 30 commits July 14, 2026 03:11
Signed-off-by: Damian Rovara <93778306+DRovara@users.noreply.github.com>
Signed-off-by: Damian Rovara <93778306+DRovara@users.noreply.github.com>
@mergify mergify Bot removed the conflict label Jul 28, 2026
@DRovara

DRovara commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a QCO reuse-qubits optimization pass, registers standalone and full pipelines, and introduces unit and compiler tests covering direct, indirect, conditional, and measurement-lifted qubit reuse.

Changes

Qubit reuse optimization

Layer / File(s) Summary
Reuse pass implementation
mlir/include/mlir/Dialect/QCO/Transforms/Passes.td, mlir/lib/Dialect/QCO/Transforms/Optimizations/ReuseQubits.cpp
Defines the reuse-qubits pass and rewrites allocations with reset operations when disjoint sink operations can be reused.
Pipeline registration
mlir/lib/Support/Passes.cpp
Registers the reuse-related passes and adds the reuse-qubits-full pipeline.
QCO optimization tests and circuit builders
mlir/unittests/Dialect/QCO/Transforms/Optimizations/*, mlir/unittests/programs/qc_programs.*
Adds reuse tests and QC program builders covering direct, indirect, partial, and lifted reuse.
Compiler and QIR integration tests
mlir/unittests/Compiler/test_compiler_pipeline.cpp, mlir/unittests/programs/qir_programs.*
Adds configurable pipeline parameters and QIR builders for reset, conditional, and controlled-X reuse cases.
Changelog linkage
CHANGELOG.md
References PR [#1923] in the qubit-reuse entry and adds its link definition.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AllocOp
  participant ReuseQubitsPattern
  participant SinkOp
  participant ResetOp
  AllocOp->>ReuseQubitsPattern: inspect allocated qubit
  ReuseQubitsPattern->>SinkOp: select disjoint sink
  ReuseQubitsPattern->>ResetOp: insert reset
  ResetOp-->>AllocOp: replace allocation
Loading

Possibly related PRs

Suggested labels: enhancement, c++, MLIR

Suggested reviewers: matthiasreumann, denialhaag, burgholzer

Poem

A rabbit hops through qubits bright,
Resets one softly, left and right.
Gates may dance, then take their leave,
New circuits bloom from what we weave.
“Reuse!” I thump beneath the moon.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding the Qubit Reuse pass.
Description check ✅ Passed The description covers the required sections with summary, context, usage, checklist, and AI disclosure; the issue line is appropriately omitted.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch mlir/qubit-reuse

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.

@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: 2

🤖 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/lib/Dialect/QCO/Transforms/Optimizations/ReuseQubits.cpp`:
- Around line 70-71: Replace the unordered `mlir::DenseSet<mlir::Operation*>
toVisit` BFS worklist with an ordered LLVM container such as `llvm::SetVector`,
preserving FIFO traversal and deduplication. Keep `visited` as the separate
membership set, and update the enqueue/dequeue operations to use the ordered
worklist so sibling traversal and compiler output are deterministic.

In
`@mlir/unittests/Dialect/QCO/Transforms/Optimizations/test_qco_reuse_qubits.cpp`:
- Around line 54-68: Update the docstring for runQubitReusePass to describe its
complete pipeline: optionally run measurement lifting and classical-control
replacement, then always run qubit reuse followed by canonicalization. Keep the
implementation unchanged.
🪄 Autofix (Beta)

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: f73ad301-2a03-418a-9201-750c4f18513c

📥 Commits

Reviewing files that changed from the base of the PR and between d14b33f and 59f08c4.

📒 Files selected for processing (11)
  • CHANGELOG.md
  • mlir/include/mlir/Dialect/QCO/Transforms/Passes.td
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/ReuseQubits.cpp
  • mlir/lib/Support/Passes.cpp
  • mlir/unittests/Compiler/test_compiler_pipeline.cpp
  • mlir/unittests/Dialect/QCO/Transforms/Optimizations/CMakeLists.txt
  • mlir/unittests/Dialect/QCO/Transforms/Optimizations/test_qco_reuse_qubits.cpp
  • mlir/unittests/programs/qc_programs.cpp
  • mlir/unittests/programs/qc_programs.h
  • mlir/unittests/programs/qir_programs.cpp
  • mlir/unittests/programs/qir_programs.h

Comment thread mlir/lib/Dialect/QCO/Transforms/Optimizations/ReuseQubits.cpp Outdated
@DRovara

DRovara commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@DRovara
DRovara requested a review from burgholzer July 28, 2026 14:11

@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/lib/Dialect/QCO/Transforms/Optimizations/ReuseQubits.cpp`:
- Around line 67-93: Update reorderUsers to traverse the SmallVector worklist
with an index instead of erasing toVisit.begin() on each iteration. Preserve the
current BFS insertion order and determinism by processing toVisit[workIndex] and
advancing the index until it reaches toVisit.size(), while leaving visited
tracking and user reordering unchanged.
🪄 Autofix (Beta)

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: 5c1682c0-d1c5-4001-9227-8fa3db7fa2ad

📥 Commits

Reviewing files that changed from the base of the PR and between d14b33f and f120dee.

📒 Files selected for processing (11)
  • CHANGELOG.md
  • mlir/include/mlir/Dialect/QCO/Transforms/Passes.td
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/ReuseQubits.cpp
  • mlir/lib/Support/Passes.cpp
  • mlir/unittests/Compiler/test_compiler_pipeline.cpp
  • mlir/unittests/Dialect/QCO/Transforms/Optimizations/CMakeLists.txt
  • mlir/unittests/Dialect/QCO/Transforms/Optimizations/test_qco_reuse_qubits.cpp
  • mlir/unittests/programs/qc_programs.cpp
  • mlir/unittests/programs/qc_programs.h
  • mlir/unittests/programs/qir_programs.cpp
  • mlir/unittests/programs/qir_programs.h

Comment thread mlir/lib/Dialect/QCO/Transforms/Optimizations/ReuseQubits.cpp
@DRovara

DRovara commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.


Your included review limit is currently reached under our Fair Usage Limits Policy. This review may still proceed through usage-based billing if eligible. Your next included review will be available in 21 minutes.

@burgholzer burgholzer added feature New feature or request MLIR Anything related to MLIR labels Jul 28, 2026
@burgholzer burgholzer added this to the MLIR Support milestone Jul 28, 2026
@mergify mergify Bot added the conflict label Jul 28, 2026
Comment on lines +416 to +420
if (size == 1) {
const auto qubit = builder.allocQubit();
qubitRegisters[id] = {qubit};
break;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@burgholzer @denialhaag
Just wanted to check with you if this change is okay. My Qubit Reuse passes only work on individually allocated qubits. Extending that to Qubit Registers likely isn't incredibly hard, but it's also not trivial, as we would have to modify the algorithm that determines what dataflow paths are unrelated and we would have to decide how to slice the reigsters.

This gets around it by making qubit[1] q declarations in QASM treated as qc.alloc. Originally, I only wanted to modify the Translation for qubit q declarations (i.e. with no size indication at all), but that already gets obfuscated during parsing, so we don't have access to this information anymore at translation time without even more changes to the parser.

Signed-off-by: Damian Rovara <93778306+DRovara@users.noreply.github.com>
@mergify mergify Bot removed the conflict label Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request MLIR Anything related to MLIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants