Skip to content

Commit

Permalink
Add C(MultiRZ) and C(Rot) gates (#614)
Browse files Browse the repository at this point in the history
* Initial commit for C(MultiRZ).

* Add an-ctrl multirz generator.

* Auto update version

* Add C(Rot) gate and tests.

* Update changelog.

* AddC++ tests for C(Rot/MultiRZ).

* Fix tidy warning.

* Auto update version

* trigger ci

* Auto update version

* trigger ci

* trigger ci

* Add C(Rot) adjoint tests.

* Auto update version

* trigger ci

* Test gate/controls against DQ+backprop.

---------

Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
vincentmr and github-actions[bot] authored Feb 23, 2024
1 parent 9719855 commit 86f22a7
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 82 deletions.
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

### Improvements

* `C(MultiRZ)` and `C(Rot)` gates are natively supported (with `LM` kernels).
[(#614)](https://github.com/PennyLaneAI/pennylane-lightning/pull/614)

* Add adjoint support for `GlobalPhase` in Lightning-GPU and Lightning-Kokkos.
[(#615)](https://github.com/PennyLaneAI/pennylane-lightning/pull/615)

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.35.0-dev16"
__version__ = "0.35.0-dev17"
11 changes: 11 additions & 0 deletions pennylane_lightning/core/src/gates/Constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ namespace Pennylane::Gates::Constant {
* @brief List of multi-qubit gates
*/
[[maybe_unused]] constexpr std::array multi_qubit_gates{GateOperation::MultiRZ};
[[maybe_unused]] constexpr std::array controlled_multi_qubit_gates{
ControlledGateOperation::MultiRZ};
/**
* @brief List of multi-qubit generators
*/
[[maybe_unused]] constexpr std::array multi_qubit_generators{
GeneratorOperation::MultiRZ,
};
[[maybe_unused]] constexpr std::array controlled_multi_qubit_generators{
ControlledGeneratorOperation::MultiRZ,
};
/**
* @brief List of multi-qubit matrix operation
*/
Expand Down Expand Up @@ -93,6 +98,7 @@ using CGateView = typename std::pair<ControlledGateOperation, std::string_view>;
CGateView{ControlledGateOperation::RX, "RX"},
CGateView{ControlledGateOperation::RY, "RY"},
CGateView{ControlledGateOperation::RZ, "RZ"},
CGateView{ControlledGateOperation::Rot, "Rot"},
CGateView{ControlledGateOperation::SWAP, "SWAP"},
CGateView{ControlledGateOperation::IsingXX, "IsingXX"},
CGateView{ControlledGateOperation::IsingXY, "IsingXY"},
Expand All @@ -108,6 +114,7 @@ using CGateView = typename std::pair<ControlledGateOperation, std::string_view>;
"DoubleExcitationMinus"},
CGateView{ControlledGateOperation::DoubleExcitationPlus,
"DoubleExcitationPlus"},
CGateView{ControlledGateOperation::MultiRZ, "MultiRZ"},
CGateView{ControlledGateOperation::GlobalPhase, "GlobalPhase"},
};

Expand Down Expand Up @@ -171,6 +178,7 @@ using CGeneratorView =
"DoubleExcitationMinus"},
CGeneratorView{ControlledGeneratorOperation::DoubleExcitationPlus,
"DoubleExcitationPlus"},
CGeneratorView{ControlledGeneratorOperation::MultiRZ, "MultiRZ"},
CGeneratorView{ControlledGeneratorOperation::GlobalPhase, "GlobalPhase"},
};

Expand Down Expand Up @@ -245,6 +253,7 @@ using CGateNWires = typename std::pair<ControlledGateOperation, size_t>;
CGateNWires{ControlledGateOperation::RX, 1},
CGateNWires{ControlledGateOperation::RY, 1},
CGateNWires{ControlledGateOperation::RZ, 1},
CGateNWires{ControlledGateOperation::Rot, 1},
CGateNWires{ControlledGateOperation::SWAP, 2},
CGateNWires{ControlledGateOperation::IsingXX, 2},
CGateNWires{ControlledGateOperation::IsingXY, 2},
Expand Down Expand Up @@ -362,6 +371,7 @@ using CGateNParams = typename std::pair<ControlledGateOperation, size_t>;
CGateNParams{ControlledGateOperation::RX, 1},
CGateNParams{ControlledGateOperation::RY, 1},
CGateNParams{ControlledGateOperation::RZ, 1},
CGateNParams{ControlledGateOperation::Rot, 3},
CGateNParams{ControlledGateOperation::SWAP, 0},
CGateNParams{ControlledGateOperation::IsingXX, 1},
CGateNParams{ControlledGateOperation::IsingXY, 1},
Expand All @@ -373,6 +383,7 @@ using CGateNParams = typename std::pair<ControlledGateOperation, size_t>;
CGateNParams{ControlledGateOperation::DoubleExcitation, 1},
CGateNParams{ControlledGateOperation::DoubleExcitationMinus, 1},
CGateNParams{ControlledGateOperation::DoubleExcitationPlus, 1},
CGateNParams{ControlledGateOperation::MultiRZ, 1},
CGateNParams{ControlledGateOperation::GlobalPhase, 1},
};
} // namespace Pennylane::Gates::Constant
3 changes: 3 additions & 0 deletions pennylane_lightning/core/src/gates/GateOperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ enum class ControlledGateOperation : uint32_t {
RX,
RY,
RZ,
Rot,
/* Two-qubit gates */
SWAP,
IsingXX,
Expand All @@ -96,6 +97,7 @@ enum class ControlledGateOperation : uint32_t {
DoubleExcitationMinus,
DoubleExcitationPlus,
/* Multi-qubit gates */
MultiRZ,
GlobalPhase,
/* END (placeholder) */
END
Expand Down Expand Up @@ -147,6 +149,7 @@ enum class ControlledGeneratorOperation : uint32_t {
DoubleExcitation,
DoubleExcitationMinus,
DoubleExcitationPlus,
MultiRZ,
GlobalPhase,
/* END (placeholder) */
END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ void assignKernelsForControlledGateOp_Default() {
instance.assignKernelForOp(ControlledGateOperation::RZ, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);
instance.assignKernelForOp(ControlledGateOperation::Rot, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);

instance.assignKernelForOp(ControlledGateOperation::SWAP, all_threading,
all_memory_model, all_qubit_numbers,
Expand Down Expand Up @@ -300,6 +303,10 @@ void assignKernelsForControlledGateOp_Default() {
instance.assignKernelForOp(ControlledGateOperation::DoubleExcitationPlus,
all_threading, all_memory_model,
all_qubit_numbers, KernelType::LM);
/* Multi-qubit gates */
instance.assignKernelForOp(ControlledGateOperation::MultiRZ, all_threading,
all_memory_model, all_qubit_numbers,
KernelType::LM);
instance.assignKernelForOp(ControlledGateOperation::GlobalPhase,
all_threading, all_memory_model,
all_qubit_numbers, KernelType::LM);
Expand Down Expand Up @@ -350,6 +357,9 @@ void assignKernelsForControlledGeneratorOp_Default() {
instance.assignKernelForOp(
ControlledGeneratorOperation::DoubleExcitationPlus, all_threading,
all_memory_model, all_qubit_numbers, KernelType::LM);
instance.assignKernelForOp(ControlledGeneratorOperation::MultiRZ,
all_threading, all_memory_model,
all_qubit_numbers, KernelType::LM);
instance.assignKernelForOp(ControlledGeneratorOperation::GlobalPhase,
all_threading, all_memory_model,
all_qubit_numbers, KernelType::LM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
&GateImplementation::template applyNCRZ<PrecisionT, ParamT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
ControlledGateOperation::Rot> {
constexpr static auto value =
&GateImplementation::template applyNCRot<PrecisionT, ParamT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
ControlledGateOperation::SWAP> {
constexpr static auto value =
Expand Down Expand Up @@ -411,6 +417,12 @@ struct ControlledGateOpToMemberFuncPtr<
ParamT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
ControlledGateOperation::MultiRZ> {
constexpr static auto value =
&GateImplementation::template applyNCMultiRZ<PrecisionT, ParamT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct ControlledGateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
ControlledGateOperation::GlobalPhase> {
constexpr static auto value =
Expand Down Expand Up @@ -663,6 +675,12 @@ struct ControlledGeneratorOpToMemberFuncPtr<
PrecisionT>;
};
template <class PrecisionT, class GateImplementation>
struct ControlledGeneratorOpToMemberFuncPtr<
PrecisionT, GateImplementation, ControlledGeneratorOperation::MultiRZ> {
constexpr static auto value =
&GateImplementation::template applyNCGeneratorMultiRZ<PrecisionT>;
};
template <class PrecisionT, class GateImplementation>
struct ControlledGeneratorOpToMemberFuncPtr<
PrecisionT, GateImplementation, ControlledGeneratorOperation::GlobalPhase> {
constexpr static auto value =
Expand Down Expand Up @@ -800,7 +818,8 @@ struct GateFuncPtr<PrecisionT, ParamT, 3> {
*/
template <class PrecisionT, class ParamT, size_t num_params>
struct ControlledGateFuncPtr {
static_assert(num_params < 2, "The given num_params is not supported.");
static_assert(num_params < 2 || num_params == 3,
"The given num_params is not supported.");
};
template <class PrecisionT, class ParamT>
struct ControlledGateFuncPtr<PrecisionT, ParamT, 0> {
Expand All @@ -816,6 +835,14 @@ struct ControlledGateFuncPtr<PrecisionT, ParamT, 1> {
const std::vector<bool> &,
const std::vector<size_t> &, bool, ParamT);
};
template <class PrecisionT, class ParamT>
struct ControlledGateFuncPtr<PrecisionT, ParamT, 3> {
using Type = void (*)(std::complex<PrecisionT> *, size_t,
const std::vector<size_t> &,
const std::vector<bool> &,
const std::vector<size_t> &, bool, ParamT, ParamT,
ParamT);
};

/**
* @brief Pointer type for a generator operation
Expand Down Expand Up @@ -977,6 +1004,19 @@ callControlledGateOps(ControlledGateFuncPtrT<PrecisionT, ParamT, 1> func,
params[0]);
}

template <class PrecisionT, class ParamT>
inline void
callControlledGateOps(ControlledGateFuncPtrT<PrecisionT, ParamT, 3> func,
std::complex<PrecisionT> *data, size_t num_qubits,
const std::vector<size_t> &controlled_wires,
const std::vector<bool> &controlled_values,
const std::vector<size_t> &wires, bool inverse,
const std::vector<ParamT> &params) {
PL_ASSERT(params.size() == 3);
func(data, num_qubits, controlled_wires, controlled_values, wires, inverse,
params[0], params[1], params[2]);
}

/// @}
/**
* @brief Call a generator operation.
Expand Down
Loading

0 comments on commit 86f22a7

Please sign in to comment.