Skip to content

Commit f594f29

Browse files
Update lightning_kokkos CUDA backend for compatibility with Catalyst (#942)
### Before submitting Please complete the following checklist when submitting a PR: - [ ] All new features must include a unit test. If you've fixed a bug or added code that should be tested, add a test to the [`tests`](../tests) directory! - [ ] All new functions and code must be clearly commented and documented. If you do make documentation changes, make sure that the docs build and render correctly by running `make docs`. - [ ] Ensure that the test suite passes, by running `make test`. - [ ] Add a new entry to the `.github/CHANGELOG.md` file, summarizing the change, and including a link back to the PR. - [ ] Ensure that code is properly formatted by running `make format`. When all the above are checked, delete everything above the dashed line and fill in the pull request template. ------------------------------------------------------------------------------------------------------------ **Context:** **Description of the Change:** **Benefits:** **Possible Drawbacks:** **Related GitHub Issues:** [sc-73064] --------- Co-authored-by: ringo-but-quantum <[email protected]>
1 parent 934e115 commit f594f29

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

.github/CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
### Improvements
1111

12+
* Update the `lightning.kokkos` CUDA backend for compatibility with Catalyst.
13+
[(#942)](https://github.com/PennyLaneAI/pennylane-lightning/pull/942)
14+
1215
### Documentation
1316

1417
### Bug fixes
@@ -23,7 +26,7 @@
2326

2427
This release contains contributions from (in alphabetical order):
2528

26-
Luis Alfredo Nuñez Meneses, Shuli Shu
29+
Ali Asadi, Luis Alfredo Nuñez Meneses, Shuli Shu
2730

2831
---
2932

pennylane_lightning/core/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
Version number (major.minor.patch[-label])
1717
"""
1818

19-
__version__ = "0.40.0-dev2"
19+
__version__ = "0.40.0-dev3"

pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/CMakeLists.txt

+27-7
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,37 @@ include(FetchContent)
1010
include("${pennylane_lightning_SOURCE_DIR}/cmake/support_catalyst.cmake")
1111
FindCatalyst(lightning_kokkos_catalyst)
1212

13+
if(Kokkos_ENABLE_CUDA)
14+
message(STATUS "Kokkos_ENABLE_CUDA is ON")
15+
16+
# KOKKOS_CUDA_OPTIONS:
17+
# - disable_malloc_async: Disable asynchronous memory allocation to ensure
18+
# that memory operations complete before proceeding.
19+
# - enable_lambda: Allow the use of lambda expressions in parallel
20+
# execution policies, used in LK kernel functors, and this flag is
21+
# required when calling those functors from an external library.
22+
# It's supported starting from NVCC v7.0.
23+
# - enable_constexpr: Enable compile-time evaluations for constants.
24+
# There are some undefined behaviour when executing end-to-end Catalyst
25+
# programs related to the value of `constexpr`s in the definition of gate kernels.
26+
# This flag should be enforced for NVCC v7.5+.
27+
if(NVCC_VERSION VERSION_LESS "7.0")
28+
message(WARNING "Building lightning_kokkos_catalyst without lambda and constexpr support.")
29+
target_compile_definitions(lightning_kokkos_catalyst PRIVATE KOKKOS_CUDA_OPTIONS="disable_malloc_async")
30+
elseif(NVCC_VERSION VERSION_LESS "7.5")
31+
message(WARNING "Building lightning_kokkos_catalyst without constexpr support.")
32+
target_compile_definitions(lightning_kokkos_catalyst PRIVATE KOKKOS_CUDA_OPTIONS="disable_malloc_async enable_lambda")
33+
else()
34+
target_compile_definitions(lightning_kokkos_catalyst PRIVATE
35+
KOKKOS_CUDA_OPTIONS="disable_malloc_async enable_lambda enable_constexpr"
36+
)
37+
endif()
38+
endif()
39+
1340
target_include_directories(lightning_kokkos_catalyst INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
1441
target_link_libraries(lightning_kokkos_catalyst PUBLIC lightning_compile_options
15-
lightning_external_libs
16-
lightning_base
17-
lightning_gates
18-
lightning_utils
19-
lightning_kokkos
2042
lightning_kokkos_algorithms
21-
lightning_kokkos_gates
2243
lightning_kokkos_measurements
23-
lightning_kokkos_utils
2444
)
2545

2646
if (BUILD_TESTS)

pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
#pragma once
2020

21-
#define __device_lightning_kokkos
22-
2321
#include <bitset>
2422
#include <cmath>
2523
#include <cstdint>
@@ -62,7 +60,8 @@ class LightningKokkosSimulator final : public Catalyst::Runtime::QuantumDevice {
6260
Catalyst::Runtime::CacheManager<Kokkos::complex<double>> cache_manager{};
6361
bool tape_recording{false};
6462

65-
std::size_t device_shots;
63+
// set default to avoid C++ tests segfaults in analytic mode
64+
std::size_t device_shots{0};
6665

6766
std::mt19937 *gen{nullptr};
6867

@@ -99,13 +98,14 @@ class LightningKokkosSimulator final : public Catalyst::Runtime::QuantumDevice {
9998
auto GenerateSamples(size_t shots) -> std::vector<size_t>;
10099

101100
public:
102-
explicit LightningKokkosSimulator(const std::string &kwargs = "{}") {
101+
explicit LightningKokkosSimulator(
102+
const std::string &kwargs = "{}") noexcept {
103103
auto &&args = Catalyst::Runtime::parse_kwargs(kwargs);
104104
device_shots = args.contains("shots")
105105
? static_cast<std::size_t>(std::stoll(args["shots"]))
106106
: 0;
107107
}
108-
~LightningKokkosSimulator() = default;
108+
~LightningKokkosSimulator() noexcept = default;
109109

110110
LightningKokkosSimulator(const LightningKokkosSimulator &) = delete;
111111
LightningKokkosSimulator &

0 commit comments

Comments
 (0)