forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.h
More file actions
942 lines (760 loc) · 36.2 KB
/
operators.h
File metadata and controls
942 lines (760 loc) · 36.2 KB
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
#pragma once
#include <functional>
#include <set>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "dynamics/evaluation.h"
#include "dynamics/operator_leafs.h"
#include "dynamics/templates.h"
#include "utils/cudaq_utils.h"
#include "utils/matrix.h"
namespace cudaq {
class spin_handler;
enum class pauli;
#define HANDLER_SPECIFIC_TEMPLATE(ConcreteTy) \
template <typename T = HandlerTy, std::enable_if_t< \
std::is_same<T, ConcreteTy>::value && \
std::is_same<HandlerTy, T>::value, bool> = true>
// utility functions for backward compatibility
#define SPIN_OPS_BACKWARD_COMPATIBILITY \
template <typename T = HandlerTy, std::enable_if_t< \
std::is_same<HandlerTy, spin_handler>::value && \
std::is_same<HandlerTy, T>::value, bool> = true>
/// @brief Represents an operator expression consisting of a sum of terms, where
/// each term is a product of elementary and scalar operators. Operator
/// expressions cannot be used within quantum kernels, but they provide methods
/// to convert them to data types that can.
template <typename HandlerTy>
class sum_op {
template <typename T>
friend class sum_op;
template <typename T>
friend class product_op;
private:
// inserts a new term combining it with an existing one if possible
void insert(product_op<HandlerTy> &&other);
void insert(const product_op<HandlerTy> &other);
void aggregate_terms();
template <typename... Args>
void aggregate_terms(product_op<HandlerTy> &&head, Args &&...args);
template <typename EvalTy>
EvalTy evaluate(operator_arithmetics<EvalTy> arithmetics) const;
protected:
std::unordered_map<std::string, int>
term_map; // quick access to term index given its id (used for aggregating
// terms)
std::vector<std::vector<HandlerTy>> terms;
std::vector<scalar_operator> coefficients;
sum_op(const sum_op<HandlerTy> &other, bool sized, int size);
sum_op(sum_op<HandlerTy> &&other, bool sized, int size);
public:
// Default constructor constructs an identity term.
constexpr sum_op() : sum_op(1.0) {}
// called const_iterator because it will *not* modify the sum,
// regardless of what is done with the products/iterator
struct const_iterator {
private:
const sum_op<HandlerTy> *sum;
typename std::unordered_map<std::string, int>::const_iterator iter;
product_op<HandlerTy> current_val;
public:
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = product_op<HandlerTy>;
using pointer = product_op<HandlerTy>*;
using reference = product_op<HandlerTy>&;
const_iterator(const sum_op<HandlerTy> *sum)
: const_iterator(sum, sum->term_map.begin()) {}
const_iterator(const sum_op<HandlerTy> *sum,
std::unordered_map<std::string, int>::const_iterator &&it)
: sum(sum), iter(std::move(it)), current_val(1.) {
if (iter != sum->term_map.end())
current_val = product_op<HandlerTy>(sum->coefficients[iter->second], sum->terms[iter->second]);
}
bool operator==(const const_iterator &other) const {
return sum == other.sum && iter == other.iter;
}
bool operator!=(const const_iterator &other) const { return !(*this == other); }
reference operator*() { return current_val; } // not const - allow to move current_value
pointer operator->() { return ¤t_val; }
// prefix
const_iterator& operator++() {
if (++iter != sum->term_map.end())
current_val = product_op<HandlerTy>(sum->coefficients[iter->second], sum->terms[iter->second]);
return *this;
}
// postfix
const_iterator operator++(int) { return const_iterator(sum, iter++); }
};
/// @brief Get iterator to beginning of operator terms
const_iterator begin() const { return const_iterator(this); }
/// @brief Get iterator to end of operator terms
const_iterator end() const { return const_iterator(this, this->term_map.cend()); }
// read-only properties
/// @brief The degrees of freedom that the operator acts on.
/// By default, degrees reflect the ordering convention (endianness) used in
/// CUDA-Q, and the ordering of the matrix returned by default by `to_matrix`.
std::vector<std::size_t> degrees(bool application_order = true) const;
/// @brief Return the number of operator terms that make up this operator sum.
std::size_t num_terms() const;
// constructors and destructors
template <typename... Args,
std::enable_if_t<std::conjunction<std::is_same<
product_op<HandlerTy>, Args>...>::value &&
sizeof...(Args),
bool> = true>
sum_op(Args &&...args);
sum_op(const product_op<HandlerTy> &other);
template <typename T,
std::enable_if_t<!std::is_same<T, HandlerTy>::value &&
std::is_constructible<HandlerTy, T>::value,
bool> = true>
sum_op(const sum_op<T> &other);
template <typename T,
std::enable_if_t<std::is_same<HandlerTy, matrix_handler>::value &&
!std::is_same<T, HandlerTy>::value &&
std::is_constructible<HandlerTy, T>::value,
bool> = true>
sum_op(const sum_op<T> &other,
const matrix_handler::commutation_behavior &behavior);
// copy constructor
sum_op(const sum_op<HandlerTy> &other);
// move constructor
sum_op(sum_op<HandlerTy> &&other);
~sum_op() = default;
// assignments
template <typename T,
std::enable_if_t<!std::is_same<T, HandlerTy>::value &&
std::is_constructible<HandlerTy, T>::value,
bool> = true>
sum_op<HandlerTy> &operator=(const product_op<T> &other);
sum_op<HandlerTy> &operator=(const product_op<HandlerTy> &other);
sum_op<HandlerTy> &operator=(product_op<HandlerTy> &&other);
template <typename T,
std::enable_if_t<!std::is_same<T, HandlerTy>::value &&
std::is_constructible<HandlerTy, T>::value,
bool> = true>
sum_op<HandlerTy> &operator=(const sum_op<T> &other);
// assignment operator
sum_op<HandlerTy> &operator=(const sum_op<HandlerTy> &other);
// move assignment operator
sum_op<HandlerTy> &operator=(sum_op<HandlerTy> &&other);
// evaluations
/// @brief Return the sum_op<HandlerTy> as a string.
std::string to_string() const;
/// @brief Return the matrix representation of the operator.
/// By default, the matrix is ordered according to the convention (endianness)
/// used in CUDA-Q, and the ordering returned by default by `degrees`.
/// @arg `dimensions` : A mapping that specifies the number of levels,
/// that is, the dimension of each degree of freedom
/// that the operator acts on. Example for two, 2-level
/// degrees of freedom: `{0:2, 1:2}`.
/// @arg `parameters` : A map of the parameter names to their concrete,
/// complex values.
complex_matrix to_matrix(std::unordered_map<int, int> dimensions = {},
const std::unordered_map<std::string, std::complex<double>>
¶meters = {},
bool application_order = true) const;
// comparisons
/// @brief True, if the other value is an sum_op<HandlerTy> with
/// equivalent terms, and False otherwise.
/// The equality takes into account that operator
/// addition is commutative, as is the product of two operators if they
/// act on different degrees of freedom.
/// The equality comparison does *not* take commutation relations into
/// account, and does not try to reorder terms `blockwise`; it may hence
/// evaluate to False, even if two operators in reality are the same.
/// If the equality evaluates to True, on the other hand, the operators
/// are guaranteed to represent the same transformation for all arguments.
bool operator==(const sum_op<HandlerTy> &other) const;
// unary operators
sum_op<HandlerTy> operator-() const &;
sum_op<HandlerTy> operator-() &&;
sum_op<HandlerTy> operator+() const &;
sum_op<HandlerTy> operator+() &&;
// right-hand arithmetics
sum_op<HandlerTy> operator*(const scalar_operator &other) const &;
sum_op<HandlerTy> operator*(const scalar_operator &other) &&;
sum_op<HandlerTy> operator/(const scalar_operator &other) const &;
sum_op<HandlerTy> operator/(const scalar_operator &other) &&;
sum_op<HandlerTy> operator+(scalar_operator &&other) const &;
sum_op<HandlerTy> operator+(scalar_operator &&other) &&;
sum_op<HandlerTy> operator+(const scalar_operator &other) const &;
sum_op<HandlerTy> operator+(const scalar_operator &other) &&;
sum_op<HandlerTy> operator-(scalar_operator &&other) const &;
sum_op<HandlerTy> operator-(scalar_operator &&other) &&;
sum_op<HandlerTy> operator-(const scalar_operator &other) const &;
sum_op<HandlerTy> operator-(const scalar_operator &other) &&;
sum_op<HandlerTy>
operator*(const product_op<HandlerTy> &other) const;
sum_op<HandlerTy>
operator+(const product_op<HandlerTy> &other) const &;
sum_op<HandlerTy>
operator+(const product_op<HandlerTy> &other) &&;
sum_op<HandlerTy>
operator+(product_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator+(product_op<HandlerTy> &&other) &&;
sum_op<HandlerTy>
operator-(const product_op<HandlerTy> &other) const &;
sum_op<HandlerTy>
operator-(const product_op<HandlerTy> &other) &&;
sum_op<HandlerTy>
operator-(product_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator-(product_op<HandlerTy> &&other) &&;
sum_op<HandlerTy> operator*(const sum_op<HandlerTy> &other) const;
sum_op<HandlerTy>
operator+(const sum_op<HandlerTy> &other) const &;
sum_op<HandlerTy> operator+(const sum_op<HandlerTy> &other) &&;
sum_op<HandlerTy> operator+(sum_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator+(sum_op<HandlerTy> &&other) &&;
sum_op<HandlerTy>
operator-(const sum_op<HandlerTy> &other) const &;
sum_op<HandlerTy> operator-(const sum_op<HandlerTy> &other) &&;
sum_op<HandlerTy> operator-(sum_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator-(sum_op<HandlerTy> &&other) &&;
sum_op<HandlerTy> &operator*=(const scalar_operator &other);
sum_op<HandlerTy> &operator/=(const scalar_operator &other);
sum_op<HandlerTy> &operator+=(scalar_operator &&other);
sum_op<HandlerTy> &operator+=(const scalar_operator &other);
sum_op<HandlerTy> &operator-=(scalar_operator &&other);
sum_op<HandlerTy> &operator-=(const scalar_operator &other);
sum_op<HandlerTy> &operator*=(const product_op<HandlerTy> &other);
sum_op<HandlerTy> &operator+=(const product_op<HandlerTy> &other);
sum_op<HandlerTy> &operator+=(product_op<HandlerTy> &&other);
sum_op<HandlerTy> &operator-=(const product_op<HandlerTy> &other);
sum_op<HandlerTy> &operator-=(product_op<HandlerTy> &&other);
sum_op<HandlerTy> &operator*=(const sum_op<HandlerTy> &other);
sum_op<HandlerTy> &operator+=(const sum_op<HandlerTy> &other);
sum_op<HandlerTy> &operator+=(sum_op<HandlerTy> &&other);
sum_op<HandlerTy> &operator-=(const sum_op<HandlerTy> &other);
sum_op<HandlerTy> &operator-=(sum_op<HandlerTy> &&other);
// left-hand arithmetics
// Being a bit permissive here, since otherwise the explicit template
// instantiation is a nightmare.
template <typename T>
friend sum_op<T> operator*(const scalar_operator &other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator*(const scalar_operator &other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
product_op<T> &&self);
// common operators
static sum_op<HandlerTy> empty();
static product_op<HandlerTy> identity();
static product_op<HandlerTy> identity(int target);
// handler specific operators
HANDLER_SPECIFIC_TEMPLATE(matrix_handler)
static product_op<T> number(int target);
HANDLER_SPECIFIC_TEMPLATE(matrix_handler)
static product_op<T> parity(int target);
HANDLER_SPECIFIC_TEMPLATE(matrix_handler)
static product_op<T> position(int target);
HANDLER_SPECIFIC_TEMPLATE(matrix_handler)
static product_op<T> momentum(int target);
HANDLER_SPECIFIC_TEMPLATE(matrix_handler)
static product_op<T> squeeze(int target);
HANDLER_SPECIFIC_TEMPLATE(matrix_handler)
static product_op<T> displace(int target);
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
static product_op<T> i(int target);
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
static product_op<T> x(int target);
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
static product_op<T> y(int target);
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
static product_op<T> z(int target);
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
static sum_op<T> plus(int target);
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
static sum_op<T> minus(int target);
HANDLER_SPECIFIC_TEMPLATE(boson_handler)
static product_op<T> create(int target);
HANDLER_SPECIFIC_TEMPLATE(boson_handler)
static product_op<T> annihilate(int target);
HANDLER_SPECIFIC_TEMPLATE(boson_handler)
static product_op<T> number(int target);
HANDLER_SPECIFIC_TEMPLATE(boson_handler)
static sum_op<T> position(int target);
HANDLER_SPECIFIC_TEMPLATE(boson_handler)
static sum_op<T> momentum(int target);
HANDLER_SPECIFIC_TEMPLATE(fermion_handler)
static product_op<T> create(int target);
HANDLER_SPECIFIC_TEMPLATE(fermion_handler)
static product_op<T> annihilate(int target);
HANDLER_SPECIFIC_TEMPLATE(fermion_handler)
static product_op<T> number(int target);
// general utility functions
void dump() const;
std::vector<sum_op<HandlerTy>> distribute_terms(std::size_t numChunks) const;
// handler specific utility functions
HANDLER_SPECIFIC_TEMPLATE(spin_handler) // naming is not very general
std::size_t num_qubits() const;
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
sum_op(const std::vector<double> &input_vec);
HANDLER_SPECIFIC_TEMPLATE(spin_handler) // could be defined for other operators as well
static product_op<HandlerTy> from_word(const std::string &word);
HANDLER_SPECIFIC_TEMPLATE(spin_handler) // could be defined for other operators as well
static sum_op<HandlerTy> random(std::size_t nQubits, std::size_t nTerms, unsigned int seed);
// utility functions for backward compatibility
SPIN_OPS_BACKWARD_COMPATIBILITY
sum_op(const std::vector<double> &input_vec, std::size_t nQubits);
SPIN_OPS_BACKWARD_COMPATIBILITY
sum_op(const std::vector<std::vector<bool>> &bsf_terms,
const std::vector<std::complex<double>> &coeffs);
SPIN_OPS_BACKWARD_COMPATIBILITY
std::vector<double> getDataRepresentation() const;
SPIN_OPS_BACKWARD_COMPATIBILITY
std::pair<std::vector<std::vector<bool>>, std::vector<std::complex<double>>>
get_raw_data() const;
SPIN_OPS_BACKWARD_COMPATIBILITY
std::string to_string(bool printCoeffs) const;
SPIN_OPS_BACKWARD_COMPATIBILITY
void for_each_term(std::function<void(sum_op<HandlerTy> &)> &&functor) const;
SPIN_OPS_BACKWARD_COMPATIBILITY
void for_each_pauli(std::function<void(pauli, std::size_t)> &&functor) const;
SPIN_OPS_BACKWARD_COMPATIBILITY
bool is_identity() const;
};
/// @brief Represents an operator expression consisting of a product of
/// elementary and scalar operators. Operator expressions cannot be used within
/// quantum kernels, but they provide methods to convert them to data types
/// that can.
template <typename HandlerTy>
class product_op {
template <typename T>
friend class product_op;
template <typename T>
friend class sum_op;
private:
// template defined as long as T implements an in-place multiplication -
// won't work if the in-place multiplication was inherited from a base class
template <typename T>
static decltype(std::declval<T>().inplace_mult(std::declval<T>()))
handler_mult(int);
template <typename T>
static std::false_type handler_mult(
...); // ellipsis ensures the template above is picked if it exists
static constexpr bool supports_inplace_mult =
!std::is_same<decltype(handler_mult<HandlerTy>(0)),
std::false_type>::value;
#if !defined(NDEBUG)
bool is_canonicalized() const;
#endif
typename std::vector<HandlerTy>::const_iterator
find_insert_at(const HandlerTy &other);
template <typename T,
std::enable_if_t<std::is_same<HandlerTy, T>::value &&
!product_op<T>::supports_inplace_mult,
std::false_type> = std::false_type()>
void insert(T &&other);
template <typename T,
std::enable_if_t<std::is_same<HandlerTy, T>::value &&
product_op<T>::supports_inplace_mult,
std::true_type> = std::true_type()>
void insert(T &&other);
void aggregate_terms();
template <typename... Args>
void aggregate_terms(HandlerTy &&head, Args &&...args);
template <typename EvalTy>
EvalTy evaluate(operator_arithmetics<EvalTy> arithmetics) const;
protected:
std::vector<HandlerTy> operators;
scalar_operator coefficient;
template <typename... Args,
std::enable_if_t<
std::conjunction<std::is_same<HandlerTy, Args>...>::value,
bool> = true>
product_op(scalar_operator coefficient, Args &&...args);
// keep this constructor protected (otherwise it needs to ensure canonical
// order)
product_op(scalar_operator coefficient,
const std::vector<HandlerTy> &atomic_operators,
int size = 0);
// keep this constructor protected (otherwise it needs to ensure canonical
// order)
product_op(scalar_operator coefficient,
std::vector<HandlerTy> &&atomic_operators, int size = 0);
public:
struct const_iterator {
private:
const product_op<HandlerTy> *prod;
std::size_t current_idx;
public:
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = const HandlerTy;
using pointer = const HandlerTy*;
using reference = const HandlerTy&;
const_iterator(const product_op<HandlerTy> *prod, std::size_t idx = 0)
: prod(prod), current_idx(idx) {}
bool operator==(const const_iterator &other) const {
return prod == other.prod && current_idx == other.current_idx;
}
bool operator!=(const const_iterator &other) const { return !(*this == other); }
reference operator*() const { return prod->operators[current_idx]; }
pointer operator->() { return &(prod->operators[current_idx]); }
// prefix
const_iterator& operator++() { ++current_idx; return *this; }
const_iterator& operator--() { --current_idx; return *this; }
// postfix
const_iterator operator++(int) { return const_iterator(prod, current_idx++); }
const_iterator operator--(int) { return const_iterator(prod, current_idx--); }
};
/// @brief Get iterator to beginning of operator terms
const_iterator begin() const { return const_iterator(this); }
/// @brief Get iterator to end of operator terms
const_iterator end() const { return const_iterator(this, this->operators.size()); }
// read-only properties
/// @brief The degrees of freedom that the operator acts on.
/// By default, degrees reflect the ordering convention (endianness) used in
/// CUDA-Q, and the ordering of the matrix returned by default by `to_matrix`.
///
/// Specifically, the indices of a statevector with two qubits are {00, 01, 10, 11}.
/// An ordering of degrees {0, 1} then indicates that a state where
/// the qubit with index 0 equals 1 with probability 1 is given by
/// the vector {0., 1., 0., 0.}.
std::vector<std::size_t> degrees(bool application_order = true) const;
/// @brief Return the number of operator terms that make up this product
/// operator.
std::size_t num_ops() const;
// Public since it is used by the CUDA-Q compiler and runtime
// to retrieve expectation values for specific terms.
// The term id uniquely identifies the operators and targets
// (degrees) that they act on, but does not include information
// about the coefficient.
std::string get_term_id() const;
scalar_operator get_coefficient() const;
// constructors and destructors
constexpr product_op() {}
product_op(double coefficient);
product_op(std::complex<double> coefficient);
product_op(HandlerTy &&atomic);
template <typename T,
std::enable_if_t<!std::is_same<T, HandlerTy>::value &&
std::is_constructible<HandlerTy, T>::value,
bool> = true>
product_op(const product_op<T> &other);
template <typename T,
std::enable_if_t<std::is_same<HandlerTy, matrix_handler>::value &&
!std::is_same<T, HandlerTy>::value &&
std::is_constructible<HandlerTy, T>::value,
bool> = true>
product_op(const product_op<T> &other,
const matrix_handler::commutation_behavior &behavior);
// copy constructor
product_op(const product_op<HandlerTy> &other, int size = 0);
// move constructor
product_op(product_op<HandlerTy> &&other, int size = 0);
~product_op() = default;
// assignments
template <typename T,
std::enable_if_t<!std::is_same<T, HandlerTy>::value &&
std::is_constructible<HandlerTy, T>::value,
bool> = true>
product_op<HandlerTy> &operator=(const product_op<T> &other);
// assignment operator
product_op<HandlerTy> &
operator=(const product_op<HandlerTy> &other);
// move assignment operator
product_op<HandlerTy> &operator=(product_op<HandlerTy> &&other);
// evaluations
/// @brief Return the `product_op<HandlerTy>` as a string.
std::string to_string() const;
/// @brief Return the matrix representation of the operator.
/// By default, the matrix is ordered according to the convention (endianness)
/// used in CUDA-Q, and the ordering returned by default by `degrees`.
/// @arg `dimensions` : A mapping that specifies the number of levels,
/// that is, the dimension of each degree of freedom
/// that the operator acts on. Example for two, 2-level
/// degrees of freedom: `{0:2, 1:2}`.
/// @arg `parameters` : A map of the parameter names to their concrete,
/// complex values.
complex_matrix to_matrix(std::unordered_map<int, int> dimensions = {},
const std::unordered_map<std::string, std::complex<double>>
¶meters = {},
bool application_order = true) const;
// comparisons
/// @brief True, if the other value is an sum_op<HandlerTy> with
/// equivalent terms, and False otherwise.
/// The equality takes into account that operator
/// addition is commutative, as is the product of two operators if they
/// act on different degrees of freedom.
/// The equality comparison does *not* take commutation relations into
/// account, and does not try to reorder terms `blockwise`; it may hence
/// evaluate to False, even if two operators in reality are the same.
/// If the equality evaluates to True, on the other hand, the operators
/// are guaranteed to represent the same transformation for all arguments.
bool operator==(const product_op<HandlerTy> &other) const;
// unary operators
product_op<HandlerTy> operator-() const &;
product_op<HandlerTy> operator-() &&;
product_op<HandlerTy> operator+() const &;
product_op<HandlerTy> operator+() &&;
// right-hand arithmetics
product_op<HandlerTy> operator*(scalar_operator &&other) const &;
product_op<HandlerTy> operator*(scalar_operator &&other) &&;
product_op<HandlerTy> operator*(const scalar_operator &other) const &;
product_op<HandlerTy> operator*(const scalar_operator &other) &&;
product_op<HandlerTy> operator/(scalar_operator &&other) const &;
product_op<HandlerTy> operator/(scalar_operator &&other) &&;
product_op<HandlerTy> operator/(const scalar_operator &other) const &;
product_op<HandlerTy> operator/(const scalar_operator &other) &&;
sum_op<HandlerTy> operator+(scalar_operator &&other) const &;
sum_op<HandlerTy> operator+(scalar_operator &&other) &&;
sum_op<HandlerTy> operator+(const scalar_operator &other) const &;
sum_op<HandlerTy> operator+(const scalar_operator &other) &&;
sum_op<HandlerTy> operator-(scalar_operator &&other) const &;
sum_op<HandlerTy> operator-(scalar_operator &&other) &&;
sum_op<HandlerTy> operator-(const scalar_operator &other) const &;
sum_op<HandlerTy> operator-(const scalar_operator &other) &&;
product_op<HandlerTy>
operator*(const product_op<HandlerTy> &other) const &;
product_op<HandlerTy>
operator*(const product_op<HandlerTy> &other) &&;
product_op<HandlerTy>
operator*(product_op<HandlerTy> &&other) const &;
product_op<HandlerTy> operator*(product_op<HandlerTy> &&other) &&;
sum_op<HandlerTy>
operator+(const product_op<HandlerTy> &other) const &;
sum_op<HandlerTy>
operator+(const product_op<HandlerTy> &other) &&;
sum_op<HandlerTy>
operator+(product_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator+(product_op<HandlerTy> &&other) &&;
sum_op<HandlerTy>
operator-(const product_op<HandlerTy> &other) const &;
sum_op<HandlerTy>
operator-(const product_op<HandlerTy> &other) &&;
sum_op<HandlerTy>
operator-(product_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator-(product_op<HandlerTy> &&other) &&;
sum_op<HandlerTy> operator*(const sum_op<HandlerTy> &other) const;
sum_op<HandlerTy>
operator+(const sum_op<HandlerTy> &other) const &;
sum_op<HandlerTy> operator+(const sum_op<HandlerTy> &other) &&;
sum_op<HandlerTy> operator+(sum_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator+(sum_op<HandlerTy> &&other) &&;
sum_op<HandlerTy>
operator-(const sum_op<HandlerTy> &other) const &;
sum_op<HandlerTy> operator-(const sum_op<HandlerTy> &other) &&;
sum_op<HandlerTy> operator-(sum_op<HandlerTy> &&other) const &;
sum_op<HandlerTy> operator-(sum_op<HandlerTy> &&other) &&;
product_op<HandlerTy> &operator*=(const scalar_operator &other);
product_op<HandlerTy> &operator/=(const scalar_operator &other);
product_op<HandlerTy> &
operator*=(const product_op<HandlerTy> &other);
product_op<HandlerTy> &operator*=(product_op<HandlerTy> &&other);
// left-hand arithmetics
// Being a bit permissive here, since otherwise the explicit template
// instantiation is a nightmare.
template <typename T>
friend product_op<T> operator*(scalar_operator &&other,
const product_op<T> &self);
template <typename T>
friend product_op<T> operator*(scalar_operator &&other,
product_op<T> &&self);
template <typename T>
friend product_op<T> operator*(const scalar_operator &other,
const product_op<T> &self);
template <typename T>
friend product_op<T> operator*(const scalar_operator &other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
const product_op<T> &self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
product_op<T> &&self);
template <typename T>
friend sum_op<T> operator*(scalar_operator &&other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator*(scalar_operator &&other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator*(const scalar_operator &other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator*(const scalar_operator &other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator+(scalar_operator &&other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator+(const scalar_operator &other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator-(scalar_operator &&other,
sum_op<T> &&self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
const sum_op<T> &self);
template <typename T>
friend sum_op<T> operator-(const scalar_operator &other,
sum_op<T> &&self);
// general utility functions
// Checks if all operators in the product are the identity.
// Note: this function returns true regardless of the value
// of the coefficient.
bool is_identity() const;
void dump() const;
// handler specific utility functions
HANDLER_SPECIFIC_TEMPLATE(spin_handler) // naming is not very general
std::size_t num_qubits() const;
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
std::string get_pauli_word() const;
HANDLER_SPECIFIC_TEMPLATE(spin_handler)
std::vector<bool> get_binary_symplectic_form() const;
// utility functions for backward compatibility
SPIN_OPS_BACKWARD_COMPATIBILITY
std::string to_string(bool printCoeffs) const;
};
/// @brief Representation of a time-dependent Hamiltonian for Rydberg system
class rydberg_hamiltonian {
public:
using coordinate = std::pair<double, double>;
/// @brief Constructor.
/// @param atom_sites List of 2D coordinates for trap sites.
/// @param amplitude Time-dependent driving amplitude, Omega(t).
/// @param phase Time-dependent driving phase, phi(t).
/// @param delta_global Time-dependent driving detuning, Delta_global(t).
/// @param atom_filling Optional. Marks occupied trap sites (1) and empty
/// sites (0). Defaults to all sites occupied.
/// @param delta_local Optional. A tuple of Delta_local(t) and site dependent
/// local detuning factors.
rydberg_hamiltonian(
const std::vector<coordinate> &atom_sites,
const scalar_operator &litude, const scalar_operator &phase,
const scalar_operator &delta_global,
const std::vector<int> &atom_filling = {},
const std::optional<std::pair<scalar_operator, std::vector<double>>>
&delta_local = std::nullopt);
/// @brief Get atom sites.
const std::vector<coordinate> &get_atom_sites() const;
/// @brief Get atom filling.
const std::vector<int> &get_atom_filling() const;
/// @brief Get amplitude operator.
const scalar_operator &get_amplitude() const;
/// @brief Get phase operator.
const scalar_operator &get_phase() const;
/// @brief Get global detuning operator.
const scalar_operator &get_delta_global() const;
private:
std::vector<coordinate> atom_sites;
std::vector<int> atom_filling;
scalar_operator amplitude;
scalar_operator phase;
scalar_operator delta_global;
std::optional<std::pair<scalar_operator, std::vector<double>>> delta_local;
};
// type aliases for convenience
typedef std::unordered_map<std::string, std::complex<double>> parameter_map;
typedef std::unordered_map<int, int> dimension_map;
typedef sum_op<matrix_handler> matrix_op;
typedef product_op<matrix_handler> matrix_op_term;
typedef sum_op<spin_handler> spin_op;
typedef product_op<spin_handler> spin_op_term;
typedef sum_op<boson_handler> boson_op;
typedef product_op<boson_handler> boson_op_term;
typedef sum_op<fermion_handler> fermion_op;
typedef product_op<fermion_handler> fermion_op_term;
#ifndef CUDAQ_INSTANTIATE_TEMPLATES
extern template class product_op<matrix_handler>;
extern template class product_op<spin_handler>;
extern template class product_op<boson_handler>;
extern template class product_op<fermion_handler>;
extern template class sum_op<matrix_handler>;
extern template class sum_op<spin_handler>;
extern template class sum_op<boson_handler>;
extern template class sum_op<fermion_handler>;
#endif
// Here only for backward compatibility
namespace spin {
product_op<spin_handler> i(std::size_t target);
product_op<spin_handler> x(std::size_t target);
product_op<spin_handler> y(std::size_t target);
product_op<spin_handler> z(std::size_t target);
}
} // namespace cudaq