ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
local.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_LOCAL_H
29 #define MEASUREMENTS_LOCAL_H
30 
35 
36 namespace measurements {
37 
38  template <class Matrix, class SymmGroup>
39  class local : public measurement<Matrix, SymmGroup> {
42  typedef std::vector<block_matrix<Matrix, SymmGroup> > op_vec;
43  typedef std::vector<std::pair<op_vec, bool> > bond_element;
44  public:
45 
46  local(std::string const& name_, const Lattice & lat,
47  op_vec const & identities_, op_vec const & fillings_,
48  std::vector<bond_element> const& terms)
49  : base(name_)
50  , lattice(lat)
51  , identities(identities_)
52  , fillings(fillings_)
53  , is_bond(true)
54  , mpo_terms(terms)
55  {
56  this->cast_to_real = all_true(mpo_terms.begin(), mpo_terms.end(), static_cast<bool (*)(bond_element const&)>(&is_hermitian_meas));
57  }
58 
59  local(std::string const& name_, const Lattice & lat,
60  op_vec const & identities_, op_vec const & fillings_,
61  op_vec const& op)
62  : base(name_)
63  , lattice(lat)
64  , identities(identities_)
65  , fillings(fillings_)
66  , is_bond(false)
67  , site_term(op)
68  , mpo_terms(std::vector<bond_element>(1, bond_element(1, std::make_pair(op, false)) ))
69  {
70  this->cast_to_real = is_hermitian_meas(site_term);
71  }
72 
73  void evaluate(MPS<Matrix, SymmGroup> const& mps, boost::optional<reduced_mps<Matrix, SymmGroup> const&> rmps = boost::none)
74  {
75  this->vector_results.clear();
76  this->labels.clear();
77 
78  typedef typename SymmGroup::subcharge subcharge;
79  if (!rmps || this->is_super_meas || is_bond)
80  return evaluate_with_mpo(mps);
81 
82  int L = mps.size();
83  this->vector_results.reserve(this->vector_results.size() + L);
84  this->labels.reserve(this->labels.size() + L);
85 
86  for (typename Lattice::pos_t p = 0; p < L; ++p) {
87  subcharge type = lattice.get_prop<subcharge>("type", p);
88  typename Matrix::value_type res = 0.;
89  bool evaluated = false;
90  if (site_term[type].n_blocks() > 0) {
92  temp.set(0, 0, site_term[type]);
93 
94  MPSTensor<Matrix, SymmGroup> vec2 = contraction::site_hamil2(mps[p], rmps.get().left(p), rmps.get().right(p), temp);
95  res += mps[p].scalar_overlap(vec2);
96  evaluated = true;
97  }
98  if (evaluated) {
99  this->vector_results.push_back(res);
100  this->labels.push_back( lattice.get_prop<std::string>("label", p) );
101  }
102  }
103  }
104 
105  protected:
107  {
108  return new local(*this);
109  }
110 
112  {
113  typedef typename SymmGroup::subcharge subcharge;
114  typedef std::map<std::string, typename Matrix::value_type> result_type;
115  result_type res;
116 
118  if (this->is_super_meas)
119  nn = dm_trace(mps, this->phys_psi);
120 
121  /// collect results from all mpo terms
122  for (typename std::vector<bond_element>::const_iterator it = mpo_terms.begin(); it != mpo_terms.end(); ++it) {
123  typedef std::map<std::string, MPO<Matrix, SymmGroup> > mpo_map;
124  mpo_map mpos = meas_prepare::local(lattice, identities, fillings, *it);
125 
126  /// measure the value at each site / bond
127  for (typename mpo_map::const_iterator mit = mpos.begin(); mit != mpos.end(); ++mit) {
128  typename result_type::iterator match = res.find(mit->first);
129  if (match == res.end())
130  boost::tie(match, boost::tuples::ignore) = res.insert( std::make_pair(mit->first, 0.) );
131 
132  if (!this->is_super_meas) {
133  match->second += expval(mps, mit->second);
134  } else {
135  MPS<Matrix, SymmGroup> super_mpo = mpo_to_smps(mit->second, this->phys_psi);
136  // static_cast needed for icpc 12.x
137  typedef typename MPS<Matrix, SymmGroup>::scalar_type (*overlap_func)(MPS<Matrix, SymmGroup> const &, MPS<Matrix, SymmGroup> const &);
138  typename MPS<Matrix, SymmGroup>::scalar_type val = ::overlap(super_mpo, mps);
139  match->second += val/nn;
140  }
141  }
142  }
143 
144  /// copy results to base
145  this->vector_results.reserve(this->vector_results.size() + res.size());
146  this->labels.reserve(this->labels.size() + res.size());
147  for (typename result_type::const_iterator it = res.begin(); it != res.end(); ++it) {
148  this->labels.push_back(it->first);
149  this->vector_results.push_back(it->second);
150  }
151  }
152 
153  private:
154  Lattice lattice;
155  op_vec identities, fillings;
156  bool is_bond;
157  op_vec site_term;
158  std::vector<bond_element> mpo_terms;
159  };
160 
161 }
162 
163 #endif
void set(index_type li, index_type ri, op_t const &op, value_type scale_=1.0)
Definition: mpotensor.hpp:106
void evaluate_with_mpo(MPS< Matrix, SymmGroup > const &mps)
Definition: local.h:111
std::vector< std::string > labels
Definition: measurement.h:78
double expval(MPS< Matrix, SymmGroup > const &mps, MPO< Matrix, SymmGroup > const &mpo, int d)
Definition: mps_mpo_ops.h:72
bool is_super_meas
Definition: measurement.h:77
bool all_true(InputIterator first, InputIterator last, Predicate pred)
Definition: utils.hpp:53
static MPSTensor< Matrix, SymmGroup > site_hamil2(MPSTensor< Matrix, SymmGroup > ket_tensor, Boundary< OtherMatrix, SymmGroup > const &left, Boundary< OtherMatrix, SymmGroup > const &right, MPOTensor< Matrix, SymmGroup > const &mpo)
Definition: contractions.h:431
measurement< Matrix, SymmGroup > * do_clone() const
Definition: local.h:106
std::map< std::string, MPO< Matrix, SymmGroup > > local(const Lattice &lat, std::vector< block_matrix< Matrix, SymmGroup > > const &identities, std::vector< block_matrix< Matrix, SymmGroup > > const &fillings, std::vector< std::pair< std::vector< block_matrix< Matrix, SymmGroup > >, bool > > const &ops)
void evaluate(MPS< Matrix, SymmGroup > const &mps, boost::optional< reduced_mps< Matrix, SymmGroup > const & > rmps=boost::none)
Definition: local.h:73
local(std::string const &name_, const Lattice &lat, op_vec const &identities_, op_vec const &fillings_, std::vector< bond_element > const &terms)
Definition: local.h:46
std::vector< typename MPS< Matrix, SymmGroup >::scalar_type > vector_results
Definition: measurement.h:80
definition of Measurement base class
bool is_hermitian_meas(std::vector< block_matrix< Matrix, SymmGroup > > const &ops)
UTILITIES.
Definition: measurement.h:175
MPS< Matrix, SymmGroup >::scalar_type overlap(MPS< Matrix, SymmGroup > const &mps1, MPS< Matrix, SymmGroup > const &mps2)
Definition: mps_mpo_ops.h:136
size_t size() const
Definition: mps.h:57
Definition: mps.h:40
T get_prop(std::string property, pos_t site) const
Definition: lattice.h:103
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.)
impl_type::pos_t pos_t
Definition: lattice.h:88
Index< SymmGroup > phys_psi
Definition: measurement.h:82
bool cast_to_real
Definition: measurement.h:77
local(std::string const &name_, const Lattice &lat, op_vec const &identities_, op_vec const &fillings_, op_vec const &op)
Definition: local.h:59
pimpl resolved Lattice
Definition: lattice.h:84
functions to prepare MPO objects needed in measurements
MPS< Matrix, SymmGroup >::scalar_type dm_trace(MPS< Matrix, SymmGroup > const &mps, Index< SymmGroup > const &phys_psi)
Definition: mps_mpo_ops.h:258