ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
correlations.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  * 2011-2013 Michele Dolfi <dolfim@phys.ethz.ch>
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 MEASUREMENTS_CORRELATIONS_H
29 #define MEASUREMENTS_CORRELATIONS_H
30 
34 
35 #include <algorithm>
36 #include <boost/iterator/counting_iterator.hpp>
37 
38 namespace measurements {
39 
40  namespace detail {
41  inline std::vector<std::vector<std::size_t> >
42  resort_labels (const std::vector<std::vector<std::size_t> >& labels,
43  std::vector<size_t> const & order,
44  bool is_nn=false)
45  {
46  std::vector<std::vector<std::size_t> > ret(labels.size());
47  for (int i=0; i<labels.size(); ++i) {
48  #ifndef NDEBUG
49  if (is_nn) assert(2*order.size() == labels[i].size());
50  else assert(order.size() == labels[i].size());
51  #endif
52  ret[i].resize(labels[i].size());
53 
54  for (int j=0; j<order.size(); ++j) {
55  if (is_nn) {
56  ret[i][order[j]] = labels[i][2*j];
57  ret[i][order[j]+1] = labels[i][2*j+1];
58  } else {
59  ret[i][order[j]] = labels[i][j];
60  }
61  }
62  }
63  return ret;
64  }
65 
66 
67  template <class Matrix, class SymmGroup>
69  {
70  public:
71  typedef std::size_t size_t;
72  typedef std::pair<std::vector<block_matrix<Matrix, SymmGroup> >, bool> inner_t;
73  typedef std::vector<inner_t> value_t;
74  CorrPermutator (value_t const & ops, bool is_nn)
75  {
76  std::vector<size_t> ord;
77  if (is_nn)
78  for (size_t i=0; i<ops.size(); i+=2) ord.push_back(i);
79  else
80  for (size_t i=0; i<ops.size(); ++i) ord.push_back(i);
81 
82  do {
83  std::vector<size_t> check_pre;
84  for (size_t i=0; i<ops.size(); ++i) {
85  if (ops[i].second) {
86  if (!is_nn)
87  check_pre.push_back(ord[i]);
88  else
89  if (i % 2 == 0)
90  check_pre.push_back(ord[i]);
91  else
92  check_pre.push_back(ord[i]+1);
93  }
94  }
95 
97  cmp.prefactor = 1.;
98  std::sort(check_pre.begin(), check_pre.end(), cmp);
99 
100  value_t tmp;
101  for (size_t i=0; i<ord.size(); ++i) {
102  tmp.push_back( ops[ord[i]] );
103  if (is_nn) {
104  tmp.push_back( ops[ord[i]+1] );
105  }
106  }
107 
108  for (int type=0; type<tmp[0].first.size(); ++type)
109  tmp[0].first[type] *= cmp.prefactor;
110 
111  perm.push_back(tmp);
112  orders.push_back(ord);
113  } while (std::next_permutation(ord.begin(), ord.end()));
114  }
115 
116  size_t size() const {return perm.size();}
117  value_t operator[](size_t i) const {return perm[i];}
118  std::vector<size_t> order(size_t i) const {return orders[i];}
119 
120  private:
121  std::vector<value_t> perm;
122  std::vector<std::vector<size_t> > orders;
123  };
124  } /// namespace detail
125 
126 
127 
128  template <class Matrix, class SymmGroup>
129  class correlations : public measurement<Matrix, SymmGroup> {
131  typedef std::size_t size_type;
132  typedef std::vector<block_matrix<Matrix, SymmGroup> > op_vec;
133  typedef std::vector<size_type> positions_type;
134 
135  public:
136  correlations(std::string const& name_, const Lattice & lat,
137  op_vec const & identities_, op_vec const & fillings_,
138  std::vector<std::pair<op_vec, bool> > const& ops_,
139  bool half_only_, bool nearest_neighbors_only,
140  positions_type const& positions_ = positions_type())
141  : base(name_)
142  , lattice(lat)
143  , positions_first(positions_)
144  , identities(identities_)
145  , fillings(fillings_)
146  , ops(ops_)
147  , half_only(half_only_)
148  , is_nn(nearest_neighbors_only)
149  {
150  if (positions_first.size() == 0)
151  std::copy(boost::counting_iterator<int>(0), boost::counting_iterator<int>(lattice.size()-(ops.size()-1)),
152  back_inserter(positions_first));
153 
154  this->cast_to_real = is_hermitian_meas(ops);
155  }
156 
157  void evaluate(MPS<Matrix, SymmGroup> const& mps, boost::optional<reduced_mps<Matrix, SymmGroup> const&> rmps = boost::none)
158  {
159  this->vector_results.clear();
160  this->labels.clear();
161 
162  if (half_only) {
163  measure_correlation(mps, ops);
164  } else {
166  for (int i=0; i<perm.size(); ++i) {
167  measure_correlation(mps, perm[i], perm.order(i));
168  }
169  }
170  }
171 
172  protected:
174  {
175  return new correlations(*this);
176  }
177 
179  std::vector<std::pair<op_vec, bool> > const & ops,
180  std::vector<size_type> const & order = std::vector<size_type>())
181  {
182  typedef boost::shared_ptr<generate_mpo::CorrMakerBase<Matrix, SymmGroup> > maker_ptr;
183 
184  for (std::vector<std::size_t>::const_iterator it = positions_first.begin(); it != positions_first.end(); ++it) {
185  if (*it >= lattice.size()-(ops.size()-1))
186  throw std::runtime_error("cannot measure correlation with first operator at p="+boost::lexical_cast<std::string>(*it)+".");
187  #ifndef NDEBUG
188  maquis::cout << " site " << *it << std::endl;
189  #endif
190 
191  /// initialize correct maker
192  maker_ptr dcorr;
193  if (is_nn) dcorr.reset(new generate_mpo::CorrMakerNN<Matrix, SymmGroup>(lattice, identities, fillings, ops, *it) );
194  else dcorr.reset(new generate_mpo::CorrMaker<Matrix, SymmGroup>(lattice, identities, fillings, ops, *it) );
195 
196  /// measure
197  MPO<Matrix, SymmGroup> mpo = dcorr->create_mpo();
198  std::vector<typename MPS<Matrix, SymmGroup>::scalar_type> dct;
199  if (!this->is_super_meas)
200  dct = multi_expval(mps, mpo);
201  else {
202  typename MPS<Matrix, SymmGroup>::scalar_type nn = dm_trace(mps, this->phys_psi);
203  MPS<Matrix, SymmGroup> super_mpo = mpo_to_smps(mpo, this->phys_psi);
204  dct = multi_overlap(super_mpo, mps);
205  for (int i=0; i<dct.size(); ++i)
206  dct[i] /= nn;
207  }
208 
209  /// save results and labels
210  this->vector_results.reserve(this->vector_results.size() + dct.size());
211  this->labels.reserve(this->labels.size() + dct.size());
212 
213  std::copy(dct.begin(), dct.end(), std::back_inserter(this->vector_results));
214 
215  std::vector<std::vector<std::size_t> > num_labels = dcorr->numeric_labels();
216  std::vector<std::string> lbt = label_strings(lattice, (order.size() > 0) ? detail::resort_labels(num_labels, order, is_nn) : num_labels );
217  std::copy(lbt.begin(), lbt.end(), std::back_inserter(this->labels));
218  }
219  }
220 
221  private:
222  Lattice lattice;
223  positions_type positions_first;
224  op_vec identities, fillings;
225  std::vector<std::pair<op_vec, bool> > ops;
226  bool half_only, is_nn;
227  };
228 
229 }
230 
231 #endif
std::vector< std::string > labels
Definition: measurement.h:78
std::vector< size_t > order(size_t i) const
Definition: correlations.h:118
value_t operator[](size_t i) const
Definition: correlations.h:117
std::vector< typename MPS< Matrix, SymmGroup >::scalar_type > multi_overlap(MPS< Matrix, SymmGroup > const &mps1, MPS< Matrix, SymmGroup > const &mps2)
Definition: mps_mpo_ops.h:154
bool is_super_meas
Definition: measurement.h:77
CorrPermutator(value_t const &ops, bool is_nn)
Definition: correlations.h:74
correlations(std::string const &name_, const Lattice &lat, op_vec const &identities_, op_vec const &fillings_, std::vector< std::pair< op_vec, bool > > const &ops_, bool half_only_, bool nearest_neighbors_only, positions_type const &positions_=positions_type())
Definition: correlations.h:136
std::vector< typename MPS< Matrix, SymmGroup >::scalar_type > multi_expval(MPS< Matrix, SymmGroup > const &mps, MPO< Matrix, SymmGroup > const &mpo)
Definition: mps_mpo_ops.h:104
std::vector< std::string > label_strings(const Lattice &lat, const std::vector< std::vector< std::size_t > > &labels)
Definition: measurement.h:190
std::vector< typename MPS< Matrix, SymmGroup >::scalar_type > vector_results
Definition: measurement.h:80
Definition: mpo.h:36
std::vector< inner_t > value_t
Definition: correlations.h:73
void evaluate(MPS< Matrix, SymmGroup > const &mps, boost::optional< reduced_mps< Matrix, SymmGroup > const & > rmps=boost::none)
Definition: correlations.h:157
definition of Measurement base class
bool is_hermitian_meas(std::vector< block_matrix< Matrix, SymmGroup > > const &ops)
UTILITIES.
Definition: measurement.h:175
static double prefactor
Definition: utils.hpp:34
void measure_correlation(MPS< Matrix, SymmGroup > const &mps, std::vector< std::pair< op_vec, bool > > const &ops, std::vector< size_type > const &order=std::vector< size_type >())
Definition: correlations.h:178
Definition: mps.h:40
MPSTensor< Matrix, SymmGroup >::scalar_type scalar_type
Definition: mps.h:51
MPS< Matrix, SymmGroup > mpo_to_smps(MPO< Matrix, SymmGroup > const &mpo, Index< SymmGroup > const &phys_i)
Definition: super_mpo.h:60
functions to operate on MPS and MPO (compute expectation values, etc.)
std::pair< std::vector< block_matrix< Matrix, SymmGroup > >, bool > inner_t
Definition: correlations.h:72
std::vector< std::vector< std::size_t > > resort_labels(const std::vector< std::vector< std::size_t > > &labels, std::vector< size_t > const &order, bool is_nn=false)
Definition: correlations.h:42
measurement< Matrix, SymmGroup > * do_clone() const
Definition: correlations.h:173
Index< SymmGroup > phys_psi
Definition: measurement.h:82
bool cast_to_real
Definition: measurement.h:77
pimpl resolved Lattice
Definition: lattice.h:84
pos_t size() const
Definition: lattice.h:115
MPS< Matrix, SymmGroup >::scalar_type dm_trace(MPS< Matrix, SymmGroup > const &mps, Index< SymmGroup > const &phys_psi)
Definition: mps_mpo_ops.h:258