Skip to content

Commit 869bbb8

Browse files
Add tag dispatch for memory location with LQ classes (#492)
* Add tag dispatch for memory location with LQ classes * Auto update version * Update changelog * Trigger CI * Move MemoryStorageLocation namespace to Memory.hpp * Exclude PLABORT unhittable from coverage --------- Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>
1 parent 824fd77 commit 869bbb8

File tree

8 files changed

+41
-6
lines changed

8 files changed

+41
-6
lines changed

.github/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
### Improvements
88

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

1114
### Bug fixes

pennylane_lightning/core/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
Version number (major.minor.patch[-label])
1717
"""
1818

19-
__version__ = "0.33.0-dev1"
19+
__version__ = "0.33.0-dev2"

pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubit.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ template <class PrecisionT, class Derived>
5757
class StateVectorLQubit : public StateVectorBase<PrecisionT, Derived> {
5858
public:
5959
using ComplexT = std::complex<PrecisionT>;
60+
using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::Undefined;
6061

6162
protected:
6263
const Threading threading_;

pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitManaged.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class StateVectorLQubitManaged final
5454
public:
5555
using PrecisionT = fp_t;
5656
using ComplexT = std::complex<PrecisionT>;
57+
using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::Internal;
5758

5859
private:
5960
using BaseType =

pennylane_lightning/core/src/simulators/lightning_qubit/StateVectorLQubitRaw.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class StateVectorLQubitRaw final
5656
public:
5757
using PrecisionT = fp_t;
5858
using ComplexT = std::complex<PrecisionT>;
59+
using MemoryStorageT = Pennylane::Util::MemoryStorageLocation::External;
5960

6061
private:
6162
using BaseType =

pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
/// @cond DEV
3333
namespace {
3434
using namespace Pennylane::Algorithms;
35+
using namespace Pennylane::Util::MemoryStorageLocation;
36+
3537
using Pennylane::LightningQubit::Util::innerProdC;
3638
using Pennylane::LightningQubit::Util::Transpose;
3739

@@ -256,12 +258,13 @@ class AdjointJacobian final
256258
// Pointer to data storage for StateVectorLQubitRaw<PrecisionT>:
257259
std::unique_ptr<std::vector<std::vector<ComplexT>>> H_lambda_storage;
258260
size_t lambda_qubits = lambda.getNumQubits();
259-
if constexpr (std::is_same_v<StateVectorLQubitManaged<PrecisionT>,
260-
StateVectorT>) {
261+
if constexpr (std::is_same_v<typename StateVectorT::MemoryStorageT,
262+
MemoryStorageLocation::Internal>) {
261263
H_lambda = std::make_unique<std::vector<StateVectorT>>(
262264
num_observables, StateVectorT{lambda_qubits});
263-
} else if constexpr (std::is_same_v<StateVectorLQubitRaw<PrecisionT>,
264-
StateVectorT>) {
265+
} else if constexpr (std::is_same_v<
266+
typename StateVectorT::MemoryStorageT,
267+
MemoryStorageLocation::External>) {
265268
H_lambda_storage =
266269
std::make_unique<std::vector<std::vector<ComplexT>>>(
267270
num_observables, std::vector<ComplexT>(lambda.getLength()));
@@ -273,6 +276,10 @@ class AdjointJacobian final
273276
(*H_lambda_storage)[ind].size());
274277
H_lambda->push_back(sv);
275278
}
279+
} else {
280+
/// LCOV_EXCL_START
281+
PL_ABORT("Undefined memory storage location for StateVectorT.");
282+
/// LCOV_EXCL_STOP
276283
}
277284

278285
StateVectorLQubitManaged<PrecisionT> mu(lambda_qubits);

pennylane_lightning/core/src/utils/Memory.hpp

+23
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,29 @@ bool operator!=([[maybe_unused]] const AlignedAllocator<T> &lhs,
172172
return lhs.alignment() != rhs.alignment();
173173
}
174174

175+
/**
176+
* @brief The following namespace holds compile-time tags for indicating where
177+
* statevector memory storage lives.
178+
*/
179+
namespace MemoryStorageLocation {
180+
/**
181+
* @brief Tag to indicate internal memory storage for compile-time dispatch.
182+
*
183+
*/
184+
struct Internal {};
185+
186+
/**
187+
* @brief Tag to indicate external memory storage for compile-time dispatch.
188+
*
189+
*/
190+
struct External {};
191+
/**
192+
* @brief Tag to indicate undefined memory storage for compile-time dispatch.
193+
*
194+
*/
195+
struct Undefined {};
196+
} // namespace MemoryStorageLocation
197+
175198
///@cond DEV
176199
template <class PrecisionT, class TypeList> struct commonAlignmentHelper {
177200
constexpr static size_t value = std::max(

pennylane_lightning/core/src/utils/Util.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -509,5 +509,4 @@ auto transpose_state_tensor(const std::vector<T> &tensor,
509509
}
510510
return transposed_tensor;
511511
}
512-
513512
} // namespace Pennylane::Util

0 commit comments

Comments
 (0)