Skip to content

Commit ada5fb2

Browse files
Refactor handling of gate matrices and inverses (#752)
## Description These commits change how matrices are created from an OpType that reduces the number of switch statements necessary. The downside of this approach is that some functions can be called with arguments that do not affect the returned value. As the user never calls these functions, I see this as acceptable. I also removed the CX_MAT and CZ_MAT, as they are only used in tests and do not occur in any other repository. Fixes #484 ## Checklist: - [x] The pull request only contains commits that are related to it. - [ ] I have added appropriate tests and documentation. - [x] I have made sure that all CI jobs on GitHub pass. - [x] The pull request introduces no new warnings and follows the project's style guidelines. I have not squashed the commits to make them easier to review, but I can squash them if desired. --------- Signed-off-by: burgholzer <[email protected]> Co-authored-by: burgholzer <[email protected]>
1 parent 3f8d6e2 commit ada5fb2

15 files changed

+1003
-878
lines changed

include/mqt-core/dd/DDpackageConfig.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct DDPackageConfig {
4545
};
4646

4747
struct StochasticNoiseSimulatorDDPackageConfig : public dd::DDPackageConfig {
48-
static constexpr std::size_t STOCHASTIC_CACHE_OPS = qc::OpType::OpCount;
48+
static constexpr std::size_t STOCHASTIC_CACHE_OPS = qc::OpType::OpTypeEnd;
4949

5050
static constexpr std::size_t CT_VEC_ADD_MAG_NBUCKET = 1U;
5151
static constexpr std::size_t CT_MAT_ADD_MAG_NBUCKET = 1U;

include/mqt-core/dd/GateMatrixDefinitions.hpp

Lines changed: 6 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -10,168 +10,20 @@
1010
#pragma once
1111

1212
#include "dd/DDDefinitions.hpp"
13+
#include "ir/operations/OpType.hpp"
1314

14-
#include <cmath>
15-
#include <complex>
15+
#include <vector>
1616

1717
namespace dd {
1818

1919
// Gate matrices
20-
constexpr GateMatrix I_MAT{1, 0, 0, 1};
21-
constexpr GateMatrix H_MAT{SQRT2_2, SQRT2_2, SQRT2_2, -SQRT2_2};
22-
constexpr GateMatrix X_MAT{0, 1, 1, 0};
23-
constexpr GateMatrix Y_MAT{0, {0, -1}, {0, 1}, 0};
24-
constexpr GateMatrix Z_MAT{1, 0, 0, -1};
25-
constexpr GateMatrix S_MAT{1, 0, 0, {0, 1}};
26-
constexpr GateMatrix SDG_MAT{1, 0, 0, {0, -1}};
27-
constexpr GateMatrix T_MAT{1, 0, 0, {SQRT2_2, SQRT2_2}};
28-
constexpr GateMatrix TDG_MAT{1, 0, 0, {SQRT2_2, -SQRT2_2}};
29-
constexpr GateMatrix SX_MAT{
30-
std::complex{0.5, 0.5}, {0.5, -0.5}, {0.5, -0.5}, {0.5, 0.5}};
31-
constexpr GateMatrix SXDG_MAT{
32-
std::complex{0.5, -0.5}, {0.5, 0.5}, {0.5, 0.5}, {0.5, -0.5}};
33-
constexpr GateMatrix V_MAT{SQRT2_2, {0, -SQRT2_2}, {0, -SQRT2_2}, SQRT2_2};
34-
constexpr GateMatrix VDG_MAT{SQRT2_2, {0, SQRT2_2}, {0, SQRT2_2}, SQRT2_2};
3520
constexpr GateMatrix MEAS_ZERO_MAT{1, 0, 0, 0};
3621
constexpr GateMatrix MEAS_ONE_MAT{0, 0, 0, 1};
3722

38-
inline GateMatrix uMat(const fp lambda, const fp phi, const fp theta) {
39-
return GateMatrix{{{std::cos(theta / 2.), 0.},
40-
{-std::cos(lambda) * std::sin(theta / 2.),
41-
-std::sin(lambda) * std::sin(theta / 2.)},
42-
{std::cos(phi) * std::sin(theta / 2.),
43-
std::sin(phi) * std::sin(theta / 2.)},
44-
{std::cos(lambda + phi) * std::cos(theta / 2.),
45-
std::sin(lambda + phi) * std::cos(theta / 2.)}}};
46-
}
23+
GateMatrix opToSingleQubitGateMatrix(qc::OpType t,
24+
const std::vector<fp>& params = {});
4725

48-
inline GateMatrix u2Mat(const fp lambda, const fp phi) {
49-
return GateMatrix{
50-
SQRT2_2,
51-
{-std::cos(lambda) * SQRT2_2, -std::sin(lambda) * SQRT2_2},
52-
{std::cos(phi) * SQRT2_2, std::sin(phi) * SQRT2_2},
53-
{std::cos(lambda + phi) * SQRT2_2, std::sin(lambda + phi) * SQRT2_2}};
54-
}
26+
TwoQubitGateMatrix opToTwoQubitGateMatrix(qc::OpType t,
27+
const std::vector<fp>& params = {});
5528

56-
inline GateMatrix pMat(const fp lambda) {
57-
return GateMatrix{1, 0, 0, {std::cos(lambda), std::sin(lambda)}};
58-
}
59-
60-
inline GateMatrix rxMat(const fp lambda) {
61-
return GateMatrix{{{std::cos(lambda / 2.), 0.},
62-
{0., -std::sin(lambda / 2.)},
63-
{0., -std::sin(lambda / 2.)},
64-
{std::cos(lambda / 2.), 0.}}};
65-
}
66-
67-
inline GateMatrix ryMat(const fp lambda) {
68-
return GateMatrix{{{std::cos(lambda / 2.), 0.},
69-
{-std::sin(lambda / 2.), 0.},
70-
{std::sin(lambda / 2.), 0.},
71-
{std::cos(lambda / 2.), 0.}}};
72-
}
73-
74-
inline GateMatrix rzMat(const fp lambda) {
75-
return GateMatrix{{{std::cos(lambda / 2.), -std::sin(lambda / 2.)},
76-
0,
77-
0,
78-
{std::cos(lambda / 2.), std::sin(lambda / 2.)}}};
79-
}
80-
81-
constexpr TwoQubitGateMatrix CX_MAT{
82-
{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}}};
83-
84-
constexpr TwoQubitGateMatrix CZ_MAT{
85-
{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, -1}}};
86-
87-
constexpr TwoQubitGateMatrix SWAP_MAT{
88-
{{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}}};
89-
90-
constexpr TwoQubitGateMatrix ISWAP_MAT{
91-
{{1, 0, 0, 0}, {0, 0, {0, 1}, 0}, {0, {0, 1}, 0, 0}, {0, 0, 0, 1}}};
92-
93-
constexpr TwoQubitGateMatrix ISWAPDG_MAT{
94-
{{1, 0, 0, 0}, {0, 0, {0, -1}, 0}, {0, {0, -1}, 0, 0}, {0, 0, 0, 1}}};
95-
96-
constexpr TwoQubitGateMatrix ECR_MAT{
97-
{{0, 0, SQRT2_2, {0, SQRT2_2}},
98-
{0, 0, {0, SQRT2_2}, SQRT2_2},
99-
{SQRT2_2, {0, -SQRT2_2}, 0, 0},
100-
{std::complex{0., -SQRT2_2}, SQRT2_2, 0, 0}}};
101-
102-
constexpr TwoQubitGateMatrix DCX_MAT{
103-
{{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}, {0, 1, 0, 0}}};
104-
105-
constexpr TwoQubitGateMatrix PERES_MAT{
106-
{{0, 0, 0, 1}, {0, 0, 1, 0}, {1, 0, 0, 0}, {0, 1, 0, 0}}};
107-
108-
constexpr TwoQubitGateMatrix PERESDG_MAT{
109-
{{0, 0, 1, 0}, {0, 0, 0, 1}, {0, 1, 0, 0}, {1, 0, 0, 0}}};
110-
111-
inline TwoQubitGateMatrix rxxMat(const fp theta) {
112-
const auto cosTheta = std::cos(theta / 2.);
113-
const auto sinTheta = std::sin(theta / 2.);
114-
115-
return TwoQubitGateMatrix{{{cosTheta, 0, 0, {0., -sinTheta}},
116-
{0, cosTheta, {0., -sinTheta}, 0},
117-
{0, {0., -sinTheta}, cosTheta, 0},
118-
{std::complex{0., -sinTheta}, 0, 0, cosTheta}}};
119-
}
120-
121-
inline TwoQubitGateMatrix ryyMat(const fp theta) {
122-
const auto cosTheta = std::cos(theta / 2.);
123-
const auto sinTheta = std::sin(theta / 2.);
124-
125-
return TwoQubitGateMatrix{{{cosTheta, 0, 0, {0., sinTheta}},
126-
{0, cosTheta, {0., -sinTheta}, 0},
127-
{0, {0., -sinTheta}, cosTheta, 0},
128-
{std::complex{0., sinTheta}, 0, 0, cosTheta}}};
129-
}
130-
131-
inline TwoQubitGateMatrix rzzMat(const fp theta) {
132-
const auto cosTheta = std::cos(theta / 2.);
133-
const auto sinTheta = std::sin(theta / 2.);
134-
135-
return TwoQubitGateMatrix{{{std::complex{cosTheta, -sinTheta}, 0, 0, 0},
136-
{0, {cosTheta, sinTheta}, 0, 0},
137-
{0, 0, {cosTheta, sinTheta}, 0},
138-
{0, 0, 0, {cosTheta, -sinTheta}}}};
139-
}
140-
141-
inline TwoQubitGateMatrix rzxMat(const fp theta) {
142-
const auto cosTheta = std::cos(theta / 2.);
143-
const auto sinTheta = std::sin(theta / 2.);
144-
145-
return TwoQubitGateMatrix{{{cosTheta, {0., -sinTheta}, 0, 0},
146-
{std::complex{0., -sinTheta}, cosTheta, 0, 0},
147-
{0, 0, cosTheta, {0., sinTheta}},
148-
{0, 0, {0., sinTheta}, cosTheta}}};
149-
}
150-
151-
inline TwoQubitGateMatrix xxMinusYYMat(const fp theta, const fp beta = 0.) {
152-
const auto cosTheta = std::cos(theta / 2.);
153-
const auto sinTheta = std::sin(theta / 2.);
154-
const auto cosBeta = std::cos(beta);
155-
const auto sinBeta = std::sin(beta);
156-
157-
return TwoQubitGateMatrix{
158-
{{cosTheta, 0, 0, {-sinBeta * sinTheta, -cosBeta * sinTheta}},
159-
{0, 1, 0, 0},
160-
{0, 0, 1, 0},
161-
{std::complex{sinBeta * sinTheta, -cosBeta * sinTheta}, 0, 0,
162-
cosTheta}}};
163-
}
164-
165-
inline TwoQubitGateMatrix xxPlusYYMat(const fp theta, const fp beta = 0.) {
166-
const auto cosTheta = std::cos(theta / 2.);
167-
const auto sinTheta = std::sin(theta / 2.);
168-
const auto cosBeta = std::cos(beta);
169-
const auto sinBeta = std::sin(beta);
170-
171-
return TwoQubitGateMatrix{
172-
{{1, 0, 0, 0},
173-
{0, cosTheta, {sinBeta * sinTheta, -cosBeta * sinTheta}, 0},
174-
{0, {-sinBeta * sinTheta, -cosBeta * sinTheta}, cosTheta, 0},
175-
{0, 0, 0, 1}}};
176-
}
17729
} // namespace dd

0 commit comments

Comments
 (0)