ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mpo_maker.hpp
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  * This software is part of the ALPS Applications, published under the ALPS
9  * Application License; you can use, redistribute it and/or modify it under
10  * the terms of the license, either version 1 or (at your option) any later
11  * version.
12  *
13  * You should have received a copy of the ALPS Application License along with
14  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
15  * available from http://alps.comp-phys.org/.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  *****************************************************************************/
26 
27 #ifndef GENERATE_MPO_MPO_MAKER_H
28 #define GENERATE_MPO_MPO_MAKER_H
29 
31 
35 
36 #include "dmrg/mp_tensors/mpo.h"
37 
38 #include "dmrg/models/lattice.h"
39 
40 #include <string>
41 #include <sstream>
42 
43 namespace generate_mpo
44 {
45  template<class Matrix, class SymmGroup>
46  class MPOMaker
47  {
49  typedef boost::tuple<size_t, size_t, op_t> block;
50  typedef vector<
51  pair<
54  >
55  > op_pairs;
56 
57  public:
58  MPOMaker(Lattice const& lat_,
59  const std::vector<block_matrix<Matrix, SymmGroup> > & ident_,
60  const std::vector<block_matrix<Matrix, SymmGroup> > & fill_)
61  : lat(lat_)
62  , length(lat.size())
63  , identities(ident_)
64  , fillings(fill_)
65  , used_dims(length)
66  , prempo(length)
67  , maximum(2)
68  , leftmost_right(length)
69  , finalized(false)
70  {
71  for (size_t p = 0; p < length; ++p)
72  {
73  if (p+1 < length)
74  prempo[p].push_back(boost::make_tuple(std::size_t(0), std::size_t(0), identities[lat.get_prop<int>("type",p)]));
75  }
76  }
77 
79  {
80  // TODO: removed const&, because of sorting (non-const operation)
81  std::vector<std::pair<typename Lattice::pos_t, op_t> > ops = term.operators;
82 
83  std::sort(ops.begin(), ops.end(), compare<std::pair<typename Lattice::pos_t, op_t> >);
84 
85  vector<size_t> positions;
86  for (typename vector<pair<typename Lattice::pos_t, op_t> >::const_iterator
87  it = ops.begin();
88  it != ops.end(); ++it)
89  positions.push_back( it->first );
90  size_t minp = *min_element(positions.begin(), positions.end());
91  size_t maxp = *max_element(positions.begin(), positions.end());
92 
93  size_t use_b = maximum++;
94 
95  vector<bool> done(length, false);
96  for (typename vector<pair<typename Lattice::pos_t, op_t> >::const_iterator it = ops.begin();
97  it != ops.end(); ++it)
98  {
99  size_t first_use_b = (it->first == minp ? 0 : use_b);
100  size_t second_use_b = (it->first == maxp ? 1 : use_b);
101  assert( it->first < prempo.size() );
102  if (minp != maxp) { // bond term
103  prempo[it->first].push_back(boost::make_tuple(first_use_b, second_use_b, it->second));
104  used_dims[it->first].insert(use_b);
105  } else // site term
106  site_terms[it->first] += it->second;
107  done[it->first] = true;
108  }
109 
110  for (size_t p = minp; p <= maxp; ++p)
111  if (!done[p]) {
112  op_t const& current_filling = (term.with_sign) ? fillings[lat.get_prop<int>("type",p)] : identities[lat.get_prop<int>("type",p)];
113  prempo[p].push_back( boost::make_tuple(use_b, use_b, current_filling));
114  used_dims[p].insert(use_b);
115  done[p] = true;
116  }
117 
118  leftmost_right = std::min(leftmost_right, maxp);
119  }
120 
122  {
123  if (!finalized) finalize();
124  MPO<Matrix, SymmGroup> r(length);
125  for (size_t p = 1; p < length - 1; ++p)
126  r[p] = as_bulk(prempo[p]);
127  r[0] = as_left(prempo[0]);
128  r[length-1] = as_right(prempo[length-1]);
129 
130  return r;
131  }
132 
134  {
135  if (!finalized) finalize();
136  MPO<Matrix, SymmGroup> r(length);
137  for (size_t p = 1; p < length - 1; ++p)
138  r[p] = as_bulk(prempo[p]);
139  r[0] = as_left(prempo[0]);
140  r[length-1] = as_right(prempo[length-1]);
141 
142  charge_sort(get_prempo(), r);
143  MPO<Matrix, SymmGroup> mpo_sorted = create_mpo();
144 
146  MPO<Matrix, SymmGroup> mpo_out(length);
147  cpor.compress(mpo_sorted, mpo_out, cutoff);
148 
149  return mpo_out;
150  }
151 
152  std::vector<std::vector<block> > & get_prempo()
153  {
154  return prempo;
155  }
156 
157  private:
158  Lattice const& lat;
159  std::size_t length;
160  std::vector<block_matrix<Matrix, SymmGroup> > identities, fillings;
161  vector<set<size_t> > used_dims;
162  vector<vector<block> > prempo;
163  std::map<std::size_t, op_t> site_terms;
164  size_t maximum, leftmost_right;
165  bool finalized;
166 
167  void finalize()
168  {
169  for (typename std::map<std::size_t, op_t>::const_iterator it = site_terms.begin();
170  it != site_terms.end(); ++it)
171  prempo[it->first].push_back( boost::make_tuple(0, 1, it->second) );
172 
173  for (size_t p = leftmost_right + 1; p < length; ++p)
174  prempo[p].push_back( boost::make_tuple(1, 1, identities[lat.get_prop<int>("type",p)]) );
175 
176  for (typename vector<vector<block> >::iterator it = prempo.begin();
177  it + 1 != prempo.end();
178  ++it)
179  compress_on_bond(*it, *(it+1));
180 
181  finalized = true;
182  }
183 
184  MPOTensor<Matrix, SymmGroup> as_bulk(vector<block> const & ops)
185  {
186  pair<size_t, size_t> rcd = rcdim(ops);
187  MPOTensor<Matrix, SymmGroup> r(rcd.first, rcd.second);
188  for (typename vector<block>::const_iterator it = ops.begin();
189  it != ops.end(); ++it)
190  {
191  r.set(get<0>(*it), get<1>(*it), get<2>(*it));
192  }
193  return r;
194  }
195 
196  MPOTensor<Matrix, SymmGroup> as_left(vector<block> const & ops)
197  {
198  pair<size_t, size_t> rcd = rcdim(ops);
199  MPOTensor<Matrix, SymmGroup> r(1, rcd.second);
200  for (typename vector<block>::const_iterator it = ops.begin();
201  it != ops.end(); ++it)
202  {
203  r.set(0, get<1>(*it), get<2>(*it));
204  }
205  return r;
206  }
207 
208  MPOTensor<Matrix, SymmGroup> as_right(vector<block> const & ops)
209  {
210  pair<size_t, size_t> rcd = rcdim(ops);
211  MPOTensor<Matrix, SymmGroup> r(rcd.first, 1);
212  for (typename vector<block>::const_iterator it = ops.begin();
213  it != ops.end(); ++it)
214  {
215  r.set(get<0>(*it), 0, get<2>(*it));
216  }
217  return r;
218  }
219  };
220 
221 }
222 
223 #endif
definition of Lattice base class
include all symmetry definitions
MPO< Matrix, SymmGroup > create_compressed_mpo(Index< SymmGroup > const &phys, double cutoff)
Definition: mpo_maker.hpp:133
declaration of block_matrix class
Definition: mpo.h:36
definition of MPO class (vector of MPOTensor)
T get_prop(std::string property, pos_t site) const
Definition: lattice.h:103
std::vector< std::vector< block > > & get_prempo()
Definition: mpo_maker.hpp:152
std::vector< op_pair_t > operators
Definition: utils.hpp:118
std::pair< size_t, size_t > rcdim(Vector const &pm)
Definition: utils.hpp:212
void compress_on_bond(Vector &pm1, Vector &pm2)
Definition: utils.hpp:187
MPOMaker(Lattice const &lat_, const std::vector< block_matrix< Matrix, SymmGroup > > &ident_, const std::vector< block_matrix< Matrix, SymmGroup > > &fill_)
Definition: mpo_maker.hpp:58
algorithms for block_matrix (gemm, svd, etc.)
void add_term(Operator_Term< Matrix, SymmGroup > const &term)
Definition: mpo_maker.hpp:78
MPO< Matrix, SymmGroup > create_mpo()
Definition: mpo_maker.hpp:121
pimpl resolved Lattice
Definition: lattice.h:84