ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
measurement.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  * 2013-2013 by Michele Dolfi <dolfim@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 MEASUREMENT_H
28 #define MEASUREMENT_H
29 
34 
35 #include "dmrg/models/lattice.h"
36 
37 #include <alps/parser/xmlstream.h>
38 
39 #include <vector>
40 #include <string>
41 #include <sstream>
42 #include <iostream>
43 
44 #include <stdexcept>
45 
46 #include <boost/regex.hpp>
47 #include <boost/static_assert.hpp>
48 
49 /// TODO: 1) move data to new object measurement_result.
50 /// 2) store only measurement description, i.e. no matrix, and pass model+lattice in evaluate method.
51 
52 template<class Matrix, class SymmGroup>
53 class measurement {
54 public:
55  typedef typename Matrix::value_type value_type;
56 
57  measurement(std::string const& n="") : cast_to_real(true), is_super_meas(false), name_(n), eigenstate(0) { }
58  virtual ~measurement() { }
59 
60  virtual void evaluate(MPS<Matrix, SymmGroup> const&, boost::optional<reduced_mps<Matrix, SymmGroup> const&> = boost::none) =0;
61  template <class Archive>
62  void save(Archive &) const;
63  void write_xml(alps::oxstream &) const;
64  virtual void print(std::ostream& os) const;
65 
66  std::string const& name() const { return name_; }
67  int& eigenstate_index() { return eigenstate; }
68  int eigenstate_index() const { return eigenstate; }
69 
70  void set_super_meas(Index<SymmGroup> const& phys_psi_);
71 
72  measurement* clone() const { return do_clone(); }
73 
74 protected:
75  virtual measurement* do_clone() const =0;
76 
78  std::vector<std::string> labels;
80  std::vector<typename MPS<Matrix, SymmGroup>::scalar_type> vector_results;
81 
83 
84 private:
85  std::string name_;
86  int eigenstate;
87 };
88 
89 template<class Matrix, class SymmGroup>
91 {
92  return m.clone();
93 }
94 
95 template <class Archive, typename T>
96 void save_val_at_index(Archive & ar, std::string const& archive_path, T const& val, std::size_t eig)
97 {
98  std::vector<T> vals;
99  if (ar.is_data(archive_path.c_str())) ar[archive_path] >> vals;
100  vals.resize( std::max(vals.size(), eig+1) );
101  vals[eig] = val;
102  ar[archive_path] << vals;
103 }
104 
105 template<class Matrix, class SymmGroup>
106 template <class Archive>
107 void measurement<Matrix, SymmGroup>::save(Archive & ar) const
108 {
109  if (vector_results.size() > 0) {
110  if (cast_to_real) {
111  save_val_at_index(ar, storage::encode(name()) + std::string("/mean/value"), maquis::real(vector_results), eigenstate_index());
112  } else {
113  save_val_at_index(ar, storage::encode(name()) + std::string("/mean/value"), vector_results, eigenstate_index());
114  }
115  if (labels.size() > 0)
116  ar[storage::encode(name()) + std::string("/labels")] << labels;
117  } else {
118  if (cast_to_real) {
119  save_val_at_index(ar, storage::encode(name()) + std::string("/mean/value"), maquis::real(result), eigenstate_index());
120  } else {
121  save_val_at_index(ar, storage::encode(name()) + std::string("/mean/value"), result, eigenstate_index());
122  }
123  }
124 }
125 
126 template<class Matrix, class SymmGroup>
127 void measurement<Matrix, SymmGroup>::write_xml(alps::oxstream & out) const
128 {
129  if (labels.size() > 0) {
130  out << alps::start_tag("VECTOR_AVERAGE") << alps::attribute("name", name());
131  for (int i=0; i<labels.size(); ++i) {
132  out << alps::start_tag("SCALAR_AVERAGE");
133  out << alps::attribute("indexvalue",labels[i]) << alps::no_linebreak;
134 
135  if (cast_to_real) {
136  out << alps::start_tag("MEAN") << alps::no_linebreak << maquis::real(vector_results[i]) << alps::end_tag("MEAN");
137  } else {
138  out << alps::start_tag("MEAN") << alps::no_linebreak << vector_results[i] << alps::end_tag("MEAN");
139  }
140  out << alps::end_tag("SCALAR_AVERAGE");
141 
142  }
143  out << alps::end_tag("VECTOR_AVERAGE");
144  } else {
145  out << alps::start_tag("SCALAR_AVERAGE") << alps::attribute("name", name()) << alps::no_linebreak;
146  out << alps::start_tag("MEAN") << alps::no_linebreak << ((cast_to_real) ? maquis::real(result) : result) << alps::end_tag("MEAN");
147  out << alps::end_tag("SCALAR_AVERAGE");
148  }
149 }
150 
151 
152 template<class Matrix, class SymmGroup>
154 {
155  phys_psi = phys_psi_;
156  is_super_meas = true;
157 }
158 
159 template<class Matrix, class SymmGroup>
160 void measurement<Matrix, SymmGroup>::print(std::ostream& os) const
161 {
162  os << "MEASURE[" << name_ << "]";
163 }
164 
165 template<class Matrix, class SymmGroup>
166 std::ostream& operator<<(std::ostream& os, measurement<Matrix, SymmGroup> const& m)
167 {
168  m.print(os);
169 }
170 
171 
172 /// UTILITIES
173 
174 template<class Matrix, class SymmGroup>
175 bool is_hermitian_meas(std::vector<block_matrix<Matrix, SymmGroup> > const & ops)
176 {
177  return all_true(ops.begin(), ops.end(),
178  boost::bind(static_cast<bool (*)(block_matrix<Matrix, SymmGroup> const&)>(&is_hermitian), _1));
179 }
180 
181 template<class Matrix, class SymmGroup>
182 bool is_hermitian_meas(std::vector<std::pair<std::vector<block_matrix<Matrix, SymmGroup> >, bool> > const & ops)
183 {
184  bool is_herm = true;
185  for (int i=0; i<ops.size() && is_herm; ++i)
186  is_herm = is_hermitian_meas(ops[i].first);
187  return is_herm;
188 }
189 
190 inline std::vector<std::string> label_strings (const Lattice& lat, const std::vector<std::vector<std::size_t> >& labels)
191 {
192  std::vector<std::string> ret;
193  ret.reserve(labels.size());
194  for (std::vector<std::vector<std::size_t> >::const_iterator it = labels.begin();
195  it != labels.end(); ++it)
196  {
197  std::ostringstream oss;
198  for (std::vector<std::size_t>::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) {
199  oss << lat.get_prop<std::string>("label", *it2);
200  if (it2 + 1 != it->end())
201  oss << " -- ";
202  }
203  ret.push_back(oss.str());
204  }
205  return ret;
206 }
207 
208 
209 
210 #endif
definition of Lattice base class
std::vector< std::string > labels
Definition: measurement.h:78
value_type result
Definition: measurement.h:79
include all symmetry definitions
virtual void evaluate(MPS< Matrix, SymmGroup > const &, boost::optional< reduced_mps< Matrix, SymmGroup > const & >=boost::none)=0
bool is_hermitian(block_matrix< Matrix, SymmGroup > const &m)
std::string encode(std::string const &s)
Definition: archive.h:82
bool is_super_meas
Definition: measurement.h:77
void set_super_meas(Index< SymmGroup > const &phys_psi_)
Definition: measurement.h:153
bool all_true(InputIterator first, InputIterator last, Predicate pred)
Definition: utils.hpp:53
std::vector< std::string > label_strings(const Lattice &lat, const std::vector< std::vector< std::size_t > > &labels)
Definition: measurement.h:190
declaration of block_matrix class
measurement(std::string const &n="")
Definition: measurement.h:57
void save_val_at_index(Archive &ar, std::string const &archive_path, T const &val, std::size_t eig)
Definition: measurement.h:96
std::vector< typename MPS< Matrix, SymmGroup >::scalar_type > vector_results
Definition: measurement.h:80
bool is_hermitian_meas(std::vector< block_matrix< Matrix, SymmGroup > > const &ops)
UTILITIES.
Definition: measurement.h:175
virtual ~measurement()
Definition: measurement.h:58
Definition: mps.h:40
void write_xml(alps::oxstream &) const
Definition: measurement.h:127
T get_prop(std::string property, pos_t site) const
Definition: lattice.h:103
int & eigenstate_index()
Definition: measurement.h:67
virtual void print(std::ostream &os) const
Definition: measurement.h:160
algorithms for block_matrix (gemm, svd, etc.)
void save(Archive &) const
Definition: measurement.h:107
measurement * clone() const
Definition: measurement.h:72
Matrix::value_type value_type
Definition: measurement.h:55
Index< SymmGroup > phys_psi
Definition: measurement.h:82
bool cast_to_real
Definition: measurement.h:77
pimpl resolved Lattice
Definition: lattice.h:84
alps::numeric::real_type< T >::type real(T f)
Definition: bindings.hpp:38
measurement< Matrix, SymmGroup > * new_clone(const measurement< Matrix, SymmGroup > &m)
Definition: measurement.h:90
virtual measurement * do_clone() const =0
std::string const & name() const
Definition: measurement.h:66
int eigenstate_index() const
Definition: measurement.h:68