ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
run_eigenstate_sim.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  * 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 ALPS_MPS_OPTIM_RUN_EIGENSTATE_SIM_HPP
28 #define ALPS_MPS_OPTIM_RUN_EIGENSTATE_SIM_HPP
29 
30 #include "dmrg_sim.hpp"
33 
34 #include <algorithm>
35 #include <iterator>
36 #include <sstream>
37 
38 #include <alps/parser/xmlstream.h>
39 
40 class save_to_xml {
41 public:
42  save_to_xml(alps::oxstream& o) : out(o) { }
43 
44  template <class Matrix, class SymmGroup>
46  {
47  m.write_xml(out);
48  }
49 
50 private:
51  alps::oxstream& out;
52 };
53 
54 template <class Matrix, class SymmGroup>
55 void run_eigenstate_sim(BaseParameters parms, bool write_xml, run_type rt)
56 {
57  int neigen = parms["NUMBER_EIGENVALUES"];
58  std::vector<std::string> checkpoints(neigen);
59  for (int eig=0; eig<neigen; ++eig) {
60  DmrgParameters myparms(parms);
61  std::string resfile = parms["resultfile"].str();
62  std::string ckpfile = parms["chkpfile"].str();
63  if (neigen > 1) {
64  std::string eig_suffix = std::string(".") + boost::lexical_cast<std::string>(eig);
65  resfile = boost::replace_last_copy(parms["resultfile"].str(), ".h5", eig_suffix+".h5");
66  ckpfile = boost::replace_last_copy(parms["chkpfile"].str(), ".chkp", eig_suffix+".chkp");
67  myparms.set("resultfile", resfile);
68  myparms.set("chkpfile", ckpfile);
69 
70  std::stringstream ortho_states;
71  std::copy(checkpoints.begin(), checkpoints.begin()+eig, std::ostream_iterator<std::string>(ortho_states, ","));
72  myparms.set("n_ortho_states", eig);
73  myparms.set("ortho_states", ortho_states.str());
74  }
75 
76  /// run optimization
77  if (rt == optim_only || rt == optim_and_measure) {
79  sim.run();
80  }
81 
82  checkpoints[eig] = ckpfile;
83  }
84 
85 
86  /// perform measurements
87  if (rt == measure_only || rt == optim_and_measure) {
88  /// Build model
89  Lattice lattice(parms);
90  Model<Matrix, SymmGroup> model(lattice, parms);
91  MPO<Matrix, SymmGroup> mpo = make_mpo(lattice, model, parms);
92 
93  /// Get measurements
94  typedef typename Model<Matrix, SymmGroup>::measurements_type measurements_type;
95  measurements_type measurements = model.measurements();
96  { // overlap measurements
97  measurements_type m = (parms.defined("nsweeps")) ? overlap_measurements<Matrix, SymmGroup>(parms, parms["nsweeps"]-1) : overlap_measurements<Matrix, SymmGroup>(parms);
98  measurements.insert(measurements.end(), m.begin(), m.end());
99  }
100 
101  std::string rfile = parms["resultfile"].str();
102  {
103  storage::archive ar(rfile, "w");
104  ar["/parameters"] << parms;
105  }
106 
107  alps::oxstream out(boost::replace_last_copy(rfile, ".h5", ".xml"));
108  out << alps::header("UTF-8") << alps::stylesheet(alps::xslt_path("ALPS.xsl"));
109  out << alps::start_tag("SIMULATION") << alps::xml_namespace("xsi","http://www.w3.org/2001/XMLSchema-instance")
110  << alps::attribute("xsi:noNamespaceSchemaLocation","http://xml.comp-phys.org/2003/10/ALPS.xsd");
111 
112  out << parms;
113 
114  out << alps::start_tag("EIGENSTATES") << alps::attribute("number", neigen);
115 
116  for (int eig=0; eig<neigen; ++eig) {
118  load(checkpoints[eig], mps);
119 
120  maquis::cout << "Measurements." << std::endl;
121  std::for_each(measurements.begin(), measurements.end(), measure_and_save<Matrix, SymmGroup>(rfile, "/spectrum/results/", mps, eig));
122 
123  std::vector<int> * measure_es_where = NULL;
124  entanglement_spectrum_type * spectra = NULL;
125  if (parms.defined("entanglement_spectra")) {
126  spectra = new entanglement_spectrum_type();
127  measure_es_where = new std::vector<int>();
128  *measure_es_where = parms.template get<std::vector<int> >("entanglement_spectra");
129  }
130  std::vector<double> entropies, renyi2;
131  if (parms["MEASURE[Entropy]"]) {
132  std::cout << "Calculating vN entropy." << std::endl;
133  entropies = calculate_bond_entropies(mps);
134  }
135  if (parms["MEASURE[Renyi2]"]) {
136  std::cout << "Calculating n=2 Renyi entropy." << std::endl;
137  renyi2 = calculate_bond_renyi_entropies(mps, 2, measure_es_where, spectra);
138  }
139 
140  double energy = maquis::real(expval(mps, mpo));
141  std::cout << "Energy: " << energy << std::endl;
142  {
143  storage::archive ar(rfile, "w");
144  save_val_at_index(ar, "/spectrum/results/Energy/mean/value", energy, eig);
145  if (entropies.size() > 0) save_val_at_index(ar, "/spectrum/results/Entropy/mean/value", entropies, eig);
146  if (renyi2.size() > 0) save_val_at_index(ar, "/spectrum/results/Renyi2/mean/value", renyi2, eig);
147  if (spectra != NULL) save_val_at_index(ar, "/spectrum/results/Entanglement Spectra/mean/value", *spectra, eig);
148  }
149 
150  if (parms["MEASURE[EnergyVariance]"]) {
152  mpo2.compress(1e-12);
153 
154  double energy2 = maquis::real(expval(mps, mpo2, true));
155 
156  maquis::cout << "Energy^2: " << energy2 << std::endl;
157  maquis::cout << "Variance: " << energy2 - energy*energy << std::endl;
158 
159  {
160  storage::archive ar(rfile, "w");
161  save_val_at_index(ar, "/spectrum/results/Energy^2/mean/value", energy2, eig);
162  save_val_at_index(ar, "/spectrum/results/EnergyVariance/mean/value", energy2 - energy*energy, eig);
163  }
164  }
165 
166  /// Output into xml file
167  out << alps::start_tag("EIGENSTATE") << alps::attribute("number", eig);
168  if (write_xml)
169  std::for_each(measurements.begin(), measurements.end(), save_to_xml(out));
170 
171  out << alps::start_tag("SCALAR_AVERAGE") << alps::attribute("name", "Energy") << alps::no_linebreak
172  << alps::start_tag("MEAN") << alps::no_linebreak << energy << alps::end_tag("MEAN")
173  << alps::end_tag("SCALAR_AVERAGE");
174 
175  out << alps::end_tag("EIGENSTATE");
176  }
177 
178  out << alps::end_tag("EIGENSTATES");
179  out << alps::end_tag("SIMULATION");
180  }
181 }
182 
183 
184 #endif
void load(alps::hdf5::archive &ar, std::string const &path, TrivialGroup::charge &value, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t > offset=std::vector< std::size_t >())
Definition: none.h:81
MPO< Matrix, SymmGroup > make_mpo(Lattice const &lat, Model< Matrix, SymmGroup > const &model, BaseParameters &parms)
double expval(MPS< Matrix, SymmGroup > const &mps, MPO< Matrix, SymmGroup > const &mpo, int d)
Definition: mps_mpo_ops.h:72
pimpl for Model
Definition: model.h:96
measurements_type measurements() const
Definition: model.h:132
std::vector< double > calculate_bond_entropies(MPS< Matrix, SymmGroup > &mps)
Definition: mps_mpo_ops.h:252
Definition: sim.h:69
void run()
Definition: dmrg_sim.hpp:63
void set(std::string const &key, T const &value)
void save_val_at_index(Archive &ar, std::string const &archive_path, T const &val, std::size_t eig)
Definition: measurement.h:96
simulation class for ground state optimization
Definition: mpo.h:36
std::vector< std::pair< std::vector< std::string >, std::vector< double > > > entanglement_spectrum_type
Definition: mps_mpo_ops.h:183
void operator()(measurement< Matrix, SymmGroup > const &m)
save_to_xml(alps::oxstream &o)
Definition: mps.h:40
void write_xml(alps::oxstream &) const
Definition: measurement.h:127
MPO< Matrix, SymmGroup > square_mpo(MPO< Matrix, SymmGroup > const &mpo)
Definition: mpo_ops.h:147
impl_type::measurements_type measurements_type
Definition: model.h:109
void run_eigenstate_sim(BaseParameters parms, bool write_xml, run_type rt)
run_type
Definition: simulation.hpp:33
std::vector< double > calculate_bond_renyi_entropies(MPS< Matrix, SymmGroup > &mps, double n, std::vector< int > *measure_es_where=NULL, entanglement_spectrum_type *spectra=NULL)
Definition: mps_mpo_ops.h:186
wrapper of eigenstate_sim for symmetry factory
pimpl resolved Lattice
Definition: lattice.h:84
alps::numeric::real_type< T >::type real(T f)
Definition: bindings.hpp:38
include all Measurements