Skip to content

Commit

Permalink
added SX to LK
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisAlfredoNu committed Jan 3, 2025
1 parent 84da495 commit 7519385
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pennylane_lightning/core/src/gates/Constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ using CGateNParams = typename std::pair<ControlledGateOperation, std::size_t>;
CGateNParams{ControlledGateOperation::Hadamard, 0},
CGateNParams{ControlledGateOperation::S, 0},
CGateNParams{ControlledGateOperation::T, 0},
CGateNParams{ControlledGateOperation::PhaseShift, 1},
CGateNParams{ControlledGateOperation::SX, 0},
CGateNParams{ControlledGateOperation::PhaseShift, 1},
CGateNParams{ControlledGateOperation::RX, 1},
CGateNParams{ControlledGateOperation::RY, 1},
CGateNParams{ControlledGateOperation::RZ, 1},
Expand Down
7 changes: 4 additions & 3 deletions pennylane_lightning/core/src/gates/Gates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ static constexpr auto getHadamard() -> std::vector<ComplexT<T>> {
* of SX data.
*/
template <template <typename...> class ComplexT, typename T>
static constexpr auto getSX() -> std::vector<ComplexT<T>> {
constexpr ComplexT<T> z0{0.5, 0.5};
constexpr ComplexT<T> z1{0.5, -0.5};
static constexpr auto getSX(const bool inverse = false) -> std::vector<ComplexT<T>> {
const T half = (inverse) ? -0.5 : 0.5;
const ComplexT<T> z0{0.5, half};
const ComplexT<T> z1{0.5,-half};
return {z0, z1, z1, z0};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,50 @@ void applyS(Kokkos::View<Kokkos::complex<PrecisionT> *> arr_,
inverse);
}

template <class ExecutionSpace, class PrecisionT>
void applyNCSX(Kokkos::View<Kokkos::complex<PrecisionT> *> arr_,
std::size_t num_qubits,
const std::vector<std::size_t> &controlled_wires,
const std::vector<bool> &controlled_values,
const std::vector<std::size_t> &wires, bool inverse = false,
[[maybe_unused]] const std::vector<PrecisionT> &params = {})
{
constexpr PrecisionT half = 0.5;
const Kokkos::complex<PrecisionT> z0{half, (inverse) ? -half : half};
const Kokkos::complex<PrecisionT> z1 = Kokkos::conj(z0);

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

template <class ExecutionSpace, class PrecisionT>
void applySX(Kokkos::View<Kokkos::complex<PrecisionT> *> arr_,
std::size_t num_qubits, const std::vector<std::size_t> &wires,
bool inverse = false,
[[maybe_unused]] const std::vector<PrecisionT> &params = {})
{
applyNCSX<ExecutionSpace, PrecisionT>(arr_, num_qubits, {}, {}, wires,
inverse);
}

template <class ExecutionSpace, class PrecisionT>
void applyNCT(Kokkos::View<Kokkos::complex<PrecisionT> *> arr_,
std::size_t num_qubits,
Expand Down Expand Up @@ -1821,6 +1865,9 @@ void applyNamedOperation(const GateOperation gateop,
case GateOperation::S:
applyS<ExecutionSpace>(arr_, num_qubits, wires, inverse, params);
return;
case GateOperation::SX:
applySX<ExecutionSpace>(arr_, num_qubits, wires, inverse, params);
return;
case GateOperation::T:
applyT<ExecutionSpace>(arr_, num_qubits, wires, inverse, params);
return;
Expand Down Expand Up @@ -1956,6 +2003,10 @@ void applyNCNamedOperation(const ControlledGateOperation gateop,
applyNCS<ExecutionSpace>(arr_, num_qubits, controlled_wires,
controlled_values, wires, inverse, params);
return;
case ControlledGateOperation::SX:
applyNCSX<ExecutionSpace>(arr_, num_qubits, controlled_wires,
controlled_values, wires, inverse, params);
return;
case ControlledGateOperation::T:
applyNCT<ExecutionSpace>(arr_, num_qubits, controlled_wires,
controlled_values, wires, inverse, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,9 @@ class GateImplementationsLM : public PauliGenerator<GateImplementationsLM> {
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<std::size_t> &controlled_wires,
const std::vector<bool> &controlled_values,
const std::vector<size_t> &wires, const bool inverse) {
const std::vector<std::size_t> &wires, const bool inverse) {
using ParamT = PrecisionT;

constexpr PrecisionT half = 0.5;
Expand Down

0 comments on commit 7519385

Please sign in to comment.