Skip to content

Commit

Permalink
(LK-C-2) Add controlled named 2/4-qubit gate (e.g. NCIsingXX) support…
Browse files Browse the repository at this point in the history
… to Lightning Kokkos (#953)

### Before submitting

Please complete the following checklist when submitting a PR:

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

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

- [ ] 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:**

**Description of the Change:**
This PR adds support for named controlled 2/4-qubit gates (e.g. Swap /
IsingXX / SingleExcitation / Double Excitation). These are defined in
`BasicGateFunctor.hpp`, and are applied through `applyNC2Functor` and
`applyNC4Functor` defined in the same file.

**Benefits:**
Performance benchmarks for gates are shown here:
https://www.notion.so/xanaduai/Lightning-Kokkos-Native-Controlled-Operation-Gate-Benchmarks-12ebc6bd17648017a2dcd237748b24fe

**Possible Drawbacks:**

**Related GitHub Issues:**

[sc-76774]
  • Loading branch information
josephleekl authored Nov 12, 2024
1 parent e89065f commit 1979bd7
Show file tree
Hide file tree
Showing 9 changed files with 1,686 additions and 516 deletions.
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-dev6"
__version__ = "0.40.0-dev4"

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,102 @@ TEMPLATE_TEST_CASE("StateVectorKokkos::applyOperation non-param "
}
}

TEMPLATE_TEST_CASE("StateVectorKokkos::applyOperation non-param "
"two-qubit with controls",
"[StateVectorKokkos_NonParam]", float, double) {
using StateVectorT = StateVectorKokkos<TestType>;

const TestType EP = 1e-4;
const std::size_t num_qubits = 4;
const bool inverse = GENERATE(true, false);
const std::size_t control = GENERATE(0, 1, 2, 3);
const std::size_t wire0 = GENERATE(0, 1, 2, 3);
const std::size_t wire1 = GENERATE(0, 1, 2, 3);

auto ini_st = createNonTrivialState<StateVectorT>(num_qubits);

SECTION("N-controlled SWAP") {
if (control != wire0 && control != wire1 && wire0 != wire1) {

Check notice on line 1102 in pennylane_lightning/core/src/simulators/lightning_kokkos/gates/tests/Test_StateVectorKokkos_NonParam.cpp

View check run for this annotation

codefactor.io / CodeFactor

pennylane_lightning/core/src/simulators/lightning_kokkos/gates/tests/Test_StateVectorKokkos_NonParam.cpp#L1102

Redundant blank line at the start of a code block should be deleted. (whitespace/blank_line)
StateVectorT kokkos_sv0{ini_st.data(), ini_st.size()};
StateVectorT kokkos_sv1{ini_st.data(), ini_st.size()};
kokkos_sv0.applyOperation("CSWAP", {control, wire0, wire1},
inverse);
auto matrix = getSWAP<Kokkos::complex, TestType>();
kokkos_sv1.applyOperation("SWAP", std::vector<std::size_t>{control},
std::vector<bool>{true},
std::vector<std::size_t>{wire0, wire1},
inverse);
auto result_sv0 = kokkos_sv0.getDataVector();
auto result_sv1 = kokkos_sv1.getDataVector();
for (std::size_t j = 0; j < exp2(num_qubits); j++) {
CHECK(real(result_sv0[j]) ==
Approx(real(result_sv1[j])).margin(EP));
CHECK(imag(result_sv0[j]) ==
Approx(imag(result_sv1[j])).margin(EP));
}
}
}
}

TEMPLATE_TEST_CASE("StateVectorKokkos::applyOperation non-param "
"two-qubit with multiple-controls",
"[StateVectorKokkos_NonParam]", float, double) {
SECTION("2-controlled SWAP") {
using ComplexT = StateVectorKokkos<TestType>::ComplexT;
const std::size_t num_qubits = 4;

std::vector<ComplexT> ini_st{ComplexT{0.33377149, 0.2511291},
ComplexT{0.33013572, -0.14778699},
ComplexT{0.17288832, -0.041744},
ComplexT{0.11004883, -0.15105962},
ComplexT{0.17120967, 0.04376507},
ComplexT{0.33010645, 0.20088901},
ComplexT{-0.12770624, -0.17329647},
ComplexT{-0.34301996, 0.11944278},
ComplexT{0.0195779, -0.03687076},
ComplexT{0.34155068, 0.07464519},
ComplexT{0.00730597, 0.03670807},
ComplexT{0.08876188, -0.18019018},
ComplexT{-0.04946055, -0.10383813},
ComplexT{0.0715367, 0.04895361},
ComplexT{-0.12377521, -0.04781011},
ComplexT{-0.14509767, 0.2102171}};

std::vector<ComplexT> expected{ComplexT{0.33377149, 0.2511291},
ComplexT{0.33013572, -0.14778699},
ComplexT{0.17288832, -0.041744},
ComplexT{0.11004883, -0.15105962},
ComplexT{0.17120967, 0.04376507},
ComplexT{0.33010645, 0.20088901},
ComplexT{-0.12770624, -0.17329647},
ComplexT{-0.34301996, 0.11944278},
ComplexT{0.0195779, -0.03687076},
ComplexT{-0.04946055, -0.10383813},
ComplexT{0.00730597, 0.03670807},
ComplexT{0.08876188, -0.18019018},
ComplexT{0.34155068, 0.07464519},
ComplexT{0.0715367, 0.04895361},
ComplexT{-0.12377521, -0.04781011},
ComplexT{-0.14509767, 0.2102171}};

SECTION("Apply using dispatcher") {
StateVectorKokkos<TestType> kokkos_sv{num_qubits};
std::vector<ComplexT> result_sv(kokkos_sv.getLength(), {0, 0});

kokkos_sv.HostToDevice(ini_st.data(), ini_st.size());
kokkos_sv.applyOperation("SWAP", {0, 2}, {true, false}, {1, 3},
false);
kokkos_sv.DeviceToHost(result_sv.data(), result_sv.size());

for (std::size_t j = 0; j < exp2(num_qubits); j++) {
CHECK(imag(expected[j]) == Approx(imag(result_sv[j])));
CHECK(real(expected[j]) == Approx(real(result_sv[j])));
}
}
}
}

TEMPLATE_TEST_CASE("StateVectorKokkos::SetStateVector",
"[StateVectorKokkos_Nonparam]", float, double) {
using PrecisionT = TestType;
Expand Down
Loading

0 comments on commit 1979bd7

Please sign in to comment.