Skip to content

Commit

Permalink
Fix tidy warning for sparse_ham.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentmr committed Oct 23, 2023
1 parent 0045e0f commit cc66546
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
16 changes: 9 additions & 7 deletions pennylane_lightning/core/src/observables/Observables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class HamiltonianBase : public Observable<StateVectorT> {
}
};

// NOLINTBEGIN
/**
* @brief Sparse representation of SparseHamiltonian<T>
*
Expand Down Expand Up @@ -480,7 +481,7 @@ class SparseHamiltonianBase : public Observable<StateVectorT> {
*
* @param arg1 Argument to construct data
* @param arg2 Argument to construct indices
* @param arg3 Argument to construct ofsets
* @param arg3 Argument to construct offsets
* @param arg4 Argument to construct wires
*/
static auto create(std::initializer_list<ComplexT> arg1,
Expand All @@ -504,19 +505,19 @@ class SparseHamiltonianBase : public Observable<StateVectorT> {
using Pennylane::Util::operator<<;
std::ostringstream ss;
ss << "SparseHamiltonian: {\n'data' : \n";
for (const auto &d : data_)
// Note that for LGPU backend, ComplexT is std::complex as of 0.33
for (const auto &d : data_) { // Note that for LGPU backend, ComplexT is
// std::complex as of 0.33
// release Need to revisit it once we set ComplexT as cuComplex
// later
ss << "{" << d.real() << ", " << d.imag() << "},"
<< "\n";
ss << "{" << d.real() << ", " << d.imag() << "}, ";
}
ss << ",\n'indices' : \n";
for (const auto &i : indices_) {
ss << i;
ss << i << ", ";
}
ss << ",\n'offsets' : \n";
for (const auto &o : offsets_) {
ss << o;
ss << o << ", ";
}
ss << "\n}";
return ss.str();
Expand All @@ -528,5 +529,6 @@ class SparseHamiltonianBase : public Observable<StateVectorT> {
return wires_;
};
};
// NOLINTEND

} // namespace Pennylane::Observables
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ using Pennylane::Util::TestVector;

#ifdef _ENABLE_PLQUBIT
constexpr bool BACKEND_FOUND = true;
constexpr bool SPARSE_HAM_SUPPORTED = false;

#include "TestHelpersStateVectors.hpp" // TestStateVectorBackends, StateVectorToName

Expand All @@ -52,6 +53,7 @@ using namespace Pennylane::LightningQubit::Util;

#elif _ENABLE_PLKOKKOS == 1
constexpr bool BACKEND_FOUND = true;
constexpr bool SPARSE_HAM_SUPPORTED = true;

#include "TestHelpersStateVectors.hpp" // TestStateVectorBackends, StateVectorToName

Expand All @@ -63,6 +65,7 @@ using namespace Pennylane::LightningKokkos::Util;

#elif _ENABLE_PLGPU == 1
constexpr bool BACKEND_FOUND = true;
constexpr bool SPARSE_HAM_SUPPORTED = true;

#include "TestHelpersStateVectors.hpp"

Expand All @@ -74,6 +77,7 @@ using namespace Pennylane::LightningGPU::Util;

#else
constexpr bool BACKEND_FOUND = false;
constexpr bool SPARSE_HAM_SUPPORTED = false;
using TestStateVectorBackends = Pennylane::Util::TypeList<void>;

template <class StateVector> struct StateVectorToName {};
Expand Down Expand Up @@ -474,15 +478,28 @@ template <typename TypeList> void testSparseHamiltonianBase() {
const std::size_t num_qubits = 3;
std::mt19937 re{1337};

DYNAMIC_SECTION("applyInPlace must fail - "
auto sparseH = SparseHamiltonianBase<StateVectorT>::create(
{ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0},
ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0},
ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}},
{7, 6, 5, 4, 3, 2, 1, 0}, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {0, 1, 2});

DYNAMIC_SECTION("getObsName - "
<< StateVectorToName<StateVectorT>::name) {
REQUIRE(sparseH->getObsName() ==
"SparseHamiltonian: {\n"
"'data' : \n"
"{1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, "
"{1, 0}, ,\n"
"'indices' : \n"
"7, 6, 5, 4, 3, 2, 1, 0, ,\n"
"'offsets' : \n"
"0, 1, 2, 3, 4, 5, 6, 7, 8, \n"
"}");
}

auto sparseH = SparseHamiltonianBase<StateVectorT>::create(
{ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0},
ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0},
ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}},
{7, 6, 5, 4, 3, 2, 1, 0}, {0, 1, 2, 3, 4, 5, 6, 7, 8},
{0, 1, 2});
DYNAMIC_SECTION("applyInPlace must fail - "
<< StateVectorToName<StateVectorT>::name) {

auto init_state =
createRandomStateVectorData<PrecisionT>(re, num_qubits);
Expand All @@ -499,7 +516,7 @@ template <typename TypeList> void testSparseHamiltonianBase() {

TEST_CASE("Methods implemented in the SparseHamiltonianBase class",
"[SparseHamiltonianBase]") {
if constexpr (BACKEND_FOUND) {
if constexpr (SPARSE_HAM_SUPPORTED) {
testSparseHamiltonianBase<TestStateVectorBackends>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#pragma once
#include <complex>
#include <cstddef>
#include <cstdlib>
#include <string>
Expand Down Expand Up @@ -170,20 +171,28 @@ class StateVectorKokkos final
*
* @param num_qubits Number of qubits
*/
StateVectorKokkos(ComplexT *hostdata_, size_t length,
StateVectorKokkos(ComplexT *hostdata_, std::size_t length,
const Kokkos::InitializationSettings &kokkos_args = {})
: StateVectorKokkos(log2(length), kokkos_args) {
PL_ABORT_IF_NOT(isPerfectPowerOf2(length),
"The size of provided data must be a power of 2.");
HostToDevice(hostdata_, length);
}

StateVectorKokkos(std::complex<PrecisionT> *hostdata_, std::size_t length,
const Kokkos::InitializationSettings &kokkos_args = {})
: StateVectorKokkos(log2(length), kokkos_args) {
PL_ABORT_IF_NOT(isPerfectPowerOf2(length),
"The size of provided data must be a power of 2.");
HostToDevice(reinterpret_cast<ComplexT *>(hostdata_), length);
}

/**
* @brief Create a new state vector from data on the host.
*
* @param num_qubits Number of qubits
*/
StateVectorKokkos(const ComplexT *hostdata_, size_t length,
StateVectorKokkos(const ComplexT *hostdata_, std::size_t length,
const Kokkos::InitializationSettings &kokkos_args = {})
: StateVectorKokkos(log2(length), kokkos_args) {
PL_ABORT_IF_NOT(isPerfectPowerOf2(length),
Expand Down Expand Up @@ -692,7 +701,7 @@ class StateVectorKokkos final
* @param new_data data pointer to new data.
* @param new_size size of underlying data storage.
*/
void updateData(ComplexT *new_data, size_t new_size) {
void updateData(ComplexT *new_data, std::size_t new_size) {
updateData(KokkosVector(new_data, new_size));
}

Expand Down Expand Up @@ -744,15 +753,15 @@ class StateVectorKokkos final
* @brief Copy data from the host space to the device space.
*
*/
inline void HostToDevice(ComplexT *sv, size_t length) {
inline void HostToDevice(ComplexT *sv, std::size_t length) {
Kokkos::deep_copy(*data_, UnmanagedComplexHostView(sv, length));
}

/**
* @brief Copy data from the device space to the host space.
*
*/
inline void DeviceToHost(ComplexT *sv, size_t length) const {
inline void DeviceToHost(ComplexT *sv, std::size_t length) const {
Kokkos::deep_copy(UnmanagedComplexHostView(sv, length), *data_);
}

Expand Down

0 comments on commit cc66546

Please sign in to comment.