ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
optimize.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * ALPS MPS DMRG Project
4  *
5  * Copyright (C) 2013 Institute for Theoretical Physics, ETH Zurich
6  * 2011-2011 by Bela Bauer <bauerb@phys.ethz.ch>
7  *
8  *
9  * This software is part of the ALPS Applications, published under the ALPS
10  * Application License; you can use, redistribute it and/or modify it under
11  * the terms of the license, either version 1 or (at your option) any later
12  * version.
13  *
14  * You should have received a copy of the ALPS Application License along with
15  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
16  * available from http://alps.comp-phys.org/.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  *****************************************************************************/
27 
28 #ifndef OPTIMIZE_H
29 #define OPTIMIZE_H
30 
31 #include <boost/random.hpp>
32 #if not defined(WIN32) && not defined(WIN64)
33 #include <sys/time.h>
34 #define HAVE_GETTIMEOFDAY
35 #endif
36 
37 #include <boost/algorithm/string.hpp>
38 
39 #include "utils/sizeof.h"
40 
41 #include "ietl_lanczos_solver.h"
42 #include "ietl_jacobi_davidson.h"
43 
46 #include "dmrg/utils/storage.h"
48 #include "dmrg/utils/placement.h"
49 
50 template<class Matrix, class SymmGroup>
51 struct SiteProblem
52 {
53  SiteProblem(Boundary<typename storage::constrained<Matrix>::type, SymmGroup> const & left_,
54  Boundary<typename storage::constrained<Matrix>::type, SymmGroup> const & right_,
55  MPOTensor<Matrix, SymmGroup> const & mpo_)
56  : left(left_)
57  , right(right_)
58  , mpo(mpo_)
59  {
60  }
61 
65  double ortho_shift;
66 };
67 
68 #define BEGIN_TIMING(name) \
69 now = boost::chrono::high_resolution_clock::now();
70 #define END_TIMING(name) \
71 then = boost::chrono::high_resolution_clock::now(); \
72 maquis::cout << "Time elapsed in " << name << ": " << boost::chrono::duration<double>(then-now).count() << std::endl;
73 
74 inline double log_interpolate(double y0, double y1, int N, int i)
75 {
76  if (N < 2)
77  return y1;
78  if (y0 == 0)
79  return 0;
80  double x = log(y1/y0)/(N-1);
81  return y0*exp(x*i);
82 }
83 
85 
86 template<class Matrix, class SymmGroup, class Storage>
88 {
89 public:
91  MPO<Matrix, SymmGroup> const & mpo_,
92  BaseParameters & parms_,
93  boost::function<bool ()> stop_callback_,
94  int site=0)
95  : mps(mps_)
96  , mpo(mpo_)
97  , parms(parms_)
98  , stop_callback(stop_callback_)
99  {
100  std::size_t L = mps.length();
101 
102  mps.canonize(site);
103  for(int i = 0; i < mps.length(); ++i)
104  Storage::evict(mps[i]);
105 
106  northo = parms_["n_ortho_states"];
107  maquis::cout << "Expecting " << northo << " states to orthogonalize to." << std::endl;
108 
109  //if (northo > 0 && parms_["ortho_states"]=="")
110  if (northo > 0 && !parms_.is_set("ortho_states"))
111  throw std::runtime_error("Parameter \"ortho_states\" is not set\n");
112 
113  ortho_mps.resize(northo);
114  std::string files_ = parms_["ortho_states"].str();
115  std::vector<std::string> files;
116  boost::split(files, files_, boost::is_any_of(", "));
117  for (int n = 0; n < northo; ++n) {
118  maquis::cout << "Loading ortho state " << n << " from " << files[n] << std::endl;
119  load(files[n], ortho_mps[n]);
120  maquis::cout << "Right end: " << ortho_mps[n][mps.length()-1].col_dim() << std::endl;
121  }
122 
123  init_left_right(mpo, site);
124  maquis::cout << "Done init_left_right" << std::endl;
125  }
126 
127  virtual ~optimizer_base() {}
128 
129  virtual void sweep(int sweep, OptimizeDirection d = Both) = 0;
130 
132 
133 protected:
134 
135  inline void boundary_left_step(MPO<Matrix, SymmGroup> const & mpo, int site)
136  {
137  left_[site+1] = contraction::overlap_mpo_left_step(mps[site], mps[site], left_[site], mpo[site]);
138  Storage::pin(left_[site+1]);
139 
140  for (int n = 0; n < northo; ++n)
141  ortho_left_[n][site+1] = contraction::overlap_left_step(mps[site], ortho_mps[n][site], ortho_left_[n][site]);
142  }
143 
144  inline void boundary_right_step(MPO<Matrix, SymmGroup> const & mpo, int site)
145  {
146  right_[site] = contraction::overlap_mpo_right_step(mps[site], mps[site], right_[site+1], mpo[site]);
147  Storage::pin(right_[site]);
148 
149  for (int n = 0; n < northo; ++n)
150  ortho_right_[n][site] = contraction::overlap_right_step(mps[site], ortho_mps[n][site], ortho_right_[n][site+1]);
151  }
152 
154  {
156  std::size_t L = mps.length();
157 
158  left_.resize(mpo.length()+1);
159  right_.resize(mpo.length()+1);
160 
161  ortho_left_.resize(northo);
162  ortho_right_.resize(northo);
163  for (int n = 0; n < northo; ++n) {
164  ortho_left_[n].resize(L+1);
165  ortho_right_[n].resize(L+1);
166 
167  ortho_left_[n][0] = mps.left_boundary()[0];
168  ortho_right_[n][L] = mps.right_boundary()[0];
169  }
170 
171  //Timer tlb("Init left boundaries"); tlb.begin();
172  Storage::drop(left_[0]);
173  left_[0] = mps.left_boundary();
174  Storage::pin(left_[0]);
175 
176  for (int i = 0; i < site; ++i) {
177  Storage::drop(left_[i+1]);
178  boundary_left_step(mpo, i);
179  Storage::evict(left_[i]);
180  }
181  Storage::evict(left_[site]);
182  //tlb.end();
183 
184  maquis::cout << "Boundaries are partially initialized...\n";
185 
186  //Timer trb("Init right boundaries"); trb.begin();
187  Storage::drop(right_[L]);
188  right_[L] = mps.right_boundary();
189  Storage::pin(right_[L]);
190 
191  for (int i = L-1; i >= site; --i) {
192  Storage::drop(right_[i]);
193  boundary_right_step(mpo, i);
194  Storage::evict(right_[i+1]);
195  }
196  Storage::evict(right_[site]);
197  //trb.end();
198 
199  maquis::cout << "Boundaries are fully initialized...\n";
200  }
201 
202  double get_cutoff(int sweep) const
203  {
204  double cutoff;
205  if (sweep >= parms.template get<int>("ngrowsweeps"))
206  cutoff = parms.template get<double>("truncation_final");
207  else
208  cutoff = log_interpolate(parms.template get<double>("truncation_initial"), parms.template get<double>("truncation_final"), parms.template get<int>("ngrowsweeps"), sweep);
209  return cutoff;
210  }
211 
212  std::size_t get_Mmax(int sweep) const
213  {
214  std::size_t Mmax;
215  if (parms.is_set("sweep_bond_dimensions")) {
216  std::vector<std::size_t> ssizes = parms.template get<std::vector<std::size_t> >("sweep_bond_dimensions");
217  if (sweep >= ssizes.size())
218  Mmax = *ssizes.rbegin();
219  else
220  Mmax = ssizes[sweep];
221  } else
222  Mmax = parms.template get<std::size_t>("max_bond_dimension");
223  return Mmax;
224  }
225 
226 
228 
231 
233  boost::function<bool ()> stop_callback;
234 
235  std::vector<Boundary<typename storage::constrained<Matrix>::type, SymmGroup> > left_, right_;
236 
237  /* This is used for multi-state targeting */
238  unsigned int northo;
239  std::vector< std::vector<block_matrix<typename storage::constrained<Matrix>::type, SymmGroup> > > ortho_left_, ortho_right_;
240  std::vector<MPS<Matrix, SymmGroup> > ortho_mps;
241 };
242 
243 #include "ss_optimize.hpp"
244 #include "ts_optimize.hpp"
245 
246 #endif
std::size_t get_Mmax(int sweep) const
Definition: optimize.h:212
optimizer_base(MPS< Matrix, SymmGroup > &mps_, MPO< Matrix, SymmGroup > const &mpo_, BaseParameters &parms_, boost::function< bool()> stop_callback_, int site=0)
Definition: optimize.h:90
void load(alps::hdf5::archive &ar, std::string const &path, TrivialGroup::charge &value, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t > offset=std::vector< std::size_t >())
Definition: none.h:81
MPOTensor< Matrix, SymmGroup > const & mpo
BaseParameters & parms
Definition: optimize.h:232
std::vector< std::vector< int > > construct_placements(const MPO< Matrix, SymmGroup > &mpo)
Definition: placement.h:101
SiteProblem(Boundary< typename storage::constrained< Matrix >::type, SymmGroup > const &left_, Boundary< typename storage::constrained< Matrix >::type, SymmGroup > const &right_, MPOTensor< Matrix, SymmGroup > const &mpo_)
Definition: optimize.h:53
call IETL Lanczos solver
bool is_set(std::string const &key) const
std::vector< Boundary< typename storage::constrained< Matrix >::type, SymmGroup > > left_
Definition: optimize.h:235
double ortho_shift
Definition: optimize.h:65
static block_matrix< OtherMatrix, SymmGroup > overlap_right_step(MPSTensor< Matrix, SymmGroup > const &bra_tensor, MPSTensor< Matrix, SymmGroup > const &ket_tensor, block_matrix< OtherMatrix, SymmGroup > const &right, block_matrix< OtherMatrix, SymmGroup > *localop=NULL)
Definition: contractions.h:71
optimization class for single-site algorithm
call IETL Jacobi-Davidson solver
Definition: optimize.h:84
std::vector< std::vector< block_matrix< typename storage::constrained< Matrix >::type, SymmGroup > > > ortho_left_
Definition: optimize.h:239
void init_left_right(MPO< Matrix, SymmGroup > const &mpo, int site)
Definition: optimize.h:153
static Boundary< OtherMatrix, SymmGroup > overlap_mpo_right_step(MPSTensor< Matrix, SymmGroup > const &bra_tensor, MPSTensor< Matrix, SymmGroup > const &ket_tensor, Boundary< OtherMatrix, SymmGroup > const &right, MPOTensor< Matrix, SymmGroup > const &mpo)
Definition: contractions.h:386
Boundary< typename storage::constrained< Matrix >::type, SymmGroup > const & left
Definition: mpo.h:36
virtual void sweep(int sweep, OptimizeDirection d=Both)=0
results_collector iteration_results_
Definition: optimize.h:227
static block_matrix< OtherMatrix, SymmGroup > overlap_left_step(MPSTensor< Matrix, SymmGroup > const &bra_tensor, MPSTensor< Matrix, SymmGroup > const &ket_tensor, block_matrix< OtherMatrix, SymmGroup > const &left, block_matrix< OtherMatrix, SymmGroup > *localop=NULL)
Definition: contractions.h:41
std::vector< std::vector< block_matrix< typename storage::constrained< Matrix >::type, SymmGroup > > > ortho_right_
Definition: optimize.h:239
Definition: mps.h:40
OptimizeDirection
Definition: optimize.h:84
boost::function< bool()> stop_callback
Definition: optimize.h:233
MPO< Matrix, SymmGroup > const & mpo
Definition: optimize.h:230
MPS< Matrix, SymmGroup > & mps
Definition: optimize.h:229
double get_cutoff(int sweep) const
Definition: optimize.h:202
static Boundary< OtherMatrix, SymmGroup > overlap_mpo_left_step(MPSTensor< Matrix, SymmGroup > const &bra_tensor, MPSTensor< Matrix, SymmGroup > const &ket_tensor, Boundary< OtherMatrix, SymmGroup > const &left, MPOTensor< Matrix, SymmGroup > const &mpo)
Definition: contractions.h:340
Logger< storage::archive > log
Definition: utils.cpp:40
unsigned int northo
Definition: optimize.h:238
Boundary< typename storage::constrained< Matrix >::type, SymmGroup > const & right
results_collector const & iteration_results() const
Definition: optimize.h:131
std::vector< Boundary< typename storage::constrained< Matrix >::type, SymmGroup > > right_
Definition: optimize.h:235
optimization class for two-site algorithm
void boundary_right_step(MPO< Matrix, SymmGroup > const &mpo, int site)
Definition: optimize.h:144
std::vector< MPS< Matrix, SymmGroup > > ortho_mps
Definition: optimize.h:240
double log_interpolate(double y0, double y1, int N, int i)
Definition: optimize.h:74
void boundary_left_step(MPO< Matrix, SymmGroup > const &mpo, int site)
Definition: optimize.h:135
virtual ~optimizer_base()
Definition: optimize.h:127