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

Integrate LGPU to Catalyst #928

Merged
merged 108 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 89 commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
72ad109
initial commit
multiphaseCFD Oct 1, 2024
ffbb5ea
Auto update version from '0.39.0-dev33' to '0.39.0-dev35'
ringo-but-quantum Oct 1, 2024
73308b4
initial commit
multiphaseCFD Oct 1, 2024
573b52b
Auto update version from '0.39.0-dev34' to '0.39.0-dev35'
ringo-but-quantum Oct 1, 2024
e475f26
add changelog
multiphaseCFD Oct 1, 2024
3ae3f25
remove non-necessary state data copy
multiphaseCFD Oct 1, 2024
c2b9a70
Trigger CIs
multiphaseCFD Oct 1, 2024
7cfdcb9
update docstring
multiphaseCFD Oct 1, 2024
16d00d9
Uniform Python format to adhere PL style; Always decompose qml.QFT in…
maliasadi Oct 1, 2024
d2d23e8
initial commit
multiphaseCFD Oct 1, 2024
94e0382
Auto update version from '0.39.0-dev34' to '0.39.0-dev35'
ringo-but-quantum Oct 1, 2024
df26bf0
add changelog
multiphaseCFD Oct 1, 2024
210e316
remove non-necessary state data copy
multiphaseCFD Oct 1, 2024
4816bc6
Trigger CIs
multiphaseCFD Oct 1, 2024
5ec6b1c
update docstring
multiphaseCFD Oct 1, 2024
b8cd68d
initial commit
multiphaseCFD Oct 2, 2024
8dec8b2
Merge branch 'direct_setbasisstate_lgpu' into add_LGPUSIM
multiphaseCFD Oct 2, 2024
ee65ad3
quick fix
multiphaseCFD Oct 2, 2024
29cf4f0
Initial commit
multiphaseCFD Oct 2, 2024
0c449bf
Auto update version from '0.39.0-dev34' to '0.39.0-dev36'
ringo-but-quantum Oct 2, 2024
dfb09be
Merge branch 'add_collapse_lgpu' into add_LGPUSIM
multiphaseCFD Oct 2, 2024
b2ee7d3
Merge branch 'master' into add_LGPUSIM
multiphaseCFD Oct 2, 2024
9723b5a
Auto update version from '0.39.0-dev35' to '0.39.0-dev36'
ringo-but-quantum Oct 2, 2024
ddc5191
renaming LK->LGPU
multiphaseCFD Oct 2, 2024
4cff264
renaming
multiphaseCFD Oct 2, 2024
224aa46
add initSV to ctor
multiphaseCFD Oct 2, 2024
40439d7
quick fix for applyMatrix
multiphaseCFD Oct 3, 2024
628bc27
epsilon to margin
multiphaseCFD Oct 3, 2024
9c0f794
Auto update version from '0.39.0-dev36' to '0.39.0-dev39'
ringo-but-quantum Oct 3, 2024
89af074
Merge branch 'master' into add_LGPUSIM
multiphaseCFD Oct 7, 2024
100e91d
Auto update version from '0.39.0-dev39' to '0.39.0-dev40'
ringo-but-quantum Oct 7, 2024
0e79e64
tidy up set basis state
multiphaseCFD Oct 7, 2024
8c7ce52
update lgpu toml
multiphaseCFD Oct 7, 2024
1582064
Merge branch 'master' into add_collapse_lgpu
multiphaseCFD Oct 7, 2024
6c36e5c
Auto update version from '0.39.0-dev39' to '0.39.0-dev40'
ringo-but-quantum Oct 7, 2024
80c3a62
tidy up code and add mpi support
multiphaseCFD Oct 8, 2024
b2a3758
Auto update version from '0.39.0-dev40' to '0.39.0-dev41'
ringo-but-quantum Oct 8, 2024
960dc84
Add native `setStateVector` support to `lightning.gpu` (#930)
multiphaseCFD Oct 8, 2024
c7ea1a8
Auto update version from '0.39.0-dev40' to '0.39.0-dev41'
ringo-but-quantum Oct 8, 2024
208782f
Merge branch 'master' into add_collapse_lgpu
multiphaseCFD Oct 8, 2024
829f8eb
Auto update version from '0.39.0-dev40' to '0.39.0-dev41'
ringo-but-quantum Oct 8, 2024
2493260
update collapse with custatevec apis
multiphaseCFD Oct 8, 2024
4238aeb
Auto update version from '0.39.0-dev41' to '0.39.0-dev42'
ringo-but-quantum Oct 8, 2024
15c7e2f
add python layer
multiphaseCFD Oct 8, 2024
6dc7c03
update changelog
multiphaseCFD Oct 8, 2024
5294b59
Merge branch 'master' into add_collapse_lgpu
multiphaseCFD Oct 8, 2024
a57b5ee
Auto update version from '0.39.0-dev41' to '0.39.0-dev42'
ringo-but-quantum Oct 8, 2024
d608f06
make format
multiphaseCFD Oct 9, 2024
63facdd
drop mpi support for mid-measurement
multiphaseCFD Oct 9, 2024
53ec72b
make format
multiphaseCFD Oct 9, 2024
f046b66
add get_c_interface
multiphaseCFD Oct 9, 2024
924b10b
Auto update version from '0.39.0-dev40' to '0.39.0-dev42'
ringo-but-quantum Oct 9, 2024
335e2b3
Merge branch 'master' into add_LGPUSIM
multiphaseCFD Oct 9, 2024
705e5ee
Auto update version from '0.39.0-dev41' to '0.39.0-dev42'
ringo-but-quantum Oct 9, 2024
bcc5c50
tidy up code
multiphaseCFD Oct 9, 2024
48676c6
Merge branch 'add_collapse_lgpu' into add_LGPUSIM
multiphaseCFD Oct 9, 2024
149fb42
revert some changes in c++ unit tests
multiphaseCFD Oct 9, 2024
54d29c6
revert concepts
multiphaseCFD Oct 9, 2024
73400bb
initial commit
multiphaseCFD Oct 9, 2024
66f65b8
Auto update version from '0.39.0-dev41' to '0.39.0-dev42'
ringo-but-quantum Oct 9, 2024
e753fc4
update changelog
multiphaseCFD Oct 9, 2024
3750b5a
fix codecov
multiphaseCFD Oct 9, 2024
190708d
Merge branch 'update_LGPU_probs' into add_LGPUSIM
multiphaseCFD Oct 9, 2024
7d82d15
add unit tests for get_c_interface
multiphaseCFD Oct 10, 2024
410d694
test mpi support
multiphaseCFD Oct 10, 2024
7d631f5
Merge branch 'master' into add_collapse_lgpu
multiphaseCFD Oct 10, 2024
6a010f1
tidy up the code
multiphaseCFD Oct 10, 2024
7f4c97f
revert mpi support
multiphaseCFD Oct 10, 2024
85dc575
Auto update version from '0.39.0-dev42' to '0.39.0-dev43'
ringo-but-quantum Oct 10, 2024
0e9f1db
tidy up code
multiphaseCFD Oct 10, 2024
f6b5202
Merge branch 'add_collapse_lgpu' into add_LGPUSIM
multiphaseCFD Oct 10, 2024
5de5721
fix catalyst frontend gradient failures
multiphaseCFD Oct 11, 2024
1c09868
update get_c_compile
multiphaseCFD Oct 15, 2024
6baaee7
Merge branch 'master' into add_LGPUSIM
multiphaseCFD Oct 16, 2024
703fff7
revert changes in SVCudaMPI
multiphaseCFD Oct 16, 2024
20a6e1f
revert some changes
multiphaseCFD Oct 16, 2024
dbd9541
Auto update version from '0.39.0-dev44' to '0.39.0-dev45'
ringo-but-quantum Oct 16, 2024
deb88b5
tidy up cmake
multiphaseCFD Oct 16, 2024
2db52a8
apply features changes brought by PR#927
multiphaseCFD Oct 16, 2024
1f48057
add changelog
multiphaseCFD Oct 16, 2024
9fea993
tidy up the code
multiphaseCFD Oct 16, 2024
30c02e8
update setup and get_c_interface
multiphaseCFD Oct 16, 2024
d5ced06
tidy up python layer
multiphaseCFD Oct 16, 2024
184c29c
Merge branch 'master' into add_LGPUSIM
multiphaseCFD Oct 16, 2024
a36a4e8
Auto update version from '0.39.0-dev45' to '0.39.0-dev46'
ringo-but-quantum Oct 16, 2024
8e92385
Merge branch 'master' into add_LGPUSIM
multiphaseCFD Oct 21, 2024
b1d70c7
Auto update version from '0.39.0-dev47' to '0.39.0-dev48'
ringo-but-quantum Oct 21, 2024
4f57fb5
update format
multiphaseCFD Oct 21, 2024
5a1cfc4
update differentiate support
multiphaseCFD Oct 21, 2024
b683c8f
add more unit tests
multiphaseCFD Oct 21, 2024
a4a31ca
quick fix
multiphaseCFD Oct 22, 2024
184697c
update ci workflows
multiphaseCFD Oct 22, 2024
9c616cc
quick fix
multiphaseCFD Oct 22, 2024
125b464
update CIs
multiphaseCFD Oct 29, 2024
8c87569
Auto update version from '0.39.0-dev48' to '0.39.0-dev52'
ringo-but-quantum Oct 29, 2024
010997c
Merge branch 'master' into add_LGPUSIM
multiphaseCFD Oct 29, 2024
6c24a2e
Auto update version from '0.39.0-dev51' to '0.39.0-dev52'
ringo-but-quantum Oct 29, 2024
ea4ecf8
apply Ali and Amintor's suggestions
multiphaseCFD Oct 29, 2024
e74bd1a
quick test
multiphaseCFD Oct 29, 2024
0920e62
quick fix
multiphaseCFD Oct 29, 2024
b087eaa
update CIs
multiphaseCFD Oct 29, 2024
36e9cfb
move liblightning_gpu_catalyst.so to build_lightning_gpu
multiphaseCFD Oct 29, 2024
b6e308e
quick update
multiphaseCFD Oct 29, 2024
363ef61
tidy up
multiphaseCFD Oct 30, 2024
51802ee
update CI workflow
multiphaseCFD Oct 30, 2024
8480fe7
Add TODOs
multiphaseCFD Oct 30, 2024
50a806b
Merge branch 'v0.39.0_rc' into add_LGPUSIM
multiphaseCFD Oct 30, 2024
ee99627
add more todos
multiphaseCFD Oct 30, 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
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### New features since last release

* Integrate the Lightning-GPU with Catalyst.
[(#928)](https://github.com/PennyLaneAI/pennylane-lightning/pull/928)

* Add `mid-circuit measurements` support to `lightning.gpu`'s single-GPU backend.
[(#931)](https://github.com/PennyLaneAI/pennylane-lightning/pull/931)

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.39.0-dev47"
__version__ = "0.39.0-dev48"
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ endif()
###############################################################################
set(COMPONENT_SUBDIRS algorithms
bindings
catalyst
measurements
gates
observables
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
cmake_minimum_required(VERSION 3.20)

project(lightning_gpu_catalyst LANGUAGES CXX)

set(LGPU_CATALYST_FILES LightningGPUSimulator.cpp CACHE INTERNAL "")
add_library(lightning_gpu_catalyst SHARED ${LGPU_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_gpu_catalyst PUBLIC ${LIGHTNING_CATALYST_SRC_PATH}/runtime/lib/backend/common)
target_include_directories(lightning_gpu_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
)

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
)

FetchContent_MakeAvailable(${HEADER_NAME})
endforeach()

# Fetching include hpp headers
set(INCLUDE_HEADERS DataView.hpp
Exception.hpp
QuantumDevice.hpp
RuntimeCAPI.h
Types.h
)

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_gpu_catalyst PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include)

endif()

target_include_directories(lightning_gpu_catalyst INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(lightning_gpu_catalyst PUBLIC lightning_compile_options
lightning_gpu_algorithms
lightning_gpu_measurements
)

if (BUILD_TESTS)
enable_testing()
add_subdirectory("tests")
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// Copyright 2024 Xanadu Quantum Technologies Inc.

// 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 "ObservablesGPU.hpp"

namespace Catalyst::Runtime::Simulator {

/**
* @brief The LightningGPUObsManager 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 LightningGPUObsManager final {
private:
using StateVectorT =
Pennylane::LightningGPU::StateVectorCudaManaged<PrecisionT>;
using ObservableT = Pennylane::Observables::Observable<StateVectorT>;
using ObservablePairType = std::pair<std::shared_ptr<ObservableT>, ObsType>;
std::vector<ObservablePairType> observables_{};

public:
LightningGPUObsManager() = default;
~LightningGPUObsManager() = default;

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

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

/**
* @brief Check the validity of observable keys.
*
* @param obsKeys The vector of observable keys
* @return 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<std::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");
return std::get<0>(this->observables_[key]);
}

/**
* @brief Get the number of observables.
*
* @return std::size_t
*/
[[nodiscard]] auto numObservables() const -> std::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<std::size_t> &wires)
-> ObsIdType {
auto &&obs_str = 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<
Pennylane::LightningGPU::Observables::NamedObs<StateVectorT>>(
obs_str, wires),
ObsType::Basic));
return static_cast<ObsIdType>(this->observables_.size() - 1);
}

/**
* @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<std::size_t> &wires) -> ObsIdType {
std::vector<std::complex<PrecisionT>> matrix_k;
matrix_k.reserve(matrix.size());
for (const auto &elem : matrix) {
matrix_k.push_back(static_cast<std::complex<PrecisionT>>(elem));
}

this->observables_.push_back(std::make_pair(
std::make_shared<Pennylane::LightningGPU::Observables::HermitianObs<
StateVectorT>>(
Pennylane::LightningGPU::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<std::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::LightningGPU::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<std::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(
std::make_shared<Pennylane::LightningGPU::Observables::Hamiltonian<
StateVectorT>>(
Pennylane::LightningGPU::Observables::Hamiltonian<StateVectorT>(
coeffs, std::move(obs_vec))),
ObsType::Hamiltonian));

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