Skip to content

Commit 158d393

Browse files
josephleeklringo-but-quantummaliasadi
authored
Add native controlled operation support for Lightning Kokkos (#950)
### 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! - [X] 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`. - [X] 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:** Native arbitrary controlled operations have already been implemented in Lightning Qubit and Lightning GPU. This PR adds support for controlled operations to Lightning Kokkos as well. This PR is the base branch of combination of multiple PRs ([1](#952), [2](#953), [3](#954), [4](#955), [5](#956), [6](#958), [7](#965)) **Description of the Change:** Controlled named-gates, matrix, generators, and adjoint differentiation capability is added to Lightning Kokkos C++ layer. **Benefits:** Better performance for controlled operations in Lightning Kokkos (CPU + GPU). Adjoint differentiation now supported in Lightning Kokkos. **Possible Drawbacks:** **Related GitHub Issues:** [sc-78995] --------- Co-authored-by: ringo-but-quantum <[email protected]> Co-authored-by: Ali Asadi <[email protected]>
1 parent 74ad9a3 commit 158d393

23 files changed

+5978
-1234
lines changed

.github/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
### New features since last release
44

5+
* Add native N-controlled gate/matrix operations and adjoint support to `lightning.kokkos`.
6+
[(#950)](https://github.com/PennyLaneAI/pennylane-lightning/pull/950)
7+
58
* Add native N-controlled generators and adjoint support to `lightning.gpu`'s single-GPU backend.
69
[(#970)](https://github.com/PennyLaneAI/pennylane-lightning/pull/970)
710

pennylane_lightning/core/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
Version number (major.minor.patch[-label])
1717
"""
1818

19-
__version__ = "0.40.0-dev25"
19+
__version__ = "0.40.0-dev26"

pennylane_lightning/core/src/algorithms/tests/Test_AdjointJacobian.cpp

+32-39
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ using namespace Pennylane::Util;
2525

2626
#ifdef _ENABLE_PLQUBIT
2727
constexpr bool BACKEND_FOUND = true;
28-
constexpr bool SUPPORTS_CTRL = true;
2928

3029
#include "AdjointJacobianLQubit.hpp"
3130
#include "ObservablesLQubit.hpp"
@@ -41,7 +40,6 @@ using namespace Pennylane::LightningQubit::Observables;
4140

4241
#elif _ENABLE_PLKOKKOS == 1
4342
constexpr bool BACKEND_FOUND = true;
44-
constexpr bool SUPPORTS_CTRL = false;
4543

4644
#include "AdjointJacobianKokkos.hpp"
4745
#include "ObservablesKokkos.hpp"
@@ -57,7 +55,6 @@ using namespace Pennylane::LightningKokkos::Observables;
5755

5856
#elif _ENABLE_PLGPU == 1
5957
constexpr bool BACKEND_FOUND = true;
60-
constexpr bool SUPPORTS_CTRL = true;
6158
#include "AdjointJacobianGPU.hpp"
6259
#include "ObservablesGPU.hpp"
6360
#include "TestHelpersStateVectors.hpp"
@@ -72,7 +69,6 @@ using namespace Pennylane::LightningGPU::Observables;
7269

7370
#else
7471
constexpr bool BACKEND_FOUND = false;
75-
constexpr bool SUPPORTS_CTRL = false;
7672
using TestStateVectorBackends = Pennylane::Util::TypeList<void>;
7773

7874
template <class StateVector> struct StateVectorToName {};
@@ -140,41 +136,38 @@ template <typename TypeList> void testAdjointJacobian() {
140136

141137
DYNAMIC_SECTION("Op=PhaseShift, Obs=Y - "
142138
<< StateVectorToName<StateVectorT>::name) {
143-
if (SUPPORTS_CTRL) {
144-
const std::vector<std::size_t> tp{0};
145-
const std::size_t num_qubits = GENERATE(2, 3, 4);
146-
147-
const std::size_t num_params = 3;
148-
const std::size_t num_obs = 1;
149-
const auto obs = std::make_shared<NamedObs<StateVectorT>>(
150-
"PauliY", std::vector<std::size_t>{num_qubits - 1});
151-
std::vector<PrecisionT> jacobian(num_obs * tp.size(), 0);
139+
const std::vector<std::size_t> tp{0};
140+
const std::size_t num_qubits = GENERATE(2, 3, 4);
141+
142+
const std::size_t num_params = 3;
143+
const std::size_t num_obs = 1;
144+
const auto obs = std::make_shared<NamedObs<StateVectorT>>(
145+
"PauliY", std::vector<std::size_t>{num_qubits - 1});
146+
std::vector<PrecisionT> jacobian(num_obs * tp.size(), 0);
147+
148+
for (const auto &p : param) {
149+
std::vector<std::vector<std::size_t>> controls{
150+
std::vector<std::size_t>(num_qubits - 1)};
151+
std::iota(controls[0].begin(), controls[0].end(), 0);
152+
std::vector<std::vector<bool>> control_values{
153+
std::vector<bool>(num_qubits - 1, true)};
154+
auto ops = OpsData<StateVectorT>(
155+
{"PhaseShift"}, {{p}}, {{num_qubits - 1}}, {false}, {{}},
156+
controls, control_values);
152157

153-
for (const auto &p : param) {
154-
std::vector<std::vector<std::size_t>> controls{
155-
std::vector<std::size_t>(num_qubits - 1)};
156-
std::iota(controls[0].begin(), controls[0].end(), 0);
157-
std::vector<std::vector<bool>> control_values{
158-
std::vector<bool>(num_qubits - 1, true)};
159-
auto ops = OpsData<StateVectorT>(
160-
{"PhaseShift"}, {{p}}, {{num_qubits - 1}}, {false},
161-
{{}}, controls, control_values);
162-
163-
std::vector<ComplexT> cdata(1U << num_qubits);
164-
cdata[cdata.size() - 2] =
165-
Pennylane::Util::INVSQRT2<PrecisionT>();
166-
cdata[cdata.size() - 1] =
167-
Pennylane::Util::INVSQRT2<PrecisionT>();
168-
169-
StateVectorT psi(cdata.data(), cdata.size());
170-
JacobianData<StateVectorT> tape{
171-
num_params, psi.getLength(), psi.getData(), {obs}, ops,
172-
tp};
173-
adj.adjointJacobian(std::span{jacobian}, tape, psi, true);
174-
175-
CAPTURE(jacobian);
176-
CHECK(cos(p) == Approx(jacobian[0]));
177-
}
158+
std::vector<ComplexT> cdata(1U << num_qubits);
159+
cdata[cdata.size() - 2] =
160+
Pennylane::Util::INVSQRT2<PrecisionT>();
161+
cdata[cdata.size() - 1] =
162+
Pennylane::Util::INVSQRT2<PrecisionT>();
163+
164+
StateVectorT psi(cdata.data(), cdata.size());
165+
JacobianData<StateVectorT> tape{
166+
num_params, psi.getLength(), psi.getData(), {obs}, ops, tp};
167+
adj.adjointJacobian(std::span{jacobian}, tape, psi, true);
168+
169+
CAPTURE(jacobian);
170+
CHECK(cos(p) == Approx(jacobian[0]));
178171
}
179172
}
180173

@@ -671,4 +664,4 @@ TEST_CASE("Algorithms::adjointJacobian", "[Algorithms]") {
671664
if constexpr (BACKEND_FOUND) {
672665
testAdjointJacobian<TestStateVectorBackends>();
673666
}
674-
}
667+
}

0 commit comments

Comments
 (0)