-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2028317
commit 7a7e60d
Showing
15 changed files
with
229 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
105 changes: 105 additions & 0 deletions
105
pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVecError.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <string> | ||
|
||
#include <custatevec.h> | ||
|
||
#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<void>(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 |
105 changes: 105 additions & 0 deletions
105
pennylane_lightning/core/src/simulators/lightning_gpu/utils/cuStateVec_helpers.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <custatevec.h> | ||
#include <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "cuStateVecError.hpp" | ||
#include "cuStateVec_helpers.hpp" | ||
|
||
namespace Pennylane::LightningGPU::Util { | ||
|
||
inline static auto pauliStringToEnum(const std::string &pauli_word) | ||
-> std::vector<custatevecPauli_t> { | ||
// Map string rep to Pauli enums | ||
const std::unordered_map<std::string, custatevecPauli_t> pauli_map{ | ||
std::pair<const std::string, custatevecPauli_t>{std::string("X"), | ||
CUSTATEVEC_PAULI_X}, | ||
std::pair<const std::string, custatevecPauli_t>{std::string("Y"), | ||
CUSTATEVEC_PAULI_Y}, | ||
std::pair<const std::string, custatevecPauli_t>{std::string("Z"), | ||
CUSTATEVEC_PAULI_Z}, | ||
std::pair<const std::string, custatevecPauli_t>{std::string("I"), | ||
CUSTATEVEC_PAULI_I}}; | ||
|
||
static constexpr std::size_t num_char = 1; | ||
|
||
std::vector<custatevecPauli_t> 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<std::string> { | ||
// Map string rep to Pauli | ||
const std::unordered_map<std::string, std::string> pauli_map{ | ||
std::pair<const std::string, std::string>{std::string("X"), | ||
std::string("PauliX")}, | ||
std::pair<const std::string, std::string>{std::string("Y"), | ||
std::string("PauliY")}, | ||
std::pair<const std::string, std::string>{std::string("Z"), | ||
std::string("PauliZ")}, | ||
std::pair<const std::string, std::string>{std::string("I"), | ||
std::string("Identity")}}; | ||
|
||
static constexpr std::size_t num_char = 1; | ||
|
||
std::vector<std::string> 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<std::remove_pointer<custatevecHandle_t>::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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.