-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyObserver.h
87 lines (75 loc) · 2.33 KB
/
MyObserver.h
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
84
85
86
87
#ifndef __MYOBSERVER_H_CMC__
#define __MYOBSERVER_H_CMC__
#include "itensor/all.h"
#include "Entanglement.h"
using namespace itensor;
template <typename SitesType>
class MyObserver : public DMRGObserver
{
public:
MyObserver (const SitesType& sites, const MPS& psi, const Args& args = Args::global())
: DMRGObserver (psi, args)
, _sites (sites)
, _ns (length(psi),0.)
, _Npar (0.)
{
_write = args.getBool ("Write",false);
_write_minm = args.getInt ("out_minm",0);
_out_dir = args.getString("out_dir",".");
}
void measure (const Args& args = Args::global());
Real Npar () const { return _Npar; }
auto const& ns () const { return _ns; }
private:
bool _write;
string _out_dir; // empty string "" if not write
int _write_minm;
vector<int> _iDel, _jDel;
vector<Real> _Delta;
SitesType _sites;
vector<Real> _ns;
Real _Npar;
};
Real Onsite_mea (const ITensor& A, const ITensor& op)
{
ITensor re = A * op;
re.noPrime ("Site");
re *= dag(A);
return re.real();
}
template <typename SitesType>
void MyObserver<SitesType> :: measure (const Args& args)
{
DMRGObserver::measure (args);
// Define your measurements below
// Call psi() to access the MPS
//
auto N = length(psi());
auto b = args.getInt("AtBond",1);
auto sw = args.getInt("Sweep",0);
auto ha = args.getInt("HalfSweep",0);
auto energy = args.getReal("Energy",0);
// On-site measure
int oc = orthoCenter(psi());
if (oc == N || ha == 2) // measure during the second half of sweep
{
// Density
ITensor n_op = _sites.op("N",oc);
Real ni = Onsite_mea (psi().A(oc), n_op);
cout << "\tn " << oc << " = " << ni << endl;
_ns.at(oc-1) = ni;
// Entanglement entropy
Real S = EntangEntropy (spectrum());
cout << "\tentang entropy " << oc << " = " << S << endl;
}
if (oc == 1 && ha == 2)
{
int m = args.getInt("MaxDim");
// Write MPS
if (_write && m >= _write_minm)
{
writeToFile (_out_dir+"/psi"+"_m"+to_string(m)+".mps", psi());
}
}
}
#endif