Skip to content

✨ Add two-qubit Weyl (KAK) decomposition library#1803

Draft
simon1hofmann wants to merge 6 commits into
mainfrom
decomp/weyl
Draft

✨ Add two-qubit Weyl (KAK) decomposition library#1803
simon1hofmann wants to merge 6 commits into
mainfrom
decomp/weyl

Conversation

@simon1hofmann

Copy link
Copy Markdown
Contributor

Description

Adds a two-qubit Weyl (KAK) decomposition on top of Matrix4x4. A 4×4 unitary is factored as (K1l ⊗ K1r) · U_canon(a,b,c) · (K2l ⊗ K2r) up to global phase, with optional canonical-gate specialization and basis-gate (CX/CZ) synthesis.
This PR is intentionally scoped to the decomposition library + tests. It does not wire an MLIR pass, emit gate sequences, or add NativeProfile / fuse-pass integration — those are planned follow-ups.

New API (mlir/Dialect/QCO/Transforms/Decomposition/Weyl.h)

  • TwoQubitWeylDecomposition — KAK decomposition with Weyl-chamber reduction and 10 canonical-gate specializations (identity, SWAP, partial-SWAP ±, controlled, mirror-controlled, fSim variants)
  • TwoQubitBasisDecomposer — decomposes a Weyl result into single-qubit factors + basis-gate count (0–3 uses)

Supporting changes

  • Matrix.h / Matrix.cpp — shared gate matrix factories: rx/ry/rz, iPauliX/Y/Z, rxx/ryy/rzz
  • Euler.h / Euler.cpp — promote EulerAngles and anglesFromUnitary() to public API (used by Weyl specialization for controlled / fSim cases)

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.

@codecov

codecov Bot commented Jun 22, 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 review

@coderabbitai

coderabbitai Bot commented Jun 22, 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 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 64627c8a-6611-445f-8e30-23a02fd9e36a

📥 Commits

Reviewing files that changed from the base of the PR and between 61ed245 and eaa1097.

📒 Files selected for processing (2)
  • mlir/lib/Dialect/QCO/Transforms/Decomposition/Weyl.cpp
  • mlir/unittests/Dialect/QCO/Transforms/Decomposition/test_weyl_decomposition.cpp

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Added Euler-angle extraction with configurable Euler basis selection for decomposing 2×2 unitaries.
    • Introduced two-qubit Weyl decomposition to obtain canonical parameters, plus optional native synthesis using a supplied entangling basis.
    • Added quantum gate matrix utilities for RX/RY/RZ, i·Pauli X/Y/Z, and RXX/RYY/RZZ.
  • Tests
    • Added/expanded unit tests covering Weyl decomposition, basis decomposition, and reconstruction accuracy across many random and structured matrices.

Walkthrough

Adds a full two-qubit Weyl decomposition and basis-gate synthesis pipeline to the MLIR QCO dialect. New rotation and i-Pauli matrix primitives are introduced in the Matrix utility layer, EulerAngles is promoted to a public header, and a new Weyl.h/Weyl.cpp pair implements TwoQubitWeylDecomposition, TwoQubitBasisDecomposer, and supporting numerical helpers. A new GTest file validates the pipeline.

Changes

Weyl Two-Qubit Decomposition Pipeline

Layer / File(s) Summary
Rotation and i-Pauli matrix primitives
mlir/include/mlir/Dialect/QCO/Utils/Matrix.h, mlir/lib/Dialect/QCO/Utils/Matrix.cpp
Declares and implements rxMatrix/ryMatrix/rzMatrix, iPauliX/iPauliY/iPauliZ, and rxxMatrix/ryyMatrix/rzzMatrix as new free functions returning rotation matrices and cached i-times-Pauli references.
EulerAngles promoted to public header
mlir/include/mlir/Dialect/QCO/Transforms/Decomposition/Euler.h, mlir/lib/Dialect/QCO/Transforms/Decomposition/Euler.cpp
Moves the EulerAngles struct from an anonymous namespace in Euler.cpp into Euler.h and changes anglesFromUnitary from a static internal helper to a public non-static function.
Weyl decomposition public API
mlir/include/mlir/Dialect/QCO/Transforms/Decomposition/Weyl.h
Defines WEYL_TOLERANCE, TwoQubitWeylDecomposition, TwoQubitNativeDecomposition, TwoQubitBasisDecomposer, and the decomposeTwoQubitWithBasis convenience function as the complete public interface for two-qubit Weyl decomposition.
Numerical helpers and specialization logic
mlir/lib/Dialect/QCO/Transforms/Decomposition/Weyl.cpp
Implements remEuclid, traceToFidelity, magicBasisTransform, closestPartialSwap, diagonalizeComplexSymmetric, decomposeTwoQubitProductGate, getTrace, bestSpecialization, and relativeEq as the internal numerical substrate.
TwoQubitWeylDecomposition::create and specialization
mlir/lib/Dialect/QCO/Transforms/Decomposition/Weyl.cpp
Implements create (SU(4) projection → magic-basis transform → eigenphase extraction → Weyl-chamber mapping → local factor decomposition → specialization → phase adjustment → reconstruction validation), canonical RZZ·RYY·RXX matrix construction, and applySpecialization for all equivalence-class rewrites.
TwoQubitBasisDecomposer create, decompose, and templates
mlir/lib/Dialect/QCO/Transforms/Decomposition/Weyl.cpp
Implements TwoQubitBasisDecomposer::create (template precomputation, super-controlled detection), twoQubitDecompose (trace scoring, basis-gate-count selection, decomp0–3 templates, phase adjustment), traces, and the decomposeTwoQubitWithBasis wrapper.
Unit tests and build wiring
mlir/unittests/Dialect/QCO/Transforms/Decomposition/test_weyl_decomposition.cpp, test_euler_decomposition.cpp, CMakeLists.txt
Adds parameterized Weyl reconstruction and basis-decomposer tests, removes now-redundant local matrix helpers from Euler tests, and expands the CMake target to build and link the new test file and additional QCO library components.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant decomposeTwoQubitWithBasis
  participant TwoQubitWeylDecomposition
  participant TwoQubitBasisDecomposer
  participant diagonalizeComplexSymmetric

  Caller->>decomposeTwoQubitWithBasis: target Matrix4x4, basisMatrix, basisFidelity, numBasisUses
  decomposeTwoQubitWithBasis->>TwoQubitBasisDecomposer: create(basisMatrix, basisFidelity)
  TwoQubitBasisDecomposer->>TwoQubitWeylDecomposition: create(basisMatrix, basisFidelity)
  TwoQubitWeylDecomposition->>diagonalizeComplexSymmetric: M·Mᵀ in magic basis
  diagonalizeComplexSymmetric-->>TwoQubitWeylDecomposition: eigenphases → a, b, c
  TwoQubitWeylDecomposition-->>TwoQubitBasisDecomposer: basis Weyl decomposition
  TwoQubitBasisDecomposer-->>decomposeTwoQubitWithBasis: TwoQubitBasisDecomposer instance

  decomposeTwoQubitWithBasis->>TwoQubitWeylDecomposition: create(target, nullopt)
  TwoQubitWeylDecomposition-->>decomposeTwoQubitWithBasis: target Weyl decomposition

  decomposeTwoQubitWithBasis->>TwoQubitBasisDecomposer: twoQubitDecompose(targetWeyl, numBasisUses)
  TwoQubitBasisDecomposer->>TwoQubitBasisDecomposer: score traces 0..3, select best count
  TwoQubitBasisDecomposer->>TwoQubitBasisDecomposer: decomp0/1/2/3 → single-qubit factors
  TwoQubitBasisDecomposer-->>decomposeTwoQubitWithBasis: optional TwoQubitNativeDecomposition
  decomposeTwoQubitWithBasis-->>Caller: optional TwoQubitNativeDecomposition
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • munich-quantum-toolkit/core#1672: Directly extends the Euler decomposition API (Euler.h, anglesFromUnitary) and unit-test infrastructure that this PR refactors by promoting EulerAngles to a public header and removing local matrix helpers from the Euler test file.
  • munich-quantum-toolkit/core#1774: Introduces the Matrix types used across the dialect; this PR extends that same Matrix.h API with additional gate-construction helpers (rx/ry/rz, iPauli*, rxx/ryy/rzz) that the new Weyl decomposition code depends on.

Suggested labels

feature, c++, MLIR

Suggested reviewers

  • burgholzer

🐇 Two qubits dance in the Weyl chamber bright,
Eigenphases twirl through the magic-basis night.
With RXX, RYY, RZZ in a row,
The decomposer hops, counting gates as they flow.
One, two, or three — the bunny keeps score,
Reconstructed unitaries, right to the core! 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.64% 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 'Add two-qubit Weyl (KAK) decomposition library' clearly and specifically describes the main change: introducing a new Weyl decomposition library for two-qubit unitaries.
Description check ✅ Passed The PR description is comprehensive, covering the main change (Weyl decomposition), new API components, supporting changes, scoping clarification, and includes the template checklist. While most checklist items are unchecked, the description itself meets requirements.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch decomp/weyl

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 and usage tips.

@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/Decomposition/Weyl.cpp`:
- Around line 112-118: The hardcoded constants 1.2602066112249388 and
0.22317849046722027 assigned to randA and randB in the if (i == 0) block lack
documentation about their origin and purpose. Add a comment above the if
statement explaining that these are perturbation coefficients specifically
chosen for the first diagonalization attempt, likely derived from a reference
implementation such as Qiskit, to clarify why these specific values are used for
the initial iteration while subsequent iterations use randomly generated values
from the distribution.

In
`@mlir/unittests/Dialect/QCO/Transforms/Decomposition/test_weyl_decomposition.cpp`:
- Around line 189-193: The test fixture classes WeylDecompositionTest and
BasisDecomposerTest are defined at translation unit scope without internal
linkage, triggering a clang-tidy warning. Wrap these class definitions (and any
related helper code that is only used within this test file) in an anonymous
namespace to limit their visibility to internal linkage and resolve the warning.
🪄 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

Run ID: b251ecb4-6a99-454e-a2b7-dc6a2ce6d4c6

📥 Commits

Reviewing files that changed from the base of the PR and between 6e15803 and 61ed245.

📒 Files selected for processing (9)
  • mlir/include/mlir/Dialect/QCO/Transforms/Decomposition/Euler.h
  • mlir/include/mlir/Dialect/QCO/Transforms/Decomposition/Weyl.h
  • mlir/include/mlir/Dialect/QCO/Utils/Matrix.h
  • mlir/lib/Dialect/QCO/Transforms/Decomposition/Euler.cpp
  • mlir/lib/Dialect/QCO/Transforms/Decomposition/Weyl.cpp
  • mlir/lib/Dialect/QCO/Utils/Matrix.cpp
  • mlir/unittests/Dialect/QCO/Transforms/Decomposition/CMakeLists.txt
  • mlir/unittests/Dialect/QCO/Transforms/Decomposition/test_euler_decomposition.cpp
  • mlir/unittests/Dialect/QCO/Transforms/Decomposition/test_weyl_decomposition.cpp
💤 Files with no reviewable changes (1)
  • mlir/unittests/Dialect/QCO/Transforms/Decomposition/test_euler_decomposition.cpp

Comment thread mlir/lib/Dialect/QCO/Transforms/Decomposition/Weyl.cpp
@simon1hofmann

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 22, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant