Skip to content

Commit

Permalink
Apply clang-tidy fixes
Browse files Browse the repository at this point in the history
---------

Co-authored-by: paul0403 <[email protected]>
  • Loading branch information
rauletorresc and paul0403 committed Nov 1, 2024
1 parent ff8f10a commit be86e3e
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace Catalyst::Runtime::Simulator {
template <typename PrecisionT> class LightningObsManager {
private:
using VectorStateT = StateVectorLQubitDynamic<PrecisionT>;
using ObservablePairType =
std::pair<std::shared_ptr<Observable<VectorStateT>>, ObsType>;
using ComplexT = typename VectorStateT::ComplexT;
using ObservablePairType = std::pair<std::shared_ptr<Observable<VectorStateT>>, ObsType>;
std::vector<ObservablePairType> observables_{};

public:
Expand All @@ -68,8 +68,8 @@ template <typename PrecisionT> class LightningObsManager {
* @param obsKeys The vector of observable keys
* @return bool
*/
[[nodiscard]] auto
isValidObservables(const std::vector<ObsIdType> &obsKeys) const -> bool {
[[nodiscard]] auto isValidObservables(const std::vector<ObsIdType> &obsKeys) const -> bool
{
return std::all_of(obsKeys.begin(), obsKeys.end(), [this](auto i) {
return (i >= 0 && static_cast<size_t>(i) < observables_.size());
});
Expand All @@ -81,8 +81,8 @@ template <typename PrecisionT> class LightningObsManager {
* @param key The observable key
* @return std::shared_ptr<Observable<VectorStateT>>
*/
[[nodiscard]] auto getObservable(ObsIdType key)
-> std::shared_ptr<Observable<VectorStateT>> {
[[nodiscard]] auto getObservable(ObsIdType key) -> std::shared_ptr<Observable<VectorStateT>>
{
RT_FAIL_IF(!isValidObservables({key}), "Invalid observable key");
return std::get<0>(observables_[key]);
}
Expand All @@ -92,9 +92,7 @@ template <typename PrecisionT> class LightningObsManager {
*
* @return size_t
*/
[[nodiscard]] auto numObservables() const -> size_t {
return observables_.size();
}
[[nodiscard]] auto numObservables() const -> size_t { return observables_.size(); }

/**
* @brief Create and cache a new NamedObs instance.
Expand All @@ -103,16 +101,14 @@ template <typename PrecisionT> class LightningObsManager {
* @param wires The vector of wires the observable acts on
* @return ObsIdType
*/
[[nodiscard]] auto createNamedObs(ObsId obsId,
const std::vector<size_t> &wires)
-> ObsIdType {
auto &&obs_str = std::string(
Lightning::lookup_obs<Lightning::simulator_observable_support_size>(
[[nodiscard]] auto createNamedObs(ObsId obsId, const std::vector<size_t> &wires) -> ObsIdType
{
auto &&obs_str =
std::string(Lightning::lookup_obs<Lightning::simulator_observable_support_size>(
Lightning::simulator_observable_support, obsId));

observables_.push_back(std::make_pair(
std::make_shared<NamedObs<VectorStateT>>(obs_str, wires),
ObsType::Basic));
std::make_shared<NamedObs<VectorStateT>>(obs_str, wires), ObsType::Basic));
return static_cast<ObsIdType>(observables_.size() - 1);
}

Expand All @@ -123,13 +119,12 @@ template <typename PrecisionT> class LightningObsManager {
* @param wires The vector of wires the observable acts on
* @return ObsIdType
*/
[[nodiscard]] auto
createHermitianObs(const std::vector<std::complex<PrecisionT>> &matrix,
const std::vector<size_t> &wires) -> ObsIdType {
observables_.push_back(
std::make_pair(std::make_shared<HermitianObs<VectorStateT>>(
HermitianObs<VectorStateT>{matrix, wires}),
ObsType::Basic));
[[nodiscard]] auto createHermitianObs(const std::vector<ComplexT> &matrix,
const std::vector<size_t> &wires) -> ObsIdType
{
observables_.push_back(std::make_pair(
std::make_shared<HermitianObs<VectorStateT>>(HermitianObs<VectorStateT>{matrix, wires}),
ObsType::Basic));

return static_cast<ObsIdType>(observables_.size() - 1);
}
Expand All @@ -140,24 +135,23 @@ template <typename PrecisionT> class LightningObsManager {
* @param obsKeys The vector of observable keys
* @return ObsIdType
*/
[[nodiscard]] auto
createTensorProdObs(const std::vector<ObsIdType> &obsKeys) -> ObsIdType {
[[nodiscard]] auto createTensorProdObs(const std::vector<ObsIdType> &obsKeys) -> ObsIdType
{
const auto key_size = obsKeys.size();
const auto obs_size = observables_.size();

std::vector<std::shared_ptr<Observable<VectorStateT>>> obs_vec;
obs_vec.reserve(key_size);

for (const auto &key : obsKeys) {
RT_FAIL_IF(static_cast<size_t>(key) >= obs_size || key < 0,
"Invalid observable key");
RT_FAIL_IF(static_cast<size_t>(key) >= obs_size || key < 0, "Invalid observable key");

auto &&[obs, type] = observables_[key];
obs_vec.push_back(obs);
}

observables_.push_back(std::make_pair(
TensorProdObs<VectorStateT>::create(obs_vec), ObsType::TensorProd));
observables_.push_back(
std::make_pair(TensorProdObs<VectorStateT>::create(obs_vec), ObsType::TensorProd));

return static_cast<ObsIdType>(obs_size);
}
Expand All @@ -169,33 +163,30 @@ template <typename PrecisionT> class LightningObsManager {
* @param obsKeys The vector of observable keys
* @return ObsIdType
*/
[[nodiscard]] auto
createHamiltonianObs(const std::vector<PrecisionT> &coeffs,
const std::vector<ObsIdType> &obsKeys) -> ObsIdType {
[[nodiscard]] auto createHamiltonianObs(const std::vector<PrecisionT> &coeffs,
const std::vector<ObsIdType> &obsKeys) -> ObsIdType
{
const auto key_size = obsKeys.size();
const auto obs_size = observables_.size();

RT_FAIL_IF(
key_size != coeffs.size(),
"Incompatible list of observables and coefficients; "
"Number of observables and number of coefficients must be equal");
RT_FAIL_IF(key_size != coeffs.size(),
"Incompatible list of observables and coefficients; "
"Number of observables and number of coefficients must be equal");

std::vector<std::shared_ptr<Observable<VectorStateT>>> obs_vec;
obs_vec.reserve(key_size);

for (auto key : obsKeys) {
RT_FAIL_IF(static_cast<size_t>(key) >= obs_size || key < 0,
"Invalid observable key");
RT_FAIL_IF(static_cast<size_t>(key) >= obs_size || key < 0, "Invalid observable key");

auto &&[obs, type] = observables_[key];
obs_vec.push_back(obs);
}

observables_.push_back(std::make_pair(
std::make_shared<Pennylane::LightningQubit::Observables::
Hamiltonian<VectorStateT>>(
Pennylane::LightningQubit::Observables::Hamiltonian<
VectorStateT>(coeffs, std::move(obs_vec))),
std::make_shared<Pennylane::LightningQubit::Observables::Hamiltonian<VectorStateT>>(
Pennylane::LightningQubit::Observables::Hamiltonian<VectorStateT>(
coeffs, std::move(obs_vec))),
ObsType::Hamiltonian));

return static_cast<ObsIdType>(obs_size);
Expand Down
Loading

0 comments on commit be86e3e

Please sign in to comment.