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

SX gate implementation for lightning.qubit #727

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8e302b0
Update Changelog
AmintorDusko Apr 29, 2024
010d543
update release candidate version
AmintorDusko Apr 29, 2024
250c1db
run agains pennylane rc
AmintorDusko Apr 29, 2024
dbbf0b5
update relese cadidate version
AmintorDusko Apr 29, 2024
b5402dd
Increase tolerance for a flaky test (test_single_return_value)
AmintorDusko Apr 29, 2024
34877ff
Update `LightningBaseFallBack` to raise an error instead of falling b…
vincentmr Apr 30, 2024
0412295
trigger CIs
AmintorDusko May 1, 2024
2236e5f
Merge branch 'v0.36.0_rc' of https://github.com/PennyLaneAI/pennylane…
AmintorDusko May 1, 2024
fdd994f
Introduce pytest-splits (#696)
vincentmr May 2, 2024
e11e58b
Update changelog (#706)
maliasadi May 2, 2024
834ca47
Use wire order specified on instantiation (#705)
albi3ro May 3, 2024
432f28e
Update wire order support for probabilities (#707)
mlxd May 3, 2024
7ef2d3c
Sync version
AmintorDusko May 3, 2024
27fdb9d
pacify CodeFactor
AmintorDusko May 3, 2024
d876ed0
make CodeFactor ignore all JET code
AmintorDusko May 3, 2024
7851423
trigger ci
vincentmr May 3, 2024
e6d81f9
trigger ci
vincentmr May 3, 2024
a025f09
build wheels for test
AmintorDusko May 6, 2024
0ec727c
remove Win LKokkos upload-pypi [skip-ci]
AmintorDusko May 6, 2024
e1705d3
[skip ci]
AmintorDusko May 6, 2024
d2147d7
trigger wheels CIs with Release event
AmintorDusko May 6, 2024
77acca4
update version
AmintorDusko May 6, 2024
c7c0b40
fix upload wheels artifact for release
AmintorDusko May 6, 2024
5ebc2e5
First dev for a fictitious gate
LuisAlfredoNu May 8, 2024
e5e5b3e
Added the SX gate to LM, including the tests
LuisAlfredoNu May 13, 2024
99b055a
Optimized SX gate computation
LuisAlfredoNu May 15, 2024
9881d67
Merge branch 'devSX'
LuisAlfredoNu May 15, 2024
2e324c9
Merge branch 'master' into master
LuisAlfredoNu May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pennylane_lightning/core/src/gates/Constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ using GateView = typename std::pair<GateOperation, std::string_view>;
GateView{GateOperation::Hadamard, "Hadamard"},
GateView{GateOperation::S, "S"},
GateView{GateOperation::T, "T"},
GateView{GateOperation::SX, "SX"},
GateView{GateOperation::PhaseShift, "PhaseShift"},
GateView{GateOperation::RX, "RX"},
GateView{GateOperation::RY, "RY"},
Expand Down Expand Up @@ -94,6 +95,7 @@ using CGateView = typename std::pair<ControlledGateOperation, std::string_view>;
CGateView{ControlledGateOperation::Hadamard, "Hadamard"},
CGateView{ControlledGateOperation::S, "S"},
CGateView{ControlledGateOperation::T, "T"},
CGateView{ControlledGateOperation::SX, "SX"},
CGateView{ControlledGateOperation::PhaseShift, "PhaseShift"},
CGateView{ControlledGateOperation::RX, "RX"},
CGateView{ControlledGateOperation::RY, "RY"},
Expand Down Expand Up @@ -208,6 +210,7 @@ using GateNWires = typename std::pair<GateOperation, std::size_t>;
GateNWires{GateOperation::Hadamard, 1},
GateNWires{GateOperation::S, 1},
GateNWires{GateOperation::T, 1},
GateNWires{GateOperation::SX, 1},
GateNWires{GateOperation::PhaseShift, 1},
GateNWires{GateOperation::RX, 1},
GateNWires{GateOperation::RY, 1},
Expand Down Expand Up @@ -245,6 +248,7 @@ using CGateNWires = typename std::pair<ControlledGateOperation, std::size_t>;
CGateNWires{ControlledGateOperation::Hadamard, 1},
CGateNWires{ControlledGateOperation::S, 1},
CGateNWires{ControlledGateOperation::T, 1},
CGateNWires{ControlledGateOperation::SX, 1},
CGateNWires{ControlledGateOperation::PhaseShift, 1},
CGateNWires{ControlledGateOperation::RX, 1},
CGateNWires{ControlledGateOperation::RY, 1},
Expand Down Expand Up @@ -322,6 +326,7 @@ using GateNParams = typename std::pair<GateOperation, std::size_t>;
GateNParams{GateOperation::Hadamard, 0},
GateNParams{GateOperation::S, 0},
GateNParams{GateOperation::T, 0},
GateNParams{GateOperation::SX, 0},
GateNParams{GateOperation::PhaseShift, 1},
GateNParams{GateOperation::RX, 1},
GateNParams{GateOperation::RY, 1},
Expand Down Expand Up @@ -364,6 +369,7 @@ using CGateNParams = typename std::pair<ControlledGateOperation, std::size_t>;
CGateNParams{ControlledGateOperation::S, 0},
CGateNParams{ControlledGateOperation::T, 0},
CGateNParams{ControlledGateOperation::PhaseShift, 1},
CGateNParams{ControlledGateOperation::SX, 0},
CGateNParams{ControlledGateOperation::RX, 1},
CGateNParams{ControlledGateOperation::RY, 1},
CGateNParams{ControlledGateOperation::RZ, 1},
Expand Down
2 changes: 2 additions & 0 deletions pennylane_lightning/core/src/gates/GateOperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum class GateOperation : uint32_t {
Hadamard,
S,
T,
SX,
PhaseShift,
RX,
RY,
Expand Down Expand Up @@ -79,6 +80,7 @@ enum class ControlledGateOperation : uint32_t {
Hadamard,
S,
T,
SX,
PhaseShift,
RX,
RY,
Expand Down
15 changes: 15 additions & 0 deletions pennylane_lightning/core/src/gates/Gates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ static constexpr auto getHadamard() -> std::vector<ComplexT<T>> {
INVSQRT2<ComplexT, T>(), -INVSQRT2<ComplexT, T>()};
}

/**
* @brief Create a matrix representation of the SX gate data in row-major
* format.
*
* @tparam ComplexT<T> Required precision of gate (`float` or `double`).
* @return constexpr std::vector<ComplexT<T>> Return constant expression
* of SX data.
*/
template <template <typename...> class ComplexT, typename T>
static constexpr auto getSX() -> std::vector<ComplexT<T>> {
const ComplexT<T> z0{0.5, 0.5};
const ComplexT<T> z1{0.5, -0.5};
return {z0, z1, z1, z0};
}

/**
* @brief Create a matrix representation of the S gate data in row-major format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ void assignKernelsForGateOp_Default() {
instance.assignKernelForOp(GateOperation::T, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);
instance.assignKernelForOp(GateOperation::SX, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);
instance.assignKernelForOp(GateOperation::PhaseShift, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);
Expand Down Expand Up @@ -254,6 +257,9 @@ void assignKernelsForControlledGateOp_Default() {
instance.assignKernelForOp(ControlledGateOperation::T, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);
instance.assignKernelForOp(ControlledGateOperation::SX, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);
instance.assignKernelForOp(ControlledGateOperation::PhaseShift,
all_threading, all_memory_model,
all_qubit_numbers, KernelType::LM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ struct GateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
&GateImplementation::template applyT<PrecisionT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct GateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
GateOperation::SX> {
constexpr static auto value =
&GateImplementation::template applySX<PrecisionT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct GateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
GateOperation::PhaseShift> {
constexpr static auto value =
Expand Down Expand Up @@ -309,6 +315,12 @@ struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
&GateImplementation::template applyNCT<PrecisionT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
ControlledGateOperation::SX> {
constexpr static auto value =
&GateImplementation::template applyNCSX<PrecisionT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
ControlledGateOperation::PhaseShift> {
constexpr static auto value =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ GateImplementationsLM::applyT<float>(std::complex<float> *, std::size_t,
template void
GateImplementationsLM::applyT<double>(std::complex<double> *, std::size_t,
const std::vector<std::size_t> &, bool);
template void
GateImplementationsLM::applySX<float>(std::complex<float> *, size_t,
const std::vector<size_t> &, bool);
template void
GateImplementationsLM::applySX<double>(std::complex<double> *, size_t,
const std::vector<size_t> &, bool);
template void GateImplementationsLM::applyPhaseShift<float, float>(
std::complex<float> *, std::size_t, const std::vector<std::size_t> &, bool,
float);
Expand Down Expand Up @@ -323,8 +329,14 @@ template void GateImplementationsLM::applyNCT<float>(
std::complex<float> *, std::size_t, const std::vector<std::size_t> &,
const std::vector<bool> &, const std::vector<std::size_t> &, bool);
template void GateImplementationsLM::applyNCT<double>(
std::complex<double> *, std::size_t, const std::vector<std::size_t> &,
const std::vector<bool> &, const std::vector<std::size_t> &, bool);
std::complex<double> *, size_t, const std::vector<size_t> &,
const std::vector<bool> &, const std::vector<size_t> &, bool);
template void GateImplementationsLM::applyNCSX<float>(
std::complex<float> *, size_t, const std::vector<size_t> &,
const std::vector<bool> &, const std::vector<size_t> &, bool);
template void GateImplementationsLM::applyNCSX<double>(
std::complex<double> *, size_t, const std::vector<size_t> &,
const std::vector<bool> &, const std::vector<size_t> &, bool);
template void GateImplementationsLM::applyNCPhaseShift<float, float>(
std::complex<float> *, std::size_t, const std::vector<std::size_t> &,
const std::vector<bool> &, const std::vector<std::size_t> &, bool, float);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class GateImplementationsLM : public PauliGenerator<GateImplementationsLM> {
GateOperation::Hadamard,
GateOperation::S,
GateOperation::T,
GateOperation::SX,
GateOperation::PhaseShift,
GateOperation::RX,
GateOperation::RY,
Expand Down Expand Up @@ -158,6 +159,7 @@ class GateImplementationsLM : public PauliGenerator<GateImplementationsLM> {
ControlledGateOperation::Hadamard,
ControlledGateOperation::S,
ControlledGateOperation::T,
ControlledGateOperation::SX,
ControlledGateOperation::PhaseShift,
ControlledGateOperation::RX,
ControlledGateOperation::RY,
Expand Down Expand Up @@ -818,6 +820,7 @@ class GateImplementationsLM : public PauliGenerator<GateImplementationsLM> {
applyNCHadamard(arr, num_qubits, {}, {}, wires, inverse);
}


template <class PrecisionT>
static void
applyNCS(std::complex<PrecisionT> *arr, const std::size_t num_qubits,
Expand Down Expand Up @@ -885,6 +888,44 @@ class GateImplementationsLM : public PauliGenerator<GateImplementationsLM> {
applyNCT(arr, num_qubits, {}, {}, wires, inverse);
}

template <class PrecisionT>
static void applyNCSX(std::complex<PrecisionT> *arr,
const size_t num_qubits,
const std::vector<size_t> &controlled_wires,
const std::vector<bool> &controlled_values,
const std::vector<size_t> &wires,
const bool inverse) {
using ParamT = PrecisionT;

const PrecisionT half = 0.5;
const std::complex<PrecisionT> z0{half, (inverse) ? -half : half};
const std::complex<PrecisionT> z1 = std::conj(z0);

auto core_function = [&z0,&z1](std::complex<PrecisionT> *arr,
const std::size_t i0, const std::size_t i1) {
const std::complex<PrecisionT> v0 = arr[i0];
const std::complex<PrecisionT> v1 = arr[i1];
arr[i0] = z0 * v0 + z1 * v1;
arr[i1] = z1 * v0 + z0 * v1;
};
if (controlled_wires.empty()) {
applyNC1<PrecisionT, ParamT, decltype(core_function), false>(
arr, num_qubits, controlled_wires, controlled_values, wires,
core_function);
} else {
applyNC1<PrecisionT, ParamT, decltype(core_function), true>(
arr, num_qubits, controlled_wires, controlled_values, wires,
core_function);
}
}

template <class PrecisionT>
static void
applySX(std::complex<PrecisionT> *arr, const size_t num_qubits,
const std::vector<size_t> &wires, const bool inverse) {
applyNCSX(arr, num_qubits, {}, {}, wires, inverse);
}

template <class PrecisionT, class ParamT = PrecisionT>
static void
applyNCPhaseShift(std::complex<PrecisionT> *arr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,33 @@ template <typename PrecisionT, class GateImplementation> void testApplyT() {
}
}
PENNYLANE_RUN_TEST(T);

template <typename PrecisionT, class GateImplementation>
void testApplySX() {
// Test using |000> state
using ComplexT = std::complex<PrecisionT>;
const size_t num_qubits = 3;

ComplexT z(0.0, 0.0);
// ComplexT p(static_cast<PrecisionT>(0.5),static_cast<PrecisionT>(0.5));
// ComplexT m(static_cast<PrecisionT>(0.5),static_cast<PrecisionT>(-0.5));
ComplexT p(0.5, 0.5);
ComplexT m(0.5, -0.5);

const std::vector<std::vector<ComplexT>> expected_results = {
{p, z, z, z, m, z, z, z},
{p, z, m, z, z, z, z, z},
{p, m, z, z, z, z, z, z}};

for (size_t index = 0; index < num_qubits; index++) {
auto st = createZeroState<ComplexT>(num_qubits);

GateImplementation::applySX(st.data(), num_qubits, {index}, false);

CHECK(st == approx(expected_results[index]));
}
}
PENNYLANE_RUN_TEST(SX);
/*******************************************************************************
* Two-qubit gates
******************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class DummyImplementation {
PENNYLANE_TESTS_DEFINE_GATE_OP(Hadamard, 0)
PENNYLANE_TESTS_DEFINE_GATE_OP(S, 0)
PENNYLANE_TESTS_DEFINE_GATE_OP(T, 0)
PENNYLANE_TESTS_DEFINE_GATE_OP(SX, 0)
PENNYLANE_TESTS_DEFINE_GATE_OP(PhaseShift, 1)
PENNYLANE_TESTS_DEFINE_GATE_OP(RX, 1)
PENNYLANE_TESTS_DEFINE_GATE_OP(RY, 1)
Expand Down
1 change: 1 addition & 0 deletions pennylane_lightning/core/src/utils/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Config file for the path to scipy.libs at compile time.
*/


#ifndef CONFIG_H
#define CONFIG_H
#define SCIPY_LIBS_PATH ""
Expand Down
1 change: 1 addition & 0 deletions pennylane_lightning/lightning_qubit/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def simulate_and_vjp(
"C(Hadamard)",
"C(S)",
"C(T)",
"C(SX)",
"C(PhaseShift)",
"C(RX)",
"C(RY)",
Expand Down
1 change: 1 addition & 0 deletions tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ def circuit():
qml.PauliY,
qml.PauliZ,
qml.Hadamard,
qml.SX,
qml.S,
qml.T,
qml.PhaseShift,
Expand Down
Loading