Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
finite_chain.hpp
Go to the documentation of this file.
1
7#pragma once
8
12
13#include <cstddef>
14#include <span>
15#include <stdexcept>
16#include <type_traits>
17#include <utility>
18#include <vector>
19
21{
22
31template <typename Scalar, Space Bond, Space Physical, BlockTensorStorage Storage = SeparateSparseBlockStorage<>>
32class FiniteMps {
33 public:
34 using value_type = std::remove_cv_t<Scalar>;
35 using bond_space_type = Bond;
36 using physical_space_type = Physical;
37 using storage_policy = Storage;
39
44 explicit FiniteMps(std::vector<site_type> sites) : sites_(std::move(sites)), revisions_(sites_.size())
45 {
46 this->validate();
47 }
48
50 auto size() const noexcept -> std::size_t { return sites_.size(); }
51
53 auto symmetry() const -> Symmetry { return sites_.front().symmetry(); }
54
58 auto site(std::size_t index) const -> site_type const& { return sites_.at(index); }
59
61 auto sites() const noexcept -> std::span<site_type const> { return sites_; }
62
66 auto revision(std::size_t index) const -> std::size_t { return revisions_.at(index); }
67
75 void replace_site(std::size_t index, site_type replacement)
76 {
77 site_type const& current = sites_.at(index);
78 if (replacement.symmetry() != this->symmetry() || replacement.domain() != current.domain() ||
79 replacement.codomain() != current.codomain())
80 {
81 throw std::invalid_argument("FiniteMps replacement site must preserve every space");
82 }
83 sites_[index] = std::move(replacement);
84 ++revisions_[index];
85 }
86
97 void replace_pair(std::size_t first_index, site_type first, site_type second)
98 {
99 if (first_index >= this->size() || first_index + 1 >= this->size())
100 throw std::out_of_range("FiniteMps pair index is out of range");
101
102 site_type const& current_first = sites_[first_index];
103 site_type const& current_second = sites_[first_index + 1];
104 if (first.symmetry() != this->symmetry() || second.symmetry() != this->symmetry() ||
105 first.domain() != current_first.domain() || second.codomain() != current_second.codomain() ||
106 second.domain().template space<1>() != current_second.domain().template space<1>() ||
107 first.codomain().template space<0>() != second.domain().template space<0>())
108 {
109 throw std::invalid_argument("FiniteMps replacement pair has incompatible spaces");
110 }
111
112 sites_[first_index] = std::move(first);
113 sites_[first_index + 1] = std::move(second);
114 ++revisions_[first_index];
115 ++revisions_[first_index + 1];
116 }
117
118 private:
119 void validate() const
120 {
121 if (sites_.empty()) throw std::invalid_argument("FiniteMps requires at least one site");
122 Symmetry const chain_symmetry = sites_.front().symmetry();
123 for (std::size_t index = 0; index < sites_.size(); ++index)
124 {
125 if (sites_[index].symmetry() != chain_symmetry)
126 throw std::invalid_argument("FiniteMps sites must share one symmetry");
127 if (index + 1 < sites_.size() &&
128 sites_[index].codomain().template space<0>() != sites_[index + 1].domain().template space<0>())
129 {
130 throw std::invalid_argument("FiniteMps adjacent bond spaces must compare exactly equal");
131 }
132 }
133 }
134
135 std::vector<site_type> sites_;
136 std::vector<std::size_t> revisions_;
137};
138
146template <typename Scalar, Space Auxiliary, Space Physical, BlockTensorStorage Storage = SeparateSparseBlockStorage<>>
148 public:
149 using value_type = std::remove_cv_t<Scalar>;
150 using auxiliary_space_type = Auxiliary;
151 using physical_space_type = Physical;
152 using storage_policy = Storage;
154
159 explicit FiniteMpo(std::vector<site_type> sites) : sites_(std::move(sites)), revisions_(sites_.size())
160 {
161 this->validate();
162 }
163
165 auto size() const noexcept -> std::size_t { return sites_.size(); }
166
168 auto symmetry() const -> Symmetry { return sites_.front().symmetry(); }
169
173 auto site(std::size_t index) const -> site_type const& { return sites_.at(index); }
174
176 auto sites() const noexcept -> std::span<site_type const> { return sites_; }
177
181 auto revision(std::size_t index) const -> std::size_t { return revisions_.at(index); }
182
188 void replace_site(std::size_t index, site_type replacement)
189 {
190 site_type const& current = sites_.at(index);
191 if (replacement.symmetry() != this->symmetry() || replacement.domain() != current.domain() ||
192 replacement.codomain() != current.codomain())
193 {
194 throw std::invalid_argument("FiniteMpo replacement site must preserve every space");
195 }
196 sites_[index] = std::move(replacement);
197 ++revisions_[index];
198 }
199
200 private:
201 void validate() const
202 {
203 if (sites_.empty()) throw std::invalid_argument("FiniteMpo requires at least one site");
204 Symmetry const chain_symmetry = sites_.front().symmetry();
205 for (std::size_t index = 0; index < sites_.size(); ++index)
206 {
207 if (sites_[index].symmetry() != chain_symmetry)
208 throw std::invalid_argument("FiniteMpo sites must share one symmetry");
209 if (index + 1 < sites_.size() &&
210 sites_[index].codomain().template space<0>() != sites_[index + 1].domain().template space<0>())
211 {
212 throw std::invalid_argument("FiniteMpo adjacent auxiliary spaces must compare exactly equal");
213 }
214 }
215 }
216
217 std::vector<site_type> sites_;
218 std::vector<std::size_t> revisions_;
219};
220
221} // namespace uni20::tensor_network
Defines local sparse and packed-complete BlockTensor storage policies.
Fixed-order tensor whose blocks obey a bosonic abelian selection rule.
Definition block_tensor.hpp:161
auto symmetry() const -> Symmetry
Return the explicit symmetry context.
Definition block_tensor.hpp:304
auto codomain() const -> codomain_type const &
Return the ordered codomain value.
Definition block_tensor.hpp:310
auto domain() const -> domain_type const &
Return the ordered domain value.
Definition block_tensor.hpp:307
Public handle to a canonicalized direct-product symmetry specification.
Definition symmetry.hpp:24
Homogeneous finite MPO owner with exact auxiliary connectivity.
Definition finite_chain.hpp:147
void replace_site(std::size_t index, site_type replacement)
Replace one MPO site without changing any of its spaces.
Definition finite_chain.hpp:188
Auxiliary auxiliary_space_type
Definition finite_chain.hpp:150
FiniteMpo(std::vector< site_type > sites)
Construct a nonempty chain and validate every shared auxiliary.
Definition finite_chain.hpp:159
auto size() const noexcept -> std::size_t
Return the number of sites.
Definition finite_chain.hpp:165
auto symmetry() const -> Symmetry
Return the common symmetry context.
Definition finite_chain.hpp:168
auto site(std::size_t index) const -> site_type const &
Return one read-only site.
Definition finite_chain.hpp:173
auto sites() const noexcept -> std::span< site_type const >
Return all sites in left-to-right order.
Definition finite_chain.hpp:176
Storage storage_policy
Definition finite_chain.hpp:152
auto revision(std::size_t index) const -> std::size_t
Return the revision of one site.
Definition finite_chain.hpp:181
std::remove_cv_t< Scalar > value_type
Definition finite_chain.hpp:149
Physical physical_space_type
Definition finite_chain.hpp:151
Homogeneous finite MPS owner with exact bond connectivity.
Definition finite_chain.hpp:32
auto size() const noexcept -> std::size_t
Return the number of sites.
Definition finite_chain.hpp:50
void replace_site(std::size_t index, site_type replacement)
Replace one site without changing any of its spaces.
Definition finite_chain.hpp:75
auto site(std::size_t index) const -> site_type const &
Return one read-only site.
Definition finite_chain.hpp:58
Physical physical_space_type
Definition finite_chain.hpp:36
Storage storage_policy
Definition finite_chain.hpp:37
auto symmetry() const -> Symmetry
Return the common symmetry context.
Definition finite_chain.hpp:53
std::remove_cv_t< Scalar > value_type
Definition finite_chain.hpp:34
void replace_pair(std::size_t first_index, site_type first, site_type second)
Replace adjacent sites while permitting their shared bond to change.
Definition finite_chain.hpp:97
auto revision(std::size_t index) const -> std::size_t
Return the revision of one site.
Definition finite_chain.hpp:66
FiniteMps(std::vector< site_type > sites)
Construct a nonempty chain and validate every shared bond.
Definition finite_chain.hpp:44
auto sites() const noexcept -> std::span< site_type const >
Return all sites in left-to-right order.
Definition finite_chain.hpp:61
Bond bond_space_type
Definition finite_chain.hpp:35
Definition qnum.hpp:403
Definition right_first_rabc.hpp:34
std::size_t span
Definition presentation.cpp:2017
Defines canonical BlockTensor types for matrix-product networks.
Defines the common tensor-space concepts.