Skip to content

Commit

Permalink
[Runtime] Build multiple Lightning simulators against the monorepo (#277
Browse files Browse the repository at this point in the history
)

**Context:**
This PR adds support for building multiple lightning simulators
(`lightning.qubit` and `lightning.kokkos` atm) against the master branch
of the Lightning monorepo. This should be merged after PR
PennyLaneAI/pennylane-lightning#497.

**Description of the Change:**
- Remove the runtime's dependency to the pl-lightning-kokkos repository
- Update function names/signatures and namespaces

[sc-43458]
  • Loading branch information
maliasadi authored Sep 14, 2023
1 parent ffdd0ab commit 7f019cd
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 92 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/build-wheel-linux-x86_64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ jobs:
fail-on-cache-miss: True

# Build Catalyst-Runtime
# TODO: remove v0.31.0 after lightning monorepo upgrade
# TODO: remove master after lightning monorepo upgrade
- name: Build Catalyst-Runtime
run: |
cmake -S runtime -B runtime-build -G Ninja \
Expand All @@ -205,8 +205,7 @@ jobs:
-DPYTHON_EXECUTABLE=$(which python${{ matrix.python_version }}) \
-Dpybind11_DIR=$(python${{ matrix.python_version }} -c "import pybind11; print(pybind11.get_cmake_dir())") \
-DENABLE_LIGHTNING_KOKKOS=ON \
-DLIGHTNING_GIT_TAG=v0.31.0 \
-DLIGHTNING_KOKKOS_GIT_TAG=latest_release \
-DLIGHTNING_GIT_TAG="master" \
-DKokkos_ENABLE_SERIAL=ON \
-DKokkos_ENABLE_OPENMP=ON \
-DENABLE_OPENQASM=ON
Expand Down
5 changes: 4 additions & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
<h3>Improvements</h3>

* Update the Lightning backend device to work with the PL-Lightning monorepo.
[(#259)](https://github.com/PennyLaneAI/catalyst/pull/259)
[(#259)](https://github.com/PennyLaneAI/catalyst/pull/259)

* Build both `"lightning.qubit"` and `"lightning.kokkos"` against the PL-Lightning monorepo.
[(#277)](https://github.com/PennyLaneAI/catalyst/pull/277)

<h3>Breaking changes</h3>

Expand Down
4 changes: 1 addition & 3 deletions runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ ENABLE_LIGHTNING_KOKKOS?=OFF
ENABLE_OPENQASM?=OFF
BUILD_QIR_STDLIB_FROM_SRC?=OFF
# TODO: Update to latest_release after v0.33.0
LIGHTNING_GIT_TAG_VALUE=master
LIGHTNING_KOKKOS_GIT_TAG_VALUE?=latest_release
LIGHTNING_GIT_TAG_VALUE= "master"
NPROC?=$(shell python -c "import os; print(os.cpu_count())")

coverage: CODE_COVERAGE=ON
Expand Down Expand Up @@ -45,7 +44,6 @@ configure:
-DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$(RT_BUILD_DIR)/lib \
-DLIGHTNING_GIT_TAG=$(LIGHTNING_GIT_TAG_VALUE) \
-DLIGHTNING_KOKKOS_GIT_TAG=$(LIGHTNING_KOKKOS_GIT_TAG_VALUE) \
-DENABLE_LIGHTNING_KOKKOS=$(ENABLE_LIGHTNING_KOKKOS) \
-DENABLE_OPENQASM=$(ENABLE_OPENQASM) \
-DCMAKE_C_COMPILER=$(C_COMPILER) \
Expand Down
22 changes: 9 additions & 13 deletions runtime/extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ FetchContent_Declare(
)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # build with -fPIC
set(ENABLE_KOKKOS OFF CACHE BOOL "Enable Kokkos")

if(ENABLE_LIGHTNING AND ENABLE_LIGHTNING_KOKKOS)
set(PL_BACKEND "lightning_qubit;lightning_kokkos" CACHE STRING "PennyLane Lightning backend")
elseif(ENABLE_LIGHTNING)
set(PL_BACKEND "lightning_qubit" CACHE STRING "PennyLane Lightning backend")
elseif(ENABLE_LIGHTNING_KOKKOS)
set(PL_BACKEND "lightning_kokkos" CACHE STRING "PennyLane Lightning backend")
endif()

set(ENABLE_PYTHON OFF CACHE BOOL "Enable compilation of the Python module")
FetchContent_MakeAvailable(pennylane_lightning)

Expand All @@ -22,19 +31,6 @@ target_include_directories(rt_interfaces INTERFACE .)

set(LIGHTNING_KOKKOS_GIT_TAG "main" CACHE STRING "GIT_TAG value to build Lightning-Kokkos")

if(ENABLE_LIGHTNING_KOKKOS)
FetchContent_Declare(
pennylane_lightning_kokkos
GIT_REPOSITORY https://github.com/PennyLaneAI/pennylane-lightning-kokkos.git
GIT_TAG ${LIGHTNING_KOKKOS_GIT_TAG}
)

set(PL_BACKEND "lightning_qubit")
set(PLKOKKOS_ENABLE_PYTHON OFF CACHE BOOL "Enable compilation of the Python bindings")
FetchContent_MakeAvailable(pennylane_lightning_kokkos)

target_link_libraries(rt_interfaces INTERFACE pennylane_lightning_kokkos)
endif()

if(ENABLE_OPENQASM)
find_package(pybind11 CONFIG)
Expand Down
4 changes: 0 additions & 4 deletions runtime/lib/backend/common/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@

#include "Types.h"

#if __has_include("StateVectorKokkos.hpp")
#include "UtilKokkos.hpp"
#endif

#if __has_include("StateVectorLQubitDynamic.hpp")
#include "Util.hpp"
#endif
Expand Down
41 changes: 23 additions & 18 deletions runtime/lib/backend/lightning-kokkos/LightningKokkosObsManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,8 @@ namespace Catalyst::Runtime::Simulator {
*/
template <typename PrecisionT> class LightningKokkosObsManager {
private:
using ObservableClassName =
Pennylane::Lightning_Kokkos::Simulators::ObservableKokkos<PrecisionT>;
using NamedObsClassName = Pennylane::Lightning_Kokkos::Simulators::NamedObsKokkos<PrecisionT>;
using HermitianObsClassName =
Pennylane::Lightning_Kokkos::Simulators::HermitianObsKokkos<PrecisionT>;
using TensorProdObsClassName =
Pennylane::Lightning_Kokkos::Simulators::TensorProdObsKokkos<PrecisionT>;
using HamiltonianClassName =
Pennylane::Lightning_Kokkos::Simulators::HamiltonianKokkos<PrecisionT>;

using VectorStateT = Pennylane::LightningKokkos::StateVectorKokkos<PrecisionT>;
using ObservableClassName = Pennylane::Observables::Observable<VectorStateT>;
using ObservablePairType = std::pair<std::shared_ptr<ObservableClassName>, ObsType>;
std::vector<ObservablePairType> observables_{};

Expand Down Expand Up @@ -105,8 +97,10 @@ template <typename PrecisionT> class LightningKokkosObsManager {
std::string(Lightning::lookup_obs<Lightning::simulator_observable_support_size>(
Lightning::simulator_observable_support, obsId));

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

Expand All @@ -120,8 +114,16 @@ template <typename PrecisionT> class LightningKokkosObsManager {
[[nodiscard]] auto createHermitianObs(const std::vector<std::complex<PrecisionT>> &matrix,
const std::vector<size_t> &wires) -> ObsIdType
{
std::vector<Kokkos::complex<PrecisionT>> matrix_k;
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<HermitianObsClassName>(HermitianObsClassName{matrix, wires}),
std::make_shared<Pennylane::LightningKokkos::Observables::HermitianObs<VectorStateT>>(
Pennylane::LightningKokkos::Observables::HermitianObs<VectorStateT>{matrix_k,
wires}),
ObsType::Basic));

return static_cast<ObsIdType>(this->observables_.size() - 1);
Expand Down Expand Up @@ -149,7 +151,9 @@ template <typename PrecisionT> class LightningKokkosObsManager {
}

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

return static_cast<ObsIdType>(obs_size);
Expand Down Expand Up @@ -182,10 +186,11 @@ template <typename PrecisionT> class LightningKokkosObsManager {
obs_vec.push_back(obs);
}

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

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

0 comments on commit 7f019cd

Please sign in to comment.