Skip to content

Commit

Permalink
Fixing unittests
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
  • Loading branch information
sacpis committed Feb 4, 2025
1 parent d635ff8 commit 8c91e88
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
34 changes: 24 additions & 10 deletions unittests/dynamics/test_cudm_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,36 @@
#include <gtest/gtest.h>

// Initialize operator_sum
template <typename HandlerTy>
template <typename HandlerTy = std::complex<double>>
cudaq::operator_sum<HandlerTy> initialize_operator_sum() {
std::vector<int> degrees = {0, 1};

// Matrix operators
cudaq::matrix_operator pauli_x("pauli_x", {0});
cudaq::matrix_operator pauli_z("pauli_z", {1});
cudaq::matrix_operator identity = cudaq::matrix_operator::identity(0);
cudaq::product_operator<cudaq::matrix_operator> identity =
cudaq::matrix_operator::identity(0);

auto prod_op_1 = cudaq::scalar_operator(std::complex<double>(1.0, 0.0)) *
pauli_x * pauli_z;
std::vector<cudaq::matrix_operator> identity_vec = identity.get_terms();
auto identity_op = cudaq::product_operator<cudaq::matrix_operator>(
cudaq::scalar_operator(1.0), identity_vec);

std::vector<cudaq::matrix_operator> pauli_x_vec = {pauli_x};
auto prod_op_1 = cudaq::product_operator<cudaq::matrix_operator>(
cudaq::scalar_operator(1.0), pauli_x_vec);

std::vector<cudaq::matrix_operator> pauli_z_vec = {pauli_z};
prod_op_1 = prod_op_1 * cudaq::product_operator<cudaq::matrix_operator>(
cudaq::scalar_operator(1.0), pauli_z_vec);

auto prod_op_2 =
cudaq::scalar_operator(std::complex<double>(0.5, -0.5)) * identity;
cudaq::product_operator<cudaq::matrix_operator>(
cudaq::scalar_operator(std::complex<double>(0.5, -0.5))) *
identity_op;

cudaq::operator_sum<HandlerTy> op_sum({prod_op_1, prod_op_2});
std::vector<cudaq::product_operator<cudaq::matrix_operator>> terms = {
prod_op_1, prod_op_2};
cudaq::operator_sum<cudaq::product_operator> op_sum(terms);

return op_sum;
}
Expand Down Expand Up @@ -89,10 +103,10 @@ TEST_F(CuDensityMatTestFixture, ComputeLindbladOp) {
TEST_F(CuDensityMatTestFixture, ConvertToCuDensityMatOperator) {
std::vector<int64_t> mode_extents = {2, 2};

auto op_sum = initialize_operator_sum<void>();
auto op_sum = initialize_operator_sum<std::complex<double>>();

EXPECT_NO_THROW({
auto result = cudaq::convert_to_cudensitymat_operator<void>(
auto result = cudaq::convert_to_cudensitymat_operator<std::complex<double>>(
handle, {}, op_sum, mode_extents);
ASSERT_NE(result, nullptr);
cudensitymatDestroyOperator(result);
Expand All @@ -104,9 +118,9 @@ TEST_F(CuDensityMatTestFixture, InvalidHandle) {
cudensitymatHandle_t invalid_handle = nullptr;

std::vector<int64_t> mode_extents = {2, 2};
auto op_sum = initialize_operator_sum<void>();
auto op_sum = initialize_operator_sum<std::complex<double>>();

EXPECT_THROW(cudaq::convert_to_cudensitymat_operator<void>(
EXPECT_THROW(cudaq::convert_to_cudensitymat_operator<std::complex<double>>(
invalid_handle, {}, op_sum, mode_extents),
std::runtime_error);
}
35 changes: 21 additions & 14 deletions unittests/dynamics/test_runge_kutta_integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RungeKuttaIntegratorTest : public ::testing::Test {
cudensitymatHandle_t handle_;
cudensitymatOperator_t liouvillian_;
std::shared_ptr<cudm_time_stepper> time_stepper_;
std::unique_ptr<runge_kutta_integrator<void>> integrator_;
std::unique_ptr<runge_kutta_integrator<std::complex<double>>> integrator_;
std::unique_ptr<cudm_state> state_;

void SetUp() override {
Expand All @@ -41,9 +41,10 @@ class RungeKuttaIntegratorTest : public ::testing::Test {

double t0 = 0.0;
// Initialize the integrator (using substeps = 4, for Runge-Kutta method)
ASSERT_NO_THROW(integrator_ =
std::make_unique<runge_kutta_integrator<void>>(
std::move(*state_), t0, time_stepper_, 4));
ASSERT_NO_THROW(
integrator_ =
std::make_unique<runge_kutta_integrator<std::complex<double>>>(
std::move(*state_), t0, time_stepper_, 4));
ASSERT_NE(integrator_, nullptr);
}

Expand All @@ -61,18 +62,22 @@ TEST_F(RungeKuttaIntegratorTest, Initialization) {

// Integration with Euler Method (substeps = 1)
TEST_F(RungeKuttaIntegratorTest, EulerIntegration) {
auto eulerIntegrator = std::make_unique<runge_kutta_integrator<void>>(
cudm_state(handle_, mock_initial_state_data(), mock_hilbert_space_dims()),
0.0, time_stepper_, 1);
auto eulerIntegrator =
std::make_unique<runge_kutta_integrator<std::complex<double>>>(
cudm_state(handle_, mock_initial_state_data(),
mock_hilbert_space_dims()),
0.0, time_stepper_, 1);
eulerIntegrator->set_option("dt", 0.1);
EXPECT_NO_THROW(eulerIntegrator->integrate(1.0));
}

// Integration with Midpoint Rule (substeps = 2)
TEST_F(RungeKuttaIntegratorTest, MidpointIntegration) {
auto midpointIntegrator = std::make_unique<runge_kutta_integrator<void>>(
cudm_state(handle_, mock_initial_state_data(), mock_hilbert_space_dims()),
0.0, time_stepper_, 2);
auto midpointIntegrator =
std::make_unique<runge_kutta_integrator<std::complex<double>>>(
cudm_state(handle_, mock_initial_state_data(),
mock_hilbert_space_dims()),
0.0, time_stepper_, 2);
midpointIntegrator->set_option("dt", 0.1);
EXPECT_NO_THROW(midpointIntegrator->integrate(1.0));
}
Expand Down Expand Up @@ -110,9 +115,11 @@ TEST_F(RungeKuttaIntegratorTest, MultipleIntegrationSteps) {

// Missing Time Step (dt)
TEST_F(RungeKuttaIntegratorTest, MissingTimeStepOption) {
auto integrator_missing_dt = std::make_unique<runge_kutta_integrator<void>>(
cudm_state(handle_, mock_initial_state_data(), mock_hilbert_space_dims()),
0.0, time_stepper_, 2);
auto integrator_missing_dt =
std::make_unique<runge_kutta_integrator<std::complex<double>>>(
cudm_state(handle_, mock_initial_state_data(),
mock_hilbert_space_dims()),
0.0, time_stepper_, 2);

EXPECT_THROW(integrator_missing_dt->integrate(1.0), std::invalid_argument);
}
Expand Down Expand Up @@ -142,7 +149,7 @@ TEST_F(RungeKuttaIntegratorTest, LargeTimeStep) {

// Invalid Substeps
TEST_F(RungeKuttaIntegratorTest, InvalidSubsteps) {
EXPECT_THROW(std::make_unique<runge_kutta_integrator<void>>(
EXPECT_THROW(std::make_unique<runge_kutta_integrator<std::complex<double>>>(
cudm_state(handle_, mock_initial_state_data(),
mock_hilbert_space_dims()),
0.0, time_stepper_, 3),
Expand Down

0 comments on commit 8c91e88

Please sign in to comment.