Cytnx v0.7.4
Loading...
Searching...
No Matches
Network.hpp
Go to the documentation of this file.
1#ifndef _H_Network_
2#define _H_Network_
3
4
5
6#include "Type.hpp"
7#include "cytnx_error.hpp"
8#include <initializer_list>
9#include <vector>
10#include <map>
11#include <fstream>
13#include "utils/utils.hpp"
14#include "Network.hpp"
15#include "UniTensor.hpp"
16#include "contraction_tree.hpp"
17namespace cytnx{
19 struct __ntwk{
20 enum __nttype{
21 Void = -1,
22 Regular = 0,
23 Fermion= 1
24 };
25 };
26 class NetworkType_class{
27 public:
28 enum : int{
29 Void= -1,
30 Regular= 0,
31 Fermion= 1
32 };
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<cytnx_int64> TOUT_labels;
46 cytnx_uint64 TOUT_iBondNum;
47
48 //bool ordered;
49
50 //Contraction order.
51 ContractionTree CtTree;
52 std::vector<std::string> ORDER_tokens;
53
54 // labels corr to the tn list.
55 std::vector< std::vector<cytnx_int64> > label_arr;
56 std::vector< cytnx_int64 > iBondNums;
57
58
59 // name of tn.
60 std::vector< std::string > names;
61 std::map<std::string,cytnx_uint64> name2pos;
62
63
64
65 friend class FermionNetwork;
66 friend class RegularNetwork;
67 friend class Network;
68 Network_base():nwrktype_id(NtType.Void){};
69
70
71 bool HasPutAllUniTensor(){
72 for(cytnx_uint64 i=0;i<this->tensors.size();i++){
73 if(this->tensors[i].uten_type()==UTenType.Void) return false;
74 }
75 return true;
76 }
77
78 //void print_network() const;
79
80 //void PreConstruct(bool force = true);
81
82 //void PutTensor(cytnx_int64 idx, const UniTensor& UniT, bool force = true);
83
84 //void PutTensor(const std::string &name, const UniTensor &UniT, bool force = true);
85
86 //UniTensor Launch(const std::string &Tname="");
87
88 //std::string GetContractOrder() const;
89 virtual void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone);
90 virtual void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone);
91 virtual void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone);
92 virtual void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout, const std::vector<bool> &is_clone, const std::vector<std::string> &alias, const std::string &contract_order);
93
94 virtual void Fromfile(const std::string& fname);
95 virtual void FromString(const std::vector<std::string> &content);
96 virtual void clear();
97 virtual UniTensor Launch(const bool &optimal=false);
98 virtual void PrintNet(std::ostream &os);
99 virtual boost::intrusive_ptr<Network_base> clone();
100 virtual void Savefile(const std::string &fname);
101 virtual ~Network_base(){};
102
103
104 };//Network_base
105
106 class RegularNetwork : public Network_base{
107
108 public:
109 RegularNetwork(){this->nwrktype_id = NtType.Regular;};
110 void Fromfile(const std::string &fname);
111 void FromString(const std::vector<std::string> &contents);
112 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true);
113 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true);
114 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true);
115 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout, const std::vector<bool> &is_clone={}, const std::vector<std::string> &alias={}, const std::string &contract_order="");
116 void clear(){
117 this->name2pos.clear();
118 this->CtTree.clear();
119 this->names.clear();
120 this->iBondNums.clear();
121 this->label_arr.clear();
122 this->TOUT_labels.clear();
123 this->TOUT_iBondNum = 0;
124 this->ORDER_tokens.clear();
125 }
126 UniTensor Launch(const bool &optimal=false);
127 boost::intrusive_ptr<Network_base> clone(){
128 RegularNetwork *tmp = new RegularNetwork();
129 tmp->name2pos = this->name2pos;
130 tmp->CtTree = this->CtTree;
131 tmp->names = this->names;
132 tmp->iBondNums = this->iBondNums;
133 tmp->label_arr = this->label_arr;
134 tmp->TOUT_labels = this->TOUT_labels;
135 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
136 tmp->ORDER_tokens = this->ORDER_tokens;
137 boost::intrusive_ptr<Network_base> out(tmp);
138 return out;
139 }
140 void PrintNet(std::ostream& os);
141 void Savefile(const std::string &fname);
142 ~RegularNetwork(){};
143
144 };
145
146 //Under dev!!
147 class FermionNetwork : public Network_base{
148 protected:
149 // [Future] Swap gates.
150
151 public:
152 FermionNetwork(){this->nwrktype_id=NtType.Fermion;};
153 void Fromfile(const std::string &fname){};
154 void FromString(const std::vector<std::string> &contents){};
155 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true){};
156 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true){};
157 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true){};
158 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout, const std::vector<bool> &is_clone={}, const std::vector<std::string> &alias={}, const std::string &contract_order=""){};
159 void clear(){
160 this->name2pos.clear();
161 this->CtTree.clear();
162 this->names.clear();
163 this->iBondNums.clear();
164 this->label_arr.clear();
165 this->TOUT_labels.clear();
166 this->TOUT_iBondNum = 0;
167 this->ORDER_tokens.clear();
168 }
169 UniTensor Launch(const bool &optimal=false){return UniTensor();};
170 boost::intrusive_ptr<Network_base> clone(){
171 FermionNetwork *tmp = new FermionNetwork();
172 tmp->name2pos = this->name2pos;
173 tmp->CtTree = this->CtTree;
174 tmp->names = this->names;
175 tmp->iBondNums = this->iBondNums;
176 tmp->label_arr = this->label_arr;
177 tmp->TOUT_labels = this->TOUT_labels;
178 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
179 tmp->ORDER_tokens = this->ORDER_tokens;
180 boost::intrusive_ptr<Network_base> out(tmp);
181 return out;
182 }
183 void PrintNet(std::ostream &os){};
184 void Savefile(const std::string &fname){};
185 ~FermionNetwork(){};
186 };
187
189
190 /* @brief the Network object for easy build tensor network.
191
192 The Network is an object that allow one to create a complex network from a pre-defined Network file. By putting the Tensors into the Network, the user simply call “Network.Launch()” to get the out-come.
193 */
194 class Network{
195 public:
197 boost::intrusive_ptr<Network_base> _impl;
198 Network(): _impl(new Network_base()){};
199 Network(const Network&rhs){this->_impl = rhs._impl;}
200 Network& operator=(const Network &rhs){this->_impl = rhs._impl; return *this;}
202
203
204
251 void Fromfile(const std::string &fname, const int &network_type=NtType.Regular){
252 if(network_type==NtType.Regular){
253 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
254 this->_impl = tmp;
255 }else{
256 cytnx_error_msg(true,"[Developing] currently only support regular type network.%s","\n");
257 }
258 this->_impl->Fromfile(fname);
259 }
260
287 void FromString(const std::vector<std::string> &contents, const int &network_type=NtType.Regular){
288 if(network_type==NtType.Regular){
289 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
290 this->_impl = tmp;
291 }else{
292 cytnx_error_msg(true,"[Developing] currently only support regular type network.%s","\n");
293 }
294 this->_impl->FromString(contents);
295 }
296 //void Savefile(const std::string &fname);
297
298
299
300 static Network Contract(const std::vector<UniTensor> &tensors, const std::string &Tout, const std::vector<bool> &is_clone={}, const std::vector<std::string> &alias={}, const std::string &contract_order=""){
301 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
302 Network out;
303 out._impl = tmp;
304 out._impl->Contract_plan(tensors,Tout,is_clone,alias,contract_order);
305 return out;
306 }
307
308
309
310 Network(const std::string &fname, const int &network_type=NtType.Regular){
311 this->Fromfile(fname,network_type);
312 }
313
314 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true){
315 this->_impl->PutUniTensor(name,utensor,is_clone);
316 }
317 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true){
318 this->_impl->PutUniTensor(idx,utensor,is_clone);
319 }
320 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true){
321 this->_impl->PutUniTensors(name,utensors,is_clone);
322 }
323 UniTensor Launch(const bool &optimal=false){
324 return this->_impl->Launch(optimal);
325 }
326 void clear(){
327 //boost::intrusive_ptr<Network_base> tmp(new Network_base());
328 this->_impl->clear();
329 }
330
332 Network out;
333 out._impl = this->_impl->clone();
334 return out;
335 }
336 void PrintNet(){
337 this->_impl->PrintNet(std::cout);
338 }
339
340 void Savefile(const std::string &fname){
341 this->_impl->Savefile(fname);
342 }
343
344 };
345
347 std::ostream& operator<<(std::ostream &os,const Network &bin);
349}
350
351
352#endif
Definition Network.hpp:194
void Fromfile(const std::string &fname, const int &network_type=NtType.Regular)
Construct Network from network file.
Definition Network.hpp:251
Network(const std::string &fname, const int &network_type=NtType.Regular)
Definition Network.hpp:310
Network clone()
Definition Network.hpp:331
static Network Contract(const std::vector< UniTensor > &tensors, const std::string &Tout, const std::vector< bool > &is_clone={}, const std::vector< std::string > &alias={}, const std::string &contract_order="")
Definition Network.hpp:300
void PrintNet()
Definition Network.hpp:336
UniTensor Launch(const bool &optimal=false)
Definition Network.hpp:323
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:287
void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true)
Definition Network.hpp:314
void clear()
Definition Network.hpp:326
void PutUniTensors(const std::vector< std::string > &name, const std::vector< UniTensor > &utensors, const bool &is_clone=true)
Definition Network.hpp:320
void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true)
Definition Network.hpp:317
void Savefile(const std::string &fname)
Definition Network.hpp:340
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1123
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
UniTensorType_class UTenType
Definition UniTensor_base.cpp:21
std::ostream & operator<<(std::ostream &os, const Scalar &in)
Definition Scalar.cpp:14
NetworkType_class NtType
Definition Network.cpp:21
uint64_t cytnx_uint64
Definition Type.hpp:22