From 53f7d82f28abcd98a30ad7af88317013d3d6f3e9 Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Wed, 30 Aug 2023 13:12:29 -0400 Subject: [PATCH 1/6] Add tag dispatch for memory location with LQ classes --- .../lightning_qubit/StateVectorLQubit.hpp | 1 + .../StateVectorLQubitManaged.hpp | 1 + .../lightning_qubit/StateVectorLQubitRaw.hpp | 1 + .../algorithms/AdjointJacobianLQubit.hpp | 13 +++++++---- pennylane_lightning/core/src/utils/Util.hpp | 23 +++++++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubit.hpp b/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubit.hpp index 987e51092a..495c64255a 100644 --- a/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubit.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubit.hpp @@ -57,6 +57,7 @@ template class StateVectorLQubit : public StateVectorBase { public: using ComplexT = std::complex; + using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::Undefined; protected: const Threading threading_; diff --git a/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitManaged.hpp b/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitManaged.hpp index 8ec760dd8e..db3415cdb8 100644 --- a/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitManaged.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitManaged.hpp @@ -54,6 +54,7 @@ class StateVectorLQubitManaged final public: using PrecisionT = fp_t; using ComplexT = std::complex; + using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::Internal; private: using BaseType = diff --git a/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitRaw.hpp b/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitRaw.hpp index aa192d5d9e..5fd49f2be5 100644 --- a/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitRaw.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitRaw.hpp @@ -56,6 +56,7 @@ class StateVectorLQubitRaw final public: using PrecisionT = fp_t; using ComplexT = std::complex; + using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::External; private: using BaseType = diff --git a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp index 941f2e716f..75dc60d862 100644 --- a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp @@ -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; @@ -256,12 +258,13 @@ class AdjointJacobian final // Pointer to data storage for StateVectorLQubitRaw: std::unique_ptr>> H_lambda_storage; size_t lambda_qubits = lambda.getNumQubits(); - if constexpr (std::is_same_v, - StateVectorT>) { + if constexpr (std::is_same_v) { H_lambda = std::make_unique>( num_observables, StateVectorT{lambda_qubits}); - } else if constexpr (std::is_same_v, - StateVectorT>) { + } else if constexpr (std::is_same_v< + typename StateVectorT::MemoryStorageT, + MemoryStorageLocation::External>) { H_lambda_storage = std::make_unique>>( num_observables, std::vector(lambda.getLength())); @@ -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."); } StateVectorLQubitManaged mu(lambda_qubits); diff --git a/pennylane_lightning/core/src/utils/Util.hpp b/pennylane_lightning/core/src/utils/Util.hpp index 71799535d3..5d125c580a 100644 --- a/pennylane_lightning/core/src/utils/Util.hpp +++ b/pennylane_lightning/core/src/utils/Util.hpp @@ -510,4 +510,27 @@ auto transpose_state_tensor(const std::vector &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 + } // namespace Pennylane::Util \ No newline at end of file From 11b580082f2f1a6d2621b4b6233616c90c6fb64a Mon Sep 17 00:00:00 2001 From: Dev version update bot Date: Wed, 30 Aug 2023 17:13:59 +0000 Subject: [PATCH 2/6] Auto update version --- pennylane_lightning/core/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index 50fe1f6c21..e3460270f5 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.33.0-dev1" +__version__ = "0.33.0-dev2" From 2cdf441ae92d08346240a4bd3426eabca951dfa8 Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Wed, 30 Aug 2023 13:15:06 -0400 Subject: [PATCH 3/6] Update changelog --- .github/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 79637aa3cb..c6f2dafc99 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -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 From a51e908bc439b2854fcc88968890eb84489b52ab Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Wed, 30 Aug 2023 13:18:04 -0400 Subject: [PATCH 4/6] Trigger CI From f68b02dd157e6411f77f7fc721e3d2a67c961a54 Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Thu, 31 Aug 2023 16:32:55 -0400 Subject: [PATCH 5/6] Move MemoryStorageLocation namespace to Memory.hpp --- pennylane_lightning/core/src/utils/Memory.hpp | 23 ++++++++++++++++++ pennylane_lightning/core/src/utils/Util.hpp | 24 ------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/pennylane_lightning/core/src/utils/Memory.hpp b/pennylane_lightning/core/src/utils/Memory.hpp index edfb637aec..df69f133da 100644 --- a/pennylane_lightning/core/src/utils/Memory.hpp +++ b/pennylane_lightning/core/src/utils/Memory.hpp @@ -172,6 +172,29 @@ bool operator!=([[maybe_unused]] const AlignedAllocator &lhs, return lhs.alignment() != rhs.alignment(); } +/** + * @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 + ///@cond DEV template struct commonAlignmentHelper { constexpr static size_t value = std::max( diff --git a/pennylane_lightning/core/src/utils/Util.hpp b/pennylane_lightning/core/src/utils/Util.hpp index 5d125c580a..4987dfa0e0 100644 --- a/pennylane_lightning/core/src/utils/Util.hpp +++ b/pennylane_lightning/core/src/utils/Util.hpp @@ -509,28 +509,4 @@ auto transpose_state_tensor(const std::vector &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 - } // namespace Pennylane::Util \ No newline at end of file From 42d5b7d72a2cca3e1bf798fa9add8516eb4a890c Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Fri, 1 Sep 2023 12:15:13 -0400 Subject: [PATCH 6/6] Exclude PLABORT unhittable from coverage --- .../lightning_qubit/algorithms/AdjointJacobianLQubit.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp index 75dc60d862..d77dd7b1ba 100644 --- a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp @@ -277,7 +277,9 @@ class AdjointJacobian final H_lambda->push_back(sv); } } else { + /// LCOV_EXCL_START PL_ABORT("Undefined memory storage location for StateVectorT."); + /// LCOV_EXCL_STOP } StateVectorLQubitManaged mu(lambda_qubits);