Skip to content

Commit

Permalink
Add subcommand to project onto all basis states (only implemented for…
Browse files Browse the repository at this point in the history
… Spin 1/2 at the moment).
  • Loading branch information
f-koehler committed Jul 13, 2022
1 parent 4896198 commit 20f2247
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 115,7 @@ mpskit_link_libraries(itensor)

add_library(mpskit_common STATIC
mpskit/cmd/dmrg.cpp
mpskit/cmd/analyze_basis.cpp
mpskit/cmd/entanglement_entropy.cpp
mpskit/cmd/list_observables.cpp
mpskit/cmd/list_one_point.cpp
Expand Down
77 changes: 77 additions & 0 deletions mpskit/cmd/analyze_basis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,77 @@
#include "analyze_basis.hpp"

#include <cmath>
#include <iostream>

#include <highfive/H5DataSet.hpp>
#include <highfive/H5DataSpace.hpp>
#include <highfive/H5File.hpp>
#include <itensor/mps/mps.h>
#include <itensor/mps/sites/spinhalf.h>
#include <itensor/util/readwrite.h>

#include "../json.hpp"
#include "../models/model.hpp"
#include "../models/registry.hpp"

int cmdAnalyzeBasis(const std::string &input_path, const std::string &psi_path, const std::string &output_path)
{
auto input = loadJSON(input_path);
auto model = createModel1D(input["model"]);
auto sites = model->getSites();

// read MPS
auto psi = itensor::readFromFile<itensor::MPS>(psi_path);

// auto sites = itensor::SiteSet(itensor::siteInds(psi));
const auto num_sites = static_cast<std::size_t>(sites.length());
const auto local_dim = static_cast<std::size_t>(sites(1).size());
const std::size_t num_states = 1ul << num_sites;

if (local_dim != 2ul)
{
std::cerr << "Currently only implement for local_dim == 2, got local_dim == " << local_dim << '\n';
return 1;
}

if (num_sites > 24ul)
{
std::cerr << "Refusing to project onto more than 2^24 states.\n";
return 1;
}

try
{
HighFive::File file(output_path,
HighFive::File::Overwrite | HighFive::File::Create | HighFive::File::ReadWrite);
HighFive::DataSet dset_states = file.createDataSet<int>(
"states", HighFive::DataSpace({static_cast<std::size_t>(num_states), static_cast<std::size_t>(num_sites)}));
HighFive::DataSet dset_overlaps =
file.createDataSet<double>("overlap", HighFive::DataSpace({static_cast<std::size_t>(num_states)}));

itensor::InitState init_state(sites, "Dn");
std::vector<std::size_t> configuration(num_sites, 0ul);

for (std::size_t index = 0; index < num_states; index)
{
for (std::size_t i = 0; i < num_sites; i)
{
bool bit = (index >> i) & 1ul;
configuration[i] = bit ? 1ul : 0ul;
init_state.set(static_cast<int>(i 1), bit ? "Up" : "Dn");
}

const auto overlap = itensor::innerC(psi, itensor::MPS(init_state));
const auto overlap_abs = std::abs(overlap);

dset_states.select({index, 0}, {1, num_sites}).write(configuration);
dset_overlaps.select({index}).write(overlap_abs * overlap_abs);
}
}
catch (HighFive::Exception &e)
{
std::cerr << e.what() << '\n';
}

return 0;
}
8 changes: 8 additions & 0 deletions mpskit/cmd/analyze_basis.hpp
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
#ifndef MPSKIT_CMD_ANALYZE_BASIS
#define MPSKIT_CMD_ANALYZE_BASIS

#include <string>

int cmdAnalyzeBasis(const std::string &input_path, const std::string &psi_path, const std::string &output_path);

#endif /* MPSKIT_CMD_ANALYZE_BASIS */
5 changes: 5 additions & 0 deletions mpskit/models/spin_half/2d/xy_spin_glass_square.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
#include "xy_spin_glass_square.hpp"

XYSpinGlassSquare::XYSpinGlassSquare(const json& js)
: SpinHalfSquare(js["Lx"].get<int>(), js["Ly"].get<int>(<))
{}
20 changes: 20 additions & 0 deletions mpskit/models/spin_half/2d/xy_spin_glass_square.hpp
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
#ifndef MPSKIT_MODELS_SPIN_HALF_2D_XY_SPIN_GLASS_SQUARE
#define MPSKIT_MODELS_SPIN_HALF_2D_XY_SPIN_GLASS_SQUARE

#include "../../../json.hpp"
#include "spin_half_square.hpp"

class XYSpinGlassSquare : public SpinHalfSquare
{
protected:
Real m_alpha;

public:
XYSpinGlassSquare(const json &js);

const Real &getAlpha() const;

virtual void print(std::ostream &stream) const override;
};

#endif /* MPSKIT_MODELS_SPIN_HALF_2D_XY_SPIN_GLASS_SQUARE */

0 comments on commit 20f2247

Please sign in to comment.