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

Downstream LightningSimulator C++ API to the pennylane-lightning repository #960

Merged
merged 67 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 66 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
bc743f0
Copy Catalyst plugin files for LQ
rauletorresc Oct 24, 2024
7ac9ba0
Auto update version from '0.39.0-dev48' to '0.39.0-dev49'
ringo-but-quantum Oct 24, 2024
73b5b49
Copy LQ shared object to build directory in editable mode
rauletorresc Oct 24, 2024
a5700cd
Merge branch 'master' into raultorres/catalyst_LQ
rauletorresc Oct 25, 2024
bb07e2d
Auto update version from '0.39.0-dev49' to '0.39.0-dev50'
ringo-but-quantum Oct 25, 2024
fd85efe
Update changelog
rauletorresc Oct 28, 2024
fe8ae7f
Auto update version from '0.39.0-dev50' to '0.39.0-dev51'
ringo-but-quantum Oct 28, 2024
1fcdc8f
Remove trailing space
rauletorresc Oct 28, 2024
0186e14
Merge branch 'master' into raultorres/catalyst_LQ
rauletorresc Oct 28, 2024
0224c4f
Fix format
rauletorresc Oct 28, 2024
a697803
Remove tests folder from LQ plugin
rauletorresc Oct 28, 2024
57316f3
Add tests
rauletorresc Oct 29, 2024
b70625b
Auto update version from '0.39.0-dev51' to '0.39.0-dev52'
ringo-but-quantum Oct 29, 2024
2a40629
Merge branch 'master' into raultorres/catalyst_LQ
rauletorresc Oct 29, 2024
afe9050
Add tests
rauletorresc Oct 29, 2024
497f0e3
Ignore LQ tests when using clang-tidy
rauletorresc Oct 29, 2024
8d68b02
Revert workflow changes
rauletorresc Oct 29, 2024
0d59583
Remove `python -m` to avoid running tests with relative modules
rauletorresc Oct 30, 2024
d9e471d
Use the same path for fetched headers for LK and LQ
rauletorresc Oct 30, 2024
b7f898c
Move Catalyst fetching process to a macro
rauletorresc Oct 31, 2024
4587497
Skip coverage for some errors, following LGPU implementation
rauletorresc Oct 31, 2024
ab696eb
Fix format
rauletorresc Oct 31, 2024
9c14f13
[NFC] Remove last year from copyright
rauletorresc Oct 31, 2024
ff8f10a
Update pennylane_lightning/core/src/simulators/lightning_qubit/cataly…
rauletorresc Oct 31, 2024
be86e3e
Apply clang-tidy fixes
rauletorresc Nov 1, 2024
98b5cfa
Fix format
rauletorresc Nov 1, 2024
aba3175
Apply clang-tidy to tests
rauletorresc Nov 1, 2024
8964618
More clang-tidy on tests
rauletorresc Nov 1, 2024
a6da775
Add Driver test and fetch related headers
rauletorresc Nov 3, 2024
a5e4691
Fix LQ driver test according to clang-tidy
rauletorresc Nov 3, 2024
3fb9410
Move entry from improvements to new features at changelog
rauletorresc Nov 3, 2024
ea7e644
Remove unnecessary libs for LG plugin
rauletorresc Nov 3, 2024
e8cbb25
Remove unnecessary define from header
rauletorresc Nov 3, 2024
73e13fd
temporarily cuild against a catalyst PR, to clang-tidy files in catal…
paul0403 Nov 4, 2024
79615d1
Auto update version from '0.39.0-dev52' to '0.39.0-dev53'
ringo-but-quantum Nov 4, 2024
26fcdd0
empty to trigger CI, to see if changes in catalyst CI is picked up
paul0403 Nov 4, 2024
561746e
empty to trigger CI, to see if changes in catalyst CI is picked up
paul0403 Nov 4, 2024
ad26043
Merge remote-tracking branch 'origin/master' into raultorres/catalyst_LQ
paul0403 Nov 4, 2024
070f44b
[no ci] only run clang tidy in CI
paul0403 Nov 4, 2024
e95d0fb
change back tidy.yml
paul0403 Nov 4, 2024
9c35882
CI
paul0403 Nov 5, 2024
a891960
CI
paul0403 Nov 5, 2024
6687149
CI for tidy
paul0403 Nov 5, 2024
5942451
Add tests for State Vector Dynamic
rauletorresc Nov 5, 2024
86f98cc
Fix format
rauletorresc Nov 5, 2024
e7c7a1c
Fix clang-tidy issue with signed int
rauletorresc Nov 5, 2024
85dba18
Fix format
rauletorresc Nov 5, 2024
2e55338
Use uppercase U
rauletorresc Nov 5, 2024
955e778
empty, tidy CI
paul0403 Nov 6, 2024
6304e73
Auto update version from '0.39.0-dev53' to '0.40.0-dev1'
ringo-but-quantum Nov 6, 2024
a8db7ef
Merge branch 'master' into raultorres/catalyst_LQ
rauletorresc Nov 6, 2024
1c70158
revert back to tracking catalyst main
paul0403 Nov 6, 2024
80714a6
Revert workflow changes
rauletorresc Nov 6, 2024
0cecdae
Revert "Revert workflow changes"
rauletorresc Nov 6, 2024
2ab8b25
Merge branch 'master' into raultorres/catalyst_LQ
maliasadi Nov 6, 2024
4d8bbb2
Auto update version from '0.40.0-dev1' to '0.40.0-dev2'
ringo-but-quantum Nov 6, 2024
e62b13e
Merge branch 'master' into raultorres/catalyst_LQ
rauletorresc Nov 6, 2024
a663ae1
Remove Catalyst header
rauletorresc Nov 11, 2024
2bcd395
Auto update version from '0.40.0-dev2' to '0.40.0-dev6'
ringo-but-quantum Nov 11, 2024
4e8096f
Merge branch 'master' into raultorres/catalyst_LQ
rauletorresc Nov 11, 2024
374924f
Do not build the LQ plugin for Windows
rauletorresc Nov 11, 2024
6e5de84
Auto update version from '0.40.0-dev6' to '0.40.0-dev7'
ringo-but-quantum Nov 11, 2024
df0d227
Merge branch 'master' into raultorres/catalyst_LQ
rauletorresc Nov 11, 2024
44f1978
Remove Catalyst folder dependency
rauletorresc Nov 11, 2024
4c87696
Remove unwanted Catalyst dependencies
rauletorresc Nov 12, 2024
68c4c77
Fix clang-tidy issues
rauletorresc Nov 12, 2024
d338851
Update changelog
rauletorresc Nov 12, 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
5 changes: 4 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

* Add native N-controlled gates support to `lightning.gpu`'s single-GPU backend.
[(#938)](https://github.com/PennyLaneAI/pennylane-lightning/pull/938)

* Add a Catalyst-specific wrapping class for Lightning Qubit.
rauletorresc marked this conversation as resolved.
Show resolved Hide resolved
[(#960)](https://github.com/PennyLaneAI/pennylane-lightning/pull/960)

### Breaking changes

Expand Down Expand Up @@ -35,7 +38,7 @@

This release contains contributions from (in alphabetical order):

Ali Asadi, Joseph Lee, Luis Alfredo Nuñez Meneses, Shuli Shu
Ali Asadi, Joseph Lee, Luis Alfredo Nuñez Meneses, Shuli Shu, Raul Torres, Haochen Paul Wang

---

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/tests_lqcpu_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ jobs:
- name: Run PennyLane-Lightning unit tests
run: |
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
OMP_NUM_THREADS=1 PL_DEVICE=${DEVICENAME} python -m pytest -n auto tests/ -k "not unitary_correct" \
# Remove `python -m` to avoid running tests with relative modules
OMP_NUM_THREADS=1 PL_DEVICE=${DEVICENAME} pytest -n auto tests/ -k "not unitary_correct" \
rauletorresc marked this conversation as resolved.
Show resolved Hide resolved
$COVERAGE_FLAGS --splits 4 --group ${{ matrix.group }} \
--durations-path='.github/workflows/python_lightning_qubit_test_durations.json' --splitting-algorithm=least_duration
PL_DEVICE=${DEVICENAME} python -m pytest tests/ -k "unitary_correct" $COVERAGE_FLAGS --cov-append
PL_DEVICE=${DEVICENAME} pytest tests/ -k "unitary_correct" $COVERAGE_FLAGS --cov-append
pl-device-test --device ${DEVICENAME} --skip-ops --shots=20000 $COVERAGE_FLAGS --cov-append
pl-device-test --device ${DEVICENAME} --shots=None --skip-ops $COVERAGE_FLAGS --cov-append
mv .coverage ${{ github.workspace }}/.coverage-${{ github.job }}-${{ matrix.pl_backend }}-${{ matrix.blas }}-${{ matrix.group }}
Expand Down
6 changes: 3 additions & 3 deletions cmake/support_catalyst.cmake
rauletorresc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro(FindCatalyst target_name)
${HEADER_NAME}
URL https://raw.githubusercontent.com/PennyLaneAI/catalyst/${CATALYST_GIT_TAG}/runtime/lib/backend/common/${HEADER}
DOWNLOAD_NO_EXTRACT True
SOURCE_DIR include
SOURCE_DIR ../../include
)

FetchContent_MakeAvailable(${HEADER_NAME})
Expand All @@ -62,13 +62,13 @@ macro(FindCatalyst target_name)
${HEADER_NAME}
URL https://raw.githubusercontent.com/PennyLaneAI/catalyst/${CATALYST_GIT_TAG}/runtime/include/${HEADER}
DOWNLOAD_NO_EXTRACT True
SOURCE_DIR include
SOURCE_DIR ../../include
)

FetchContent_MakeAvailable(${HEADER_NAME})
endforeach()

target_include_directories(${target_name} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include)
target_include_directories(${target_name} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/../../include)

endif()
endmacro()
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.40.0-dev6"
__version__ = "0.40.0-dev7"
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ set(COMPONENT_SUBDIRS algorithms
utils
)

# Do not build the LQ plugin for Windows
if (NOT WIN32)
set(COMPONENT_SUBDIRS ${COMPONENT_SUBDIRS}
catalyst
)
endif()

foreach(COMP ${COMPONENT_SUBDIRS})
add_subdirectory(${COMP})
endforeach()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.20)

project(lightning_qubit_catalyst LANGUAGES CXX)

set(LQ_CATALYST_FILES LightningSimulator.cpp CACHE INTERNAL "")
add_library(lightning_qubit_catalyst SHARED ${LQ_CATALYST_FILES})

include(FetchContent)

include("${pennylane_lightning_SOURCE_DIR}/cmake/support_catalyst.cmake")
FindCatalyst(lightning_qubit_catalyst)

target_include_directories(lightning_qubit_catalyst INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(lightning_qubit_catalyst PUBLIC lightning_compile_options
lightning_external_libs
lightning_qubit_algorithms
lightning_qubit_measurements
lightning_qubit_observables
)

if (BUILD_TESTS)
enable_testing()
add_subdirectory("tests")
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Copyright 2022 Xanadu Quantum Technologies Inc.
rauletorresc 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 "Exception.hpp"
#include "Types.h"
#include "Utils.hpp"

#include "ObservablesLQubit.hpp"
#include "StateVectorLQubitDynamic.hpp"

namespace {
using Pennylane::LightningQubit::StateVectorLQubitDynamic;
using Pennylane::LightningQubit::Observables::HermitianObs;
using Pennylane::LightningQubit::Observables::NamedObs;
using Pennylane::LightningQubit::Observables::TensorProdObs;
using Pennylane::Observables::Observable;
} // namespace

namespace Catalyst::Runtime::Simulator {

/**
* @brief The LightningObsManager 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 LightningObsManager {
private:
using VectorStateT = StateVectorLQubitDynamic<PrecisionT>;
using ComplexT = typename VectorStateT::ComplexT;
using ObservablePairType =
std::pair<std::shared_ptr<Observable<VectorStateT>>, ObsType>;
std::vector<ObservablePairType> observables_{};

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

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

/**
* @brief A helper function to clear constructed observables in the program.
*/
void clear() { 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<size_t>(i) < observables_.size());
});
}

/**
* @brief Get the constructed observable instance.
*
* @param key The observable key
* @return 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]);
}

/**
* @brief Get the number of observables.
*
* @return size_t
*/
[[nodiscard]] auto numObservables() const -> size_t {
return 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));

observables_.push_back(std::make_pair(
std::make_shared<NamedObs<VectorStateT>>(obs_str, wires),
ObsType::Basic));
return static_cast<ObsIdType>(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<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);
}

/**
* @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 = 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");

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

observables_.push_back(std::make_pair(
TensorProdObs<VectorStateT>::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 = 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<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");

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))),
ObsType::Hamiltonian));

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