Skip to content

Commit

Permalink
Move functions to base class (#841)
Browse files Browse the repository at this point in the history
**Context:** Some of these functions are needed in Catalyst. Since
Catalyst does not use LM, but instead uses LightningDynamic, the least
upper bound in the class hierarchy is `StateVectorLQubit`.

**Description of the Change:** Move these functions to
`StateVetctorLQubit`.

**Benefits:** Code-reuse

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: ringo-but-quantum <[email protected]>
Co-authored-by: Vincent Michaud-Rioux <[email protected]>
Co-authored-by: Ali Asadi <[email protected]>
  • Loading branch information
4 people authored Aug 9, 2024
1 parent 8f517d2 commit 3993e0b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
* Add `initial_state_prep` option to Catalyst TOML file.
[(#826)](https://github.com/PennyLaneAI/pennylane-lightning/pull/826)

* Move `setBasisState`, `setStateVector` and `resetStateVector` from `StateVectorLQubitManaged` to `StateVectorLQubit`.
[(#841)](https://github.com/PennyLaneAI/pennylane-lightning/pull/841)

### Documentation

* Updated the README and added citation format for Lightning arxiv preprint.
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.38.0-dev26"
__version__ = "0.38.0-dev27"
Original file line number Diff line number Diff line change
Expand Up @@ -687,5 +687,50 @@ class StateVectorLQubit : public StateVectorBase<PrecisionT, Derived> {
arr[k] *= inv_norm;
}
}

/**
* @brief Prepares a single computational basis state.
*
* @param index Index of the target element.
*/
void setBasisState(const std::size_t index) {
auto length = this->getLength();
PL_ABORT_IF(index > length - 1, "Invalid index");

auto *arr = this->getData();
std::fill(arr, arr + length, 0.0);
arr[index] = {1.0, 0.0};
}

/**
* @brief Set values for a batch of elements of the state-vector.
*
* @param values Values to be set for the target elements.
* @param indices Indices of the target elements.
*/
void setStateVector(const std::vector<std::size_t> &indices,
const std::vector<ComplexT> &values) {
auto num_indices = indices.size();
PL_ABORT_IF(num_indices != values.size(),
"Indices and values length must match");

auto *arr = this->getData();
auto length = this->getLength();
std::fill(arr, arr + length, 0.0);
for (std::size_t i = 0; i < num_indices; i++) {
PL_ABORT_IF(i >= length, "Invalid index");
arr[indices[i]] = values[i];
}
}

/**
* @brief Reset the data back to the \f$\ket{0}\f$ state.
*
*/
void resetStateVector() {
if (this->getLength()) {
setBasisState(0U);
}
}
};
} // namespace Pennylane::LightningQubit
Original file line number Diff line number Diff line change
Expand Up @@ -140,39 +140,6 @@ class StateVectorLQubitManaged final

~StateVectorLQubitManaged() = default;

/**
* @brief Prepares a single computational basis state.
*
* @param index Index of the target element.
*/
void setBasisState(const std::size_t index) {
std::fill(data_.begin(), data_.end(), 0.0);
data_[index] = {1.0, 0.0};
}

/**
* @brief Set values for a batch of elements of the state-vector.
*
* @param values Values to be set for the target elements.
* @param indices Indices of the target elements.
*/
void setStateVector(const std::vector<std::size_t> &indices,
const std::vector<ComplexT> &values) {
for (std::size_t n = 0; n < indices.size(); n++) {
data_[indices[n]] = values[n];
}
}

/**
* @brief Reset the data back to the \f$\ket{0}\f$ state.
*
*/
void resetStateVector() {
if (this->getLength() > 0) {
setBasisState(0U);
}
}

[[nodiscard]] auto getData() -> ComplexT * { return data_.data(); }

[[nodiscard]] auto getData() const -> const ComplexT * {
Expand Down Expand Up @@ -218,4 +185,4 @@ class StateVectorLQubitManaged final
return data_.get_allocator();
}
};
} // namespace Pennylane::LightningQubit
} // namespace Pennylane::LightningQubit

0 comments on commit 3993e0b

Please sign in to comment.