Skip to content
Draft
Show file tree
Hide file tree
Changes from 229 commits
Commits
Show all changes
247 commits
Select commit Hold shift + click to select a range
20404bc
snapshot
taminob Sep 15, 2025
8fd23f6
copied rust code
taminob Sep 16, 2025
5e806f6
🎨 pre-commit fixes
pre-commit-ci[bot] Sep 16, 2025
70082d8
snapshot
taminob Oct 6, 2025
debe427
snapshot
taminob Oct 7, 2025
b86844b
snapshot
taminob Oct 7, 2025
e4dec47
minor additions
taminob Oct 9, 2025
9ce8ac3
add pass to Passes.td
taminob Oct 9, 2025
f45107b
snapshot
taminob Oct 9, 2025
81e6230
compiling snapshot
taminob Oct 13, 2025
588db20
tmp
taminob Oct 15, 2025
84fa825
switch to long double
taminob Oct 16, 2025
3dd1266
testing a.cpp
taminob Oct 16, 2025
3c214ad
add zgemm for testing, start euler decomposition implementation
taminob Oct 19, 2025
62aa918
non-working but finished snapshot
taminob Oct 21, 2025
eec099c
fix out-of-bounds issues
taminob Oct 22, 2025
b1a4286
test using Armadillo
taminob Oct 22, 2025
cee23a7
minor fix
taminob Oct 22, 2025
5550adc
tmp
taminob Oct 24, 2025
b541500
work on gate application
taminob Oct 27, 2025
0b80624
use laughing-umbrella implementation for decompose_two_qubit_product_…
taminob Oct 27, 2025
299d6f1
add more eigen decomposition implementations
taminob Oct 27, 2025
96b39ad
work on applySeries()
taminob Oct 27, 2025
3947566
Revert "use laughing-umbrella implementation for decompose_two_qubit_…
taminob Oct 27, 2025
ab188ae
fix transpose_conjugate
taminob Oct 27, 2025
410fadd
add test
taminob Oct 30, 2025
05885a1
actually kind of working snapshot?
taminob Oct 30, 2025
c87b02a
fix test case
taminob Oct 30, 2025
619d0d6
minor fix
taminob Oct 30, 2025
a998ef7
fix test case
taminob Oct 30, 2025
71420a2
add new test, fix tests
taminob Oct 30, 2025
bd8cdbd
iniital complexSeries test
taminob Nov 4, 2025
85b03c2
use Eigen for multiply() and determinant()
taminob Nov 4, 2025
0de0b43
fix gate matrix multiplication
taminob Nov 4, 2025
cc9f1be
add simple (too simple) series complexity
taminob Nov 4, 2025
13721df
fix insertion when operations which are not part of the series are in…
taminob Nov 4, 2025
6305604
add gate matrices
taminob Nov 4, 2025
dd130b9
sequence complexity()
taminob Nov 6, 2025
fb742d8
print debugging :)
taminob Nov 6, 2025
e6840d4
longer complex test to pass sequence complexity check
taminob Nov 6, 2025
08e1991
use CXGate() definition for decomposer
taminob Nov 10, 2025
0e6ff8e
add cnot test other direction
taminob Nov 10, 2025
5814ade
tmp (to be deleted)
taminob Nov 10, 2025
70d604e
re-write using Eigen
taminob Nov 10, 2025
a5dead7
delete pulse-optimizer code (KEEP for future; should be re-added at s…
taminob Nov 10, 2025
5aeb866
use designated initializer lists
taminob Nov 11, 2025
0714745
fix warnings, rename all variables
taminob Nov 11, 2025
579b6e8
test normalized magic basis, allow complex eigenvector decomposition
taminob Nov 11, 2025
7ca5341
finally figured some stuff out
taminob Nov 11, 2025
e1548d2
try out lauging-umbrella weyl-chamber
taminob Nov 12, 2025
9dfd453
try to implement weylchamber.c1c2c3 algorithm
taminob Nov 12, 2025
e78b4f3
on_gates two-qubit product decompose
taminob Nov 12, 2025
cf9d649
minor additions
taminob Nov 12, 2025
c02bfa6
try ordering all four eigenvectors
taminob Nov 12, 2025
3a800c6
undo ordering of all columns
taminob Nov 12, 2025
c155100
implement algorithm based on quantumflow.canonical_decomposition
taminob Nov 13, 2025
b6ff17f
sanity checks and stuff
taminob Nov 13, 2025
83c089e
add original implementation again which seems to kind of work?
taminob Nov 13, 2025
a94195f
generate openqasm code of generated sequence
taminob Nov 14, 2025
ab57b7d
some more small fixes, maybe breakthrough?
taminob Nov 14, 2025
af6f411
rework getTwoQubitSeries logic
taminob Nov 14, 2025
8a9447b
generate openqasm code of found series
taminob Nov 14, 2025
e4b68d1
fix new getTwoQubitSeries logic
taminob Nov 14, 2025
1eea4af
working except global phase?
taminob Nov 15, 2025
bb4b796
fix global phase issues
taminob Nov 15, 2025
837363f
first working snapshot
taminob Nov 16, 2025
2582c66
very minor cleanup
taminob Nov 16, 2025
8ec690b
add more sanity checks for unitary
taminob Nov 16, 2025
e26a374
find and fix minor formula mistake
taminob Nov 16, 2025
bc1475e
test update
taminob Nov 16, 2025
4048bf6
make calculations exact without approximation
taminob Nov 17, 2025
cff036c
respect gphase of circuit and fix series collection bug
taminob Nov 17, 2025
21701e8
change global phase to local phase
taminob Nov 17, 2025
85c1f86
revert to global phase, fix controlled gate qubit order
taminob Nov 18, 2025
9c0ecb4
fix first couple of tests
taminob Nov 18, 2025
00986dd
start working on basis set and remove dead code
taminob Nov 18, 2025
bcaf798
decompose for all bases, choose best
taminob Nov 18, 2025
83868c0
add checks for most tests
taminob Nov 18, 2025
b16bc9a
TMP, needs to be removed; allow OpenQASM export with round-trip pass
taminob Nov 18, 2025
8cdce44
add single-qubit backtracking
taminob Nov 19, 2025
e919bcd
fix atol issues
taminob Nov 19, 2025
1f91271
add more euler bases to pattern
taminob Nov 19, 2025
d4f1b49
minor cleanup (renaming, comments, disable printing)
taminob Nov 19, 2025
5620b9e
Revert "TMP, needs to be removed; allow OpenQASM export with round-tr…
taminob Nov 19, 2025
82ba25d
remove armadillo in cmake
taminob Nov 20, 2025
3d1dc8a
more cleanup
taminob Nov 20, 2025
1eeedba
fix (?) tests
taminob Nov 20, 2025
3afb374
fix final test
taminob Nov 20, 2025
17b02c5
another minor cleanup
taminob Nov 20, 2025
ca9c9f1
clean sanity check prints up
taminob Nov 20, 2025
0174a3d
properly add Eigen lib to cmake
taminob Nov 20, 2025
3c70713
remove unnecessary correction
taminob Nov 20, 2025
9f645a0
🎨 pre-commit fixes
pre-commit-ci[bot] Nov 20, 2025
b8f6b0b
fix eigen compilation issues
taminob Nov 20, 2025
5cd8ca4
more cleanup
taminob Nov 20, 2025
412a829
clean up weyl decomposition creation
taminob Nov 21, 2025
6f04dbb
remove openqasm code generation
taminob Nov 21, 2025
eac38de
fix includes and use SmallVector instead of std::vector
taminob Nov 21, 2025
4ea3120
fix or silence linter
taminob Nov 21, 2025
0cf2292
incorporate coderabbitai feedback
taminob Nov 21, 2025
b4d419c
remove global phase accumulation
taminob Nov 25, 2025
75672af
fix :math: latex formatting
taminob Nov 25, 2025
fe08c78
minor comment fixes
taminob Nov 25, 2025
5128d83
replace CHECK-DAG by CHECK again
taminob Nov 25, 2025
9194ae9
add comments to weyl decomposition
taminob Nov 25, 2025
ac4fb41
extract TwoQubitWeylDecomposition::getTrace()
taminob Nov 25, 2025
56a2990
transform basis decomposer into actual class with private members
taminob Nov 25, 2025
76ca4d0
attempt to fix CI test
taminob Nov 25, 2025
ec7f8c3
add supercontrolled check
taminob Nov 26, 2025
49a769a
extract decomposition into headers
taminob Nov 26, 2025
910384e
slight improvement to weyl decomposition for testing purposes
taminob Nov 26, 2025
4c42850
add unittest for weyl decomposition
taminob Nov 26, 2025
3453d2c
fix linter issues
taminob Nov 26, 2025
7f0d1f6
add tests for basis decomposer
taminob Nov 26, 2025
e904dc1
fix linter issues
taminob Nov 27, 2025
c586338
clang-tidy: Add IgnoreHeader for include cleaner for Eigen
taminob Nov 27, 2025
f2efbcb
remove exact values from MLIR test
taminob Nov 27, 2025
78daaba
fix mlir test/linter issues
taminob Nov 27, 2025
741660e
remove std::numbers because not supported by all compilers in CI
taminob Nov 27, 2025
9a6c691
remove unused Helpers.h from src dir
taminob Nov 27, 2025
ad8da23
remove auto* because of windows build
taminob Nov 27, 2025
1e89778
try to fix windows ARM ci
taminob Nov 28, 2025
854d9a0
pass matrices by reference
taminob Nov 28, 2025
25b377d
allow non-optimal vectorization
taminob Nov 28, 2025
cd8ae96
🎨 pre-commit fixes
pre-commit-ci[bot] Nov 28, 2025
2f634f5
try to fix
taminob Nov 28, 2025
f9b033d
integrate single-qubit decomposition
taminob Nov 28, 2025
aaab4b8
add tests for single-qubit decomposition
taminob Nov 28, 2025
fa78cbb
add unittests for euler decomposition
taminob Nov 30, 2025
d103e7a
fix linter issues
taminob Nov 30, 2025
7fc31ae
fix include cleaner issues
taminob Dec 1, 2025
43e9d31
add test parametrizations for other specializations
taminob Dec 1, 2025
95424ee
add more matrix definitions
taminob Dec 1, 2025
a5c54e6
handle barrier gates in series collection
taminob Dec 1, 2025
aa3212d
🎨 pre-commit fixes
pre-commit-ci[bot] Dec 1, 2025
474a5d9
fix include cleaner issues
taminob Dec 1, 2025
21e9b7b
remove unused globalPhase from TwoQubitSeries
taminob Dec 1, 2025
6815b26
fix barrier handling at start of series
taminob Dec 1, 2025
202fa7f
simplify weyl specialization test parameters
taminob Dec 1, 2025
49a60a7
remove dynamic_casts due to RTTI issue
taminob Dec 19, 2025
8fc3b53
Revert "remove dynamic_casts due to RTTI issue"
taminob Dec 19, 2025
68890af
move GateDecompositionPass to new QCO dialect
taminob Dec 20, 2025
9a7006b
add GateDecompositionPass to compiler pipeline
taminob Dec 20, 2025
935ad7d
🎨 pre-commit fixes
pre-commit-ci[bot] Dec 20, 2025
d296638
add UnitaryMatrixType to operation and add Eigen dependency to dialect
taminob Dec 28, 2025
a8dbfcb
first non-working attempt
taminob Dec 28, 2025
e9da9b0
second non-working attempt
taminob Jan 4, 2026
5b325a5
compiling approach
taminob Jan 4, 2026
b3161cc
formatting
taminob Jan 5, 2026
01886fb
avoid use of implementation-dependent argument names
taminob Jan 5, 2026
30198a9
finally got template function working
taminob Jan 5, 2026
1c7dd1d
add to compiler pipeline and add (failing) test case
taminob Jan 5, 2026
a8fbec2
make unitary function const, remove HasMatrixDefinition bool template…
taminob Jan 6, 2026
f9f7f04
start working on ctrl matrix
taminob Jan 6, 2026
d8d7b19
use new unitary matrix functions in GateDecompositionPattern
taminob Jan 6, 2026
ce79ed2
add debug prints
taminob Jan 6, 2026
6c965ed
🎨 pre-commit fixes
pre-commit-ci[bot] Jan 6, 2026
1588388
add helpers for CtrlOp matrix
taminob Jan 7, 2026
00491d7
add matrix definition to all operations
taminob Jan 7, 2026
69ebd05
fixes for unitary matrix definition
taminob Jan 7, 2026
df22236
fixes for gate decomposition pattern
taminob Jan 7, 2026
31f4a02
fix formatting in MatrixUtils.h
taminob Jan 7, 2026
9e2e0e6
(dirty) fix for CtrlOp in gate decomposition pattern; runs, but SSA e…
taminob Jan 7, 2026
f9e4ed4
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Jan 19, 2026
0bb5a86
:memo: Add PR of unitary matrices to CHANGELOG
taminob Jan 20, 2026
a0051a9
remove matrix draft remains
taminob Jan 21, 2026
1a8fa76
fix compilation issues with new matrix implementation
taminob Jan 21, 2026
0514c54
🎨 pre-commit fixes
pre-commit-ci[bot] Jan 21, 2026
1bb1f63
fix issue with CtrlOp
taminob Jan 22, 2026
0334614
fixes and improvements (forceApplication+singleQubitOnly) for QCO dia…
taminob Jan 24, 2026
98a703e
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Jan 24, 2026
9fde078
🎨 pre-commit fixes
pre-commit-ci[bot] Jan 24, 2026
7b93aec
fix nested ops issue and others in pass/pattern
taminob Jan 27, 2026
7348caf
set GPhase complexity to 0 and fix complexity calculation for backtra…
taminob Jan 30, 2026
c46f159
thesis evaluation: add statistics and disable canonicalizations
taminob Jan 31, 2026
a79324c
evaluation scripts
taminob Jan 31, 2026
90c46f4
evaluation results
taminob Jan 31, 2026
3a8b02a
evaluation script fixes and improvements
taminob Feb 2, 2026
b4af378
update evaluation results
taminob Feb 2, 2026
5e676bf
evaluate circuit collection for qiskit, total_evaluate fixes
taminob Feb 3, 2026
2ddfc6c
update evaluation results
taminob Feb 3, 2026
007b575
evaluation script improvements
taminob Feb 3, 2026
c66c4ec
update evaluation figures
taminob Feb 3, 2026
8b7f0bc
Revert thesis evaluation changes
taminob Feb 4, 2026
84a367a
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Feb 4, 2026
595edb9
:memo: Add PR to CHANGELOG
taminob Feb 4, 2026
b506244
fix typo in comment
taminob Feb 4, 2026
a418cc1
improve naming in getDefaultNbasis
taminob Feb 4, 2026
4b1a607
coderabbit feedback and cleanup
taminob Feb 5, 2026
f5b2ff1
remove broken unittest; need to think of ways to verify optimizations
taminob Feb 5, 2026
fd4f99b
remove helpers::kroneckerProduct, IDENTITY_GATE and fix unitary matrices
taminob Feb 5, 2026
ad50bef
more fixes
taminob Feb 5, 2026
0b09375
relax isUnitaryMatrix() default tolerance to Eigen's default value
taminob Feb 6, 2026
bc905ed
fix mysterious segfault in Eigen::kroneckerProduct (still no idea why…
taminob Feb 6, 2026
ac7fb91
reduce random test time limit to 3s
taminob Feb 6, 2026
80fa001
add C_ prefix to complex IM, M_IM
taminob Feb 6, 2026
f28b77e
remove custom types
taminob Feb 6, 2026
69e989f
remove constants and other stuff
taminob Feb 6, 2026
618f2dc
fix typo in comment
taminob Feb 6, 2026
b8cacc5
replace exceptions by llvm error reporting
taminob Feb 6, 2026
f50760d
some coderabbitai comments and complexity caching
taminob Feb 6, 2026
4d1f561
remove crash test
taminob Feb 6, 2026
9e1cab9
make Weyl decomposition non-trivial class; some other fixes
taminob Feb 6, 2026
f1b33bb
better random basis tests and other fixes
taminob Feb 6, 2026
fa2e420
more coderabbitai fixes
taminob Feb 6, 2026
aa3e9fa
add utils for tests
taminob Feb 6, 2026
b8ec913
coderabbit feedback
taminob Feb 6, 2026
b8e868c
fix includes
taminob Feb 6, 2026
a5af558
separate definitions into source files
taminob Feb 7, 2026
bb27f47
🎨 pre-commit fixes
pre-commit-ci[bot] Feb 7, 2026
be46de4
incorporate coderabbitai feedback and fix linter issues
taminob Feb 7, 2026
4f608e5
remove constexpr from gate constants
taminob Feb 7, 2026
e2cbf76
🎨 pre-commit fixes
pre-commit-ci[bot] Feb 7, 2026
61140da
improve comments, other minor fixes
taminob Feb 7, 2026
799ed77
add some caching for available gates and improve comments
taminob Feb 7, 2026
831628a
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Feb 7, 2026
1a1a8fc
minor comment and one const fix
taminob Feb 7, 2026
91c1f2a
fix annoying static initialization issue in gtest with Eigen
taminob Feb 7, 2026
a908ba5
switch to fully deterministic random matrix generation
taminob Feb 8, 2026
c8110be
fix potential alignment issues in SmallVector inline storage
taminob Feb 8, 2026
fae71dd
fix alignment issue for static-sized matrices on stack
taminob Feb 8, 2026
926ab1b
🎨 pre-commit fixes
pre-commit-ci[bot] Feb 8, 2026
9f264cf
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Feb 8, 2026
38e2293
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Feb 8, 2026
0c3a4cd
linter/coderabbit fixes
taminob Feb 8, 2026
e728711
remove static random generator for better reproducibility
taminob Feb 8, 2026
7d9aa33
minor fixes
taminob Feb 8, 2026
b3b4a86
🎨 pre-commit fixes
pre-commit-ci[bot] Feb 8, 2026
4ea6694
minor fixes
taminob Feb 8, 2026
310d35a
reapply thesis-evaluation (revert 8b7f0bc5640cd93093db534bf608652c02a…
taminob Feb 13, 2026
8f58cb4
evaluation script update: automated Qiskit Rust eval
taminob Feb 13, 2026
97755cf
evaluation results
taminob Feb 14, 2026
e61854f
update qiskit python results without Rust timing code
taminob Feb 14, 2026
a3c68bf
add Qiskit copyright notice to decomposition classes
taminob Feb 14, 2026
97c1187
add XZX euler basis to mqt decomposition
taminob Feb 14, 2026
0b9a10d
update evaluation results for mqt and figures
taminob Feb 14, 2026
f4b6d7f
fix evaluation graph y axis + add improved circuit calculation
taminob Feb 16, 2026
b21868f
update figures for corrected y axis
taminob Feb 16, 2026
c3d412b
Revert thesis-evaluation changes
taminob Feb 16, 2026
279a5b5
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Feb 21, 2026
8a54aa1
fix compilation issues after merge
taminob Feb 22, 2026
6c78c31
Merge remote-tracking branch 'origin/main' into taminob/mlir-two-qubi…
taminob Feb 23, 2026
d058d03
fix linter issue
taminob Feb 23, 2026
7bc9a35
remove eigen alignment limit from cmake
taminob Feb 25, 2026
044b3ff
Merge branch 'main' into HEAD
MatthiasReumann Mar 10, 2026
b88cc89
cleanup [skip ci]
taminob Mar 10, 2026
9e84295
Merge branch 'main' into taminob/mlir-two-qubit-gate-decomposition-pass
burgholzer Mar 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This project adheres to [Semantic Versioning], with the exception that minor rel

### Added

- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264], [#1402], [#1428], [#1430], [#1436], [#1443], [#1446], [#1464], [#1465], [#1470], [#1471], [#1472], [#1474], [#1475]) ([**@burgholzer**], [**@denialhaag**], [**@taminob**], [**@DRovara**], [**@li-mingbao**])
- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264], [#1206], [#1402], [#1426], [#1428], [#1430], [#1436], [#1443], [#1446], [#1464], [#1465], [#1470], [#1471], [#1472], [#1474], [#1475]) ([**@burgholzer**], [**@denialhaag**], [**@taminob**], [**@DRovara**], [**@li-mingbao**])

### Changed

Expand Down Expand Up @@ -344,6 +344,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool
[#1436]: https://github.com/munich-quantum-toolkit/core/pull/1436
[#1430]: https://github.com/munich-quantum-toolkit/core/pull/1430
[#1428]: https://github.com/munich-quantum-toolkit/core/pull/1428
[#1426]: https://github.com/munich-quantum-toolkit/core/pull/1426
[#1415]: https://github.com/munich-quantum-toolkit/core/pull/1415
[#1414]: https://github.com/munich-quantum-toolkit/core/pull/1414
[#1413]: https://github.com/munich-quantum-toolkit/core/pull/1413
Expand Down Expand Up @@ -394,6 +395,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool
[#1210]: https://github.com/munich-quantum-toolkit/core/pull/1210
[#1209]: https://github.com/munich-quantum-toolkit/core/pull/1209
[#1207]: https://github.com/munich-quantum-toolkit/core/pull/1207
[#1206]: https://github.com/munich-quantum-toolkit/core/pull/1206
[#1186]: https://github.com/munich-quantum-toolkit/core/pull/1186
[#1181]: https://github.com/munich-quantum-toolkit/core/pull/1181
[#1180]: https://github.com/munich-quantum-toolkit/core/pull/1180
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
#
# Licensed under the MIT License

add_subdirectory(Dialect)
add_subdirectory(Conversion)
add_subdirectory(Dialect)
add_subdirectory(Passes)
5 changes: 5 additions & 0 deletions mlir/include/mlir/Compiler/CompilerPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ class QuantumCompilerPipeline {
*/
static void addCleanupPasses(PassManager& pm);

/**
* @brief Add all available optimization passes
*/
static void addOptimizationPasses(PassManager& pm);

/**
* @brief Configure PassManager with diagnostic options
*
Expand Down
12 changes: 12 additions & 0 deletions mlir/include/mlir/Passes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
# All rights reserved.
#
# SPDX-License-Identifier: MIT
#
# Licensed under the MIT License

set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name QCO)
add_public_tablegen_target(QcoPassesIncGen)
add_mlir_doc(Passes QcoPasses Passes/ -gen-pass-doc)
235 changes: 235 additions & 0 deletions mlir/include/mlir/Passes/Decomposition/BasisDecomposer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
* Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
* Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
* All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* Licensed under the MIT License
*/

#pragma once

#include "EulerBasis.h"
#include "GateSequence.h"
#include "WeylDecomposition.h"

#include <Eigen/Core>
#include <array>
#include <cassert>
#include <complex>
#include <cstdint>
#include <llvm/ADT/SmallVector.h>
#include <optional>
#include <utility>

namespace mlir::qco::decomposition {

/**
* Decomposer that must be initialized with a two-qubit basis gate that will
* be used to generate a circuit equivalent to a canonical gate (RXX+RYY+RZZ).
*/
class TwoQubitBasisDecomposer {
public:
/**
* Create decomposer that allows two-qubit decompositions based on the
* specified basis gate.
* This basis gate will appear between 0 and 3 times in each decompositions.
* The order of qubits is relevant and will change the results accordingly.
* The decomposer cannot handle different basis gates in the same
* decomposition (different order of the qubits also counts as a different
* basis gate).
*/
[[nodiscard]] static TwoQubitBasisDecomposer create(const Gate& basisGate,
double basisFidelity);

/**
* Perform decomposition using the basis gate of this decomposer.
*
* @param targetDecomposition Prepared Weyl decomposition of unitary matrix
* to be decomposed.
* @param target1qEulerBases List of euler bases that should be tried out to
* find the best one for each euler decomposition.
* All bases will be mixed to get the best overall
* result.
* @param basisFidelity Fidelity for lowering the number of basis gates
* required
* @param approximate If true, use basisFidelity or, if std::nullopt, use
* basisFidelity of this decomposer. If false, fidelity
* of 1.0 will be assumed.
* @param numBasisGateUses Force use of given number of basis gates.
*/
[[nodiscard]] std::optional<TwoQubitGateSequence> twoQubitDecompose(
const decomposition::TwoQubitWeylDecomposition& targetDecomposition,
const llvm::SmallVector<EulerBasis>& target1qEulerBases,
std::optional<double> basisFidelity, bool approximate,
std::optional<std::uint8_t> numBasisGateUses) const;

protected:
// NOLINTBEGIN(modernize-pass-by-value)
/**
* Constructs decomposer instance.
*/
TwoQubitBasisDecomposer(
Gate basisGate, double basisFidelity,
const decomposition::TwoQubitWeylDecomposition& basisDecomposer,
bool isSuperControlled, const Eigen::Matrix2cd& u0l,
const Eigen::Matrix2cd& u0r, const Eigen::Matrix2cd& u1l,
const Eigen::Matrix2cd& u1ra, const Eigen::Matrix2cd& u1rb,
const Eigen::Matrix2cd& u2la, const Eigen::Matrix2cd& u2lb,
const Eigen::Matrix2cd& u2ra, const Eigen::Matrix2cd& u2rb,
const Eigen::Matrix2cd& u3l, const Eigen::Matrix2cd& u3r,
const Eigen::Matrix2cd& q0l, const Eigen::Matrix2cd& q0r,
const Eigen::Matrix2cd& q1la, const Eigen::Matrix2cd& q1lb,
const Eigen::Matrix2cd& q1ra, const Eigen::Matrix2cd& q1rb,
const Eigen::Matrix2cd& q2l, const Eigen::Matrix2cd& q2r)
: basisGate{std::move(basisGate)}, basisFidelity{basisFidelity},
basisDecomposer{basisDecomposer}, isSuperControlled{isSuperControlled},
u0l{u0l}, u0r{u0r}, u1l{u1l}, u1ra{u1ra}, u1rb{u1rb}, u2la{u2la},
u2lb{u2lb}, u2ra{u2ra}, u2rb{u2rb}, u3l{u3l}, u3r{u3r}, q0l{q0l},
q0r{q0r}, q1la{q1la}, q1lb{q1lb}, q1ra{q1ra}, q1rb{q1rb}, q2l{q2l},
q2r{q2r} {}
// NOLINTEND(modernize-pass-by-value)

/**
* Calculate decompositions when no basis gate is required.
*
* Decompose target :math:`\sim U_d(x, y, z)` with 0 uses of the
* basis gate. Result :math:`U_r` has trace:
*
* .. math::
*
* \Big\vert\text{Tr}(U_r\cdot U_\text{target}^{\dag})\Big\vert =
* 4\Big\vert (\cos(x)\cos(y)\cos(z)+ j \sin(x)\sin(y)\sin(z)\Big\vert
*
* which is optimal for all targets and bases
*
* @note The inline storage of llvm::SmallVector must be set to 0 to ensure
* correct Eigen alignment via heap allocation
*/
[[nodiscard]] static llvm::SmallVector<Eigen::Matrix2cd, 0>
decomp0(const decomposition::TwoQubitWeylDecomposition& target);

/**
* Calculate decompositions when one basis gate is required.
*
* Decompose target :math:`\sim U_d(x, y, z)` with 1 use of the
* basis gate math:`\sim U_d(a, b, c)`. Result :math:`U_r` has trace:
*
* .. math::
*
* \Big\vert\text{Tr}(U_r \cdot U_\text{target}^{\dag})\Big\vert =
* 4\Big\vert \cos(x-a)\cos(y-b)\cos(z-c) + j
* \sin(x-a)\sin(y-b)\sin(z-c)\Big\vert
*
* which is optimal for all targets and bases with ``z==0`` or ``c==0``.
*
* @note The inline storage of llvm::SmallVector must be set to 0 to ensure
* correct Eigen alignment via heap allocation
*/
[[nodiscard]] llvm::SmallVector<Eigen::Matrix2cd, 0>
decomp1(const decomposition::TwoQubitWeylDecomposition& target) const;

/**
* Calculate decompositions when two basis gates are required.
*
* Decompose target :math:`\sim U_d(x, y, z)` with 2 uses of the
* basis gate.
*
* For supercontrolled basis :math:`\sim U_d(\pi/4, b, 0)`, all b, result
* :math:`U_r` has trace
*
* .. math::
*
* \Big\vert\text{Tr}(U_r \cdot U_\text{target}^\dag) \Big\vert =
* 4\cos(z)
*
* which is the optimal approximation for basis of CNOT-class
* :math:`\sim U_d(\pi/4, 0, 0)` or DCNOT-class
* :math:`\sim U_d(\pi/4, \pi/4, 0)` and any target. It may be sub-optimal
* for :math:`b \neq 0` (i.e. there exists an exact decomposition for any
* target using :math:`B \sim U_d(\pi/4, \pi/8, 0)`, but it may not be this
* decomposition). This is an exact decomposition for supercontrolled basis
* and target :math:`\sim U_d(x, y, 0)`. No guarantees for
* non-supercontrolled basis.
*
* @note The inline storage of llvm::SmallVector must be set to 0 to ensure
* correct Eigen alignment via heap allocation
*/
[[nodiscard]] llvm::SmallVector<Eigen::Matrix2cd, 0> decomp2Supercontrolled(
const decomposition::TwoQubitWeylDecomposition& target) const;

/**
* Calculate decompositions when three basis gates are required.
*
* Decompose target with 3 uses of the basis.
*
* This is an exact decomposition for supercontrolled basis
* :math:`\sim U_d(\pi/4, b, 0)`, all b, and any target. No guarantees for
* non-supercontrolled basis.
*
* @note The inline storage of llvm::SmallVector must be set to 0 to ensure
* correct Eigen alignment via heap allocation
*/
[[nodiscard]] llvm::SmallVector<Eigen::Matrix2cd, 0> decomp3Supercontrolled(
const decomposition::TwoQubitWeylDecomposition& target) const;

/**
* Calculate traces for a combination of the parameters of the canonical
* gates of the target and basis decompositions.
* This can be used to determine the smallest number of basis gates that are
* necessary to construct an equivalent to the canonical gate.
*/
[[nodiscard]] std::array<std::complex<double>, 4>
traces(const decomposition::TwoQubitWeylDecomposition& target) const;
/**
* Decompose a single-qubit unitary matrix into a single-qubit gate
* sequence. Multiple euler bases may be specified and the one with the
* least complexity will be chosen.
*/
[[nodiscard]] static OneQubitGateSequence
unitaryToGateSequence(const Eigen::Matrix2cd& unitaryMat,
const llvm::SmallVector<EulerBasis>& targetBasisList,
QubitId /*qubit*/,
// TODO: add error map here: per qubit a mapping of
// operation to error value for better calculateError()
bool simplify, std::optional<double> atol);

[[nodiscard]] static bool relativeEq(double lhs, double rhs, double epsilon,
double maxRelative);

private:
// basis gate of this decomposer instance
Gate basisGate{};
// fidelity with which the basis gate decomposition has been calculated
double basisFidelity;
// cached decomposition for basis gate
decomposition::TwoQubitWeylDecomposition basisDecomposer;
// true if basis gate is super-controlled
bool isSuperControlled;

// pre-built components for decomposition with 3 basis gates
Eigen::Matrix2cd u0l;
Eigen::Matrix2cd u0r;
Eigen::Matrix2cd u1l;
Eigen::Matrix2cd u1ra;
Eigen::Matrix2cd u1rb;
Eigen::Matrix2cd u2la;
Eigen::Matrix2cd u2lb;
Eigen::Matrix2cd u2ra;
Eigen::Matrix2cd u2rb;
Eigen::Matrix2cd u3l;
Eigen::Matrix2cd u3r;

// pre-built components for decomposition with 2 basis gates
Eigen::Matrix2cd q0l;
Eigen::Matrix2cd q0r;
Eigen::Matrix2cd q1la;
Eigen::Matrix2cd q1lb;
Eigen::Matrix2cd q1ra;
Eigen::Matrix2cd q1rb;
Eigen::Matrix2cd q2l;
Eigen::Matrix2cd q2r;
};

} // namespace mlir::qco::decomposition
49 changes: 49 additions & 0 deletions mlir/include/mlir/Passes/Decomposition/EulerBasis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
* Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
* All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* Licensed under the MIT License
*/

#pragma once

#include "ir/operations/OpType.hpp"

#include <cstdint>
#include <llvm/ADT/SmallVector.h>

namespace mlir::qco::decomposition {
/**
* Largest number that will be assumed as zero for the euler decompositions.
*/
inline constexpr auto DEFAULT_ATOL = 1e-12;

/**
* EulerBasis for a euler decomposition.
*/
enum class EulerBasis : std::uint8_t {
U3 = 0,
U321 = 1,
U = 2,
PSX = 3,
U1X = 4,
RR = 5,
ZYZ = 6,
ZXZ = 7,
XZX = 8,
XYX = 9,
ZSXX = 10,
ZSX = 11,
};

/**
* Get list of gates potentially involved in euler basis after euler
* decomposition.
*/
[[nodiscard]] llvm::SmallVector<qc::OpType, 3>
getGateTypesForEulerBasis(EulerBasis eulerBasis);

} // namespace mlir::qco::decomposition
Loading
Loading