-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathupgrademps.cc
More file actions
83 lines (70 loc) · 2.04 KB
/
upgrademps.cc
File metadata and controls
83 lines (70 loc) · 2.04 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
#include "itensor/all.h"
using namespace itensor;
using std::string;
//
// This code is for fixing MPS written to disk
// prior to version 2.1.0
//
// Calling this code as:
// ./upgrademps mpsfile sitefile new_mpsfile
// Reads in an MPS from the file "mpsfile"
// and a site set from the file "sitefile"
// and writes the upgraded MPS to the file "new_mpsfile"
// such that it is usable with ITensor version 2.1.0
// and later
//
template<typename MPSType>
MPSType
v20read(std::istream & s, int N)
{
using T = MPSType::TensorT;
auto psi = MPSType(N);
for(auto j : range(N+2))
{
psi.setA(j,itensor::read<T>(s));
}
auto llim = itensor::read<int>(s);
auto rlim = itensor::read<int>(s);
psi.leftLim(llim);
psi.rightLim(rlim);
return psi;
}
template<typename MPSType>
void
do_upgrade(string mpsfile,
string sitefile,
string new_mpsfile)
{
auto sites = readFromFile<SiteSet>(sitefile);
auto s = std::ifstream(mpsfile.c_str(),std::ios::binary);
if(!s.good()) throw ITError("Couldn't open file \"" + mpsfile + "\" for reading");
auto psi = v20read<MPSType>(s,sites.N());
s.close();
printfln("Writing upgraded MPS to file \"%s\"",new_mpsfile);
writeToFile<MPSType>(new_mpsfile,psi);
}
int
main(int argc, char* argv[])
{
if(argc != 4)
{
println("Usage ./upgrademps mpsfile sitefile new_mpsfile");
return 0;
}
string mpsfile = argv[1];
string sitefile = argv[2];
string new_mpsfile = argv[3];
Print(mpsfile);
Print(sitefile);
println("Which type of MPS? Type 1 for MPS; 2 for IQMPS.");
int type = 0;
auto inputok = [&type]() { return type == 1 || type == 2; };
while(not inputok())
{
std::cin >> type;
if(not inputok()) printfln("Please input either 1 for MPS, or 2 for IQMPS. (Got %d.)",type);
}
if(type == 1) do_upgrade<MPS>(mpsfile,sitefile,new_mpsfile);
else if(type == 2) do_upgrade<IQMPS>(mpsfile,sitefile,new_mpsfile);
return 0;
}