Cytnx 1.1.2.dev761+ge28145acd
Loading...
Searching...
No Matches
Network.hpp
Go to the documentation of this file.
1#ifndef CYTNX_NETWORK_H_
2#define CYTNX_NETWORK_H_
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
6#include <initializer_list>
7#include <memory>
8#include <vector>
9#include <map>
10#include <fstream>
12#include "utils/utils.hpp"
13#include "UniTensor.hpp"
14#include "contraction_tree.hpp"
15
16#ifdef UNI_GPU
17 #ifdef UNI_CUQUANTUM
18 #include <cutensornet.h>
19 #endif
20#endif
21
22namespace cytnx {
24#if defined(UNI_GPU) && defined(UNI_CUQUANTUM)
25 class cutensornet;
26#endif
27 struct __ntwk {
28 enum __nttype { Void = -1, Regular = 0, Fermion = 1 };
29 };
30 class NetworkType_class {
31 public:
32 enum : int { Void = -1, Regular = 0, Fermion = 1 };
33 std::string getname(const int &nwrktype_id);
34 };
35 extern NetworkType_class NtType;
37
39 class Network_base : public intrusive_ptr_base<Network_base> {
40 // protected:
41 public:
42 int nwrktype_id;
43 std::string filename;
44 std::vector<UniTensor> tensors;
45 std::vector<std::string> TOUT_labels;
46
47 cytnx_uint64 TOUT_iBondNum;
48
49 // bool ordered;
50
51 // Contraction order.
52 ContractionTree CtTree;
53 std::vector<std::string> ORDER_tokens;
54 // order line
55 std::string order_line = "";
56 // einsum path representation of order
57 std::vector<std::pair<cytnx_int64, cytnx_int64>> einsum_path;
58
59 // labels corr to the tn list.
60 std::vector<std::vector<std::string>> label_arr;
61 std::vector<cytnx_int64> iBondNums;
62
63 // name of tn.
64 std::vector<std::string> names;
65 std::map<std::string, cytnx_uint64> name2pos;
66
67 // maintan tout leg position : (tesnor id, leg idx) for each open leg.
68 std::vector<std::pair<int, int>> TOUT_pos;
69
70 // pure int version of the contract labels
71 std::vector<std::vector<cytnx_int64>> int_modes;
72 std::vector<cytnx_int64> int_out_mode;
73
74#ifdef UNI_GPU
75 #ifdef UNI_CUQUANTUM
76 // The cuTensorNet session of the latest setOrder(optimal=true). Launch() contracts with the
77 // optimizer info this session holds, so it has to stay alive as long as the network does.
78 std::shared_ptr<cutensornet> optimal_order_session;
79 #endif
80#endif
81 friend class FermionNetwork;
82 friend class RegularNetwork;
83 friend class Network;
84 Network_base() : nwrktype_id(NtType.Void){};
85
86 bool HasPutAllUniTensor() {
87 for (cytnx_uint64 i = 0; i < this->tensors.size(); i++) {
88 if (this->tensors[i].uten_type() == UTenType.Void) return false;
89 }
90 return true;
91 }
92
93 // void print_network() const;
94
95 // void PreConstruct(bool force = true);
96
97 // void PutTensor(cytnx_int64 idx, const UniTensor& UniT, bool force = true);
98
99 // void PutTensor(const std::string &name, const UniTensor &UniT, bool force = true);
100
101 // UniTensor Launch(const std::string &Tname="");
102
103 // std::string GetContractOrder() const;
104 virtual void PutUniTensor(const std::string &name, const UniTensor &utensor);
105 virtual void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor);
106 virtual void PutUniTensors(const std::vector<std::string> &name,
107 const std::vector<UniTensor> &utensors);
108
109 virtual void RmUniTensor(const cytnx_uint64 &idx);
110 virtual void RmUniTensor(const std::string &name);
111 virtual void RmUniTensors(const std::vector<std::string> &name);
112
113 virtual void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
114 const std::vector<std::string> &alias,
115 const std::string &contract_order);
116
117 virtual void Fromfile(const std::string &fname);
118 virtual void FromString(const std::vector<std::string> &content);
119 virtual void clear();
120 virtual std::string getOptimalOrder();
121 // virtual UniTensor Launch(const bool &optimal = false, const std::string &contract_order =
122 // "");
123
124 virtual std::string getOrder();
125 virtual void setOrder(const bool &optimal = false, const std::string &contract_order = "");
126
127 virtual UniTensor Launch();
128
129 virtual void construct(const std::vector<std::string> &alias,
130 const std::vector<std::vector<std::string>> &labels,
131 const std::vector<std::string> &outlabel, const cytnx_int64 &outrk,
132 const std::string &order, const bool optim);
133 virtual void PrintNet(std::ostream &os);
134 virtual boost::intrusive_ptr<Network_base> clone();
135 virtual void Savefile(const std::string &fname);
136 virtual ~Network_base(){};
137
138 }; // Network_base
139
140 class RegularNetwork : public Network_base {
141 public:
142 RegularNetwork() { this->nwrktype_id = NtType.Regular; };
143 void Fromfile(const std::string &fname);
144 void FromString(const std::vector<std::string> &contents);
145 void PutUniTensor(const std::string &name, const UniTensor &utensor);
146 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor);
147 void PutUniTensors(const std::vector<std::string> &name,
148 const std::vector<UniTensor> &utensors);
149 // void initialize_CtTree();
150 void RmUniTensor(const cytnx_uint64 &idx);
151 void RmUniTensor(const std::string &name);
152 void RmUniTensors(const std::vector<std::string> &name);
153
154 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
155 const std::vector<std::string> &alias = {},
156 const std::string &contract_order = "");
157 void clear() {
158 this->tensors.clear();
159 this->name2pos.clear();
160 this->CtTree.clear();
161 this->names.clear();
162 this->iBondNums.clear();
163 this->label_arr.clear();
164 this->TOUT_labels.clear();
165 this->TOUT_iBondNum = 0;
166 this->ORDER_tokens.clear();
167 }
168 std::string getOptimalOrder();
169 // UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "");
170
171 std::string getOrder();
172 void setOrder(const bool &optimal = false, const std::string &contract_order = "");
173 UniTensor Launch();
174
175 void construct(const std::vector<std::string> &alias,
176 const std::vector<std::vector<std::string>> &labels,
177 const std::vector<std::string> &outlabel, const cytnx_int64 &outrk,
178 const std::string &order, const bool optim);
179 boost::intrusive_ptr<Network_base> clone() {
180 RegularNetwork *tmp = new RegularNetwork();
181 tmp->name2pos = this->name2pos;
182 tmp->CtTree = this->CtTree;
183 tmp->names = this->names;
184 tmp->iBondNums = this->iBondNums;
185 tmp->label_arr = this->label_arr;
186 tmp->TOUT_labels = this->TOUT_labels;
187 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
188 tmp->ORDER_tokens = this->ORDER_tokens;
189 boost::intrusive_ptr<Network_base> out(tmp);
190 return out;
191 }
192 void PrintNet(std::ostream &os);
193 void Savefile(const std::string &fname);
194 ~RegularNetwork(){};
195 };
196
197 // Under dev!!
198 class FermionNetwork : public Network_base {
199 protected:
200 // [Future] Swap gates.
201
202 public:
203 FermionNetwork() { this->nwrktype_id = NtType.Fermion; };
204 void Fromfile(const std::string &fname){};
205 void FromString(const std::vector<std::string> &contents){};
206 void RmUniTensor(const cytnx_uint64 &idx){};
207 void RmUniTensor(const std::string &name){};
208 void RmUniTensors(const std::vector<std::string> &name){};
209
210 void PutUniTensor(const std::string &name, const UniTensor &utensor){};
211 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor){};
212 void PutUniTensors(const std::vector<std::string> &name,
213 const std::vector<UniTensor> &utensors){};
214 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
215 const std::vector<std::string> &alias = {},
216 const std::string &contract_order = ""){};
217 void clear() {
218 this->name2pos.clear();
219 this->CtTree.clear();
220 this->names.clear();
221 this->iBondNums.clear();
222 this->label_arr.clear();
223 this->TOUT_labels.clear();
224 this->TOUT_iBondNum = 0;
225 this->ORDER_tokens.clear();
226 }
227 UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "") {
228 return UniTensor();
229 };
230 boost::intrusive_ptr<Network_base> clone() {
231 FermionNetwork *tmp = new FermionNetwork();
232 tmp->name2pos = this->name2pos;
233 tmp->CtTree = this->CtTree;
234 tmp->names = this->names;
235 tmp->iBondNums = this->iBondNums;
236 tmp->label_arr = this->label_arr;
237 tmp->TOUT_labels = this->TOUT_labels;
238 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
239 tmp->ORDER_tokens = this->ORDER_tokens;
240 boost::intrusive_ptr<Network_base> out(tmp);
241 return out;
242 }
243 void PrintNet(std::ostream &os){};
244 void Savefile(const std::string &fname){};
245 ~FermionNetwork(){};
246 };
247
249
250 /* @brief the Network object for easy build tensor network.
251
252 The Network is an object that allow one to create a complex network from a pre-defined
253 Network file. By putting the Tensors into the Network, the user simply call “Network.Launch()”
254 to get the out-come.
255 */
256 class Network {
257 public:
259 boost::intrusive_ptr<Network_base> _impl;
260 Network() : _impl(new Network_base()){};
261 Network(const Network &rhs) { this->_impl = rhs._impl; }
262 Network &operator=(const Network &rhs) {
263 this->_impl = rhs._impl;
264 return *this;
265 }
267
315 void Fromfile(const std::string &fname, const int &network_type = NtType.Regular) {
316 if (network_type == NtType.Regular) {
317 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
318 this->_impl = tmp;
319 } else {
320 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
321 }
322 this->_impl->Fromfile(fname);
323 }
324
352 void FromString(const std::vector<std::string> &contents,
353 const int &network_type = NtType.Regular) {
354 if (network_type == NtType.Regular) {
355 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
356 this->_impl = tmp;
357 } else {
358 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
359 }
360 this->_impl->FromString(contents);
361 }
362 // void Savefile(const std::string &fname);
363
364 static Network Contract(const std::vector<UniTensor> &tensors, const std::string &Tout,
365 const std::vector<std::string> &alias = {},
366 const std::string &contract_order = "") {
367 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
368 Network out;
369 out._impl = tmp;
370 out._impl->Contract_plan(tensors, Tout, alias, contract_order);
371 return out;
372 }
373
374 Network(const std::string &fname, const int &network_type = NtType.Regular) {
375 this->Fromfile(fname, network_type);
376 }
377
378 void RmUniTensor(const std::string &name) { this->_impl->RmUniTensor(name); }
379 void RmUniTensor(const cytnx_uint64 &idx) { this->_impl->RmUniTensor(idx); }
380 void RmUniTensors(const std::vector<std::string> &names) { this->_impl->RmUniTensors(names); }
381 void PutUniTensor(const std::string &name, const UniTensor &utensor,
382 const std::vector<std::string> &label_order = {}) {
383 if (label_order.size()) {
384 auto tmpu = utensor.permute(label_order);
385 this->_impl->PutUniTensor(name, tmpu);
386 } else
387 this->_impl->PutUniTensor(name, utensor);
388 }
389 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor,
390 const std::vector<std::string> &label_order = {}) {
391 if (label_order.size()) {
392 auto tmpu = utensor.permute(label_order);
393 this->_impl->PutUniTensor(idx, tmpu);
394 } else
395 this->_impl->PutUniTensor(idx, utensor);
396 }
397
398 void PutUniTensors(const std::vector<std::string> &name,
399 const std::vector<UniTensor> &utensors) {
400 this->_impl->PutUniTensors(name, utensors);
401 }
402 std::string getOptimalOrder(const int &network_type = NtType.Regular) {
403 if (network_type == NtType.Regular) {
404 return this->_impl->getOptimalOrder();
405 } else {
406 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
407 }
408 }
409
410 std::string getOrder() { return this->_impl->getOrder(); }
411
412 void setOrder(const bool &optimal, const std::string &contract_order /*default ""*/) {
413 return this->_impl->setOrder(optimal, contract_order);
414 }
415
416 UniTensor Launch(const int &network_type = NtType.Regular) {
417 if (network_type == NtType.Regular) {
418 return this->_impl->Launch();
419 } else {
420 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
421 }
422 }
423
424 void construct(const std::vector<std::string> &alias,
425 const std::vector<std::vector<std::string>> &labels,
426 const std::vector<std::string> &outlabel = std::vector<std::string>(),
427 const cytnx_int64 &outrk = 0, const std::string &order = "",
428 const bool optim = false, const int &network_type = NtType.Regular) {
429 if (network_type == NtType.Regular) {
430 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
431 this->_impl = tmp;
432 } else {
433 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
434 }
435 this->_impl->construct(alias, labels, outlabel, outrk, order, optim);
436 }
437
438 void clear() {
439 // boost::intrusive_ptr<Network_base> tmp(new Network_base());
440 this->_impl->clear();
441 }
442
444 Network out;
445 out._impl = this->_impl->clone();
446 return out;
447 }
448 void PrintNet() { this->_impl->PrintNet(std::cout); }
449
450 void Savefile(const std::string &fname) { this->_impl->Savefile(fname); }
451 };
452
454 std::ostream &operator<<(std::ostream &os, const Network &bin);
456} // namespace cytnx
457
458#endif // CYTNX_NETWORK_H_
Definition Network.hpp:256
void Fromfile(const std::string &fname, const int &network_type=NtType.Regular)
Construct Network from network file.
Definition Network.hpp:315
Network(const std::string &fname, const int &network_type=NtType.Regular)
Definition Network.hpp:374
Network clone()
Definition Network.hpp:443
void setOrder(const bool &optimal, const std::string &contract_order)
Definition Network.hpp:412
void construct(const std::vector< std::string > &alias, const std::vector< std::vector< std::string > > &labels, const std::vector< std::string > &outlabel=std::vector< std::string >(), const cytnx_int64 &outrk=0, const std::string &order="", const bool optim=false, const int &network_type=NtType.Regular)
Definition Network.hpp:424
void PrintNet()
Definition Network.hpp:448
std::string getOptimalOrder(const int &network_type=NtType.Regular)
Definition Network.hpp:402
void RmUniTensors(const std::vector< std::string > &names)
Definition Network.hpp:380
UniTensor Launch(const int &network_type=NtType.Regular)
Definition Network.hpp:416
void RmUniTensor(const cytnx_uint64 &idx)
Definition Network.hpp:379
static Network Contract(const std::vector< UniTensor > &tensors, const std::string &Tout, const std::vector< std::string > &alias={}, const std::string &contract_order="")
Definition Network.hpp:364
void FromString(const std::vector< std::string > &contents, const int &network_type=NtType.Regular)
Construct Network from a list of strings, where each string is the same as each line in network file.
Definition Network.hpp:352
void clear()
Definition Network.hpp:438
void RmUniTensor(const std::string &name)
Definition Network.hpp:378
std::string getOrder()
Definition Network.hpp:410
void PutUniTensors(const std::vector< std::string > &name, const std::vector< UniTensor > &utensors)
Definition Network.hpp:398
void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const std::vector< std::string > &label_order={})
Definition Network.hpp:389
void PutUniTensor(const std::string &name, const UniTensor &utensor, const std::vector< std::string > &label_order={})
Definition Network.hpp:381
void Savefile(const std::string &fname)
Definition Network.hpp:450
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:2773
UniTensor permute(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1) const
permute the legs of the UniTensor
Definition UniTensor.hpp:3803
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:118
Definition Accessor.hpp:12
UniTensorType_class UTenType
UniTensor type.
@ Void
Definition Symmetry.hpp:30