Skip to content

Commit bacf6ba

Browse files
Fixed negative globalphase, added all tests
1 parent 7f8e302 commit bacf6ba

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/algorithms/StatePreparation.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ gatesToUncompute(std::vector<std::complex<double>>& amplitudes,
259259
}
260260
}
261261
// adjust global phase according to the last e^(it)
262-
double const arg = -std::arg(std::accumulate(
263-
amplitudes.begin(), amplitudes.end(), std::complex<double>(0, 0)));
262+
double const arg = std::arg(std::accumulate(
263+
amplitudes.begin(), amplitudes.end(), std::complex<double>(0, 0)));
264264
if (std::abs(arg) > eps) {
265265
disentangler.gphase(arg);
266266
}

test/algorithms/test_statepreparation.cpp

+29-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <string>
2323
#include <vector>
2424

25+
constexpr double EPS = 1e-10;
26+
2527
class StatePreparation
2628
: public testing::TestWithParam<std::vector<std::complex<double>>> {
2729
protected:
@@ -33,11 +35,29 @@ class StatePreparation
3335

3436
INSTANTIATE_TEST_SUITE_P(
3537
StatePreparation, StatePreparation,
36-
testing::Values(std::vector{std::complex{1 / std::sqrt(2)},
37-
std::complex{-1 / std::sqrt(2)}},
38-
std::vector<std::complex<double>>{
39-
0, std::complex{1 / std::sqrt(2)},
40-
std::complex{-1 / std::sqrt(2)}, 0}));
38+
testing::Values(
39+
std::vector{std::complex{1 / std::sqrt(2)},
40+
std::complex{-1 / std::sqrt(2)}},
41+
std::vector<std::complex<double>>{
42+
std::complex<double>{1 / std::sqrt(2)},
43+
std::complex<double>{0, -1 / std::sqrt(2)}},
44+
std::vector<std::complex<double>>{0, std::complex{1 / std::sqrt(2)},
45+
std::complex{-1 / std::sqrt(2)}, 0},
46+
std::vector<std::complex<double>>{
47+
std::complex<double>{1 / std::sqrt(13)},
48+
std::complex<double>{-1 / std::sqrt(13)},
49+
std::complex<double>{1 / std::sqrt(13), -1 / std::sqrt(13)},
50+
std::complex<double>{0, 3 / std::sqrt(13)}},
51+
std::vector<std::complex<double>>{
52+
std::complex<double>{1. / 4}, std::complex<double>{1. / 4},
53+
std::complex<double>{1. / 4}, std::complex<double>{1. / 4},
54+
std::complex<double>{1. / 4}, std::complex<double>{1. / 4},
55+
std::complex<double>{1. / 4}, std::complex<double>{3. / 4}},
56+
std::vector<std::complex<double>>{
57+
std::complex<double>{1. / 4}, std::complex<double>{0, 1. / 4},
58+
std::complex<double>{1. / 4}, std::complex<double>{0, 1. / 4},
59+
std::complex<double>{0, 1. / 4}, std::complex<double>{1. / 4},
60+
std::complex<double>{1. / 4}, std::complex<double>{3. / 4}}));
4161

4262
TEST_P(StatePreparation, StatePreparationCircuitSimulation) {
4363
const auto expectedAmplitudes = GetParam();
@@ -48,7 +68,10 @@ TEST_P(StatePreparation, StatePreparationCircuitSimulation) {
4868
ASSERT_NO_THROW(
4969
{ e = dd::simulate(qc, dd->makeZeroState(qc.getNqubits()), *dd); });
5070
auto result = e.getVector();
51-
ASSERT_EQ(expectedAmplitudes, result);
71+
for (size_t i = 0; i < expectedAmplitudes.size(); ++i) {
72+
ASSERT_NEAR(expectedAmplitudes[i].real(), result[i].real(), EPS);
73+
ASSERT_NEAR(expectedAmplitudes[i].imag(), result[i].imag(), EPS);
74+
}
5275
}
5376

5477
TEST_P(StatePreparation, StatePreparationCircuit) {}

0 commit comments

Comments
 (0)