diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp index f4c509f76d..eae1aa2413 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/MPIWorker.hpp @@ -21,6 +21,7 @@ #include "MPIManager.hpp" #include "MPI_helpers.hpp" #include "cuError.hpp" +#include "cuStateVecError.hpp" #include #include #include diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp index 3e40420bd5..fba307ebed 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaBase.hpp @@ -27,6 +27,7 @@ #include "DevTag.hpp" #include "Error.hpp" #include "StateVectorBase.hpp" +#include "cuStateVecError.hpp" #include "cuda_helpers.hpp" /// @cond DEV diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp index c7da5b82c3..0496a8042e 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp @@ -36,6 +36,8 @@ #include "StateVectorCudaBase.hpp" #include "cuGateCache.hpp" #include "cuGates_host.hpp" +#include "cuStateVecError.hpp" +#include "cuStateVec_helpers.hpp" #include "cuda_helpers.hpp" #include "CPUMemoryModel.hpp" diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp index 15fd843190..4e7c749ba6 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp @@ -37,6 +37,8 @@ #include "CPUMemoryModel.hpp" #include "cuError.hpp" +#include "cuStateVecError.hpp" +#include "cuStateVec_helpers.hpp" #include "LinearAlg.hpp" diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp index fca0175177..dc213c126f 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPU.hpp @@ -34,6 +34,8 @@ #include "Observables.hpp" #include "ObservablesGPU.hpp" #include "StateVectorCudaManaged.hpp" +#include "cuStateVecError.hpp" +#include "cuStateVec_helpers.hpp" #include "cuda_helpers.hpp" /// @cond DEV diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp index d82ebfd0e4..9d0d986fa4 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/measurements/MeasurementsGPUMPI.hpp @@ -39,6 +39,7 @@ #include "ObservablesGPUMPI.hpp" #include "StateVectorCudaMPI.hpp" #include "StateVectorCudaManaged.hpp" +#include "cuStateVecError.hpp" #include "cuda_helpers.hpp" /// @cond DEV diff --git a/pennylane_lightning/core/src/utils/cuda_utils/MPI_helpers.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/utils/MPI_helpers.hpp similarity index 100% rename from pennylane_lightning/core/src/utils/cuda_utils/MPI_helpers.hpp rename to pennylane_lightning/core/src/simulators/lightning_gpu/utils/MPI_helpers.hpp diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVecError.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVecError.hpp new file mode 100644 index 0000000000..6d7fb760b4 --- /dev/null +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVecError.hpp @@ -0,0 +1,105 @@ +// Copyright 2022-2023 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. + +// Adapted from JET: https://github.com/XanaduAI/jet.git +// and from Lightning: https://github.com/PennylaneAI/pennylane-lightning.git +/** + * @file cuStateVecError.hpp + */ + +#pragma once +#include + +#include + +#include "Error.hpp" +#include "Util.hpp" +// LCOV_EXCL_START +namespace { +using namespace Pennylane::Util; +} + +#ifndef CUDA_UNSAFE + +/** + * @brief Macro that throws Exception from cuStateVec failure error codes. + * + * @param err cuStateVec function error-code. + */ +#define PL_CUSTATEVEC_IS_SUCCESS(err) \ + PL_ABORT_IF_NOT( \ + err == CUSTATEVEC_STATUS_SUCCESS, \ + Pennylane::LightningGPU::Util::GetCuStateVecErrorString(err).c_str()) + +#else +#define PL_CUSTATEVEC_IS_SUCCESS(err) \ + { static_cast(err); } +#endif + +namespace Pennylane::LightningGPU::Util { +static const std::string +GetCuStateVecErrorString(const custatevecStatus_t &err) { + std::string result; + switch (err) { + case CUSTATEVEC_STATUS_SUCCESS: + result = "No errors"; + break; + case CUSTATEVEC_STATUS_NOT_INITIALIZED: + result = "custatevec not initialized"; + break; + case CUSTATEVEC_STATUS_ALLOC_FAILED: + result = "custatevec memory allocation failed"; + break; + case CUSTATEVEC_STATUS_INVALID_VALUE: + result = "custatevec invalid value"; + break; + case CUSTATEVEC_STATUS_ARCH_MISMATCH: + result = "custatevec CUDA device architecture mismatch"; + break; + case CUSTATEVEC_STATUS_EXECUTION_FAILED: + result = "custatevec execution failed"; + break; + case CUSTATEVEC_STATUS_INTERNAL_ERROR: + result = "custatevec internal error"; + break; + case CUSTATEVEC_STATUS_NOT_SUPPORTED: + result = "custatevec unsupported operation/device"; + break; + case CUSTATEVEC_STATUS_INSUFFICIENT_WORKSPACE: + result = + "custatevec insufficient memory for gate-application workspace"; + break; + case CUSTATEVEC_STATUS_SAMPLER_NOT_PREPROCESSED: + result = "custatevec sampler not preprocessed"; + break; + case CUSTATEVEC_STATUS_NO_DEVICE_ALLOCATOR: + result = "custatevec no device allocator"; + break; + case CUSTATEVEC_STATUS_DEVICE_ALLOCATOR_ERROR: + result = "custatevec device allocator error"; + break; + case CUSTATEVEC_STATUS_COMMUNICATOR_ERROR: + result = "custatevec communicator failure"; + break; + case CUSTATEVEC_STATUS_LOADING_LIBRARY_FAILED: + result = "custatevec dynamic library load failure"; + break; + default: + result = + "custatevec status not found. Error code=" + std::to_string(err); + } + return result; +} +} // namespace Pennylane::LightningGPU::Util + // LCOV_EXCL_STOP \ No newline at end of file diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVec_helpers.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVec_helpers.hpp new file mode 100644 index 0000000000..fdbaf75f88 --- /dev/null +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVec_helpers.hpp @@ -0,0 +1,105 @@ +// Copyright 2022-2023 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. + +// Adapted from JET: https://github.com/XanaduAI/jet.git + +/** + * @file cudaStateVec_helpers.hpp + */ + +#pragma once +#include +#include +#include +#include +#include +#include + +#include "cuStateVecError.hpp" +#include "cuStateVec_helpers.hpp" + +namespace Pennylane::LightningGPU::Util { + +inline static auto pauliStringToEnum(const std::string &pauli_word) + -> std::vector { + // Map string rep to Pauli enums + const std::unordered_map pauli_map{ + std::pair{std::string("X"), + CUSTATEVEC_PAULI_X}, + std::pair{std::string("Y"), + CUSTATEVEC_PAULI_Y}, + std::pair{std::string("Z"), + CUSTATEVEC_PAULI_Z}, + std::pair{std::string("I"), + CUSTATEVEC_PAULI_I}}; + + static constexpr std::size_t num_char = 1; + + std::vector output; + output.reserve(pauli_word.size()); + + for (const auto ch : pauli_word) { + auto out = pauli_map.at(std::string(num_char, ch)); + output.push_back(out); + } + return output; +} + +inline static auto pauliStringToOpNames(const std::string &pauli_word) + -> std::vector { + // Map string rep to Pauli + const std::unordered_map pauli_map{ + std::pair{std::string("X"), + std::string("PauliX")}, + std::pair{std::string("Y"), + std::string("PauliY")}, + std::pair{std::string("Z"), + std::string("PauliZ")}, + std::pair{std::string("I"), + std::string("Identity")}}; + + static constexpr std::size_t num_char = 1; + + std::vector output; + output.reserve(pauli_word.size()); + + for (const auto ch : pauli_word) { + auto out = pauli_map.at(std::string(num_char, ch)); + output.push_back(out); + } + return output; +} + +/** + * Utility function object to tell std::shared_ptr how to + * release/destroy cuStateVec objects. + */ +struct handleDeleter { + void operator()(custatevecHandle_t handle) const { + PL_CUSTATEVEC_IS_SUCCESS(custatevecDestroy(handle)); + } +}; + +using SharedCusvHandle = + std::shared_ptr::type>; + +/** + * @brief Creates a SharedCusvHandle (a shared pointer to a custatevecHandle) + */ +inline SharedCusvHandle make_shared_cusv_handle() { + custatevecHandle_t h; + PL_CUSTATEVEC_IS_SUCCESS(custatevecCreate(&h)); + return {h, handleDeleter()}; +} +} // namespace Pennylane::LightningGPU::Util diff --git a/pennylane_lightning/core/src/utils/config.h b/pennylane_lightning/core/src/utils/config.h index 8f887c7d2d..63a6dff294 100644 --- a/pennylane_lightning/core/src/utils/config.h +++ b/pennylane_lightning/core/src/utils/config.h @@ -14,10 +14,11 @@ /** * @file - * Record the path to scipy.libs at compile time. + * Config file for the path to scipy.libs at compile time. */ #ifndef CONFIG_H #define CONFIG_H -#define SCIPY_LIBS_PATH "" -#endif \ No newline at end of file +#define SCIPY_LIBS_PATH \ + "/home/shuli/env/lib/python3.10/site-packages/scipy.libs" +#endif diff --git a/pennylane_lightning/core/src/utils/cuda_utils/LinearAlg.hpp b/pennylane_lightning/core/src/utils/cuda_utils/LinearAlg.hpp index 13a7ec9a90..eef358e9c4 100644 --- a/pennylane_lightning/core/src/utils/cuda_utils/LinearAlg.hpp +++ b/pennylane_lightning/core/src/utils/cuda_utils/LinearAlg.hpp @@ -22,7 +22,6 @@ #include #include #include -#include #include "DataBuffer.hpp" #include "cuError.hpp" @@ -247,17 +246,12 @@ struct HandleDeleter { void operator()(cublasHandle_t handle) const { PL_CUBLAS_IS_SUCCESS(cublasDestroy(handle)); } - void operator()(custatevecHandle_t handle) const { - PL_CUSTATEVEC_IS_SUCCESS(custatevecDestroy(handle)); - } void operator()(cusparseHandle_t handle) const { PL_CUSPARSE_IS_SUCCESS(cusparseDestroy(handle)); } }; using SharedCublasCaller = std::shared_ptr; -using SharedCusvHandle = - std::shared_ptr::type>; using SharedCusparseHandle = std::shared_ptr::type>; @@ -268,15 +262,6 @@ inline SharedCublasCaller make_shared_cublas_caller() { return std::make_shared(); } -/** - * @brief Creates a SharedCusvHandle (a shared pointer to a custatevecHandle) - */ -inline SharedCusvHandle make_shared_cusv_handle() { - custatevecHandle_t h; - PL_CUSTATEVEC_IS_SUCCESS(custatevecCreate(&h)); - return {h, HandleDeleter()}; -} - /** * @brief Creates a SharedCusparseHandle (a shared pointer to a cusparseHandle) */ diff --git a/pennylane_lightning/core/src/utils/cuda_utils/MPILinearAlg.hpp b/pennylane_lightning/core/src/utils/cuda_utils/MPILinearAlg.hpp index 9f0f3972a6..cfe9ec7d81 100644 --- a/pennylane_lightning/core/src/utils/cuda_utils/MPILinearAlg.hpp +++ b/pennylane_lightning/core/src/utils/cuda_utils/MPILinearAlg.hpp @@ -8,7 +8,6 @@ #include #include #include -#include #include "DataBuffer.hpp" #include "cuError.hpp" diff --git a/pennylane_lightning/core/src/utils/cuda_utils/MPIManager.hpp b/pennylane_lightning/core/src/utils/cuda_utils/MPIManager.hpp index e32295c9c6..d42d39082a 100644 --- a/pennylane_lightning/core/src/utils/cuda_utils/MPIManager.hpp +++ b/pennylane_lightning/core/src/utils/cuda_utils/MPIManager.hpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -30,6 +29,10 @@ #include #include +#ifdef _ENABLE_PLGPU +#include +#endif + #include "DataBuffer.hpp" #include "Error.hpp" @@ -155,7 +158,9 @@ class MPIManager final { {cppTypeToString(), MPI_C_FLOAT_COMPLEX}, {cppTypeToString(), MPI_C_DOUBLE_COMPLEX}, {cppTypeToString(), MPI_C_DOUBLE_COMPLEX}, +#ifdef _ENABLE_PLGPU {cppTypeToString(), MPI_INT64_T}, +#endif // cuda related types {cppTypeToString(), MPI_UINT8_T}, {cppTypeToString(), MPI_UINT8_T}}; diff --git a/pennylane_lightning/core/src/utils/cuda_utils/cuError.hpp b/pennylane_lightning/core/src/utils/cuda_utils/cuError.hpp index 11ff04e3b9..bd6a623524 100644 --- a/pennylane_lightning/core/src/utils/cuda_utils/cuError.hpp +++ b/pennylane_lightning/core/src/utils/cuda_utils/cuError.hpp @@ -25,7 +25,6 @@ #include #include #include -#include #include "Error.hpp" #include "Util.hpp" @@ -50,16 +49,6 @@ using namespace Pennylane::Util; #define PL_CUSPARSE_IS_SUCCESS(err) \ PL_ABORT_IF_NOT(err == CUSPARSE_STATUS_SUCCESS, GetCuSparseErrorString(err)) -/** - * @brief Macro that throws Exception from cuQuantum failure error codes. - * - * @param err cuQuantum function error-code. - */ -#define PL_CUSTATEVEC_IS_SUCCESS(err) \ - PL_ABORT_IF_NOT( \ - err == CUSTATEVEC_STATUS_SUCCESS, \ - Pennylane::LightningGPU::Util::GetCuStateVecErrorString(err).c_str()) - #else #define PL_CUDA_IS_SUCCESS(err) \ { static_cast(err); } @@ -67,8 +56,6 @@ using namespace Pennylane::Util; { static_cast(err); } #define PL_CUSPARSE_IS_SUCCESS(err) \ { static_cast(err); } -#define PL_CUSTATEVEC_IS_SUCCESS(err) \ - { static_cast(err); } #endif namespace Pennylane::LightningGPU::Util { @@ -150,58 +137,5 @@ static const std::string GetCuSparseErrorString(const cusparseStatus_t &err) { return result; } -static const std::string -GetCuStateVecErrorString(const custatevecStatus_t &err) { - std::string result; - switch (err) { - case CUSTATEVEC_STATUS_SUCCESS: - result = "No errors"; - break; - case CUSTATEVEC_STATUS_NOT_INITIALIZED: - result = "custatevec not initialized"; - break; - case CUSTATEVEC_STATUS_ALLOC_FAILED: - result = "custatevec memory allocation failed"; - break; - case CUSTATEVEC_STATUS_INVALID_VALUE: - result = "custatevec invalid value"; - break; - case CUSTATEVEC_STATUS_ARCH_MISMATCH: - result = "custatevec CUDA device architecture mismatch"; - break; - case CUSTATEVEC_STATUS_EXECUTION_FAILED: - result = "custatevec execution failed"; - break; - case CUSTATEVEC_STATUS_INTERNAL_ERROR: - result = "custatevec internal error"; - break; - case CUSTATEVEC_STATUS_NOT_SUPPORTED: - result = "custatevec unsupported operation/device"; - break; - case CUSTATEVEC_STATUS_INSUFFICIENT_WORKSPACE: - result = - "custatevec insufficient memory for gate-application workspace"; - break; - case CUSTATEVEC_STATUS_SAMPLER_NOT_PREPROCESSED: - result = "custatevec sampler not preprocessed"; - break; - case CUSTATEVEC_STATUS_NO_DEVICE_ALLOCATOR: - result = "custatevec no device allocator"; - break; - case CUSTATEVEC_STATUS_DEVICE_ALLOCATOR_ERROR: - result = "custatevec device allocator error"; - break; - case CUSTATEVEC_STATUS_COMMUNICATOR_ERROR: - result = "custatevec communicator failure"; - break; - case CUSTATEVEC_STATUS_LOADING_LIBRARY_FAILED: - result = "custatevec dynamic library load failure"; - break; - default: - result = - "custatevec status not found. Error code=" + std::to_string(err); - } - return result; -} } // namespace Pennylane::LightningGPU::Util // LCOV_EXCL_STOP \ No newline at end of file diff --git a/pennylane_lightning/core/src/utils/cuda_utils/cuda_helpers.hpp b/pennylane_lightning/core/src/utils/cuda_utils/cuda_helpers.hpp index c7eeeb975c..1f0aa824cd 100644 --- a/pennylane_lightning/core/src/utils/cuda_utils/cuda_helpers.hpp +++ b/pennylane_lightning/core/src/utils/cuda_utils/cuda_helpers.hpp @@ -33,7 +33,6 @@ #include #include #include -#include #include "DevTag.hpp" #include "cuError.hpp" @@ -339,56 +338,6 @@ static std::pair getGPUArch(int device_number = 0) { return std::make_pair(deviceProp.major, deviceProp.minor); } -inline static auto pauliStringToEnum(const std::string &pauli_word) - -> std::vector { - // Map string rep to Pauli enums - const std::unordered_map pauli_map{ - std::pair{std::string("X"), - CUSTATEVEC_PAULI_X}, - std::pair{std::string("Y"), - CUSTATEVEC_PAULI_Y}, - std::pair{std::string("Z"), - CUSTATEVEC_PAULI_Z}, - std::pair{std::string("I"), - CUSTATEVEC_PAULI_I}}; - - static constexpr std::size_t num_char = 1; - - std::vector output; - output.reserve(pauli_word.size()); - - for (const auto ch : pauli_word) { - auto out = pauli_map.at(std::string(num_char, ch)); - output.push_back(out); - } - return output; -} - -inline static auto pauliStringToOpNames(const std::string &pauli_word) - -> std::vector { - // Map string rep to Pauli - const std::unordered_map pauli_map{ - std::pair{std::string("X"), - std::string("PauliX")}, - std::pair{std::string("Y"), - std::string("PauliY")}, - std::pair{std::string("Z"), - std::string("PauliZ")}, - std::pair{std::string("I"), - std::string("Identity")}}; - - static constexpr std::size_t num_char = 1; - - std::vector output; - output.reserve(pauli_word.size()); - - for (const auto ch : pauli_word) { - auto out = pauli_map.at(std::string(num_char, ch)); - output.push_back(out); - } - return output; -} - /** * Utility hash function for complex vectors representing matrices. */