ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils.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_UTILS_H
28 #define GENERATE_MPO_UTILS_H
29 
33 
34 #include "dmrg/models/op_handler.h"
35 #include "dmrg/models/lattice.h"
36 
37 #include <string>
38 #include <sstream>
39 
40 #include <boost/bind.hpp>
41 
42 namespace generate_mpo
43 {
44  template<class Matrix, class SymmGroup>
46  {
48  typedef typename Lattice::pos_t pos_t;
49  typedef std::pair<pos_t, tag_type> op_pair_t;
50 
51  std::vector<op_pair_t> operators;
53  typename Matrix::value_type scale;
54  bool with_sign;
55 
56  Operator_Tag_Term() : scale(1.), with_sign(false) {}
57 
58  void canonical_order() // TODO: check and fix for fermions
59  {
60  std::sort(operators.begin(), operators.end(),
61  boost::bind(&op_pair_t::first, _1) <
62  boost::bind(&op_pair_t::first, _2));
63  }
64 
65  bool operator< (Operator_Tag_Term const & rhs) const
66  {
67  if (operators[0].first == rhs.operators[0].first)
68  return operators.size() >= rhs.operators.size();
69  return operators[0].first < rhs.operators[0].first;
70  }
71 
72  bool site_match (Operator_Tag_Term const & rhs) const
73  {
74  if (operators.size() == rhs.operators.size())
75  {
76  bool ret = true;
77  for (std::size_t p=0; p<operators.size() && ret; ++p)
78  ret = (operators[p].first == rhs.operators[p].first);
79  return ret;
80  } else if (operators.size() == 2 && rhs.operators.size() == 1)
81  return (operators[0].first == rhs.operators[0].first || operators[1].first == rhs.operators[0].first);
82  else if (operators.size() == 1 && rhs.operators.size() == 2)
83  return (operators[0].first == rhs.operators[0].first || operators[0].first == rhs.operators[1].first);
84  else
85  {
86  throw std::runtime_error("site_match not implemented for this type of operator." );
87  return false;
88  }
89 
90  }
91 
92  bool overlap (Operator_Tag_Term const & rhs) const
93  {
94  return !( (operators.rbegin()->first < rhs.operators.begin()->first) || (rhs.operators.rbegin()->first < operators.begin()->first) );
95  }
96  };
97 
98  template<class Matrix, class SymmGroup>
99  std::ostream & operator<< (std::ostream & os, Operator_Tag_Term<Matrix, SymmGroup> const& op)
100  {
101  os << "fill: " << op.fill_operator << std::endl;
102  os << "sign: " << op.with_sign << std::endl;
103  os << "scale: " << op.scale << std::endl;
104  os << "operators:";
105  for (int i=0; i<op.operators.size(); ++i)
106  os << " {" << op.operators[i].first << "," << op.operators[i].second << "}";
107  os << std::endl;
108  return os;
109  }
110 
111  template<class Matrix, class SymmGroup>
113  {
115  typedef typename Lattice::pos_t pos_t;
116  typedef std::pair<pos_t, op_t> op_pair_t;
117 
118  std::vector<op_pair_t> operators;
120  bool with_sign;
121 
122  Operator_Term() : with_sign(false) {}
123 
124  void canonical_order() // TODO: check and fix for fermions
125  {
126  std::sort(operators.begin(), operators.end(),
127  boost::bind(&op_pair_t::first, _1) <
128  boost::bind(&op_pair_t::first, _2));
129  }
130 
131  bool operator< (Operator_Term const & rhs) const
132  {
133  if (operators[0].first == rhs.operators[0].first)
134  return operators.size() >= rhs.operators.size();
135  return operators[0].first < rhs.operators[0].first;
136  }
137 
138  bool site_match (Operator_Term const & rhs) const
139  {
140  if (operators.size() == rhs.operators.size())
141  {
142  bool ret = true;
143  for (std::size_t p=0; p<operators.size() && ret; ++p)
144  ret = (operators[p].first == rhs.operators[p].first);
145  return ret;
146  } else if (operators.size() == 2 && rhs.operators.size() == 1)
147  return (operators[0].first == rhs.operators[0].first || operators[1].first == rhs.operators[0].first);
148  else if (operators.size() == 1 && rhs.operators.size() == 2)
149  return (operators[0].first == rhs.operators[0].first || operators[0].first == rhs.operators[1].first);
150  else
151  {
152  throw std::runtime_error("site_match not implemented for this type of operator." );
153  return false;
154  }
155 
156  }
157 
158  bool overlap (Operator_Term const & rhs) const
159  {
160  return !( (operators.rbegin()->first < rhs.operators.begin()->first) || (rhs.operators.rbegin()->first < operators.begin()->first) );
161  }
162 
163  };
164 
165  using namespace std;
166  using namespace boost::tuples;
167 
168  inline size_t next_free(vector<size_t> const & out_taken,
169  vector<size_t> const & in_taken)
170  {
171  for (size_t k = 0; true; ++k)
172  {
173  if (count(out_taken.begin(), out_taken.end(), k) == 0 &&
174  count(in_taken.begin(), in_taken.end(), k) == 0)
175  return k;
176  }
177  }
178 
179  inline size_t next_free(set<size_t> const & s)
180  {
181  for (size_t k = 2; true; ++k)
182  if (s.count(k) == 0)
183  return k;
184  }
185 
186  template<class Vector>
187  void compress_on_bond(Vector & pm1, Vector & pm2)
188  {
189  std::set<size_t> bond_used_dims;
190  for (typename Vector::iterator it = pm1.begin(); it != pm1.end(); ++it)
191  if (get<1>(*it) > 1)
192  bond_used_dims.insert(get<1>(*it));
193  for (typename Vector::iterator it = pm2.begin(); it != pm2.end(); ++it)
194  if (get<0>(*it) > 1)
195  bond_used_dims.insert(get<0>(*it));
196 
197  std::map<size_t, size_t> compression_map;
198  size_t c = 2;
199  for (set<size_t>::iterator it = bond_used_dims.begin();
200  it != bond_used_dims.end(); ++it)
201  compression_map[*it] = c++;
202 
203  for (typename Vector::iterator it = pm1.begin(); it != pm1.end(); ++it)
204  if (compression_map.count(get<1>(*it)) > 0)
205  get<1>(*it) = compression_map[get<1>(*it)];
206  for (typename Vector::iterator it = pm2.begin(); it != pm2.end(); ++it)
207  if (compression_map.count(get<0>(*it)) > 0)
208  get<0>(*it) = compression_map[get<0>(*it)];
209  }
210 
211  template<class Vector>
212  std::pair<size_t, size_t> rcdim(Vector const & pm)
213  {
214  std::list<size_t> l, r;
215  for (typename Vector::const_iterator it = pm.begin(); it != pm.end(); ++it) {
216  l.push_back( get<0>(*it) );
217  r.push_back( get<1>(*it) );
218  }
219 
220  size_t ldim=0, rdim=0;
221  if (l.size() > 0) ldim = *max_element(l.begin(), l.end())+1;
222  if (r.size() > 0) rdim = *max_element(r.begin(), r.end())+1;
223  return make_pair(ldim, rdim);
224  }
225 
226  template<class Pair>
227  bool compare(Pair const & p1, Pair const & p2)
228  {
229  return p1.first < p2.first;
230  }
231 
232  struct pos_tag_lt {
233  typedef boost::tuple<int, unsigned int> value_type;
234  inline bool operator() (value_type const& lhs, value_type const& rhs)
235  {
236  return (boost::get<0>(lhs) < boost::get<0>(rhs));
237  }
238  };
239 }
240 
241 #endif
std::pair< pos_t, tag_type > op_pair_t
Definition: utils.hpp:49
bool compare(Pair const &p1, Pair const &p2)
Definition: utils.hpp:227
definition of Lattice base class
bool operator<(Operator_Tag_Term const &rhs) const
Definition: utils.hpp:65
boost::tuple< int, unsigned int > value_type
Definition: utils.hpp:233
include all symmetry definitions
block_matrix< Matrix, SymmGroup > op_t
Definition: utils.hpp:114
OPTable< Matrix, SymmGroup >::tag_type tag_type
Definition: utils.hpp:47
tag_detail::tag_type tag_type
Definition: op_handler.h:46
Matrix::value_type scale
Definition: utils.hpp:53
declaration of block_matrix class
bool overlap(Operator_Tag_Term const &rhs) const
Definition: utils.hpp:92
std::pair< pos_t, op_t > op_pair_t
Definition: utils.hpp:116
bool site_match(Operator_Term const &rhs) const
Definition: utils.hpp:138
std::vector< op_pair_t > operators
Definition: utils.hpp:118
impl_type::pos_t pos_t
Definition: lattice.h:88
Lattice::pos_t pos_t
Definition: utils.hpp:115
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
algorithms for block_matrix (gemm, svd, etc.)
size_t next_free(vector< size_t > const &out_taken, vector< size_t > const &in_taken)
Definition: utils.hpp:168
bool site_match(Operator_Tag_Term const &rhs) const
Definition: utils.hpp:72
declaration of OPTable, TagHandler and KronHandler
std::vector< op_pair_t > operators
Definition: utils.hpp:51
bool overlap(Operator_Term const &rhs) const
Definition: utils.hpp:158
bool operator<(Operator_Term const &rhs) const
Definition: utils.hpp:131