xerus
a general purpose tensor library
als.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include "../ttNetwork.h"
28 #include "../performanceData.h"
29 
30 namespace xerus {
31 
37  class ALSVariant {
38  public:
40 
41  protected:
42  double solve(const TTOperator *_Ap, TTTensor &_x, const TTTensor &_b, size_t _numHalfSweeps, value_t _convergenceEpsilon, PerformanceData &_perfData = NoPerfData) const;
43 
46  std::vector<Tensor> left, right;
47  };
48  const ALSVariant &ALS;
49  const TTOperator *A;
51  const TTTensor &b;
52  std::vector<size_t> targetRank;
56  std::pair<size_t, size_t> optimizedRange;
58  size_t corePosAtTheEnd;
59  std::function<value_t()> energy_f;
60  std::function<value_t()> residual_f;
61 
62  size_t currIndex;
66  size_t halfSweepCount;
68 
76  void prepare_x_for_als();
77 
78  TensorNetwork localOperatorSlice(size_t _pos);
79  TensorNetwork localRhsSlice(size_t _pos);
80 
86  void prepare_stacks();
87 
94 
95  ALSAlgorithmicData(const ALSVariant &, const TTOperator *, TTTensor &, const TTTensor &);
96 
102  void move_to_next_index();
103  };
104 
107  bool check_for_end_of_sweep(ALSAlgorithmicData& _data, size_t _numHalfSweeps, value_t _convergenceEpsilon, PerformanceData &_perfData) const;
108  public:
109  const size_t FLAG_FINISHED_HALFSWEEP = 1;
110  const size_t FLAG_FINISHED_FULLSWEEP = 3; // contains the flag for a new halfsweep!
111 
113  size_t numHalfSweeps;
115 // value_t minimumLocalResidual; ///< below this bound for the local residual, no local solver will be called.
118  bool assumeSPD;
119 
120  // TODO std::function endCriterion
121 
123  using LocalSolver = std::function<void(const TensorNetwork &, std::vector<Tensor> &, const TensorNetwork &, const ALSAlgorithmicData &)>;
125 
127  static void lapack_solver(const TensorNetwork &_A, std::vector<Tensor> &_x, const TensorNetwork &_b, const ALSAlgorithmicData &_data);
128  static void ASD_solver(const TensorNetwork &_A, std::vector<Tensor> &_x, const TensorNetwork &_b, const ALSAlgorithmicData &_data);
129 
130  //TODO add local CG solver
131  //TODO add AMEn solver
132 
134  ALSVariant(uint _sites, size_t _numHalfSweeps, LocalSolver _localSolver, bool _assumeSPD,
135  bool _useResidual=false
136  )
137  : sites(_sites), numHalfSweeps(_numHalfSweeps), convergenceEpsilon(1e-6),
138  useResidualForEndCriterion(_useResidual), preserveCorePosition(true), assumeSPD(_assumeSPD), localSolver(_localSolver)
139  {
140  XERUS_REQUIRE(_sites>0, "");
141  }
142 
152  double operator()(const TTOperator &_A, TTTensor &_x, const TTTensor &_b, value_t _convergenceEpsilon, PerformanceData &_perfData = NoPerfData) const {
153  return solve(&_A, _x, _b, numHalfSweeps, _convergenceEpsilon, _perfData);
154  }
155 
165  double operator()(const TTOperator &_A, TTTensor &_x, const TTTensor &_b, size_t _numHalfSweeps, PerformanceData &_perfData = NoPerfData) const {
166  return solve(&_A, _x, _b, _numHalfSweeps, convergenceEpsilon, _perfData);
167  }
168 
177  double operator()(const TTOperator &_A, TTTensor &_x, const TTTensor &_b, PerformanceData &_perfData = NoPerfData) const {
178  return solve(&_A, _x, _b, numHalfSweeps, convergenceEpsilon, _perfData);
179  }
180 
189  double operator()(TTTensor &_x, const TTTensor &_b, value_t _convergenceEpsilon, PerformanceData &_perfData = NoPerfData) const {
190  return solve(nullptr, _x, _b, numHalfSweeps, _convergenceEpsilon, _perfData);
191  }
192 
201  double operator()(TTTensor &_x, const TTTensor &_b, size_t _numHalfSweeps, PerformanceData &_perfData = NoPerfData) const {
202  return solve(nullptr, _x, _b, _numHalfSweeps, convergenceEpsilon, _perfData);
203  }
204 
205  double operator()(TTTensor &_x, const TTTensor &_b, PerformanceData &_perfData = NoPerfData) const {
206  return solve(nullptr, _x, _b, numHalfSweeps, convergenceEpsilon, _perfData);
207  }
208  };
209 
211  extern const ALSVariant ALS;
213  extern const ALSVariant ALS_SPD;
214 
216  extern const ALSVariant DMRG;
218  extern const ALSVariant DMRG_SPD;
219 
221  extern const ALSVariant ASD;
223  extern const ALSVariant ASD_SPD;
224 }
225 
LocalSolver localSolver
Definition: als.h:124
double operator()(TTTensor &_x, const TTTensor &_b, value_t _convergenceEpsilon, PerformanceData &_perfData=NoPerfData) const
Definition: als.h:189
TensorNetwork construct_local_operator(ALSAlgorithmicData &_data) const
Definition: als.cpp:383
value_t lastEnergy2
energy at the end of last full sweep
Definition: als.h:63
static void lapack_solver(const TensorNetwork &_A, std::vector< Tensor > &_x, const TensorNetwork &_b, const ALSAlgorithmicData &_data)
local solver that calls the corresponding lapack routines (LU solver)
Definition: als.cpp:43
const ALSVariant ASD
default variant of the alternating steepest descent for non-symmetric operators
const ALSVariant ASD_SPD
default variant of the alternating steepest descent for symmetric positive-definite operators ...
value_t normB
norm of the (global) right hand side
Definition: als.h:55
double operator()(const TTOperator &_A, TTTensor &_x, const TTTensor &_b, value_t _convergenceEpsilon, PerformanceData &_perfData=NoPerfData) const
Definition: als.h:152
value_t energy
current value of the energy residual
Definition: als.h:65
Very general class used to represent arbitary tensor networks.
Definition: tensorNetwork.h:42
ALSVariant(uint _sites, size_t _numHalfSweeps, LocalSolver _localSolver, bool _assumeSPD, bool _useResidual=false)
fully defining constructor. alternatively ALSVariants can be created by copying a predefined variant ...
Definition: als.h:134
std::function< value_t()> energy_f
the energy functional used for this calculation
Definition: als.h:59
TensorNetwork localOperatorSlice(size_t _pos)
Definition: als.cpp:184
ContractedTNCache rhsCache
stacks for the right-hand-side (either xb or xAtb)
Definition: als.h:54
double operator()(const TTOperator &_A, TTTensor &_x, const TTTensor &_b, size_t _numHalfSweeps, PerformanceData &_perfData=NoPerfData) const
Definition: als.h:165
const size_t FLAG_FINISHED_HALFSWEEP
Definition: als.h:109
Specialized TensorNetwork class used to represent TTTensor and TToperators.
size_t halfSweepCount
current count of halfSweeps
Definition: als.h:66
The main namespace of xerus.
Definition: basic.h:37
size_t corePosAtTheEnd
core position that should be restored at the end of the algorithm
Definition: als.h:58
void prepare_stacks()
prepares the initial stacks for the local operator and local right-hand-side
Definition: als.cpp:217
void move_to_next_index()
performs one step in direction, updating all stacks
Definition: als.cpp:340
void prepare_x_for_als()
Finds the range of notes that need to be optimized and orthogonalizes _x properly.
Definition: als.cpp:105
Storage class for the performance data collected during an algorithm (typically iteration count...
std::pair< size_t, size_t > optimizedRange
range of indices for the nodes of _x that need to be optimized
Definition: als.h:56
uint sites
the number of sites that are simultaneously optimized
Definition: als.h:112
std::function< value_t()> residual_f
the functional to calculate the current residual
Definition: als.h:60
Wrapper class for all ALS variants (dmrg etc.)
Definition: als.h:37
const TTTensor & b
global right-hand-side
Definition: als.h:51
bool preserveCorePosition
if true the core will be moved to its original position at the end
Definition: als.h:117
double operator()(const TTOperator &_A, TTTensor &_x, const TTTensor &_b, PerformanceData &_perfData=NoPerfData) const
Definition: als.h:177
TensorNetwork construct_local_RHS(ALSAlgorithmicData &_data) const
Definition: als.cpp:404
const TTOperator * A
global operator A
Definition: als.h:49
bool assumeSPD
if true the operator A will be assumed to be symmetric positive definite
Definition: als.h:118
size_t currIndex
position that is currently being optimized
Definition: als.h:62
double solve(const TTOperator *_Ap, TTTensor &_x, const TTTensor &_b, size_t _numHalfSweeps, value_t _convergenceEpsilon, PerformanceData &_perfData=NoPerfData) const
Definition: als.cpp:483
std::vector< size_t > targetRank
rank for the final x
Definition: als.h:52
value_t lastEnergy
energy at the end of last halfsweep
Definition: als.h:64
unsigned int uint
Definition: standard.h:51
double operator()(TTTensor &_x, const TTTensor &_b, size_t _numHalfSweeps, PerformanceData &_perfData=NoPerfData) const
Definition: als.h:201
#define XERUS_REQUIRE(condition, message)
Checks whether condition is true. logs an error otherwise via XERUS_LOG(error, message).
Definition: check.h:65
PerformanceData NoPerfData
void choose_energy_functional()
chooses the fitting energy functional according to settings and whether an operator A was given ...
Definition: als.cpp:255
std::function< void(const TensorNetwork &, std::vector< Tensor > &, const TensorNetwork &, const ALSAlgorithmicData &)> LocalSolver
the algorithm that is used to solve the local problems
Definition: als.h:123
bool check_for_end_of_sweep(ALSAlgorithmicData &_data, size_t _numHalfSweeps, value_t _convergenceEpsilon, PerformanceData &_perfData) const
Definition: als.cpp:426
bool useResidualForEndCriterion
calculates the residual to decide if the ALS converged. recommended if _perfdata is given...
Definition: als.h:116
TTTensor & x
current iterate x
Definition: als.h:50
double value_t
The type of values to be used by xerus.
Definition: basic.h:43
TensorNetwork localRhsSlice(size_t _pos)
Definition: als.cpp:202
static void ASD_solver(const TensorNetwork &_A, std::vector< Tensor > &_x, const TensorNetwork &_b, const ALSAlgorithmicData &_data)
Definition: als.cpp:73
const ALSVariant & ALS
the algorithm this data belongs to
Definition: als.h:48
const ALSVariant DMRG
default variant of the two-site DMRG algorithm for non-symmetric operators using the lapack solver ...
bool canonicalizeAtTheEnd
whether _x should be canonicalized at the end
Definition: als.h:57
const size_t FLAG_FINISHED_FULLSWEEP
Definition: als.h:110
size_t numHalfSweeps
maximum number of sweeps to perform. set to 0 for infinite
Definition: als.h:113
ALSAlgorithmicData(const ALSVariant &, const TTOperator *, TTTensor &, const TTTensor &)
Definition: als.cpp:322
double operator()(TTTensor &_x, const TTTensor &_b, PerformanceData &_perfData=NoPerfData) const
Definition: als.h:205
Direction direction
direction of current sweep
Definition: als.h:67
const ALSVariant DMRG_SPD
default variant of the two-site DMRG algorithm for symmetric positive-definite operators using the la...
const ALSVariant ALS_SPD
default variant of the single-site ALS algorithm for symmetric positive-definite operators using the ...
value_t convergenceEpsilon
default value for the change in the energy functional at which the ALS assumes it is converged ...
Definition: als.h:114
ContractedTNCache localOperatorCache
stacks for the local operator (either xAx or xAtAx)
Definition: als.h:53