Cytnx v0.7.3
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 clear();
96 virtual UniTensor Launch(const bool &optimal=false);
97 virtual void PrintNet(std::ostream &os);
98 virtual boost::intrusive_ptr<Network_base> clone();
99 virtual void Savefile(const std::string &fname);
100 virtual ~Network_base(){};
101
102
103 };//Network_base
104
105 class RegularNetwork : public Network_base{
106
107 public:
108 RegularNetwork(){this->nwrktype_id = NtType.Regular;};
109 void Fromfile(const std::string &fname);
110 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true);
111 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true);
112 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true);
113 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="");
114 void clear(){
115 this->name2pos.clear();
116 this->CtTree.clear();
117 this->names.clear();
118 this->iBondNums.clear();
119 this->label_arr.clear();
120 this->TOUT_labels.clear();
121 this->TOUT_iBondNum = 0;
122 this->ORDER_tokens.clear();
123 }
124 UniTensor Launch(const bool &optimal=false);
125 boost::intrusive_ptr<Network_base> clone(){
126 RegularNetwork *tmp = new RegularNetwork();
127 tmp->name2pos = this->name2pos;
128 tmp->CtTree = this->CtTree;
129 tmp->names = this->names;
130 tmp->iBondNums = this->iBondNums;
131 tmp->label_arr = this->label_arr;
132 tmp->TOUT_labels = this->TOUT_labels;
133 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
134 tmp->ORDER_tokens = this->ORDER_tokens;
135 boost::intrusive_ptr<Network_base> out(tmp);
136 return out;
137 }
138 void PrintNet(std::ostream& os);
139 void Savefile(const std::string &fname);
140 ~RegularNetwork(){};
141
142 };
143
144 //Under dev!!
145 class FermionNetwork : public Network_base{
146 protected:
147 // [Future] Swap gates.
148
149 public:
150 FermionNetwork(){this->nwrktype_id=NtType.Fermion;};
151 void Fromfile(const std::string &fname){};
152 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true){};
153 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true){};
154 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true){};
155 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=""){};
156 void clear(){
157 this->name2pos.clear();
158 this->CtTree.clear();
159 this->names.clear();
160 this->iBondNums.clear();
161 this->label_arr.clear();
162 this->TOUT_labels.clear();
163 this->TOUT_iBondNum = 0;
164 this->ORDER_tokens.clear();
165 }
166 UniTensor Launch(const bool &optimal=false){return UniTensor();};
167 boost::intrusive_ptr<Network_base> clone(){
168 FermionNetwork *tmp = new FermionNetwork();
169 tmp->name2pos = this->name2pos;
170 tmp->CtTree = this->CtTree;
171 tmp->names = this->names;
172 tmp->iBondNums = this->iBondNums;
173 tmp->label_arr = this->label_arr;
174 tmp->TOUT_labels = this->TOUT_labels;
175 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
176 tmp->ORDER_tokens = this->ORDER_tokens;
177 boost::intrusive_ptr<Network_base> out(tmp);
178 return out;
179 }
180 void PrintNet(std::ostream &os){};
181 void Savefile(const std::string &fname){};
182 ~FermionNetwork(){};
183 };
184
186
187 /* @brief the Network object for easy build tensor network.
188
189 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.
190 */
191 class Network{
192 public:
194 boost::intrusive_ptr<Network_base> _impl;
195 Network(): _impl(new Network_base()){};
196 Network(const Network&rhs){this->_impl = rhs._impl;}
197 Network& operator=(const Network &rhs){this->_impl = rhs._impl; return *this;}
199
200
201
248 void Fromfile(const std::string &fname, const int &network_type=NtType.Regular){
249 if(network_type==NtType.Regular){
250 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
251 this->_impl = tmp;
252 }else{
253 cytnx_error_msg(true,"[Developing] currently only support regular type network.%s","\n");
254 }
255 this->_impl->Fromfile(fname);
256 }
257
258 //void Savefile(const std::string &fname);
259
260
261
262 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=""){
263 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
264 Network out;
265 out._impl = tmp;
266 out._impl->Contract_plan(tensors,Tout,is_clone,alias,contract_order);
267 return out;
268 }
269
270
271 Network(const std::string &fname, const int &network_type=NtType.Regular){
272 this->Fromfile(fname,network_type);
273 }
274
275 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true){
276 this->_impl->PutUniTensor(name,utensor,is_clone);
277 }
278 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true){
279 this->_impl->PutUniTensor(idx,utensor,is_clone);
280 }
281 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true){
282 this->_impl->PutUniTensors(name,utensors,is_clone);
283 }
284 UniTensor Launch(const bool &optimal=false){
285 return this->_impl->Launch(optimal);
286 }
287 void clear(){
288 //boost::intrusive_ptr<Network_base> tmp(new Network_base());
289 this->_impl->clear();
290 }
291
293 Network out;
294 out._impl = this->_impl->clone();
295 return out;
296 }
297 void PrintNet(){
298 this->_impl->PrintNet(std::cout);
299 }
300
301 void Savefile(const std::string &fname){
302 this->_impl->Savefile(fname);
303 }
304
305 };
306
308 std::ostream& operator<<(std::ostream &os,const Network &bin);
310}
311
312
313#endif
Definition Network.hpp:191
void Fromfile(const std::string &fname, const int &network_type=NtType.Regular)
Construct Network from network file.
Definition Network.hpp:248
Network(const std::string &fname, const int &network_type=NtType.Regular)
Definition Network.hpp:271
Network clone()
Definition Network.hpp:292
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:262
void PrintNet()
Definition Network.hpp:297
UniTensor Launch(const bool &optimal=false)
Definition Network.hpp:284
void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true)
Definition Network.hpp:275
void clear()
Definition Network.hpp:287
void PutUniTensors(const std::vector< std::string > &name, const std::vector< UniTensor > &utensors, const bool &is_clone=true)
Definition Network.hpp:281
void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true)
Definition Network.hpp:278
void Savefile(const std::string &fname)
Definition Network.hpp:301
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1072
#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