Template Class DynamicDispatcher¶
Defined in File DynamicDispatcher.hpp
Class Documentation¶
-
template<typename PrecisionT>
class DynamicDispatcher¶ DynamicDispatcher class.
This is a singleton class that can call a gate/generator operation dynamically. Currently, all gate operations (gates/generators/matrices) are registered to this class when the library is loaded. As all functions besides registration functions are already thread-safe, we can use this class in multithreading environment without any problem. In addition, adding mutex is not required unless kernel functions are registered in multiple threads.
Public Types
-
using CFP_t = std::complex<PrecisionT>¶
-
using GateFunc = std::function<void(std::complex<PrecisionT>*, std::size_t, const std::vector<std::size_t>&, bool, const std::vector<PrecisionT>&)>¶
-
using ControlledGateFunc = std::function<void(std::complex<PrecisionT>*, std::size_t, const std::vector<std::size_t>&, const std::vector<bool>&, const std::vector<std::size_t>&, bool, const std::vector<PrecisionT>&)>¶
-
using GeneratorFunc = Gates::GeneratorFuncPtrT<PrecisionT>¶
-
using ControlledGeneratorFunc = Gates::ControlledGeneratorFuncPtrT<PrecisionT>¶
-
using MatrixFunc = Gates::MatrixFuncPtrT<PrecisionT>¶
-
using ControlledMatrixFunc = Gates::ControlledMatrixFuncPtrT<PrecisionT>¶
Public Functions
-
DynamicDispatcher(const DynamicDispatcher&) = delete¶
-
DynamicDispatcher(DynamicDispatcher&&) = delete¶
-
DynamicDispatcher &operator=(const DynamicDispatcher&) = delete¶
-
DynamicDispatcher &operator=(DynamicDispatcher&&) = delete¶
-
~DynamicDispatcher() = default¶
-
inline auto registeredKernels() const -> std::vector<KernelType>¶
Get all registered kernels.
-
inline auto isRegisteredKernel(KernelType kernel) const¶
Check whether the kernel is registered to a dispatcher.
-
inline void registerKernelName(KernelType kernel, std::string name)¶
Register kernel name.
- Parameters
kernel – Kernel
name – Name of the kernel
-
inline auto getKernelName(KernelType kernel) const -> std::string¶
Get registered name of the kernel.
- Parameters
kernel – Kernel
-
inline auto registeredGatesForKernel(KernelType kernel) const -> std::unordered_set<GateOperation>¶
Get registered gates for the given kernel.
- Parameters
kernel – Kernel
-
inline auto registeredControlledGatesForKernel(KernelType kernel) const -> std::unordered_set<ControlledGateOperation>¶
Get registered controlled gates for the given kernel.
- Parameters
kernel – Kernel
-
inline auto registeredGeneratorsForKernel(KernelType kernel) const -> std::unordered_set<GeneratorOperation>¶
Get registered generators for the given kernel.
- Parameters
kernel – Kernel
-
inline auto registeredControlledGeneratorsForKernel(KernelType kernel) const -> std::unordered_set<ControlledGeneratorOperation>¶
Get registered controlled generators for the given kernel.
- Parameters
kernel – Kernel
-
inline auto registeredMatricesForKernel(KernelType kernel) const -> std::unordered_set<MatrixOperation>¶
Get registered matrix operations for the given kernel.
- Parameters
kernel – Kernel
-
inline auto registeredControlledMatricesForKernel(KernelType kernel) const -> std::unordered_set<ControlledMatrixOperation>¶
Get registered controlled matrix operations for the given kernel.
- Parameters
kernel – Kernel
-
inline auto strToGateOp(const std::string &gate_name) const -> GateOperation¶
Gate name to gate operation.
- Parameters
gate_name – Gate name
-
inline auto strToControlledGateOp(const std::string &gate_name) const -> ControlledGateOperation¶
Gate name to controlled gate operation.
- Parameters
gate_name – Gate name
-
inline auto hasGateOp(const std::string &gate_name) const -> bool¶
Returns true if the gate operation exists.
- Parameters
gate_name – Gate name
-
inline auto strToGeneratorOp(const std::string &gntr_name) const -> GeneratorOperation¶
Generator name to generator operation.
- Parameters
gntr_name – Generator name without “Generator” prefix
-
inline auto strToControlledGeneratorOp(const std::string &gntr_name) const -> ControlledGeneratorOperation¶
Generator name to controlled generator operation.
- Parameters
gntr_name – Generator name without “Generator” prefix
-
template<typename FunctionType>
inline void registerGateOperation(GateOperation gate_op, KernelType kernel, FunctionType &&func)¶ Register a new gate operation for the operation. Can pass a custom kernel.
-
template<typename FunctionType>
inline void registerControlledGateOperation(ControlledGateOperation gate_op, KernelType kernel, FunctionType &&func)¶ Register a new controlled gate operation for the operation. Can pass a custom kernel.
-
template<typename FunctionType>
inline void registerGeneratorOperation(GeneratorOperation gntr_op, KernelType kernel, FunctionType &&func)¶ Register a new gate generator for the operation. Can pass a custom kernel.
-
template<typename FunctionType>
inline void registerControlledGeneratorOperation(ControlledGeneratorOperation gen_op, KernelType kernel, FunctionType &&func)¶ Register a new controlled gate generator for the operation. Can pass a custom kernel.
-
inline void registerMatrixOperation(MatrixOperation mat_op, KernelType kernel, MatrixFunc func)¶
Register a new matrix operation. Can pass a custom kernel.
-
inline void registerControlledMatrixOperation(ControlledMatrixOperation mat_op, KernelType kernel, ControlledMatrixFunc func)¶
Register a new controlled matrix operation.
-
inline bool isRegistered(GateOperation gate_op, KernelType kernel) const¶
Check if a kernel function is registered for the given gate operation and kernel.
- Parameters
gate_op – Gate operation
kernel – Kernel
-
inline bool isRegistered(ControlledGateOperation gate_op, KernelType kernel) const¶
Check if a kernel function is registered for the given controlled gate operation and kernel.
- Parameters
gate_op – Gate operation
kernel – Kernel
-
inline bool isRegistered(GeneratorOperation gntr_op, KernelType kernel) const¶
Check if a kernel function is registered for the given generator operation and kernel.
- Parameters
gntr_op – Generator operation
kernel – Kernel
-
inline bool isRegistered(ControlledGeneratorOperation gen_op, KernelType kernel) const¶
Check if a kernel function is registered for the given controlled generator operation and kernel.
- Parameters
gntr_op – Generator operation
kernel – Kernel
-
inline bool isRegistered(MatrixOperation mat_op, KernelType kernel) const¶
Check if a kernel function is registered for the given matrix operation and kernel.
- Parameters
mat_op – Matrix operation
kernel – Kernel
-
inline bool isRegistered(ControlledMatrixOperation mat_op, KernelType kernel) const¶
Check if a kernel function is registered for the given controlled matrix operation and kernel.
- Parameters
mat_op – Controlled matrix operation
kernel – Kernel
-
inline void applyOperation(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::string &op_name, const std::vector<std::size_t> &wires, bool inverse, const std::vector<PrecisionT> ¶ms = {}) const¶
Apply a single gate to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
op_name – Gate operation name.
wires – Wires to apply gate to.
inverse – Indicates whether to use inverse of gate.
params – Optional parameter list for parametric gates.
-
inline void applyOperation(KernelType kernel, CFP_t *data, std::size_t num_qubits, GateOperation gate_op, const std::vector<std::size_t> &wires, bool inverse, const std::vector<PrecisionT> ¶ms = {}) const¶
Apply a single gate to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
gate_op – Gate operation.
wires – Wires to apply gate to.
inverse – Indicates whether to use inverse of gate.
params – Optional parameter list for parametric gates.
-
inline void applyControlledGate(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::string &op_name, const std::vector<std::size_t> &controlled_wires, const std::vector<bool> &controlled_values, const std::vector<std::size_t> &wires, bool inverse, const std::vector<PrecisionT> ¶ms = {}) const¶
Apply a single controlled gate to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
gate_op – Gate operation.
controlled_wires – Control wires.
controlled_values – Control values (false or true).
wires – Wires to apply gate to.
inverse – Indicates whether to use inverse of gate.
params – Optional parameter list for parametric gates.
-
inline void applyControlledGate(KernelType kernel, CFP_t *data, std::size_t num_qubits, const ControlledGateOperation op_name, const std::vector<std::size_t> &controlled_wires, const std::vector<bool> &controlled_values, const std::vector<std::size_t> &wires, bool inverse, const std::vector<PrecisionT> ¶ms = {}) const¶
Apply a single controlled gate to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
gate_op – Gate operation.
controlled_wires – Control wires.
controlled_values – Control values (false or true).
wires – Wires to apply gate to.
inverse – Indicates whether to use inverse of gate.
params – Optional parameter list for parametric gates.
-
inline void applyOperations(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::vector<std::string> &ops, const std::vector<std::vector<std::size_t>> &wires, const std::vector<bool> &inverse, const std::vector<std::vector<PrecisionT>> ¶ms) const¶
Apply multiple gates to the state-vector using a registered kernel.
- Parameters
data – Pointer to data.
num_qubits – Number of qubits.
ops – List of Gate operation names.
wires – List of wires to apply each gate to.
inverse – List of inverses
params – List of parameters
-
inline void applyOperations(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::vector<std::string> &ops, const std::vector<std::vector<std::size_t>> &wires, const std::vector<bool> &inverse) const¶
Apply multiple (non-parameterized) gates to the state-vector using a registered kernel.
- Parameters
data – Pointer to data.
num_qubits – Number of qubits.
ops – List of Gate operation names.
wires – List of wires to apply each gate to.
inverse – List of inverses
-
inline void applyMatrix(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::complex<PrecisionT> *matrix, const std::vector<std::size_t> &wires, bool inverse) const¶
Apply a given matrix directly to the statevector.
- Parameters
kernel – Kernel to use for this operation
data – Pointer to the statevector.
num_qubits – Number of qubits.
matrix – Perfect square matrix in row-major order.
wires – Wires the gate applies to.
inverse – Indicate whether inverse should be taken.
-
inline void applyMatrix(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::vector<std::complex<PrecisionT>> &matrix, const std::vector<std::size_t> &wires, bool inverse) const¶
Apply a given matrix directly to the statevector.
- Parameters
kernel – Kernel to use for this operation
data – Pointer to the statevector.
num_qubits – Number of qubits.
matrix – Perfect square matrix in row-major order.
wires – Wires the gate applies to.
inverse – Indicate whether inverse should be taken.
-
inline void applyControlledMatrix(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::complex<PrecisionT> *matrix, const std::vector<std::size_t> &controlled_wires, const std::vector<bool> &controlled_values, const std::vector<std::size_t> &wires, bool inverse) const¶
Apply a given matrix and controls directly to the statevector.
- Parameters
kernel – Kernel to use for this operation
data – Pointer to the statevector.
num_qubits – Number of qubits.
matrix – Perfect square matrix in row-major order.
wires – Control wires.
wires – Wires the gate applies to.
inverse – Indicate whether inverse should be taken.
-
inline auto applyGenerator(KernelType kernel, CFP_t *data, std::size_t num_qubits, GeneratorOperation gntr_op, const std::vector<std::size_t> &wires, bool adj) const -> PrecisionT¶
Apply a single generator to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
gntr_op – Generator operation.
wires – Wires to apply gate to.
adj – Indicates whether to use adjoint of gate.
-
inline auto applyGenerator(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::string &op_name, const std::vector<std::size_t> &wires, bool adj) const -> PrecisionT¶
Apply a single generator to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
op_name – Generator operation name.
wires – Wires to apply gate to.
adj – Indicates whether to use adjoint of gate.
-
inline auto applyControlledGenerator(KernelType kernel, CFP_t *data, std::size_t num_qubits, const ControlledGeneratorOperation gntr_op, const std::vector<std::size_t> &controlled_wires, const std::vector<bool> &controlled_values, const std::vector<std::size_t> &wires, bool inverse) const -> PrecisionT¶
Apply a single controlled generator to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
gntr_op – Generator operation.
controlled_wires – Control wires.
controlled_values – Control values (false or true).
wires – Wires to apply gate to.
adj – Indicates whether to use adjoint of gate.
-
inline auto applyControlledGenerator(KernelType kernel, CFP_t *data, std::size_t num_qubits, const std::string &op_name, const std::vector<std::size_t> &controlled_wires, const std::vector<bool> &controlled_values, const std::vector<std::size_t> &wires, bool inverse) const -> PrecisionT¶
Apply a single controlled generator to the state-vector using the given kernel.
- Parameters
kernel – Kernel to run the gate operation.
data – Pointer to data.
num_qubits – Number of qubits.
op_name – Gate operation name.
controlled_wires – Control wires.
controlled_values – Control values (false or true).
wires – Wires to apply gate to.
adj – Indicates whether to use adjoint of gate.
Public Static Functions
-
static inline DynamicDispatcher &getInstance()¶
Get the singleton instance.
-
using CFP_t = std::complex<PrecisionT>¶