Xped
Loading...
Searching...
No Matches
SiteOperator.hpp
Go to the documentation of this file.
1#ifndef XPED_SITEOPERATOR_HPP_
2#define XPED_SITEOPERATOR_HPP_
3
5
6namespace Xped {
7
8template <typename Scalar_, typename Symmetry_>
10{
11 using Scalar = Scalar_;
12 using Symmetry = Symmetry_;
13 using qType = typename Symmetry::qType;
15
16 SiteOperator() = default;
17
19
21 const Qbasis<Symmetry, 1>& basis,
22 const std::unordered_map<std::string, std::pair<qType, std::size_t>>& labels,
23 const mpi::XpedWorld& world = mpi::getUniverse());
24
25 const auto operator()(const qType& bra, const qType& ket) const
26 {
27 FusionTree<2, Symmetry> k{.q_uncoupled = {ket, Q}, .q_coupled = bra, .dims = {data.uncoupledCodomain()[0].inner_dim(ket), 1}};
28 k.computeDim();
29 FusionTree<1, Symmetry> b{.q_uncoupled = {bra}, .q_coupled = bra, .dims = {data.coupledDomain().inner_dim(bra)}};
30 b.computeDim();
31 return data.subMatrix(b, k);
32 }
33 auto operator()(const qType& bra, const qType& ket)
34 {
35 FusionTree<2, Symmetry> k{.q_uncoupled = {ket, Q}, .q_coupled = bra, .dims = {data.uncoupledCodomain()[0].inner_dim(ket), 1}};
36 k.computeDim();
37 FusionTree<1, Symmetry> b{.q_uncoupled = {bra}, .q_coupled = bra, .dims = {data.coupledDomain().inner_dim(bra)}};
38 b.computeDim();
39 return data.subMatrix(b, k);
40 }
41
42 const Scalar& operator()(const std::string& bra, const std::string& ket) const
43 {
44 auto it_bra = label_dict.find(bra);
45 assert(it_bra != label_dict.end() and "label_dict in SiteOperator does not contain bra label");
46 auto it_ket = label_dict.find(ket);
47 assert(it_ket != label_dict.end() and "label_dict in SiteOperator does not contain ket label");
48
49 return this->operator()(it_bra->second.first, it_ket->second.first)(it_bra->second.second, it_ket->second.second);
50 }
51 Scalar& operator()(const std::string& bra, const std::string& ket)
52 {
53 auto it_bra = label_dict.find(bra);
54 assert(it_bra != label_dict.end() and "label_dict in SiteOperator does not contain bra label");
55 auto it_ket = label_dict.find(ket);
56 assert(it_ket != label_dict.end() and "label_dict in SiteOperator does not contain ket label");
57 return this->operator()(it_bra->second.first, it_ket->second.first)(it_bra->second.second, it_ket->second.second);
58 }
59
61
62 void setZero() { data.setZero(); }
63 void setIdentity() { data.setIdentity(); }
64 void setRandom() { data.setRandom(); }
65
67 prod(XPED_CONST SiteOperator<Scalar, Symmetry>& O1, XPED_CONST SiteOperator<Scalar, Symmetry>& O2, const qType& target);
69 outerprod(XPED_CONST SiteOperator<Scalar, Symmetry>& O1, XPED_CONST SiteOperator<Scalar, Symmetry>& O2, const qType& target);
71 {
72 auto target = Symmetry::reduceSilent(O1.Q, O2.Q);
73 assert(target.size() == 1 and "Use outerprod overload with specification of fuse quantum number!");
74 return SiteOperator<Scalar, Symmetry>::outerprod(O1, O2, target[0]);
75 }
76
77 Scalar norm() const;
78
79 std::vector<MatrixType> plain() const;
80
81 std::string print() const;
82 // template <typename OtherScalar>
83 // SiteOperator<OtherScalar, Symmetry> cast() const;
84
85 std::string& label() { return label_; }
88 std::unordered_map<std::string, std::pair<qType, std::size_t>> label_dict;
89 std::string label_;
90};
91
92template <typename Scalar, typename Symmetry, typename OtherScalar>
94{
95 SiteOperator<std::common_type_t<Scalar, OtherScalar>, Symmetry> out(op.Q, op.data.coupledDomain(), op.data.world());
96 out.data = s * op.data;
97 return out;
98}
99
100template <typename Scalar, typename Symmetry, typename OtherScalar>
102{
104 return s * tmp_op;
105}
106
107template <typename Scalar, typename Symmetry, typename OtherScalar>
109{
110 SiteOperator<std::common_type_t<Scalar, OtherScalar>, Symmetry> out(op.Q, op.data.coupledDomain(), op.data.world());
111 out.data = s * op.data;
112 return out;
113}
114
115template <typename Scalar, typename Symmetry>
117{
118 auto Qtots = Symmetry::reduceSilent(O1.Q, O2.Q);
119 assert(Qtots.size() == 1 and
120 "The operator * for SiteOperator can only be used if the target quantumnumber is unique. Use SiteOperator::prod() instead.");
121 auto Oout = SiteOperator<Scalar, Symmetry>::prod(O1, O2, Qtots[0]);
122 return Oout;
123}
124
125template <typename Scalar, typename Symmetry>
127{
128 assert(O1.Q == O2.Q and "For addition of SiteOperator the operator quantum number needs to be the same.");
129 SiteOperator<Scalar, Symmetry> out(O1.Q, O1.data.coupledDomain());
130 out.data = O1.data + O2.data;
131 return out;
132}
133
134template <typename Scalar, typename Symmetry>
136{
137 assert(O1.Q == O2.Q and "For subtraction of SiteOperator the operator quantum number needs to be the same.");
138 SiteOperator<Scalar, Symmetry> out(O1.Q, O1.data.coupledDomain());
139 out.data = O1.data - O2.data;
140 return out;
141}
142
143template <typename Scalar, typename Symmetry>
145{
147}
148
149template <typename Scalar, typename Symmetry>
151 XPED_CONST SiteOperator<Scalar, Symmetry>& O2,
152 bool ADD_TWIST = false,
153 bool REVERSE_ORDER = false)
154{
155 if(not REVERSE_ORDER) {
156 Qbasis<Symmetry, 1> Otarget_op;
157 Otarget_op.push_back(Symmetry::qvacuum(), 1);
159 {{O2.data.uncoupledCodomain()[1], O1.data.uncoupledCodomain()[1]}}, {{Otarget_op}}, O1.data.world());
160 couple.setConstant(1.);
161 auto tmp1 = ADD_TWIST ? O1.data.twist(2).template contract<std::array{-1, -2, 1}, std::array{-3, 1, -4}, 2>(couple)
162 : O1.data.template contract<std::array{-1, -2, 1}, std::array{-3, 1, -4}, 2>(couple);
163 return tmp1.template contract<std::array{-1, -3, 1, -5}, std::array{-2, -4, 1}, 2>(O2.data).template trim<4>();
164 } else {
165 Qbasis<Symmetry, 1> Otarget_op;
166 Otarget_op.push_back(Symmetry::qvacuum(), 1);
167 Tensor<double, 2, 1, Symmetry, false> couple(
168 {{O1.data.uncoupledCodomain()[1], O2.data.uncoupledCodomain()[1]}}, {{Otarget_op}}, O1.data.world());
169 couple.setConstant(1.);
170 auto tmp1 = ADD_TWIST ? O1.data.twist(2).template contract<std::array{-1, -2, 1}, std::array{1, -3, -4}, 2>(couple)
171 : O1.data.template contract<std::array{-1, -2, 1}, std::array{1, -3, -4}, 2>(couple);
172 return tmp1.template contract<std::array{-1, -3, 1, -5}, std::array{-2, -4, 1}, 2>(O2.data).template trim<4>();
173 }
174}
175
176template <typename Scalar, typename Symmetry>
178{
181 return tprod(tmp_O1, tmp_O2);
182}
183
184template <typename Scalar, typename Symmetry>
185std::ostream& operator<<(std::ostream& os, const SiteOperator<Scalar, Symmetry>& Op)
186{
187 os << Op.print();
188 return os;
189}
190
191} // namespace Xped
192
193#ifndef XPED_COMPILED_LIB
194# include "Physics/SiteOperator.cpp"
195#endif
196
197#endif
Definition: Qbasis.hpp:39
void push_back(const qType &q, const size_t &inner_dim)
Definition: Qbasis.cpp:32
Definition: Tensor.hpp:40
XpedWorld & getUniverse()
Definition: Mpi.hpp:49
Definition: bench.cpp:62
XTensor< TRACK, Scalar, Rank, CoRank, Symmetry > operator-(const Tensor< Scalar, Rank, CoRank, Symmetry, true > &t, Scalar s)
Definition: ADTensor.hpp:428
Tensor< Scalar, 2, 2, Symmetry, false > tprod(XPED_CONST SiteOperator< Scalar, Symmetry > &O1, XPED_CONST SiteOperator< Scalar, Symmetry > &O2, bool ADD_TWIST=false, bool REVERSE_ORDER=false)
Definition: SiteOperator.hpp:150
XTensor< TRACK, Scalar, Rank, CoRank, Symmetry > operator+(const Tensor< Scalar, Rank, CoRank, Symmetry, true > &t, Scalar s)
Definition: ADTensor.hpp:440
XTensor< TRACK, Scalar, Rank, CoRank, Symmetry > operator*(const Tensor< Scalar, Rank, CoRank, Symmetry, true > &t, Scalar s)
Definition: ADTensor.hpp:452
SiteOperator< Scalar, Symmetry > kroneckerProduct(XPED_CONST SiteOperator< Scalar, Symmetry > &O1, XPED_CONST SiteOperator< Scalar, Symmetry > &O2)
Definition: SiteOperator.hpp:144
std::ostream & operator<<(std::ostream &os, const FusionTree< depth, Symmetry > &tree)
Definition: FusionTree.hpp:93
Definition: FusionTree.hpp:24
std::array< qType, Rank > q_uncoupled
Definition: FusionTree.hpp:28
Definition: SiteOperator.hpp:10
Tensor< Scalar, 1, 2, Symmetry, false > data
Definition: SiteOperator.hpp:86
std::string print() const
Definition: SiteOperator.cpp:110
Symmetry_ Symmetry
Definition: SiteOperator.hpp:12
const auto operator()(const qType &bra, const qType &ket) const
Definition: SiteOperator.hpp:25
static SiteOperator< Scalar, Symmetry > prod(XPED_CONST SiteOperator< Scalar, Symmetry > &O1, XPED_CONST SiteOperator< Scalar, Symmetry > &O2, const qType &target)
Definition: SiteOperator.cpp:36
void setZero()
Definition: SiteOperator.hpp:62
static SiteOperator< Scalar, Symmetry > outerprod(XPED_CONST SiteOperator< Scalar, Symmetry > &O1, XPED_CONST SiteOperator< Scalar, Symmetry > &O2, const qType &target)
Definition: SiteOperator.cpp:51
typename Symmetry::qType qType
Definition: SiteOperator.hpp:13
std::string & label()
Definition: SiteOperator.hpp:85
void setIdentity()
Definition: SiteOperator.hpp:63
std::string label_
Definition: SiteOperator.hpp:89
static SiteOperator< Scalar, Symmetry > outerprod(XPED_CONST SiteOperator< Scalar, Symmetry > &O1, XPED_CONST SiteOperator< Scalar, Symmetry > &O2)
Definition: SiteOperator.hpp:70
auto operator()(const qType &bra, const qType &ket)
Definition: SiteOperator.hpp:33
SiteOperator()=default
void setRandom()
Definition: SiteOperator.hpp:64
Scalar & operator()(const std::string &bra, const std::string &ket)
Definition: SiteOperator.hpp:51
Scalar norm() const
Definition: SiteOperator.cpp:86
typename Tensor< Scalar, 1, 2, Symmetry, false >::MatrixType MatrixType
Definition: SiteOperator.hpp:14
Scalar_ Scalar
Definition: SiteOperator.hpp:11
std::unordered_map< std::string, std::pair< qType, std::size_t > > label_dict
Definition: SiteOperator.hpp:88
const Scalar & operator()(const std::string &bra, const std::string &ket) const
Definition: SiteOperator.hpp:42
SiteOperator< Scalar, Symmetry > adjoint() XPED_CONST
Definition: SiteOperator.cpp:28
std::vector< MatrixType > plain() const
Definition: SiteOperator.cpp:92
qType Q
Definition: SiteOperator.hpp:87
Definition: Mpi.hpp:34