Skip to content

Commit

Permalink
Merge branch '24-GPUenv' into 23-sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
KowerKoint authored Jan 4, 2024
2 parents c44330f + 8396e49 commit e24412a
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions tests/state/state_vector_test.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include <gtest/gtest.h>

#include <Eigen/Core>
#include <Kokkos_Core.hpp>
#include <numeric>
#include <cmath>
#include <state/state_vector.hpp>

#include "../test_environment.hpp"
#include "util/utility.hpp"

using CComplex = std::complex<double>;

namespace qulacs {

const double eps = 1e-12;
Expand All @@ -17,7 +18,7 @@ bool same_state(const StateVector& s1, const StateVector& s2) {
auto s2_cp = s2.amplitudes();
assert(s1.n_qubits() == s2.n_qubits());
for (UINT i = 0; i < s1.dim(); ++i) {
if (Kokkos::abs(s1_cp[i] - s2_cp[i]) > eps) return false;
if (std::abs((CComplex)s1_cp[i] - (CComplex)s2_cp[i]) > eps) return false;
}
return true;
};
Expand All @@ -38,7 +39,7 @@ TEST(StateVectorTest, ZeroNormState) {
auto state_cp = state.amplitudes();

for (UINT i = 0; i < state.dim(); ++i) {
ASSERT_EQ(state_cp[i], Complex(0, 0));
ASSERT_EQ((CComplex)state_cp[i], CComplex(0, 0));
}
}

Expand All @@ -51,9 +52,9 @@ TEST(StateVectorTest, ComputationalBasisState) {

for (UINT i = 0; i < state.dim(); ++i) {
if (i == 31) {
ASSERT_EQ(state_cp[i], Complex(1, 0));
ASSERT_EQ((CComplex)state_cp[i], CComplex(1, 0));
} else {
ASSERT_EQ(state_cp[i], Complex(0, 0));
ASSERT_EQ((CComplex)state_cp[i], CComplex(0, 0));
}
}
}
Expand Down Expand Up @@ -86,13 +87,14 @@ TEST(StateVectorTest, AddState) {
auto new_vec = state1.amplitudes();

for (UINT i = 0; i < state1.dim(); ++i) {
ASSERT_NEAR(new_vec[i].real(), vec1[i].real() + vec2[i].real(), eps);
ASSERT_NEAR(new_vec[i].imag(), vec1[i].imag() + vec2[i].imag(), eps);
CComplex res = new_vec[i], val = (CComplex)vec1[i] + (CComplex)vec2[i];
ASSERT_NEAR(res.real(), val.real(), eps);
ASSERT_NEAR(res.imag(), val.imag(), eps);
}
}

TEST(StateVectorTest, AddStateWithCoef) {
const std::complex<double> coef(2.5, 1.3);
const CComplex coef(2.5, 1.3);
const UINT n = 10;
StateVector state1(StateVector::Haar_random_state(n));
StateVector state2(StateVector::Haar_random_state(n));
Expand All @@ -103,27 +105,25 @@ TEST(StateVectorTest, AddStateWithCoef) {
auto new_vec = state1.amplitudes();

for (UINT i = 0; i < state1.dim(); ++i) {
ASSERT_NEAR(new_vec[i].real(),
vec1[i].real() + coef.real() * vec2[i].real() - coef.imag() * vec2[i].imag(),
eps);
ASSERT_NEAR(new_vec[i].imag(),
vec1[i].imag() + coef.real() * vec2[i].imag() + coef.imag() * vec2[i].real(),
eps);
CComplex res = new_vec[i], val = (CComplex)vec1[i] + coef * (CComplex)vec2[i];
ASSERT_NEAR(res.real(), val.real(), eps);
ASSERT_NEAR(res.imag(), val.imag(), eps);
}
}

TEST(StateVectorTest, MultiplyCoef) {
const UINT n = 10;
const Complex coef(0.5, 0.2);
const CComplex coef(0.5, 0.2);

StateVector state(StateVector::Haar_random_state(n));
auto vec = state.amplitudes();
state.multiply_coef(coef);
auto new_vec = state.amplitudes();

for (UINT i = 0; i < state.dim(); ++i) {
ASSERT_NEAR(new_vec[i].real(), (coef * vec[i]).real(), eps);
ASSERT_NEAR(new_vec[i].imag(), (coef * vec[i]).imag(), eps);
CComplex res = new_vec[i], val = coef * (CComplex)vec[i];
ASSERT_NEAR(res.real(), val.real(), eps);
ASSERT_NEAR(res.imag(), val.imag(), eps);
}
}

Expand Down Expand Up @@ -154,12 +154,13 @@ TEST(StateVectorTest, EntropyCalculation) {
auto state_cp = state.amplitudes();
ASSERT_NEAR(state.compute_squared_norm(), 1, eps);
Eigen::VectorXcd test_state(dim);
for (UINT i = 0; i < dim; ++i) test_state[i] = (Complex)state_cp[i];
for (UINT i = 0; i < dim; ++i) test_state[i] = (CComplex)state_cp[i];

for (UINT target = 0; target < n; ++target) {
double ent = 0;
for (UINT ind = 0; ind < dim; ++ind) {
double prob = norm2(test_state[ind]);
CComplex z = test_state[ind];
double prob = z.real() * z.real() + z.imag() * z.imag();
if (prob > eps) ent += -prob * log(prob);
}
ASSERT_NEAR(ent, state.get_entropy(), eps);
Expand Down

0 comments on commit e24412a

Please sign in to comment.