Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tag dispatch for memory location with LQ classes #492

Merged
merged 7 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

### Improvements

* Add memory locality tag reporting and adjoint diff dispatch for `lightning.qubit` statevector classes.
[(#492)](https://github.com/PennyLaneAI/pennylane-lightning/pull/492)

### Documentation

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.33.0-dev1"
__version__ = "0.33.0-dev2"
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ template <class PrecisionT, class Derived>
class StateVectorLQubit : public StateVectorBase<PrecisionT, Derived> {
public:
using ComplexT = std::complex<PrecisionT>;
using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::Undefined;

protected:
const Threading threading_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class StateVectorLQubitManaged final
public:
using PrecisionT = fp_t;
using ComplexT = std::complex<PrecisionT>;
using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::Internal;

private:
using BaseType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class StateVectorLQubitRaw final
public:
using PrecisionT = fp_t;
using ComplexT = std::complex<PrecisionT>;
using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::External;

private:
using BaseType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
/// @cond DEV
namespace {
using namespace Pennylane::Algorithms;
using namespace Pennylane::Util::MemoryStorageLocation;

using Pennylane::LightningQubit::Util::innerProdC;
using Pennylane::LightningQubit::Util::Transpose;

Expand Down Expand Up @@ -256,12 +258,13 @@ class AdjointJacobian final
// Pointer to data storage for StateVectorLQubitRaw<PrecisionT>:
std::unique_ptr<std::vector<std::vector<ComplexT>>> H_lambda_storage;
size_t lambda_qubits = lambda.getNumQubits();
if constexpr (std::is_same_v<StateVectorLQubitManaged<PrecisionT>,
StateVectorT>) {
if constexpr (std::is_same_v<typename StateVectorT::MemoryStorageT,
MemoryStorageLocation::Internal>) {
H_lambda = std::make_unique<std::vector<StateVectorT>>(
num_observables, StateVectorT{lambda_qubits});
} else if constexpr (std::is_same_v<StateVectorLQubitRaw<PrecisionT>,
StateVectorT>) {
} else if constexpr (std::is_same_v<
typename StateVectorT::MemoryStorageT,
MemoryStorageLocation::External>) {
H_lambda_storage =
std::make_unique<std::vector<std::vector<ComplexT>>>(
num_observables, std::vector<ComplexT>(lambda.getLength()));
Expand All @@ -273,6 +276,8 @@ class AdjointJacobian final
(*H_lambda_storage)[ind].size());
H_lambda->push_back(sv);
}
} else {
PL_ABORT("Undefined memory storage location for StateVectorT.");
mlxd marked this conversation as resolved.
Show resolved Hide resolved
}

StateVectorLQubitManaged<PrecisionT> mu(lambda_qubits);
Expand Down
23 changes: 23 additions & 0 deletions pennylane_lightning/core/src/utils/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,27 @@ auto transpose_state_tensor(const std::vector<T> &tensor,
return transposed_tensor;
}

/**
* @brief The following namespace holds compile-time tags for indicating where
* statevector memory storage lives.
*/
namespace MemoryStorageLocation {
/**
* @brief Tag to indicate internal memory storage for compile-time dispatch.
*
*/
struct Internal {};

/**
* @brief Tag to indicate external memory storage for compile-time dispatch.
*
*/
struct External {};
/**
* @brief Tag to indicate undefined memory storage for compile-time dispatch.
*
*/
struct Undefined {};
} // namespace MemoryStorageLocation

AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
} // namespace Pennylane::Util