44 template<
bool isOperator>
45 class TTNetwork final :
public TensorNetwork {
48 static constexpr
const size_t N = isOperator?2:1;
98 explicit TTNetwork(
const Tensor& _tensor,
const double _eps=
EPSILON,
const size_t _maxRank=std::numeric_limits<size_t>::max());
128 template<
class distribution=std::normal_distribution<value_t>,
class generator=std::mt19937_64>
130 const size_t numComponents = _dimensions.size()/
N;
131 XERUS_REQUIRE(_dimensions.size()%N==0,
"Illegal number of dimensions for TTOperator.");
132 XERUS_REQUIRE(_ranks.size()+1 == numComponents,
"Non-matching amount of ranks given to TTNetwork::random.");
133 XERUS_REQUIRE(!misc::contains(_dimensions,
size_t(0)),
"Trying to construct a TTTensor with dimension 0 is not possible.");
134 XERUS_REQUIRE(!misc::contains(_ranks,
size_t(0)),
"Trying to construct random TTTensor with rank 0 is illegal.");
141 for(
size_t i = 0; i < numComponents; ++i) {
142 const size_t leftRank = i==0 ? 1 : targetRank[i-1];
143 const size_t rightRank = (i==numComponents-1) ? 1 : targetRank[i];
146 const auto rndComp =
Tensor::random({leftRank, _dimensions[i], _dimensions[numComponents+i], rightRank}, _dist, _rnd);
147 result.set_component(i, rndComp);
149 const auto rndComp =
Tensor::random({leftRank, _dimensions[i], rightRank}, _dist, _rnd);
150 result.set_component(i, rndComp);
166 template<
class distribution=std::normal_distribution<value_t>,
class generator=std::mt19937_64>
168 return TTNetwork::random(_dimensions, std::vector<size_t>(_dimensions.size()/N-1, _rank), _dist, _rnd);
178 template<
class distribution=std::normal_distribution<value_t>,
class generator=std::mt19937_64>
180 const std::function<
void(
Tensor&)> &_modifySingularValues,
183 const size_t numComponents = _dimensions.size()/
N;
187 const Index i,j,k,l,m;
189 for (
size_t pos = 0; pos+1 < numComponents; ++pos) {
191 A(i,j^N,k^N,l) = result.component(pos)(i,j^
N,m) * result.component(pos+1)(m,k^
N,l);
193 (U(i&1,j),S(j,k),Vt(k,l&1)) =
SVD(A(i/2,l/2));
194 _modifySingularValues(S);
195 Vt(j,l&1) = S(j,k) * Vt(k,l&1);
196 result.set_component(pos, U);
197 result.set_component(pos+1, Vt);
198 result.assume_core_position(pos+1);
201 result.require_correct_format();
203 result.canonicalize_left();
220 template<bool B = isOperator, typename std::enable_if<B, int>::type = 0>
282 static std::vector<size_t>
reduce_to_maximal_ranks(std::vector<size_t> _ranks,
const std::vector<size_t>& _dimensions);
287 static size_t degrees_of_freedom(
const std::vector<size_t> &_dimensions,
const std::vector<size_t> &_ranks);
294 virtual void fix_mode(
const size_t _mode,
const size_t _slatePosition)
override;
296 virtual void resize_mode(
const size_t _mode,
const size_t _newDim,
const size_t _cutPos=~0ul)
override;
344 std::pair<TensorNetwork, TensorNetwork>
chop(
const size_t _position)
const;
352 void round(
const std::vector<size_t>& _maxRanks,
const double _eps =
EPSILON);
359 void round(
const size_t _maxRank);
366 void round(
const int _maxRank);
380 void soft_threshold(
const double _tau,
const bool _preventZero =
false);
387 void soft_threshold(
const std::vector<double>& _taus,
const bool _preventZero =
false);
394 std::vector<size_t>
ranks()
const;
402 size_t rank(
const size_t _i)
const;
413 void move_core(
const size_t _position,
const bool _keepRank=
false);
442 template<bool B = isOperator, typename std::enable_if<B, int>::type = 0>
444 const std::vector<size_t> shuffle({0,2,1,3});
445 for (
size_t n = 0; n <
degree()/
N; ++n) {
530 template<
bool isOperator>
542 template<
bool isOperator>
553 template<
bool isOperator>
564 template<
bool isOperator>
574 template<
bool isOperator>
582 template<
bool isOperator>
591 template<
bool isOperator>
600 template<
bool isOperator>
610 template<
bool isOperator>
616 template<
bool isOperator>
virtual void resize_mode(const size_t _mode, const size_t _newDim, const size_t _cutPos=~0ul) override
Resizes a specific mode of the TensorNetwork.
std::pair< TensorNetwork, TensorNetwork > chop(const size_t _position) const
Splits the TTNetwork into two parts by removing the node.
Header file for CHECK and REQUIRE macros.
static TTNetwork XERUS_warn_unused identity(const std::vector< size_t > &_dimensions)
: Construct a TTOperator with the given dimensions representing the identity.
Helper class to allow an intuitive syntax for SVD factorisations.
void assume_core_position(const size_t _pos)
stores _pos as the current core position without verifying of ensuring that this is the case ...
static constexpr const size_t N
The number of external links in each node, i.e. one for TTTensors and two for TTOperators.
Internal representation of an read and write and moveable indexed Tensor or TensorNetwork.
static bool specialized_contraction_f(std::unique_ptr< internal::IndexedTensorMoveable< TensorNetwork >> &_out, internal::IndexedTensorReadOnly< TensorNetwork > &&_me, internal::IndexedTensorReadOnly< TensorNetwork > &&_other)
void reshuffle(Tensor &_out, const Tensor &_base, const std::vector< size_t > &_shuffle)
: Performs a simple reshuffle. Much less powerfull then a full evaluate, but more efficient...
void transpose()
Transpose the TTOperator.
void soft_threshold(const double _tau, const bool _preventZero=false)
Applies the soft threshholding operation to all ranks.
Header file for the Index class.
Header file for the IndexedTensorMoveable class.
Header file defining lists of indexed tensors.
Header file for the standard contaienr support functions.
static Tensor XERUS_warn_unused random(DimensionTuple _dimensions, distribution &_dist=xerus::misc::defaultNormalDistribution, generator &_rnd=xerus::misc::randomEngine)
Constructs a dense Tensor with the given dimensions and uses the given random generator and distribut...
Tensor & component(const size_t _idx)
Complete access to a specific component of the TT decomposition.
Very general class used to represent arbitary tensor networks.
Internal representation of an readable indexed Tensor or TensorNetwork.
Header file for the classes defining factorisations of Tensors.
TTNetwork< true > TTOperator
static bool specialized_sum_f(std::unique_ptr< internal::IndexedTensorMoveable< TensorNetwork >> &_out, internal::IndexedTensorReadOnly< TensorNetwork > &&_me, internal::IndexedTensorReadOnly< TensorNetwork > &&_other)
Specialized TensorNetwork class used to represent TTTensor and TToperators.
The main namespace of xerus.
Class that handles simple (non-decomposed) tensors in a dense or sparse representation.
static TTNetwork XERUS_warn_unused random(const std::vector< size_t > &_dimensions, const std::vector< size_t > &_ranks, const std::function< void(Tensor &)> &_modifySingularValues, distribution &_dist=xerus::misc::defaultNormalDistribution, generator &_rnd=xerus::misc::randomEngine)
Random constructs a TTNetwork with the given dimensions and ranks.
thread_local std::normal_distribution< double > defaultNormalDistribution
virtual void specialized_evaluation(internal::IndexedTensorWritable< TensorNetwork > &&_me, internal::IndexedTensorReadOnly< TensorNetwork > &&_other) override
(Internal) Evaluates _other into _me. Requires that *this is the tensorObjectReadOnly of _me...
#define XERUS_INTERNAL_CHECK(condition, message)
Checks whether condition is true. logs a critical error otherwise via XERUS_LOG(fatal, message). and prints information on how to report a bug.
void canonicalize_right()
Move the core to the left.
bool canonicalized
Flag indicating whether the TTNetwork is canonicalized.
Tensor operator*(const value_t _factor, Tensor _tensor)
Calculates the entrywise multiplication of the Tensor _tensor with the constant _factor.
std::vector< size_t > DimensionTuple
: Represention of the dimensions of a Tensor.
size_t degree() const
Gets the degree of the TensorNetwork.
static void construct_train_from_full(TensorNetwork &_out, const Tensor &_A, const double _eps)
Constructs a TTNetwork in _out by decomposing the given Tensor _A.
#define XERUS_warn_unused
Header file for the Tensor class.
void move_core(const size_t _position, const bool _keepRank=false)
Move the core to a new position.
TTNetwork()
Constructs an order zero TTNetwork.
TTNetwork< false > TTTensor
std::vector< size_t > ranks() const
Gets the ranks of the TTNetwork.
FileFormat
possible file formats for tensor storage
static TTNetwork XERUS_warn_unused ones(const std::vector< size_t > &_dimensions)
: Returns a the (rank one) TT-Tensor with all entries equal to one.
void set_component(const size_t _idx, Tensor _T)
Sets a specific component of the TT decomposition.
void use_dense_representations()
Converts all components to use dense representations.
Tensor operator-(Tensor _lhs, const Tensor &_rhs)
Calculates the entrywise difference between _lhs and _rhs.
static TTNetwork XERUS_warn_unused random(std::vector< size_t > _dimensions, const std::vector< size_t > &_ranks, distribution &_dist=xerus::misc::defaultNormalDistribution, generator &_rnd=xerus::misc::randomEngine)
Transforms a given TensorNetwork to a TTNetwork.
TTNetwork & operator=(const TTNetwork &_other)=default
TTNetworks are default assignable.
virtual void fix_mode(const size_t _mode, const size_t _slatePosition) override
Fixes a specific mode to a specific value, effectively reducing the order by one. ...
#define XERUS_REQUIRE(condition, message)
Checks whether condition is true. logs an error otherwise via XERUS_LOG(error, message).
constexpr const value_t EPSILON
The default epsilon value in xerus.
bool exceeds_maximal_ranks() const
Tests whether any rank exceeds the theoretic maximal value it should have.
Tensor operator+(Tensor _lhs, const Tensor &_rhs)
Calculates the entrywise sum of _lhs and _rhs.
static TTNetwork XERUS_warn_unused random(const std::vector< size_t > &_dimensions, const size_t _rank, distribution &_dist=xerus::misc::defaultNormalDistribution, generator &_rnd=xerus::misc::randomEngine)
Random constructs a TTNetwork with the given dimensions and ranks limited by the given rank...
virtual bool specialized_sum(std::unique_ptr< internal::IndexedTensorMoveable< TensorNetwork >> &_out, internal::IndexedTensorReadOnly< TensorNetwork > &&_me, internal::IndexedTensorReadOnly< TensorNetwork > &&_other) const override
(Internal) Calculates the sum between _me and _other and stores the result in _out. Requires that *this is the tensorObjectReadOnly of _me.
static std::vector< size_t > reduce_to_maximal_ranks(std::vector< size_t > _ranks, const std::vector< size_t > &_dimensions)
Reduces the given ranks to the maximal possible.
static TTNetwork XERUS_warn_unused dirac(std::vector< size_t > _dimensions, const std::vector< size_t > &_position)
: Returns a TTNetwork with a single entry equals oen and all other zero.
virtual TensorNetwork * get_copy() const override
Returns a new copy of the network.
Abstract internal representation of an read and writeable indexed Tensor or TensorNetwork.
double value_t
The type of values to be used by xerus.
virtual value_t frob_norm() const override
Calculates the frobenious norm of the TensorNetwork.
void stream_writer(std::ostream &_stream, const TTNetwork< isOperator > &_obj, misc::FileFormat _format)
Pipes all information necessary to restore the current TensorNetwork into _stream.
virtual void require_correct_format() const override
Tests whether the network resembles that of a TTTensor and checks consistency with the underlying ten...
Class used to represent indices that can be used to write tensor calculations in index notation...
static TTNetwork XERUS_warn_unused kronecker(const std::vector< size_t > &_dimensions)
: Returns a TTNetwork representation of the kronecker delta.
std::vector< size_t > RankTuple
: Represention of the ranks of a TensorNetwork.
TTNetwork< isOperator > dyadic_product(const TTNetwork< isOperator > &_lhs, const TTNetwork< isOperator > &_rhs)
Computes the dyadic product of _lhs and _rhs.
size_t degrees_of_freedom()
calculates the number of degrees of freedom of the manifold of fixed tt-rank that the given TTTensor ...
thread_local std::mt19937_64 randomEngine
virtual bool specialized_contraction(std::unique_ptr< internal::IndexedTensorMoveable< TensorNetwork >> &_out, internal::IndexedTensorReadOnly< TensorNetwork > &&_me, internal::IndexedTensorReadOnly< TensorNetwork > &&_other) const override
(Internal) Calculates the contraction between _me and _other and stores the result in _out...
TTNetwork & operator-=(const TTNetwork &_other)
Subtracts the _other TTNetwork entrywise from this one.
Header file for the TensorNetwork class.
virtual void contract_unconnected_subnetworks() override
Contracts all nodes that are not connected to any external links.
Tensor entrywise_product(const Tensor &_A, const Tensor &_B)
calculates the entrywise product of two Tensors
TTNetwork & operator+=(const TTNetwork &_other)
Adds a given TTNetwork to this one.
size_t rank(const size_t _i) const
Gets the rank of a specific egde of the TTNetwork.
virtual void operator/=(const value_t _divisor) override
Calculates the entrywise divison of this TensorNetwork by a constant _divisor.
size_t num_ranks() const
Return the number of ranks, i.e. 0 for degree zero and degree()/N-1 otherwise.
void canonicalize_left()
Move the core to the left.
void round(const std::vector< size_t > &_maxRanks, const double _eps=EPSILON)
Reduce all ranks up to a given accuracy and maximal number.
const Tensor & get_component(const size_t _idx) const
Read access to a specific component of the TT decomposition.
size_t corePosition
The position of the core.
internal::IndexedTensorMoveable< Tensor > operator/(internal::IndexedTensorReadOnly< Tensor > &&_b, internal::IndexedTensorReadOnly< Tensor > &&_A)
void stream_reader(std::istream &_stream, TTNetwork< isOperator > &_obj, const misc::FileFormat _format)
Restores the TTNetwork from a stream of data.
Header file for the IndexedTensor class.
virtual void operator*=(const value_t _factor) override
Calculates the entrywise multiplication of this TensorNetwork with a constant _factor.