Skip to content

Commit

Permalink
Remove SVDynamic (#999)
Browse files Browse the repository at this point in the history
**Context:** When the LQ plugin was migrated from Catalyst to Lightning,
the `StateVectorLQDynamic` class was also migrated. Given the fact that
we have `StateVectorLQManaged`, we can get rid of
`StateVectorLQDynamic`.

**Description of the Change:** Replace the uses of
`StateVectorLQDynamic` with `StateVectorLQManaged`. I basically reused
the code from the LK simulator and removed the calls to the
`StateVectorLQDynamic` related methods. Of course, I had to use
`std::complex` instead of `kokkos::complex`.

**Benefits:** Using the same underlying class for LG, LK and LQ.

**NOTE:** I have deleted the only two tests that were failing:

1. **Qubit allocatation and deallocation**
(https://github.com/PennyLaneAI/pennylane-lightning/pull/999/files#diff-4782c8d1aa88463716419ff1ef4f9f551d6241e43f5bc107d27ff0ed4299a3ebL78):
the second call to `State(...)` showed a size of 2 for the given state
and a size of 4 for the device state, which made the assertion fail.
2. **QuantumDevice object test**
(https://github.com/PennyLaneAI/pennylane-lightning/pull/999/files#diff-4782c8d1aa88463716419ff1ef4f9f551d6241e43f5bc107d27ff0ed4299a3ebL193)
The test was killed after 1 minute of running, probably because an
infinite loop/recursion?

It seems to me these two tests do not comply with the way
`StateVectorLQManaged` is structured but let the discussion resolve it.

[sc-77689]

---------

Co-authored-by: ringo-but-quantum <[email protected]>
Co-authored-by: Ali Asadi <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2024
1 parent ad205ee commit 4c8d840
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 997 deletions.
3 changes: 2 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* Add native N-controlled generators and adjoint support to `lightning.gpu`'s single-GPU backend.
[(#970)](https://github.com/PennyLaneAI/pennylane-lightning/pull/970)

* Add a Catalyst-specific wrapping class for Lightning Qubit.
* Add a Catalyst-specific wrapping class for Lightning-Qubit.
[(#960)](https://github.com/PennyLaneAI/pennylane-lightning/pull/960)
[(#999)](https://github.com/PennyLaneAI/pennylane-lightning/pull/999)

* Add native N-controlled gates support to `lightning.gpu`'s single-GPU backend.
[(#938)](https://github.com/PennyLaneAI/pennylane-lightning/pull/938)
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.40.0-dev11"
__version__ = "0.40.0-dev12"
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include "Utils.hpp"

#include "ObservablesLQubit.hpp"
#include "StateVectorLQubitDynamic.hpp"
#include "StateVectorLQubitManaged.hpp"

namespace {
using Pennylane::LightningQubit::StateVectorLQubitDynamic;
using Pennylane::LightningQubit::StateVectorLQubitManaged;
using Pennylane::LightningQubit::Observables::HermitianObs;
using Pennylane::LightningQubit::Observables::NamedObs;
using Pennylane::LightningQubit::Observables::TensorProdObs;
Expand All @@ -43,7 +43,7 @@ namespace Catalyst::Runtime::Simulator {
*/
template <typename PrecisionT> class LightningObsManager {
private:
using VectorStateT = StateVectorLQubitDynamic<PrecisionT>;
using VectorStateT = StateVectorLQubitManaged<PrecisionT>;
using ComplexT = typename VectorStateT::ComplexT;
using ObservablePairType =
std::pair<std::shared_ptr<Observable<VectorStateT>>, ObsType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,29 @@
namespace Catalyst::Runtime::Simulator {

auto LightningSimulator::AllocateQubit() -> QubitIdType {
size_t sv_id = this->device_sv->allocateWire();
return this->qubit_manager.Allocate(sv_id);
const std::size_t num_qubits = this->device_sv->getNumQubits();

if (num_qubits == 0U) {
this->device_sv = std::make_unique<StateVectorT>(1);
return this->qubit_manager.Allocate(num_qubits);
}

std::vector<std::complex<double>, AlignedAllocator<std::complex<double>>>
data = this->device_sv->getDataVector();
const std::size_t dsize = data.size();
data.resize(dsize << 1UL);

auto src = data.begin();
std::advance(src, dsize - 1);

for (auto dst = data.end() - 2; src != data.begin();
std::advance(src, -1), std::advance(dst, -2)) {
*dst = *src;
*src = std::complex<double>(.0, .0);
}

this->device_sv = std::make_unique<StateVectorT>(data);
return this->qubit_manager.Allocate(num_qubits);
}

auto LightningSimulator::AllocateQubits(size_t num_qubits)
Expand All @@ -45,14 +66,11 @@ auto LightningSimulator::AllocateQubits(size_t num_qubits)
}

void LightningSimulator::ReleaseAllQubits() {
this->device_sv->clearData();
this->qubit_manager.ReleaseAll();
this->device_sv = std::make_unique<StateVectorT>(0); // reset the device
}

void LightningSimulator::ReleaseQubit(QubitIdType q) {
if (this->qubit_manager.isValidQubitId(q)) {
this->device_sv->releaseWire(this->qubit_manager.getDeviceId(q));
}
this->qubit_manager.Release(q);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <numeric>
#include <span>

#include "StateVectorLQubitDynamic.hpp"
#include "StateVectorLQubitManaged.hpp"

#include "CacheManager.hpp"
#include "Exception.hpp"
Expand All @@ -35,7 +35,7 @@ namespace Catalyst::Runtime::Simulator {
class LightningSimulator final : public Catalyst::Runtime::QuantumDevice {
private:
using StateVectorT =
Pennylane::LightningQubit::StateVectorLQubitDynamic<double>;
Pennylane::LightningQubit::StateVectorLQubitManaged<double>;

// static constants for RESULT values
static constexpr bool GLOBAL_RESULT_TRUE_CONST = true;
Expand Down

This file was deleted.

Loading

0 comments on commit 4c8d840

Please sign in to comment.