-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathDynamicDispatcher.hpp
538 lines (483 loc) · 18.9 KB
/
DynamicDispatcher.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright 2021 Xanadu Quantum Technologies Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file DynamicDispatcher.hpp
* Defines DynamicDispatcher class. Can be used to call a gate operation by
* string.
*/
#pragma once
#include "Constant.hpp"
#include "ConstantUtil.hpp"
#include "Error.hpp"
#include "GateUtil.hpp"
#include "KernelType.hpp"
#include "Macros.hpp"
#include "OpToMemberFuncPtr.hpp"
#include "Util.hpp"
#include <cassert>
#include <complex>
#include <functional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>
/// @cond DEV
namespace Pennylane::Internal {
constexpr auto generatorNamesWithoutPrefix() {
constexpr std::string_view prefix = "Generator";
namespace GateConstant = Gates::Constant;
std::array<std::pair<Gates::GeneratorOperation, std::string_view>,
GateConstant::generator_names.size()>
res;
for (size_t i = 0; i < GateConstant::generator_names.size(); i++) {
const auto [gntr_op, gntr_name] = GateConstant::generator_names[i];
res[i].first = gntr_op;
res[i].second = gntr_name.substr(prefix.size());
}
return res;
}
} // namespace Pennylane::Internal
/// @endcond
namespace Pennylane {
/**
* @brief 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.
*/
template <typename PrecisionT> class DynamicDispatcher {
public:
using CFP_t = std::complex<PrecisionT>;
using GateFunc = std::function<void(
std::complex<PrecisionT> * /*data*/, size_t /*num_qubits*/,
const std::vector<size_t> & /*wires*/, bool /*inverse*/,
const std::vector<PrecisionT> & /*params*/)>;
using GeneratorFunc = Gates::GeneratorFuncPtrT<PrecisionT>;
using MatrixFunc = Gates::MatrixFuncPtrT<PrecisionT>;
private:
std::unordered_map<std::string, Gates::GateOperation> str_to_gates_;
std::unordered_map<std::string, Gates::GeneratorOperation> str_to_gntrs_;
std::unordered_map<std::pair<Gates::GateOperation, Gates::KernelType>,
GateFunc, Util::PairHash>
gate_kernels_;
std::unordered_map<std::pair<Gates::GeneratorOperation, Gates::KernelType>,
GeneratorFunc, Util::PairHash>
generator_kernels_;
std::unordered_map<std::pair<Gates::MatrixOperation, Gates::KernelType>,
MatrixFunc, Util::PairHash>
matrix_kernels_;
std::unordered_map<Gates::KernelType, std::string> kernel_names_;
DynamicDispatcher() {
using Gates::KernelType;
constexpr static auto gntr_names_without_prefix =
Internal::generatorNamesWithoutPrefix();
for (const auto &[gate_op, gate_name] : Gates::Constant::gate_names) {
str_to_gates_.emplace(gate_name, gate_op);
}
for (const auto &[gntr_op, gntr_name] : gntr_names_without_prefix) {
str_to_gntrs_.emplace(gntr_name, gntr_op);
}
}
public:
DynamicDispatcher(const DynamicDispatcher &) = delete;
DynamicDispatcher(DynamicDispatcher &&) = delete;
DynamicDispatcher &operator=(const DynamicDispatcher &) = delete;
DynamicDispatcher &operator=(DynamicDispatcher &&) = delete;
~DynamicDispatcher() = default;
/**
* @brief Get the singleton instance
*/
static DynamicDispatcher &getInstance() {
static DynamicDispatcher singleton;
return singleton;
}
/**
* @brief Get all registered kernels
*/
[[nodiscard]] auto registeredKernels() const
-> std::vector<Gates::KernelType> {
std::vector<Gates::KernelType> kernels;
kernels.reserve(kernel_names_.size());
for (const auto &[kernel, name] : kernel_names_) {
kernels.emplace_back(kernel);
}
return kernels;
}
/**
* @brief Check whether the kernel is registered to a dispatcher
*/
[[nodiscard]] auto isRegisteredKernel(Gates::KernelType kernel) const {
return kernel_names_.contains(kernel);
}
/**
* @brief Register kernel name
*
* @param kernel Kernel
* @param name Name of the kernel
*/
void registerKernelName(Gates::KernelType kernel, std::string name) {
kernel_names_.emplace(kernel, std::move(name));
}
/**
* @brief Get registered name of the kernel
*
* @param kernel Kernel
*/
[[nodiscard]] auto getKernelName(Gates::KernelType kernel) const
-> std::string {
return kernel_names_.at(kernel);
}
/**
* @brief Get registered gates for the given kernel
*
* @param kernel Kernel
*/
[[nodiscard]] auto registeredGatesForKernel(Gates::KernelType kernel) const
-> std::unordered_set<Gates::GateOperation> {
std::unordered_set<Gates::GateOperation> gates;
for (const auto &[key, val] : gate_kernels_) {
if (key.second == kernel) {
gates.emplace(key.first);
}
}
return gates;
}
[[nodiscard]] auto
registeredGeneratorsForKernel(Gates::KernelType kernel) const
-> std::unordered_set<Gates::GeneratorOperation> {
std::unordered_set<Gates::GeneratorOperation> gntrs;
for (const auto &[key, val] : generator_kernels_) {
if (key.second == kernel) {
gntrs.emplace(key.first);
}
}
return gntrs;
}
[[nodiscard]] auto
registeredMatricesForKernel(Gates::KernelType kernel) const
-> std::unordered_set<Gates::MatrixOperation> {
std::unordered_set<Gates::MatrixOperation> matrices;
for (const auto &[key, val] : matrix_kernels_) {
if (key.second == kernel) {
matrices.emplace(key.first);
}
}
return matrices;
}
/**
* @brief Gate name to gate operation
*
* @param gate_name Gate name
*/
[[nodiscard]] auto strToGateOp(const std::string &gate_name) const
-> Gates::GateOperation {
return str_to_gates_.at(gate_name);
}
/**
* @brief Generator name to generator operation
*
* @param gntr_name Generator name without "Generator" prefix
*/
[[nodiscard]] auto strToGeneratorOp(const std::string &gntr_name) const
-> Gates::GeneratorOperation {
return str_to_gntrs_.at(gntr_name);
}
/**
* @brief Register a new gate operation for the operation. Can pass a custom
* kernel
*/
template <typename FunctionType>
void registerGateOperation(Gates::GateOperation gate_op,
Gates::KernelType kernel, FunctionType &&func) {
gate_kernels_.emplace(std::make_pair(gate_op, kernel),
std::forward<FunctionType>(func));
}
/**
* @brief Register a new gate generator for the operation. Can pass a custom
* kernel
*/
template <typename FunctionType>
void registerGeneratorOperation(Gates::GeneratorOperation gntr_op,
Gates::KernelType kernel,
FunctionType &&func) {
generator_kernels_.emplace(std::make_pair(gntr_op, kernel),
std::forward<FunctionType>(func));
}
/**
* @brief Register a new matrix operation. Can pass a custom
* kernel
*/
void registerMatrixOperation(Gates::MatrixOperation mat_op,
Gates::KernelType kernel, MatrixFunc func) {
matrix_kernels_.emplace(std::make_pair(mat_op, kernel), func);
}
/**
* @brief Check if a kernel function is registered for the given
* gate operation and kernel.
*
* @param gate_op Gate operation
* @param kernel Kernel
*/
bool isRegistered(Gates::GateOperation gate_op,
Gates::KernelType kernel) const {
return gate_kernels_.find(std::make_pair(gate_op, kernel)) !=
gate_kernels_.cend();
}
/**
* @brief Check if a kernel function is registered for the given
* generator operation and kernel.
*
* @param gntr_op Generator operation
* @param kernel Kernel
*/
bool isRegistered(Gates::GeneratorOperation gntr_op,
Gates::KernelType kernel) const {
return generator_kernels_.find(std::make_pair(gntr_op, kernel)) !=
generator_kernels_.cend();
}
/**
* @brief Check if a kernel function is registered for the given
* matrix operation and kernel.
*
* @param mat_op Matrix operation
* @param kernel Kernel
*/
bool isRegistered(Gates::MatrixOperation mat_op,
Gates::KernelType kernel) const {
return matrix_kernels_.find(std::make_pair(mat_op, kernel)) !=
matrix_kernels_.cend();
}
/**
* @brief Apply a single gate to the state-vector using the given kernel.
*
* @param kernel Kernel to run the gate operation.
* @param data Pointer to data.
* @param num_qubits Number of qubits.
* @param op_name Gate operation name.
* @param wires Wires to apply gate to.
* @param inverse Indicates whether to use inverse of gate.
* @param params Optional parameter list for parametric gates.
*/
void applyOperation(Gates::KernelType kernel, CFP_t *data,
size_t num_qubits, const std::string &op_name,
const std::vector<size_t> &wires, bool inverse,
const std::vector<PrecisionT> ¶ms = {}) const {
const auto iter =
gate_kernels_.find(std::make_pair(strToGateOp(op_name), kernel));
if (iter == gate_kernels_.cend()) {
throw std::invalid_argument(
"Cannot find a registered kernel for a given gate "
"and kernel pair");
}
(iter->second)(data, num_qubits, wires, inverse, params);
}
/**
* @brief Apply a single gate to the state-vector using the given kernel.
*
* @param kernel Kernel to run the gate operation.
* @param data Pointer to data.
* @param num_qubits Number of qubits.
* @param gate_op Gate operation.
* @param wires Wires to apply gate to.
* @param inverse Indicates whether to use inverse of gate.
* @param params Optional parameter list for parametric gates.
*/
void applyOperation(Gates::KernelType kernel, CFP_t *data,
size_t num_qubits, Gates::GateOperation gate_op,
const std::vector<size_t> &wires, bool inverse,
const std::vector<PrecisionT> ¶ms = {}) const {
const auto iter = gate_kernels_.find(std::make_pair(gate_op, kernel));
if (iter == gate_kernels_.cend()) {
throw std::invalid_argument(
"Cannot find a registered kernel for a given gate "
"and kernel pair");
}
(iter->second)(data, num_qubits, wires, inverse, params);
}
/**
* @brief Apply multiple gates to the state-vector using a registered kernel
*
* @param data Pointer to data.
* @param num_qubits Number of qubits.
* @param ops List of Gate operation names.
* @param wires List of wires to apply each gate to.
* @param inverse List of inverses
* @param params List of parameters
*/
void
applyOperations(Gates::KernelType kernel, CFP_t *data, size_t num_qubits,
const std::vector<std::string> &ops,
const std::vector<std::vector<size_t>> &wires,
const std::vector<bool> &inverse,
const std::vector<std::vector<PrecisionT>> ¶ms) const {
const size_t numOperations = ops.size();
if (numOperations != wires.size() || numOperations != params.size()) {
throw std::invalid_argument(
"Invalid arguments: number of operations, wires, and "
"parameters must all be equal");
}
for (size_t i = 0; i < numOperations; i++) {
applyOperation(kernel, data, num_qubits, ops[i], wires[i],
inverse[i], params[i]);
}
}
/**
* @brief Apply multiple (non-parameterized) gates to the state-vector
* using a registered kernel
*
* @param data Pointer to data.
* @param num_qubits Number of qubits.
* @param ops List of Gate operation names.
* @param wires List of wires to apply each gate to.
* @param inverse List of inverses
*/
void applyOperations(Gates::KernelType kernel, CFP_t *data,
size_t num_qubits, const std::vector<std::string> &ops,
const std::vector<std::vector<size_t>> &wires,
const std::vector<bool> &inverse) const {
const size_t numOperations = ops.size();
if (numOperations != wires.size()) {
throw std::invalid_argument(
"Invalid arguments: number of operations, wires, and "
"parameters must all be equal");
}
for (size_t i = 0; i < numOperations; i++) {
applyOperation(kernel, data, num_qubits, ops[i], wires[i],
inverse[i], {});
}
}
/**
* @brief Apply a given matrix directly to the statevector.
*
* @param kernel Kernel to use for this operation
* @param data Pointer to the statevector.
* @param num_qubits Number of qubits.
* @param matrix Perfect square matrix in row-major order.
* @param wires Wires the gate applies to.
* @param inverse Indicate whether inverse should be taken.
*/
void applyMatrix(Gates::KernelType kernel, CFP_t *data, size_t num_qubits,
const std::complex<PrecisionT> *matrix,
const std::vector<size_t> &wires, bool inverse) const {
using Gates::MatrixOperation;
assert(num_qubits >= wires.size());
const auto mat_op = [n_wires = wires.size()]() {
switch (n_wires) {
case 1:
return MatrixOperation::SingleQubitOp;
case 2:
return MatrixOperation::TwoQubitOp;
default:
return MatrixOperation::MultiQubitOp;
}
}();
const auto iter = matrix_kernels_.find(std::make_pair(mat_op, kernel));
if (iter == matrix_kernels_.end()) {
throw std::invalid_argument(
std::string(
Util::lookup(Gates::Constant::matrix_names, mat_op)) +
" is not registered for the given kernel");
}
(iter->second)(data, num_qubits, matrix, wires, inverse);
}
/**
* @brief Apply a given matrix directly to the statevector.
*
* @param kernel Kernel to use for this operation
* @param data Pointer to the statevector.
* @param num_qubits Number of qubits.
* @param matrix Perfect square matrix in row-major order.
* @param wires Wires the gate applies to.
* @param inverse Indicate whether inverse should be taken.
*/
void applyMatrix(Gates::KernelType kernel, CFP_t *data, size_t num_qubits,
const std::vector<std::complex<PrecisionT>> &matrix,
const std::vector<size_t> &wires, bool inverse) const {
if (matrix.size() != Util::exp2(2 * wires.size())) {
throw std::invalid_argument(
"The size of matrix does not match with the given "
"number of wires");
}
applyMatrix(kernel, data, num_qubits, matrix.data(), wires, inverse);
}
/**
* @brief Apply a single generator to the state-vector using the given
* kernel.
*
* @param kernel Kernel to run the gate operation.
* @param data Pointer to data.
* @param num_qubits Number of qubits.
* @param gntr_op Generator operation.
* @param wires Wires to apply gate to.
* @param adj Indicates whether to use adjoint of gate.
*/
auto applyGenerator(Gates::KernelType kernel, CFP_t *data,
size_t num_qubits, Gates::GeneratorOperation gntr_op,
const std::vector<size_t> &wires, bool adj) const
-> PrecisionT {
using Gates::Constant::generator_names;
const auto iter =
generator_kernels_.find(std::make_pair(gntr_op, kernel));
if (iter == generator_kernels_.cend()) {
throw std::invalid_argument(
"Cannot find a registered kernel for a given generator "
"and kernel pair.");
}
return (iter->second)(data, num_qubits, wires, adj);
}
/**
* @brief Apply a single generator to the state-vector using the given
* kernel.
*
* @param kernel Kernel to run the gate operation.
* @param data Pointer to data.
* @param num_qubits Number of qubits.
* @param op_name Gate operation name.
* @param wires Wires to apply gate to.
* @param adj Indicates whether to use adjoint of gate.
*/
auto applyGenerator(Gates::KernelType kernel, CFP_t *data,
size_t num_qubits, const std::string &op_name,
const std::vector<size_t> &wires, bool adj) const
-> PrecisionT {
const auto iter = generator_kernels_.find(
std::make_pair(strToGeneratorOp(op_name), kernel));
if (iter == generator_kernels_.cend()) {
throw std::invalid_argument(
"Cannot find a registered kernel for a given generator "
"and kernel pair.");
}
return (iter->second)(data, num_qubits, wires, adj);
}
};
} // namespace Pennylane
/// @cond DEV
namespace Pennylane::Internal {
int registerAllAvailableKernels_Float();
int registerAllAvailableKernels_Double();
/**
* @brief These functions are only used to register kernels to the dynamic
* dispatcher.
*/
struct RegisterBeforeMain_Float {
const static inline int dummy = registerAllAvailableKernels_Float();
};
struct RegisterBeforeMain_Double {
const static inline int dummy = registerAllAvailableKernels_Double();
};
} // namespace Pennylane::Internal
/// @endcond