TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
hilbert_space.hpp
Go to the documentation of this file.
1// Copyright (c) 2015-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2015-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2022 Simons Foundation
4// Copyright (c) 2016 Igor Krivenko
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You may obtain a copy of the License at
17// https://www.gnu.org/licenses/gpl-3.0.txt
18//
19// Authors: Michel Ferrero, Igor Krivenko, Olivier Parcollet, Nils Wentzell
20
25
26#pragma once
27
30
31#include <boost/container/flat_map.hpp>
32#include <h5/h5.hpp>
33
34#include <cstdint>
35#include <set>
36#include <string>
37#include <vector>
38
39namespace triqs::hilbert_space {
40
45
53 using fock_state_t = uint64_t;
54
72 public:
74 hilbert_space() : dim_(0) {}
75
84 hilbert_space(fundamental_operator_set const &fops) : dim_(static_cast<int>(1ull << fops.size())) {}
85
87 [[nodiscard]] auto size() const { return dim_; }
88
90 bool operator==(hilbert_space const &hs) const { return dim_ == hs.dim_; }
91
98 [[nodiscard]] bool has_state(fock_state_t f) const { return f < dim_; }
99
109 [[nodiscard]] auto get_state_index(fock_state_t f) const {
110 if (f >= dim_) TRIQS_RUNTIME_ERROR << "Error in hilbert_space::get_state_index: Fock state is not in H: f = " << f;
111 return static_cast<int>(f);
112 }
113
122 [[nodiscard]] fock_state_t get_fock_state(int f) const {
123 if (f >= dim_) TRIQS_RUNTIME_ERROR << "Error in hilbert_space::get_fock_state: Index is >= dim(H): " << f;
124 return f;
125 }
126
137 std::set<fundamental_operator_set::indices_t> const &alphas) const {
138 fock_state_t f = 0;
139 for (auto const &alpha : alphas) f += 1 << fops[alpha];
140 return f;
141 }
142
144 [[nodiscard]] static std::string hdf5_format() { return "hilbert_space"; }
145
153 friend void h5_write(h5::group g, std::string const &name, hilbert_space const &hs) {
154 auto gr = g.create_group(name);
155 h5::write(gr, "dim", hs.dim_);
156 }
157
165 friend void h5_read(h5::group g, std::string const &name, hilbert_space &hs) {
166 auto gr = g.open_group(name);
167 h5::read(gr, "dim", hs.dim_);
168 }
169
170 private:
171 int dim_;
172 };
173
194 public:
199 sub_hilbert_space(int m = -1) : m_(m) {}
200
201#ifdef TRIQS_WORKAROUND_INTEL_COMPILER_BUGS
202 // Workaround needed for icc, checked with 17.0.1 20161005)
203 sub_hilbert_space(sub_hilbert_space const &) = default;
205 sub_hilbert_space &operator=(sub_hilbert_space const &x) {
206 index = x.index;
207 fock_states = x.fock_states;
208 fock_to_index = x.fock_to_index;
209 return *this;
210 }
211 sub_hilbert_space &operator=(sub_hilbert_space &&) = default;
212#endif
213
226 int const M = static_cast<int>(fock_states_.size());
227 fock_states_.push_back(f);
228 fock_to_index_.insert(std::make_pair(f, M));
229 }
230
232 [[nodiscard]] auto size() const { return static_cast<int>(fock_states_.size()); }
233
235 bool operator==(sub_hilbert_space const &hs) const { return m_ == hs.m_ && fock_states_ == hs.fock_states_; }
236
245 [[nodiscard]] auto get_state_index(fock_state_t f) const { return fock_to_index_.find(f)->second; }
246
253 [[nodiscard]] bool has_state(fock_state_t f) const { return fock_to_index_.count(f) == 1; }
254
261 [[nodiscard]] auto get_fock_state(int i) const { return fock_states_[i]; }
262
264 [[nodiscard]] auto const &get_all_fock_states() const { return fock_states_; }
265
267 [[nodiscard]] auto get_index() const { return m_; };
268
273 void set_index(int m) { m_ = m; }
274
276 [[nodiscard]] static std::string hdf5_format() { return "sub_hilbert_space"; }
277
285 friend void h5_write(h5::group g, std::string const &name, sub_hilbert_space const &hs) {
286 auto gr = g.create_group(name);
287 h5::write(gr, "index", hs.m_);
288 h5::write(gr, "fock_states", hs.fock_states_);
289 }
290
298 friend void h5_read(h5::group g, std::string const &name, sub_hilbert_space &hs) {
299 auto gr = g.open_group(name);
300 h5::read(gr, "index", hs.m_);
301 h5::read(gr, "fock_states", hs.fock_states_);
302 hs.fock_to_index_.clear();
303 for (auto f : hs.fock_states_) hs.fock_to_index_.insert(std::make_pair(f, static_cast<int>(hs.fock_to_index_.size())));
304 }
305
306 private:
307 int m_;
308 std::vector<fock_state_t> fock_states_;
309 boost::container::flat_map<fock_state_t, int> fock_to_index_;
310 };
311
313
314} // namespace triqs::hilbert_space
Class representing a fundamental operator set.
fock_state_t get_fock_state(fundamental_operator_set const &fops, std::set< fundamental_operator_set::indices_t > const &alphas) const
Get the Fock state .
static std::string hdf5_format()
Get the HDF5 format tag.
friend void h5_write(h5::group g, std::string const &name, hilbert_space const &hs)
Write a triqs::hilbert_space::hilbert_space to HDF5.
auto size() const
Get the dimension of the Hilbert (Fock) space .
bool operator==(hilbert_space const &hs) const
Equal-to operator compares the dimensions of the two Hilbert (Fock) spaces.
bool has_state(fock_state_t f) const
Check if a given Fock state belongs to the Hilbert (Fock) space.
friend void h5_read(h5::group g, std::string const &name, hilbert_space &hs)
Read a triqs::hilbert_space::hilbert_space from HDF5.
fock_state_t get_fock_state(int f) const
Get the Fock state corresponding to a given integer .
auto get_state_index(fock_state_t f) const
Get the integer corresponding to a given Fock state .
hilbert_space(fundamental_operator_set const &fops)
Construct a Hilbert (Fock) space from a given triqs::hilbert_space::fundamental_operator_set object.
hilbert_space()
Default constructor constructs an empty Hilbert (Fock) space of dimension .
Subspace of a fermionic Hilbert (Fock) space.
static std::string hdf5_format()
Get the HDF5 format tag.
auto const & get_all_fock_states() const
Get , the ordered set of all Fock states in the subspace.
void set_index(int m)
Set the subspace index of this subspace .
auto get_state_index(fock_state_t f) const
Get the index of a given Fock state within this subspace.
auto size() const
Get the dimension of the subspace.
auto get_fock_state(int i) const
Get the Fock state for a given index .
bool operator==(sub_hilbert_space const &hs) const
Equal-to operator compares the subspace index and the ordered set .
friend void h5_read(h5::group g, std::string const &name, sub_hilbert_space &hs)
Read a triqs::hilbert_space::sub_hilbert_space from HDF5.
friend void h5_write(h5::group g, std::string const &name, sub_hilbert_space const &hs)
Write a triqs::hilbert_space::sub_hilbert_space to HDF5.
sub_hilbert_space(int m=-1)
Construct an empty subspace with a given index .
void add_fock_state(fock_state_t f)
Add a Fock state to the subspace and increase its dimension by one.
auto get_index() const
Get the subspace index .
bool has_state(fock_state_t f) const
Check if a given Fock state belongs to this subspace.
TRIQS exception hierarchy and related macros.
uint64_t fock_state_t
Integer type representing a fermionic fock state .
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.
Provides a fundamental operator set class.