Skip to content

Commit

Permalink
Fix Lightning GPU Rot adjoint bug and enable Rot in toml (#1004)
Browse files Browse the repository at this point in the history
### Before submitting

Please complete the following checklist when submitting a PR:

- [X] 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`.

- [X] Add a new entry to the `.github/CHANGELOG.md` file, summarizing
the
      change, and including a link back to the PR.

- [X] 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:**
Previously the implementation of Rot and CRot gates (e.g. `Rot(a, b,
c)`) with adjoint in LGPU ended up multiplying `Rot(-a, -b, -c)` instead
of `Rot(-c, -b, -a)`. The error was not found in tests since the C++
tests used the wrong adjoint rotation matrix, and since `Adjoint(Rot)`
and `Adjoint(CRot)` were not in the frozenset, they were not executed in
the Python tests. The error is found when adding the invertible
capability for Rot and CRot and [the following
test](https://github.com/PennyLaneAI/pennylane-lightning/blob/45a67d495552ca9aca3c05467b16b1b6d2e1d4ab/tests/test_apply.py#L1362)
fails.

**Description of the Change:**
This PR fixes the implementation and C++ tests. The toml file is also
updated to enable `Adjoint(Rot)` and `Adjoint(CRot)` from Python.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

[sc-78931]

---------

Co-authored-by: ringo-but-quantum <github-ringo-but-quantum@xanadu.ai>
  • Loading branch information
josephleekl and ringo-but-quantum authored Nov 25, 2024
1 parent c7846c3 commit d37e638
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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-dev15"
__version__ = "0.40.0-dev16"
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ class StateVectorCudaMPI final
adjoint);
} 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[0], params[1], params[2]);
cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
applyDeviceMatrixGate(rot_matrix.data(), ctrls, tgts, adjoint);
} else if (opName == "Matrix") {
applyDeviceMatrixGate(gate_matrix.data(), ctrls, tgts, adjoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,9 @@ 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 =
cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
applyDeviceMatrixGate_(rot_matrix.data(), ctrls, tgts, adjoint);
} else if (opName == "Matrix") {
applyDeviceMatrixGate_(gate_matrix.data(), ctrls, tgts, adjoint);
} else if (par_gates_.find(opName) != par_gates_.end()) {
Expand Down Expand Up @@ -407,9 +401,7 @@ class StateVectorCudaManaged
params.front(), adjoint);
} else if (opName == "Rot") {
auto rot_matrix =
adjoint
? cuGates::getRot<CFP_t>(params[2], params[1], params[0])
: cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
cuGates::getRot<CFP_t>(params[0], params[1], params[2]);
applyDeviceGeneralGate_(rot_matrix.data(), ctrlsInt, tgtsInt,
ctrls_valuesInt, adjoint);
} else if (par_gates_.find(opName) != par_gates_.end()) {
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

0 comments on commit d37e638

Please sign in to comment.