Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Catalyst specific Lightning Kokkos class #770

Merged
merged 42 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
c659ba5
move catalyst files as is
AmintorDusko Jun 19, 2024
f1d99b3
update year
AmintorDusko Jun 19, 2024
b384b86
add a CMakeLists file to build against local and GH catalyst TAG
AmintorDusko Jun 19, 2024
84036a4
add the new catalyst directory to the build system
AmintorDusko Jun 19, 2024
341e85f
Merge branch 'master' into add/rtd_lightning_kokkos
AmintorDusko Jun 19, 2024
0f363c9
not today Ringo
AmintorDusko Jun 19, 2024
03b5b8e
empty commit
AmintorDusko Jun 19, 2024
55b50dd
update changelog
AmintorDusko Jun 19, 2024
4f1f279
format catalyst files
AmintorDusko Jun 19, 2024
48c0a2d
comment out add subdir tests
AmintorDusko Jun 19, 2024
eb75e34
update LKokkos catalyst build to make its include directories public
AmintorDusko Jun 20, 2024
ade1883
add cmake build file for LKokkos catalyst tests
AmintorDusko Jun 20, 2024
8109f4e
add runner_lightning_kokkos_catalyst
AmintorDusko Jun 20, 2024
b8a8c71
add constructibility and gates tests for LightningKokkosSimulator
AmintorDusko Jun 20, 2024
cec3ced
Auto update version from '0.37.0-dev44' to '0.37.0-dev46'
ringo-but-quantum Jun 20, 2024
263f865
Merge branch 'master' into add/rtd_lightning_kokkos
AmintorDusko Jun 20, 2024
70f3658
expand coverage
AmintorDusko Jun 21, 2024
d6ddd85
format
AmintorDusko Jun 21, 2024
cc8a855
trigger cis
AmintorDusko Jun 21, 2024
2b59c49
trigger cis
AmintorDusko Jun 21, 2024
6913d8a
add measurements tests
AmintorDusko Jun 21, 2024
8a8da4b
add structure for tests for gradients
AmintorDusko Jun 21, 2024
4a84341
Add gradient tests
maliasadi Jun 24, 2024
43f1761
Update tests
maliasadi Jun 24, 2024
4f57dbe
Remove device declarations macros
maliasadi Jun 24, 2024
899a214
Auto update version from '0.37.0-dev46' to '0.37.0-dev47'
ringo-but-quantum Jun 24, 2024
7c68454
Merge branch 'master' into add/rtd_lightning_kokkos
maliasadi Jun 24, 2024
463cca4
Update config
maliasadi Jun 24, 2024
2f9569c
trigger CIs
AmintorDusko Jun 25, 2024
068bccc
Merge branch 'add/rtd_lightning_kokkos' of https://github.com/PennyLa…
AmintorDusko Jun 25, 2024
a5423c1
Update code coverage
maliasadi Jun 25, 2024
a56a266
Merge branch 'master' into add/rtd_lightning_kokkos
AmintorDusko Jun 25, 2024
db9f17a
review suggestions
AmintorDusko Jun 26, 2024
dcb468a
Auto update version from '0.37.0-dev49' to '0.37.0-dev50'
ringo-but-quantum Jun 26, 2024
8481ea3
more review suggestions
AmintorDusko Jun 27, 2024
022aa3c
improve docstrings
AmintorDusko Jun 27, 2024
6d57152
Merge branch 'master' into add/rtd_lightning_kokkos
AmintorDusko Jun 27, 2024
44fe033
format
AmintorDusko Jun 27, 2024
221efc2
Merge branch 'add/rtd_lightning_kokkos' of https://github.com/PennyLa…
AmintorDusko Jun 27, 2024
f4db5e9
Merge branch 'master' into add/rtd_lightning_kokkos
AmintorDusko Jul 8, 2024
7555e49
add changelog
AmintorDusko Jul 8, 2024
ee1d8b5
update changelog
AmintorDusko Jul 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Breaking changes

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

### Documentation

Expand All @@ -14,6 +16,8 @@

This release contains contributions from (in alphabetical order):

Amintor Dusko

---

# Release 0.37.0
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-dev0"
__version__ = "0.38.0-dev1"
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ endif()
###############################################################################
set(COMPONENT_SUBDIRS algorithms
bindings
catalyst
gates
measurements
observables
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
cmake_minimum_required(VERSION 3.20)

project(lightning_kokkos_catalyst LANGUAGES CXX)

set(LK_CATALYST_FILES LightningKokkosSimulator.cpp CACHE INTERNAL "")
add_library(lightning_kokkos_catalyst SHARED ${LK_CATALYST_FILES})

include(FetchContent)

if(LIGHTNING_CATALYST_SRC_PATH)
if(NOT IS_ABSOLUTE ${LIGHTNING_CATALYST_SRC_PATH})
message(FATAL_ERROR " LIGHTNING_CATALYST_SRC_PATH=${LIGHTNING_CATALYST_SRC_PATH} must be set to an absolute path")
endif()
if(CATALYST_GIT_TAG)
message(WARN " Setting `LIGHTNING_CATALYST_SRC_PATH=${LIGHTNING_CATALYST_SRC_PATH}` overrides `CATALYST_GIT_TAG=${CATALYST_GIT_TAG}`")
endif()

# Acquire local git hash and use for CATALYST_GIT_TAG
execute_process(COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${LIGHTNING_CATALYST_SRC_PATH}
OUTPUT_VARIABLE CATALYST_GIT_TAG
)
message(INFO " Building against local Catalyst - path: ${LIGHTNING_CATALYST_SRC_PATH} - GIT TAG: ${CATALYST_GIT_TAG}")

target_include_directories(lightning_kokkos_catalyst PUBLIC ${LIGHTNING_CATALYST_SRC_PATH}/runtime/lib/backend/common)
target_include_directories(lightning_kokkos_catalyst PUBLIC ${LIGHTNING_CATALYST_SRC_PATH}/runtime/include)

else()
if(NOT CATALYST_GIT_TAG)
set(CATALYST_GIT_TAG "main" CACHE STRING "GIT_TAG value to build Catalyst")
endif()
message(INFO " Building against Catalyst GIT TAG ${CATALYST_GIT_TAG}")

# Fetching /lib/backend/common hpp headers
set(LIB_BACKEND_COMMON_HEADERS CacheManager.hpp
QubitManager.hpp
Utils.hpp
)
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved

foreach(HEADER ${LIB_BACKEND_COMMON_HEADERS})
string(REGEX REPLACE "\\.[^.]*$" "" HEADER_NAME ${HEADER})
FetchContent_Declare(
${HEADER_NAME}
URL https://raw.githubusercontent.com/PennyLaneAI/catalyst/${CATALYST_GIT_TAG}/runtime/lib/backend/common/${HEADER}
DOWNLOAD_NO_EXTRACT True
SOURCE_DIR include
)
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved

FetchContent_MakeAvailable(${HEADER_NAME})
endforeach()

# Fetching include hpp headers
set(INCLUDE_HEADERS DataView.hpp
Exception.hpp
QuantumDevice.hpp
RuntimeCAPI.h
Types.h
)
maliasadi marked this conversation as resolved.
Show resolved Hide resolved

foreach(HEADER ${INCLUDE_HEADERS})
string(REGEX REPLACE "\\.[^.]*$" "" HEADER_NAME ${HEADER})
FetchContent_Declare(
${HEADER_NAME}
URL https://raw.githubusercontent.com/PennyLaneAI/catalyst/${CATALYST_GIT_TAG}/runtime/include/${HEADER}
DOWNLOAD_NO_EXTRACT True
SOURCE_DIR include
)

FetchContent_MakeAvailable(${HEADER_NAME})
endforeach()

target_include_directories(lightning_kokkos_catalyst PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include)
mlxd marked this conversation as resolved.
Show resolved Hide resolved

endif()
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved

target_include_directories(lightning_kokkos_catalyst INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(lightning_kokkos_catalyst PUBLIC lightning_compile_options
lightning_external_libs
lightning_base
lightning_gates
lightning_utils
lightning_kokkos
lightning_kokkos_algorithms
lightning_kokkos_gates
lightning_kokkos_measurements
lightning_kokkos_utils
)

if (BUILD_TESTS)
enable_testing()
add_subdirectory("tests")
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// Copyright 2023-2024 Xanadu Quantum Technologies Inc.
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <array>
#include <stdexcept>
#include <tuple>
#include <utility>

#include "Types.h"
#include "Utils.hpp"

#include "ObservablesKokkos.hpp"

namespace Catalyst::Runtime::Simulator {
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief The LightningKokkosObsManager caches observables of a program at
* runtime and maps each one to a const unique index (`int64_t`) in the scope of
* the global context manager.
*/
template <typename PrecisionT> class LightningKokkosObsManager final {
private:
using StateVectorT =
Pennylane::LightningKokkos::StateVectorKokkos<PrecisionT>;
using ObservableT = Pennylane::Observables::Observable<StateVectorT>;
using ObservablePairType = std::pair<std::shared_ptr<ObservableT>, ObsType>;
std::vector<ObservablePairType> observables_{};

public:
LightningKokkosObsManager() = default;
~LightningKokkosObsManager() = default;
maliasadi marked this conversation as resolved.
Show resolved Hide resolved

LightningKokkosObsManager(const LightningKokkosObsManager &) = delete;
LightningKokkosObsManager &
operator=(const LightningKokkosObsManager &) = delete;
LightningKokkosObsManager(LightningKokkosObsManager &&) = delete;
LightningKokkosObsManager &operator=(LightningKokkosObsManager &&) = delete;

/**
* @brief A helper function to clear constructed observables in the program.
*/
void clear() { this->observables_.clear(); }

/**
* @brief Check the validity of observable keys.
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
*
* @param obsKeys The vector of observable keys
* @return bool
*/
[[nodiscard]] auto
isValidObservables(const std::vector<ObsIdType> &obsKeys) const -> bool {
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
return std::all_of(obsKeys.begin(), obsKeys.end(), [this](auto i) {
return (i >= 0 &&
static_cast<size_t>(i) < this->observables_.size());
});
}

/**
* @brief Get the constructed observable instance.
*
* @param key The observable key
* @return std::shared_ptr<ObservableT>
*/
[[nodiscard]] auto getObservable(ObsIdType key)
-> std::shared_ptr<ObservableT> {
RT_FAIL_IF(!this->isValidObservables({key}), "Invalid observable key");
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
return std::get<0>(this->observables_[key]);
}

/**
* @brief Get the number of observables.
*
* @return size_t
*/
[[nodiscard]] auto numObservables() const -> size_t {
return this->observables_.size();
}

/**
* @brief Create and cache a new NamedObs instance.
*
* @param obsId The named observable id of type ObsId
* @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>(
Lightning::simulator_observable_support, obsId));
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved

this->observables_.push_back(std::make_pair(
std::make_shared<Pennylane::LightningKokkos::Observables::NamedObs<
StateVectorT>>(obs_str, wires),
ObsType::Basic));
return static_cast<ObsIdType>(this->observables_.size() - 1);
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @brief Create and cache a new HermitianObs instance.
*
* @param matrix The row-wise Hermitian matrix
* @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 {
std::vector<Kokkos::complex<PrecisionT>> matrix_k;
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
matrix_k.reserve(matrix.size());
for (const auto &elem : matrix) {
matrix_k.push_back(static_cast<Kokkos::complex<PrecisionT>>(elem));
}

this->observables_.push_back(std::make_pair(
std::make_shared<Pennylane::LightningKokkos::Observables::
HermitianObs<StateVectorT>>(
Pennylane::LightningKokkos::Observables::HermitianObs<
StateVectorT>{matrix_k, wires}),
ObsType::Basic));

return static_cast<ObsIdType>(this->observables_.size() - 1);
}

/**
* @brief Create and cache a new TensorProd instance.
*
* @param obsKeys The vector of observable keys
* @return ObsIdType
*/
[[nodiscard]] auto
createTensorProdObs(const std::vector<ObsIdType> &obsKeys) -> ObsIdType {
const auto key_size = obsKeys.size();
const auto obs_size = this->observables_.size();

std::vector<std::shared_ptr<ObservableT>> 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");

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

this->observables_.push_back(std::make_pair(
Pennylane::LightningKokkos::Observables::TensorProdObs<
StateVectorT>::create(obs_vec),
ObsType::TensorProd));

return static_cast<ObsIdType>(obs_size);
}

/**
* @brief Create and cache a new HamiltonianObs instance.
*
* @param coeffs The vector of coefficients
* @param obsKeys The vector of observable keys
* @return 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 = this->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");

std::vector<std::shared_ptr<ObservableT>> 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");
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved

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

this->observables_.push_back(std::make_pair(
std::make_shared<Pennylane::LightningKokkos::Observables::
Hamiltonian<StateVectorT>>(
Pennylane::LightningKokkos::Observables::Hamiltonian<
StateVectorT>(coeffs, std::move(obs_vec))),
ObsType::Hamiltonian));

return static_cast<ObsIdType>(obs_size);
}
};
} // namespace Catalyst::Runtime::Simulator
Loading
Loading