Skip to content

Commit 5b752b4

Browse files
committed
🐇 Address Rabbit's Comments
1 parent f976173 commit 5b752b4

3 files changed

Lines changed: 16 additions & 43 deletions

File tree

mlir/unittests/Dialect/QCO/Transforms/Decomposition/decomposition_test_utils.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
#pragma once
1212

13-
#include "TestCaseUtils.h"
1413
#include "mlir/Dialect/QCO/Transforms/Decomposition/Helpers.h"
15-
#include "mlir/Dialect/QCO/Transforms/Decomposition/UnitaryMatrices.h"
1614

1715
#include <Eigen/Core>
1816
#include <Eigen/QR>
@@ -23,15 +21,6 @@
2321

2422
namespace mlir::qco::decomposition_test {
2523

26-
using mqt::test::isEquivalentUpToGlobalPhase;
27-
28-
/// Standard `U3(theta, phi, lambda)` matrix. Thin wrapper over the library
29-
/// `uMatrix` so every test uses the same implementation.
30-
[[nodiscard]] inline Eigen::Matrix2cd u3Matrix(double theta, double phi,
31-
double lambda) {
32-
return decomposition::uMatrix(theta, phi, lambda);
33-
}
34-
3524
template <typename MatrixType>
3625
[[nodiscard]] MatrixType randomUnitaryMatrix(std::mt19937& rng) {
3726
static_assert(MatrixType::RowsAtCompileTime != Eigen::Dynamic &&

mlir/unittests/Dialect/QCO/Transforms/Decomposition/test_euler_decomposition.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ static std::size_t countGatesOfType(const OneQubitGateSequence& seq,
4646
/// Compare ``seq.getUnitaryMatrix()`` to ``u`` embedded on qubit 0 (4×4
4747
/// layout).
4848
static bool sequenceMatchesSingleQubitMatrix(const Eigen::Matrix2cd& u,
49-
const OneQubitGateSequence& seq,
50-
double tol = 1e-10) {
49+
const OneQubitGateSequence& seq) {
5150
const Eigen::Matrix4cd expanded = expandToTwoQubits(u, 0);
52-
return expanded.isApprox(seq.getUnitaryMatrix(), tol);
51+
return expanded.isApprox(seq.getUnitaryMatrix());
5352
}
5453

5554
// NOLINTNEXTLINE(misc-use-internal-linkage) -- gtest `TEST_P` at global scope
@@ -117,10 +116,19 @@ TEST(EulerDecompositionTest, ZyzAnglesFromUnitaryReconstructHadamard) {
117116

118117
const auto angles =
119118
EulerDecomposition::anglesFromUnitary(hadamard, EulerBasis::ZYZ);
119+
OneQubitGateSequence zyzSeq{
120+
.gates =
121+
{
122+
{.type = GateKind::RZ, .parameter = {angles[2]}},
123+
{.type = GateKind::RY, .parameter = {angles[0]}},
124+
{.type = GateKind::RZ, .parameter = {angles[1]}},
125+
},
126+
.globalPhase = angles[3],
127+
};
120128
const Eigen::Matrix2cd reconstructed =
121-
u3Matrix(angles[0], angles[1], angles[2]);
129+
EulerDecompositionTest::restore(zyzSeq);
122130

123-
EXPECT_TRUE(isEquivalentUpToGlobalPhase(hadamard, reconstructed));
131+
EXPECT_TRUE(reconstructed.isApprox(hadamard));
124132
}
125133

126134
TEST(EulerDecompositionTest, NativeEulerBasesRandomReconstruction) {
@@ -134,7 +142,7 @@ TEST(EulerDecompositionTest, NativeEulerBasesRandomReconstruction) {
134142
const double phase = angleDist(rng);
135143
const Eigen::Matrix2cd unitary =
136144
std::exp(std::complex<double>(0.0, phase)) *
137-
u3Matrix(theta, phi, lambda);
145+
decomposition::uMatrix(theta, phi, lambda);
138146
const Eigen::Matrix4cd expanded = expandToTwoQubits(unitary, 0);
139147

140148
const auto u3Seq = EulerDecomposition::generateCircuit(
@@ -144,10 +152,8 @@ TEST(EulerDecompositionTest, NativeEulerBasesRandomReconstruction) {
144152
const auto zsxxSeq = EulerDecomposition::generateCircuit(
145153
EulerBasis::ZSXX, unitary, true, std::nullopt);
146154

147-
EXPECT_TRUE(
148-
isEquivalentUpToGlobalPhase(expanded, u3Seq.getUnitaryMatrix()));
149-
EXPECT_TRUE(
150-
isEquivalentUpToGlobalPhase(expanded, zsxSeq.getUnitaryMatrix()));
155+
EXPECT_TRUE(expanded.isApprox(u3Seq.getUnitaryMatrix()));
156+
EXPECT_TRUE(expanded.isApprox(zsxSeq.getUnitaryMatrix()));
151157
EXPECT_TRUE(sequenceMatchesSingleQubitMatrix(unitary, zsxSeq));
152158
EXPECT_TRUE(sequenceMatchesSingleQubitMatrix(unitary, zsxxSeq));
153159

mlir/unittests/TestCaseUtils.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include <llvm/Support/raw_ostream.h>
1919
#include <mlir/IR/BuiltinOps.h>
2020

21-
#include <cmath>
22-
#include <complex> // NOLINT(misc-include-cleaner)
2321
#include <cstddef>
2422
#include <cstdlib>
2523
#include <string>
@@ -28,26 +26,6 @@
2826

2927
namespace mqt::test {
3028

31-
/**
32-
* Check whether two unitary matrices are equal up to a single unit-modulus
33-
* global phase factor.
34-
*
35-
* The comparison is symmetric and numerically stable in the sense that a near
36-
* zero overlap (``|trace(rhs^H * lhs)| <= atol``) is treated as "not
37-
* equivalent" to avoid division by a tiny number.
38-
*/
39-
template <typename Matrix>
40-
[[nodiscard]] bool isEquivalentUpToGlobalPhase(const Matrix& lhs,
41-
const Matrix& rhs,
42-
double atol = 1e-10) {
43-
const auto overlap = (rhs.adjoint() * lhs).trace();
44-
if (std::abs(overlap) <= atol) {
45-
return false;
46-
}
47-
const auto factor = overlap / std::abs(overlap);
48-
return lhs.isApprox(factor * rhs, atol);
49-
}
50-
5129
template <typename BuilderT> struct NamedBuilder {
5230
const char* name = nullptr;
5331
void (*fn)(BuilderT&) = nullptr;

0 commit comments

Comments
 (0)