Skip to content

✨ Add bottom-up makeThreeQubitGateDD for RCCX - #1950

Merged
burgholzer merged 9 commits into
mainfrom
feat/dd-three-qubit-bottom-up
Jul 29, 2026
Merged

✨ Add bottom-up makeThreeQubitGateDD for RCCX#1950
burgholzer merged 9 commits into
mainfrom
feat/dd-three-qubit-bottom-up

Conversation

@simon1hofmann

@simon1hofmann simon1hofmann commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

Summary

  • Add ThreeQubitGateMatrix / makeThreeQubitGateDD for direct bottom-up DD construction of three-qubit gates (same style as makeGateDD / makeTwoQubitGateDD), starting with RCCX via opToThreeQubitGateMatrix.
  • Use MSB-among-operands bit order for three-qubit matrices (targets[0] = high bit), matching makeTwoQubitGateDD and QCO (embed / CtrlOp); RCCX is the 5/6/7 form (not the Qiskit little-endian 3/5/7 labeling).
  • Route RCCX through the new path in getStandardOperationDD instead of the multiply-based construction.
  • Share control-wrapping, range checks, and terminal materialization helpers across 1q/2q/3q gate DD builders.
  • Wire QCO buildFunctionality / simulate dense path through makeThreeQubitGateDD for 3-wire unitaries (plain copy; full-width makeDDFromMatrix still remaps QCO MSB → global DD LSB).
  • Consolidate RCCX DD construction tests (target permutations, overload wrappers, extra pos/neg controls) and cover partial-width 3q dense inv in QCO DD functionality tests.

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).
  • 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.

Direct DD construction for three-qubit gates (same style as
makeGateDD / makeTwoQubitGateDD), with shared control-wrapping
helpers, GateMatrixDefinitions, and tests.

Assisted-by: Cursor Grok 4.5 via Cursor
Co-authored-by: Cursor <cursoragent@cursor.com>
@simon1hofmann
simon1hofmann force-pushed the feat/dd-three-qubit-bottom-up branch from 7fd25f8 to b0ecac6 Compare July 27, 2026 12:54
@simon1hofmann simon1hofmann changed the title ✨ Add bottom-up makeThreeQubitGateDD for RCCX ✨ Add bottom-up makeThreeQubitGateDD for RCCX Jul 27, 2026
@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.48428% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
mlir/lib/Dialect/QCO/Utils/DDFunctionality.cpp 69.2% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Jul 27, 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: 3 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

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: f6a81b0f-f56d-4dc8-a0b8-7fec3009fd2d

📥 Commits

Reviewing files that changed from the base of the PR and between 578ebcf and 111efdb.

📒 Files selected for processing (2)
  • src/dd/Package.cpp
  • test/dd/test_package.cpp
📝 Walkthrough

Walkthrough

Adds native RCCX three-qubit matrix conversion and decision-diagram construction, including controlled variants. Shared DD-building helpers replace RCCX-specific construction, with expanded QCO integration, matrix, circuit, invalid-operation, and controlled-gate tests.

Changes

RCCX decision-diagram support

Layer / File(s) Summary
Three-qubit matrix contracts
include/mqt-core/dd/*, src/dd/GateMatrixDefinitions.cpp, mlir/lib/Dialect/QCO/...
Adds the fixed-size three-qubit matrix type, RCCX matrix conversion, public makeThreeQubitGateDD overloads, and QCO handling for three-qubit unitaries.
Generic controlled DD construction
src/dd/Package.cpp, src/dd/Operations.cpp
Centralizes validation, terminal construction, control wrapping, and three-qubit matrix reduction; RCCX now uses the generic path.
RCCX validation and release updates
test/dd/*, mlir/unittests/..., CHANGELOG.md
Expands RCCX, invalid-operation, partial-width, matrix, and circuit coverage, updates supported-operation documentation, and adds the changelog reference.

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

Sequence Diagram(s)

sequenceDiagram
  participant Operations
  participant Package
  participant GateMatrixDefinitions
  participant DecisionDiagram
  Operations->>Package: makeThreeQubitGateDD
  Package->>GateMatrixDefinitions: opToThreeQubitGateMatrix
  GateMatrixDefinitions-->>Package: return RCCX matrix
  Package->>DecisionDiagram: reduce matrix and wrap controls
  DecisionDiagram-->>Operations: return mEdge
Loading

Possibly related PRs

Suggested labels: dependencies, DD, c++, Core, ZX, MLIR

Poem

I’m a rabbit with matrices bright,
Hopping through DDs day and night.
RCCX now takes flight,
Controls wrap left and right—
Three qubits bloom in quantum light!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.45% 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 is concise and accurately highlights the main change: bottom-up makeThreeQubitGateDD support for RCCX.
Description check ✅ Passed The description includes a clear summary, motivation, context, testing/docs checklist, and AI disclosure, matching the template well.
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.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch feat/dd-three-qubit-bottom-up

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.

- Updated DDFunctionality.h to include three-qubit gates in the supported programs.
- Modified RCCXOp to correctly define the unitary matrix for three-qubit operations.
- Implemented logic in DDFunctionality.cpp to handle three-qubit gate matrices.
- Added tests for three-qubit gates in the unit test suite to ensure proper functionality.
@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

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.

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/dd/Package.cpp (1)

156-174: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Reject empty packages before subtracting from nqubits.

For Package(0), nqubits - 1U underflows, so target/control 0 passes validation and construction proceeds with an out-of-range DD level. This affects all three gate builders using this helper.

Proposed fix
 [[noreturn]] void throwGateQubitOutOfRange(const std::size_t nqubits) {
+  if (nqubits == 0U) {
+    throw std::runtime_error(
+        "Cannot construct a gate in a package with zero qubits.");
+  }
   throw std::runtime_error{
       "Requested gate acting on qubit(s) with index larger than " +
       std::to_string(nqubits - 1U) +
@@
 void ensureGateQubitsInRange(const std::size_t nqubits,
                              const qc::Controls& controls,
                              const std::initializer_list<qc::Qubit> targets) {
-  const auto maxQubit = static_cast<Qubit>(nqubits - 1U);
-  if (std::ranges::any_of(
-          controls, [maxQubit](const auto& c) { return c.qubit > maxQubit; }) ||
-      std::ranges::any_of(targets,
-                          [maxQubit](const Qubit t) { return t > maxQubit; })) {
+  if (nqubits == 0U ||
+      std::ranges::any_of(controls, [nqubits](const auto& c) {
+        return static_cast<std::size_t>(c.qubit) >= nqubits;
+      }) ||
+      std::ranges::any_of(targets, [nqubits](const Qubit target) {
+        return static_cast<std::size_t>(target) >= nqubits;
+      })) {
     throwGateQubitOutOfRange(nqubits);
   }
 }
🤖 Prompt for 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.

In `@src/dd/Package.cpp` around lines 156 - 174, Update ensureGateQubitsInRange to
reject nqubits == 0 before computing nqubits - 1U, using the existing
throwGateQubitOutOfRange error path so all gate builders receive consistent
validation. Preserve the current control and target range checks for non-empty
packages.
🤖 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.

Outside diff comments:
In `@src/dd/Package.cpp`:
- Around line 156-174: Update ensureGateQubitsInRange to reject nqubits == 0
before computing nqubits - 1U, using the existing throwGateQubitOutOfRange error
path so all gate builders receive consistent validation. Preserve the current
control and target range checks for non-empty packages.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 1156ed52-c2ba-45c6-ba10-a52932b0df80

📥 Commits

Reviewing files that changed from the base of the PR and between 31d077c and d47b05b.

📒 Files selected for processing (6)
  • mlir/include/mlir/Dialect/QCO/Utils/DDFunctionality.h
  • mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RCCXOp.cpp
  • mlir/lib/Dialect/QCO/Utils/DDFunctionality.cpp
  • mlir/unittests/Dialect/QCO/Utils/test_dd_functionality.cpp
  • src/dd/GateMatrixDefinitions.cpp
  • src/dd/Package.cpp

@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

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.

@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 `@src/dd/Package.cpp`:
- Around line 169-181: Extend ensureGateQubitsInRange to reject duplicate target
qubits and any control qubit that matches a target, while preserving the
existing range validation and throwing through throwGateQubitOutOfRange. Add
regression coverage for duplicate targets and control/target overlap, including
the currently skipped RCCX cases.
🪄 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: 12002321-85db-487c-a80b-42dab6b7e6dd

📥 Commits

Reviewing files that changed from the base of the PR and between d47b05b and 578ebcf.

📒 Files selected for processing (2)
  • src/dd/Package.cpp
  • test/dd/test_package.cpp

Comment thread src/dd/Package.cpp
- Introduced a new function `throwGateQubitsNotDistinct` to handle errors for duplicate or overlapping control/target qubits.
- Enhanced `ensureGateQubitsInRange` to check for distinct qubit targets and overlapping controls, throwing appropriate exceptions when violations occur.
- Added unit tests to `DDPackageTest` to verify that overlapping gate qubits are correctly rejected during gate construction.
@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

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
simon1hofmann marked this pull request as ready for review July 27, 2026 16:29
@simon1hofmann
simon1hofmann requested review from burgholzer and denialhaag and removed request for denialhaag July 27, 2026 16:38
@simon1hofmann simon1hofmann self-assigned this Jul 29, 2026
@simon1hofmann simon1hofmann added the enhancement Improvement of existing feature label Jul 29, 2026
@simon1hofmann simon1hofmann added DD Anything related to the DD package c++ Anything related to C++ code MLIR Anything related to MLIR labels Jul 29, 2026
@simon1hofmann simon1hofmann added this to the MLIR Support milestone Jul 29, 2026
Signed-off-by: Lukas Burgholzer <burgholzer@me.com>

@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 feels really clean. Let's get this in 🚀

@burgholzer
burgholzer enabled auto-merge (squash) July 29, 2026 14:02
@burgholzer
burgholzer disabled auto-merge July 29, 2026 15:38
@burgholzer
burgholzer merged commit b43afaf into main Jul 29, 2026
31 checks passed
@burgholzer
burgholzer deleted the feat/dd-three-qubit-bottom-up branch July 29, 2026 15:38
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 DD Anything related to the DD package enhancement Improvement of existing feature MLIR Anything related to MLIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants