VMPS++
Loading...
Searching...
No Matches
UmpsCompressor.h
Go to the documentation of this file.
1#ifndef UMPS_COMPRESSOR_H_
2#define UMPS_COMPRESSOR_H_
3
5#include "termcolor.hpp" //from https://github.com/ikalnytskyi/termcolor
7
9
11
21template<typename Symmetry, typename Scalar, typename MpoScalar=double>
23{
24public:
25
27 :CHOSEN_VERBOSITY(VERBOSITY)
28 {};
29
30 //---info stuff---
32
33 string info() const;
34
35 // string t_info() const;
36
38 double memory (MEMUNIT memunit=GB) const;
40
41 //---compression schemes---
43
54 size_t Dinit_input, size_t Qinit_input, double tol_input, size_t max_iterations=100ul, size_t min_iterations=10ul);
56
57private:
58
60
61 // for |Vout> ≈ |Vin>
62 vector<PivotOverlap1<Symmetry,Scalar> > Env;
63
65
66 void build_cellEnv (const Umps<Symmetry,Scalar> &Vbra, const Umps<Symmetry,Scalar> &Vket);
67 void build_LR (const Umps<Symmetry,Scalar> &Vbra, const Umps<Symmetry,Scalar> &Vket);
68 void calc_error(const Umps<Symmetry,Scalar> &Vout);
69
70
72 inline size_t minus1modL (size_t l) const {return (l==0)? N_sites-1 : (l-1);}
73
74 size_t N_sites;
76 size_t Dinit, Qinit;
77 double err_var, tol;
78 complex<double> lambdaL;
79 double t_fixedL;
80 double t_fixedR;
81};
82
83template<typename Symmetry, typename Scalar, typename MpoScalar>
85info() const
86{
87 stringstream ss;
88 ss << "UmpsCompressor: ";
89 ss << "Dinit=" << Dinit << ", ";
90 // ss << "Mmax=" << Mmax;
91 // if (Mmax != Mmax_new)
92 // {
93 // ss << "→" << Mmax_new << ", ";
94 // }
95 // else
96 // {
97 // ss << " (not changed), ";
98 // }
99
100 ss << "|AL*C-AC/λ|^2=";
101 if (err_var <= tol) {ss << termcolor::colorize << termcolor::green;}
102 else {ss << termcolor::colorize << termcolor::red;}
103 ss << err_var << termcolor::reset << ", ";
104 ss << "iterations=" << N_iterations << ", ";
105 ss << "mem=" << round(memory(GB),3) << "GB";
106 return ss.str();
107}
108
109template<typename Symmetry, typename Scalar, typename MpoScalar>
111memory (MEMUNIT memunit) const
112{
113 double res = 0.;
114 for (size_t l=0; l<Env.size(); ++l)
115 {
116 res += Env[l].L.memory(memunit);
117 res += Env[l].R.memory(memunit);
118 }
119 for (size_t l=0; l<Env.size(); ++l)
120 {
121 res += Env[l].L.memory(memunit);
122 res += Env[l].R.memory(memunit);
123 }
124 return res;
125}
126
127template<typename Symmetry, typename Scalar, typename MpoScalar>
130 size_t Dinit_input, size_t Qinit_input, double tol_input, size_t max_iterations, size_t min_iterations)
131{
132 Dinit = Dinit_input;
133 Qinit = Qinit_input;
134 tol = tol_input;
135 N_sites = Vin.length();
136
137 //set initial state to random
138 Vout = Umps<Symmetry,Scalar>(Vin.locBasis(), Vin.Qtarget(), Vin.length(), Dinit, Qinit);
139 Vout.setRandom();
140 cout << Vout.test_ortho() << endl;
141 size_t loc=0;
142 err_var = 1.;
143
144 //set initial cell environments to random
145 Env.clear();
146 Env.resize(N_sites);
147 Env[0].L.setRandom(Vout.inBasis(0), Vin.inBasis(0));
148 Env[N_sites-1].R.setRandom(Vin.outBasis(N_sites-1), Vout.outBasis(N_sites-1));
149 for (size_t l=0; l<N_sites; ++l)
150 {
151 Env[l].qloc = Vin.locBasis(l);
152 }
153
154 //apply variational optimization of the overlap
155 while ((err_var > tol and N_iterations < max_iterations) or N_iterations < min_iterations or N_iterations%2 != 0)
156 {
157 optimize_parallel(Vin,Vout);
158 }
159}
160
161template<typename Symmetry, typename Scalar, typename MpoScalar>
164{
165 // Make environment for the unit cell
166 build_LR(Vin, Vout);
167
168 // Make environment for each site of the unit cell
169 for (size_t l=1; l<N_sites; ++l)
170 {
171 contract_L(Env[l-1].L,
172 Vout.A[GAUGE::L][l-1], Vin.A[GAUGE::L][l-1],
173 Env[l-1].qloc, Env[l].L);
174 }
175
176 for (int l=N_sites-2; l>=0; --l)
177 {
178 contract_R(Env[l+1].R,
179 Vout.A[GAUGE::R][l+1], Vin.A[GAUGE::R][l+1],
180 Env[l+1].qloc, Env[l].R);
181 }
182
183}
184
185template<typename Symmetry, typename Scalar, typename MpoScalar>
188{
189 vector<vector<qarray<Symmetry::Nq> > > qloc_complete(N_sites);
190 for (size_t loc=0; loc<N_sites; loc++) { qloc_complete[loc] = Env[loc].qloc; }
191 TransferMatrix<Symmetry,Scalar> TL(GAUGE::R, Vout.A[GAUGE::L], Vin.A[GAUGE::L], qloc_complete);
192 TransferMatrix<Symmetry,Scalar> TR(GAUGE::L, Vout.A[GAUGE::R], Vin.A[GAUGE::R], qloc_complete);
193 Scalar eigval_dump=0.;
194 Scalar eigval_used=0.;
196 TransferVector<Symmetry,Scalar> xR(Env[N_sites-1].R);
197 Stopwatch<> FixedR;
198 ArnoldiSolver<TransferMatrix<Symmetry,Scalar>,TransferVector<Symmetry,Scalar> > John(TR,xR,eigval_dump);
199 t_fixedR = FixedR.time();
200 Stopwatch<> FixedL;
201 ArnoldiSolver<TransferMatrix<Symmetry,Scalar>,TransferVector<Symmetry,Scalar> > Lucy(TL,xL,eigval_used);
202 t_fixedL = FixedL.time();
203 if (CHOSEN_VERBOSITY >= DMRG::VERBOSITY::HALFSWEEPWISE)
204 {
205 lout << "right fixed point (t=" << round(t_fixedR,2) << "): " << John.info() << endl;
206 lout << "left fixed point (t=" << round(t_fixedL,2) << "): " << Lucy.info() << endl;
207 }
208 // cout << "Fixed points:" << endl;
209 // cout << xL.data[0].print(false) << endl;
210 // cout << xR.data[0].print(false) << endl << endl;
211
212 Env[0].L = xL.data[0];
213 Env[N_sites-1].R = xR.data[0];
214 lambdaL = eigval_used;
215}
216
217template<typename Symmetry, typename Scalar, typename MpoScalar>
220{
221 Stopwatch<> IterationTimer;
222 //calculate fixed points L and R of the mixed transfer matrix (Vin.A-Vout.A)
223 build_cellEnv(Vin, Vout);
224 for (size_t loc=0; loc<N_sites; ++loc)
225 {
226 //update AC
227 for(size_t s=0; s<Env[loc].qloc.size(); s++)
228 {
229 Vout.A[GAUGE::C][loc][s] = Env[loc].L * Vin.A[GAUGE::C][loc][s] * Env[loc].R;
230 }
231 //update C
232 Vout.C[loc] = Env[loc].L * Vin.C[loc] * Env[loc].R;
233 }
234 Vout.normalize_C();
235 //calc new AL and AR from AC and C
236 for (size_t loc=0; loc<N_sites; ++loc)
237 {
238 (err_var>0.01)? Vout.svdDecompose(loc) : Vout.polarDecompose(loc);
239 }
240 // cout << Vout.test_ortho() << endl;
241
242 calc_error(Vout);
243
244 ++N_iterations;
245
246 // print stuff
247 if (CHOSEN_VERBOSITY >= DMRG::VERBOSITY::HALFSWEEPWISE)
248 {
249 size_t standard_precision = cout.precision();
250 Vout.calc_entropy();
251 lout << "S=" << Vout.entropy().transpose() << endl;
252 lout << info() << endl;
253 lout << Vout.info() << endl;
254 lout << IterationTimer.info("full parallel iteration") << endl;
255 lout << endl;
256 }
257}
258
259template<typename Symmetry, typename Scalar, typename MpoScalar>
262{
263 err_var=0.;
264 for (size_t loc=0; loc<N_sites; loc++)
265 for (size_t s=0; s<Env[loc].qloc.size(); s++)
266 {
267 err_var += (Vout.A[GAUGE::L][loc][s] * Vout.C[loc] - ( (1./lambdaL) * Vout.A[GAUGE::C][loc][s] )).norm().sum();
268 // err_var += (Vout.A[GAUGE::L][loc][s] * Vout.C[loc] - Vout.A[GAUGE::C][loc][s]).norm().sum();
269 }
270}
271#endif //UMPS_COMPRESSOR_H_
double norm(const PivotMatrix0< Symmetry, Scalar, MpoScalar > &H)
void build_LR(const Umps< Symmetry, Scalar > &Vbra, const Umps< Symmetry, Scalar > &Vket)
UmpsCompressor(DMRG::VERBOSITY::OPTION VERBOSITY=DMRG::VERBOSITY::SILENT)
DMRG::VERBOSITY::OPTION CHOSEN_VERBOSITY
void stateCompress(const Umps< Symmetry, Scalar > &Vin, Umps< Symmetry, Scalar > &Vout, size_t Dinit_input, size_t Qinit_input, double tol_input, size_t max_iterations=100ul, size_t min_iterations=10ul)
void build_cellEnv(const Umps< Symmetry, Scalar > &Vbra, const Umps< Symmetry, Scalar > &Vket)
double memory(MEMUNIT memunit=GB) const
complex< double > lambdaL
string info() const
vector< PivotOverlap1< Symmetry, Scalar > > Env
void optimize_parallel(const Umps< Symmetry, Scalar > &Vin, Umps< Symmetry, Scalar > &Vout)
void calc_error(const Umps< Symmetry, Scalar > &Vout)
size_t minus1modL(size_t l) const
Definition: Umps.h:42
vector< qarray< Symmetry::Nq > > locBasis(size_t loc) const
Definition: Umps.h:129
size_t length() const
Definition: Umps.h:147
Qbasis< Symmetry > inBasis(size_t loc) const
Definition: Umps.h:133
void svdDecompose(size_t loc, GAUGE::OPTION gauge=GAUGE::C)
Definition: Umps.h:1513
void polarDecompose(size_t loc, GAUGE::OPTION gauge=GAUGE::C)
Definition: Umps.h:1358
void calc_entropy(size_t loc, bool PRINT=false)
Definition: Umps.h:1157
qarray< Nq > Qtarget() const
Definition: Umps.h:211
Qbasis< Symmetry > outBasis(size_t loc) const
Definition: Umps.h:137
string test_ortho(double tol=1e-6) const
Definition: Umps.h:972
void setRandom()
Definition: Umps.h:863
std::array< vector< vector< Biped< Symmetry, MatrixType > > >, 3 > A
Definition: Umps.h:368
void normalize_C()
Definition: Umps.h:852
VectorXd entropy() const
Definition: Umps.h:109
vector< Biped< Symmetry, MatrixType > > C
Definition: Umps.h:371
string info() const
Definition: Umps.h:393
void contract_R(const Tripod< Symmetry, MatrixType2 > &Rold, const vector< Biped< Symmetry, MatrixType > > &Abra, const vector< vector< vector< Biped< Symmetry, MpoMatrixType > > > > &W, const vector< Biped< Symmetry, MatrixType > > &Aket, const vector< qarray< Symmetry::Nq > > &qloc, const vector< qarray< Symmetry::Nq > > &qOp, Tripod< Symmetry, MatrixType2 > &Rnew, bool RANDOMIZE=false, tuple< CONTRACT_LR_MODE, size_t > MODE_input=make_pair(FULL, 0), const std::unordered_map< pair< qarray< Symmetry::Nq >, size_t >, size_t > &basis_order_map={})
void contract_L(const Tripod< Symmetry, MatrixType2 > &Lold, const vector< Biped< Symmetry, MatrixType > > &Abra, const vector< vector< vector< Biped< Symmetry, MpoMatrixType > > > > &W, const vector< Biped< Symmetry, MatrixType > > &Aket, const vector< qarray< Symmetry::Nq > > &qloc, const vector< qarray< Symmetry::Nq > > &qOp, Tripod< Symmetry, MatrixType2 > &Lnew, bool RANDOMIZE=false, tuple< CONTRACT_LR_MODE, size_t > MODE_input=make_pair(FULL, 0), const std::unordered_map< pair< qarray< Symmetry::Nq >, size_t >, size_t > &basis_order_map={})
Biped< Symmetry, Matrix< Scalar_, Dynamic, Dynamic > > data