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

Fix Lightning GPU Rot adjoint bug and enable Rot in toml #1004

Merged
merged 11 commits into from
Nov 25, 2024
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

### Bug fixes

* Fix issue with `lightning.gpu` Rot operation with adjoint.
[(#1004)](https://github.com/PennyLaneAI/pennylane-lightning/pull/1004)

* Fix issue with adjoint-jacobian of adjoint ops.
[(#996)](https://github.com/PennyLaneAI/pennylane-lightning/pull/996)

Expand Down
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-dev14"
__version__ = "0.40.0-dev15"
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,11 @@ class StateVectorCudaMPI final
} else if (opName == "Rot" || opName == "CRot") {
auto rot_matrix =
adjoint
? cuGates::getRot<CFP_t>(params[2], params[1], params[0])
? cuGates::getRot<CFP_t>(-params[2], -params[1], -params[0])
: cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
applyDeviceMatrixGate(rot_matrix.data(), ctrls, tgts, adjoint);
// Use default adjoint=false for applyDeviceMatrixGate() as reversed
// rot_matrix explicitly used
applyDeviceMatrixGate(rot_matrix.data(), ctrls, tgts);
} else if (opName == "Matrix") {
applyDeviceMatrixGate(gate_matrix.data(), ctrls, tgts, adjoint);
} else if (par_gates_.find(opName) != par_gates_.end()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,13 @@ class StateVectorCudaManaged
applyParametricPauliGate_({opName}, ctrls, tgts, params.front(),
adjoint);
} else if (opName == "Rot" || opName == "CRot") {
if (adjoint) {
auto rot_matrix =
cuGates::getRot<CFP_t>(params[2], params[1], params[0]);
applyDeviceMatrixGate_(rot_matrix.data(), ctrls, tgts, true);
} else {
auto rot_matrix =
cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
applyDeviceMatrixGate_(rot_matrix.data(), ctrls, tgts, false);
}
auto rot_matrix =
adjoint
? cuGates::getRot<CFP_t>(-params[2], -params[1], -params[0])
: cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
// Use default adjoint=false for applyDeviceGeneralGate_() as
// reversed rot_matrix explicitly used
applyDeviceMatrixGate_(rot_matrix.data(), ctrls, tgts);
} else if (opName == "Matrix") {
applyDeviceMatrixGate_(gate_matrix.data(), ctrls, tgts, adjoint);
} else if (par_gates_.find(opName) != par_gates_.end()) {
Expand Down Expand Up @@ -408,10 +406,12 @@ class StateVectorCudaManaged
} else if (opName == "Rot") {
auto rot_matrix =
adjoint
? cuGates::getRot<CFP_t>(params[2], params[1], params[0])
? cuGates::getRot<CFP_t>(-params[2], -params[1], -params[0])
: cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
// Use default adjoint=false for applyDeviceGeneralGate_() as
// reversed rot_matrix explicitly used
applyDeviceGeneralGate_(rot_matrix.data(), ctrlsInt, tgtsInt,
ctrls_valuesInt, adjoint);
ctrls_valuesInt);
} else if (par_gates_.find(opName) != par_gates_.end()) {
// TODO: offload to par_gates_ if available
auto &gateMap =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyRot", "[LightningGPU_Param]", float,
for (std::size_t i = 0; i < angles.size(); i++) {
const auto rot_mat =
(adjoint) ? Gates::getRot<std::complex, TestType>(
-angles[i][0], -angles[i][1], -angles[i][2])
-angles[i][2], -angles[i][1], -angles[i][0])
: Gates::getRot<std::complex, TestType>(
angles[i][0], angles[i][1], angles[i][2]);
expected_results[i][0] = rot_mat[0];
Expand Down Expand Up @@ -419,7 +419,7 @@ TEMPLATE_TEST_CASE("LightningGPU::applyCRot", "[LightningGPU_Param]", float,

std::vector<cp_t> expected_results(8);
const auto rot_mat = (adjoint) ? Gates::getRot<std::complex, TestType>(
-angles[0], -angles[1], -angles[2])
-angles[2], -angles[1], -angles[0])
: Gates::getRot<std::complex, TestType>(
angles[0], angles[1], angles[2]);

Expand Down
4 changes: 2 additions & 2 deletions pennylane_lightning/lightning_gpu/lightning_gpu.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ PhaseShift = { properties = [ "invertible", "controllable", "differe
RX = { properties = [ "invertible", "controllable", "differentiable" ] }
RY = { properties = [ "invertible", "controllable", "differentiable" ] }
RZ = { properties = [ "invertible", "controllable", "differentiable" ] }
Rot = { properties = [ "controllable", "differentiable" ] }
Rot = { properties = [ "invertible", "controllable", "differentiable" ] }
CNOT = { properties = [ "invertible", "differentiable" ] }
CY = { properties = [ "invertible", "differentiable" ] }
CZ = { properties = [ "invertible", "differentiable" ] }
Expand All @@ -47,7 +47,7 @@ ControlledPhaseShift = { properties = [ "invertible", "differe
CRX = { properties = [ "invertible", "differentiable" ] }
CRY = { properties = [ "invertible", "differentiable" ] }
CRZ = { properties = [ "invertible", "differentiable" ] }
CRot = { }
CRot = { properties = [ "invertible" ] }
SingleExcitation = { properties = [ "invertible", "controllable", "differentiable" ] }
SingleExcitationPlus = { properties = [ "invertible", "controllable", "differentiable" ] }
SingleExcitationMinus = { properties = [ "invertible", "controllable", "differentiable" ] }
Expand Down
Loading