1#ifndef XPED_TWO_SITE_OBSERVABLE_HPP_
2#define XPED_TWO_SITE_OBSERVABLE_HPP_
6#include <highfive/H5DataSpace.hpp>
17template <
typename,
typename, std::
size_t,
bool, Opts::CTMCheckpo
int>
20template <
typename Scalar,
typename Symmetry,
bool HERMITIAN = true>
57 if(std::all_of(charges.
cbegin(), charges.
cend(), [](
auto c) { return c == Symmetry::qvacuum(); })) {
return *
this; }
59 for(
int x = 0; x <
data_h.pat.Lx; ++x) {
60 for(
int y = 0; y <
data_h.pat.Ly; ++y) {
61 if(not
data_h.pat.isUnique(x, y)) {
continue; }
72 auto tmp =
data_h(x, y).template contract<std::array{-1, -3, -5, -7}, std::array{-2, -4, -6, -8}, 4>(id);
74 {{
data_h(x, y).uncoupledCodomain()[0].combine(c1).forgetHistory()}});
77 {{
data_h(x, y).uncoupledCodomain()[1].combine(c2).forgetHistory()}});
80 tmp.template contract<std::array{-1, -2, -3, -4, 1, 2, -6, -7}, std::array{1, 2, -5}, 4>(fuser1)
81 .
template contract<std::array{-1, -2, -3, -4, -5, 1, 2}, std::array{1, 2, -6}, 4>(fuser2)
82 .
template contract<std::array{1, 2, -2, -3, -4, -5}, std::array{-1, 1, 2}, 3>(fuser1.adjoint().eval().twist(1).twist(2))
83 .
template contract<std::array{-1, 1, 2, -3, -4}, std::array{-2, 1, 2}, 2>(fuser2.adjoint().eval().twist(1).twist(2));
95 auto tmp =
data_v(x, y).template contract<std::array{-1, -3, -5, -7}, std::array{-2, -4, -6, -8}, 4>(id);
97 {{
data_v(x, y).uncoupledCodomain()[0].combine(c1).forgetHistory()}});
100 {{
data_v(x, y).uncoupledCodomain()[1].combine(c2).forgetHistory()}});
101 fuser2.setIdentity();
103 tmp.template contract<std::array{-1, -2, -3, -4, 1, 2, -6, -7}, std::array{1, 2, -5}, 4>(fuser1)
104 .
template contract<std::array{-1, -2, -3, -4, -5, 1, 2}, std::array{1, 2, -6}, 4>(fuser2)
105 .
template contract<std::array{1, 2, -2, -3, -4, -5}, std::array{-1, 1, 2}, 3>(fuser1.adjoint().eval().twist(1).twist(2))
106 .
template contract<std::array{-1, 1, 2, -3, -4}, std::array{-2, 1, 2}, 2>(fuser2.adjoint().eval().twist(1).twist(2));
117 auto tmp =
data_d1(x, y).template contract<std::array{-1, -3, -5, -7}, std::array{-2, -4, -6, -8}, 4>(id);
119 {{
data_d1(x, y).uncoupledCodomain()[0].combine(c1).forgetHistory()}});
120 fuser1.setIdentity();
122 {{
data_d1(x, y).uncoupledCodomain()[1].combine(c2).forgetHistory()}});
123 fuser2.setIdentity();
125 tmp.template contract<std::array{-1, -2, -3, -4, 1, 2, -6, -7}, std::array{1, 2, -5}, 4>(fuser1)
126 .
template contract<std::array{-1, -2, -3, -4, -5, 1, 2}, std::array{1, 2, -6}, 4>(fuser2)
127 .
template contract<std::array{1, 2, -2, -3, -4, -5}, std::array{-1, 1, 2}, 3>(fuser1.adjoint().eval().twist(1).twist(2))
128 .
template contract<std::array{-1, 1, 2, -3, -4}, std::array{-2, 1, 2}, 2>(fuser2.adjoint().eval().twist(1).twist(2));
139 auto tmp =
data_d2(x, y).template contract<std::array{-1, -3, -5, -7}, std::array{-2, -4, -6, -8}, 4>(id);
141 {{
data_d2(x, y).uncoupledCodomain()[0].combine(c1).forgetHistory()}});
142 fuser1.setIdentity();
144 {{
data_d2(x, y).uncoupledCodomain()[1].combine(c2).forgetHistory()}});
145 fuser2.setIdentity();
147 tmp.template contract<std::array{-1, -2, -3, -4, 1, 2, -6, -7}, std::array{1, 2, -5}, 4>(fuser1)
148 .
template contract<std::array{-1, -2, -3, -4, -5, 1, 2}, std::array{1, 2, -6}, 4>(fuser2)
149 .
template contract<std::array{1, 2, -2, -3, -4, -5}, std::array{-1, 1, 2}, 3>(fuser1.adjoint().eval().twist(1).twist(2))
150 .
template contract<std::array{-1, 1, 2, -3, -4}, std::array{-2, 1, 2}, 2>(fuser2.adjoint().eval().twist(1).twist(2));
157 void loadFromMatlab(
const std::filesystem::path& p,
const std::string& root_name,
int qn_scale = 1)
159 HighFive::File file(p.string(), HighFive::File::ReadOnly);
160 auto root = file.getGroup(root_name);
183 auto H_ref = root.getDataSet(
"H");
184 std::vector<HighFive::Reference> H;
186 auto g_Hh = H[2 * i].template dereference<HighFive::Group>(root);
187 auto g_Hv = H[2 * i + 1].template dereference<HighFive::Group>(root);
189 Xped::IO::loadMatlabTensor<Scalar, 4, 0, Symmetry, Xped::HeapPolicy>(g_Hh, root, std::array{
false,
false,
true,
true}, qn_scale)
190 .
template permute<2, 0, 1, 2, 3>();
192 Xped::IO::loadMatlabTensor<Scalar, 4, 0, Symmetry, Xped::HeapPolicy>(g_Hv, root, std::array{
false,
false,
true,
true}, qn_scale)
193 .
template permute<2, 0, 1, 2, 3>();
195 fmt::print(
"Loaded hamiltonian from matlab.\n");
198 virtual std::string
getResString(
const std::string& offset)
const override
202 fmt::format_to(std::back_inserter(res),
203 "{}{:<10}: avg_h={:+.2f}, vals_h={::+.4f}\n",
210 fmt::format_to(std::back_inserter(res),
211 "{}{:<10}: avg_v={:+.2f}, vals_v={::+.4f}\n",
218 fmt::format_to(std::back_inserter(res),
219 "{}{:<10}: avg_d1={:+.2f}, vals_d1={::+.4f}\n",
226 fmt::format_to(std::back_inserter(res),
227 "{}{:<10}: avg_d2={:+.2f}, vals_d2={::+.4f}",
236 virtual void toFile(HighFive::File& file,
const std::string& root =
"/")
const override
238 auto write_component = [&file, &root](std::string
name,
const auto& o) {
239 if(not file.exist(root +
name)) {
240 HighFive::DataSpace dataspace = HighFive::DataSpace({0, 0}, {HighFive::DataSpace::UNLIMITED, HighFive::DataSpace::UNLIMITED});
243 HighFive::DataSetCreateProps props;
244 props.add(HighFive::Chunking(std::vector<hsize_t>{2, 2}));
247 HighFive::DataSet dataset = file.createDataSet(root +
name, dataspace, HighFive::create_datatype<ObsScalar>(), props);
249 auto d = file.getDataSet(root +
name);
250 std::vector<std::vector<ObsScalar>> data;
251 data.push_back(o.uncompressedVector());
252 std::size_t curr_size = d.getDimensions()[0];
253 d.resize({curr_size + 1, data[0].size()});
254 d.select({curr_size, 0}, {1, data[0].size()}).write(data);
268 virtual std::string
getObsString(
const std::string&)
const {
return ""; }
270 virtual void obsToFile(HighFive::File&,
const std::string& =
"/")
const {}
272 template <
typename Ar>
275 ar& YAS_OBJECT_NVP(
"TwoSiteobservable",
Definition: Qbasis.hpp:39
void push_back(const qType &q, const size_t &inner_dim)
Definition: Qbasis.cpp:32
Definition: Tensor.hpp:40
Bond
Definition: Bonds.hpp:10
Definition: ObservableBase.hpp:11
std::string name
Definition: ObservableBase.hpp:24
Definition: CTMOpts.hpp:96
Definition: Pattern.hpp:18
std::size_t uniqueSize() const
Definition: Pattern.hpp:43
Definition: ScalarTraits.hpp:10
Definition: TMatrix.hpp:13
Ttype sum() const
Definition: TMatrix.hpp:77
auto cbegin() const
Definition: TMatrix.hpp:70
std::size_t size() const
Definition: TMatrix.hpp:38
std::vector< Ttype > uncompressedVector() const
Definition: TMatrix.hpp:85
auto cend() const
Definition: TMatrix.hpp:71
Definition: TwoSiteObservable.hpp:22
virtual void computeObs(XPED_CONST CTM< double, Symmetry, 1, false, Opts::CTMCheckpoint{}> &env)
Definition: TwoSiteObservable.hpp:266
TwoSiteObservable< Scalar, Symmetry, HERMITIAN > shiftQN(const TMatrix< typename Symmetry::qType > &charges) const
Definition: TwoSiteObservable.hpp:55
TMatrix< ObsScalar > obs_d1
Definition: TwoSiteObservable.hpp:293
TMatrix< Tensor< Scalar, 2, 2, Symmetry, false > > data_h
Definition: TwoSiteObservable.hpp:287
TwoSiteObservable(const Pattern &pat, Opts::Bond bond, const std::string &name_in="")
Definition: TwoSiteObservable.hpp:33
virtual std::string getResString(const std::string &offset) const override
Definition: TwoSiteObservable.hpp:198
virtual void computeObs(XPED_CONST CTM< std::complex< double >, Symmetry, 2, false, Opts::CTMCheckpoint{}> &env)
Definition: TwoSiteObservable.hpp:265
virtual std::string getObsString(const std::string &) const
Definition: TwoSiteObservable.hpp:268
void serialize(Ar &ar)
Definition: TwoSiteObservable.hpp:273
void loadFromMatlab(const std::filesystem::path &p, const std::string &root_name, int qn_scale=1)
Definition: TwoSiteObservable.hpp:157
TMatrix< Tensor< Scalar, 2, 2, Symmetry, false > > data_v
Definition: TwoSiteObservable.hpp:288
TMatrix< Tensor< Scalar, 2, 2, Symmetry, false > > data_d1
Definition: TwoSiteObservable.hpp:289
TMatrix< ObsScalar > obs_v
Definition: TwoSiteObservable.hpp:292
Opts::Bond bond
Definition: TwoSiteObservable.hpp:295
TMatrix< ObsScalar > obs_h
Definition: TwoSiteObservable.hpp:291
std::conditional_t< HERMITIAN, typename ScalarTraits< Scalar >::Real, typename ScalarTraits< Scalar >::Comp > ObsScalar
Definition: TwoSiteObservable.hpp:23
virtual void computeObs(XPED_CONST CTM< double, Symmetry, 2, false, Opts::CTMCheckpoint{}> &env)
Definition: TwoSiteObservable.hpp:264
virtual void setDefaultObs()
Definition: TwoSiteObservable.hpp:262
TMatrix< ObsScalar > obs_d2
Definition: TwoSiteObservable.hpp:294
TwoSiteObservable()=default
virtual void toFile(HighFive::File &file, const std::string &root="/") const override
Definition: TwoSiteObservable.hpp:236
virtual void obsToFile(HighFive::File &, const std::string &="/") const
Definition: TwoSiteObservable.hpp:270
TMatrix< Tensor< Scalar, 2, 2, Symmetry, false > > data_d2
Definition: TwoSiteObservable.hpp:290
Definition: UnitCell.hpp:15
Pattern pattern
Definition: UnitCell.hpp:27
void loadFromMatlab(const std::filesystem::path &p, const std::string &root_name)
Definition: UnitCell.cpp:26