From 02eb2904438d09c988a270381d2a6315b113442b Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Thu, 17 Oct 2024 18:29:55 +0000 Subject: [PATCH] initial commit --- .../lightning_gpu/StateVectorCudaMPI.hpp | 9 + .../lightning_gpu/StateVectorCudaManaged.hpp | 184 ++++++++---------- .../lightning_gpu/bindings/LGPUBindings.hpp | 11 ++ .../Test_StateVectorCudaManaged_Param.cpp | 9 +- .../mpi/Test_StateVectorCudaMPI_Param.cpp | 13 ++ .../src/simulators/lightning_gpu/initSV.cu | 132 +------------ .../tests/Test_StateVectorCudaManaged.cpp | 9 +- .../lightning_gpu/_state_vector.py | 10 +- 8 files changed, 134 insertions(+), 243 deletions(-) diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp index 964c5e69c..f9c0f0116 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaMPI.hpp @@ -386,6 +386,15 @@ class StateVectorCudaMPI final if (opName == "Identity") { return; + } else if (opName == "GlobalPhase") { + PrecisionT param = adjoint ? -params[0] : params[0]; + CFP_t scale_factor{std::cos(param), -std::sin(param)}; + scaleC_CUDA( + scale_factor, BaseType::getDataBuffer().getData(), + BaseType::getDataBuffer().getLength(), + BaseType::getDataBuffer().getDevTag().getDeviceID(), + BaseType::getDataBuffer().getDevTag().getStreamID(), + getCublasCaller()); } else if (native_gates_.find(opName) != native_gates_.end()) { applyParametricPauliGate({opName}, ctrls, tgts, params.front(), adjoint); diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp index f5aeb4abb..2e31bbe2f 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/StateVectorCudaManaged.hpp @@ -67,25 +67,6 @@ extern void setBasisState_CUDA(cuDoubleComplex *sv, cuDoubleComplex &value, const std::size_t index, bool async, cudaStream_t stream_id); -extern void globalPhaseStateVector_CUDA(cuComplex *sv, std::size_t num_sv, - cuComplex phase, - std::size_t thread_per_block, - cudaStream_t stream_id); -extern void globalPhaseStateVector_CUDA(cuDoubleComplex *sv, std::size_t num_sv, - cuDoubleComplex phase, - std::size_t thread_per_block, - cudaStream_t stream_id); - -extern void cGlobalPhaseStateVector_CUDA(cuComplex *sv, std::size_t num_sv, - bool adjoint, cuComplex *phase, - std::size_t thread_per_block, - cudaStream_t stream_id); -extern void cGlobalPhaseStateVector_CUDA(cuDoubleComplex *sv, - std::size_t num_sv, bool adjoint, - cuDoubleComplex *phase, - std::size_t thread_per_block, - cudaStream_t stream_id); - /** * @brief Managed memory CUDA state-vector class using custateVec backed * gate-calls. @@ -257,44 +238,6 @@ class StateVectorCudaManaged use_async); } - /** - * @brief Multiplies the state-vector by a global phase. - * - * @param adjoint Indicates whether to use adjoint of gate. - * @param param Complex phase generator. - */ - template - void globalPhaseStateVector(const bool adjoint, const Precision param) { - auto stream_id = BaseType::getDataBuffer().getDevTag().getStreamID(); - std::complex phase = - std::exp(std::complex{0, (adjoint) ? param : -param}); - auto cuPhase = complexToCu(phase); - globalPhaseStateVector_CUDA(BaseType::getData(), BaseType::getLength(), - cuPhase, thread_per_block, stream_id); - } - - /** - * @brief Multiplies the state-vector by a controlled global phase. - * - * @param phase Controlled complex phase vector. - */ - template - void cGlobalPhaseStateVector(const bool adjoint, - const std::vector &phase, - const bool async = false) { - PL_ABORT_IF_NOT(BaseType::getLength() == phase.size(), - "The state-vector data must have the same size as the " - "controlled-phase data.") - auto device_id = BaseType::getDataBuffer().getDevTag().getDeviceID(); - auto stream_id = BaseType::getDataBuffer().getDevTag().getStreamID(); - DataBuffer d_phase{phase.size(), device_id, stream_id, - true}; - d_phase.CopyHostDataToGpu(phase.data(), d_phase.getLength(), async); - cGlobalPhaseStateVector_CUDA(BaseType::getData(), BaseType::getLength(), - adjoint, d_phase.getData(), - thread_per_block, stream_id); - } - /** * @brief Apply a single gate to the state-vector. Offloads to custatevec * specific API calls if available. If unable, attempts to use prior cached @@ -347,13 +290,18 @@ class StateVectorCudaManaged wires.end()}; if (opName == "Identity") { return; - } else if (opName == "C(GlobalPhase)") { - cGlobalPhaseStateVector(adjoint, gate_matrix); } else if (opName == "GlobalPhase") { - globalPhaseStateVector(adjoint, params[0]); + PrecisionT param = adjoint ? -params[0] : params[0]; + CFP_t scale_factor{std::cos(param), -std::sin(param)}; + scaleC_CUDA( + scale_factor, BaseType::getDataBuffer().getData(), + BaseType::getDataBuffer().getLength(), + BaseType::getDataBuffer().getDevTag().getDeviceID(), + BaseType::getDataBuffer().getDevTag().getStreamID(), + getCublasCaller()); } else if (native_gates_.find(opName) != native_gates_.end()) { - applyParametricPauliGate({opName}, ctrls, tgts, params.front(), - adjoint); + applyParametricPauliGate_({opName}, ctrls, tgts, params.front(), + adjoint); } else if (opName == "Rot" || opName == "CRot") { if (adjoint) { auto rot_matrix = @@ -407,25 +355,44 @@ class StateVectorCudaManaged * @param opName Name of gate to apply. * @param controlled_wires Control wires. * @param controlled_values Control values (false or true). - * @param wires Wires to apply gate to. - * @param inverse Indicates whether to use adjoint of gate. + * @param tgt_wires Wires to apply gate to. + * @param adjoint Indicates whether to use adjoint of gate. * @param params Optional parameter list for parametric gates. * @param params Optional std gate matrix if opName doesn't exist. */ - template