mptensor  v0.3.0
Parallel Library for Tensor Network Methods
mptensor::Tensor< Matrix, C > Class Template Reference

Tensor class. The main object of mptensor. More...

#include <tensor.hpp>

Public Types

typedef C value_type
 double or complex More...
 
typedef Matrix< C > matrix_type
 type of Matrix class More...
 
typedef Matrix< C >::comm_type comm_type
 type of communicator. More...
 

Public Member Functions

const Shapeshape () const
 Shape of tensor. More...
 
size_t rank () const
 Rank of tensor. More...
 
size_t ndim () const
 Rank of tensor. More...
 
size_t local_size () const
 Size of local storage. More...
 
size_t get_upper_rank () const
 Upper rank for matrix representation. More...
 
const Axesget_axes_map () const
 Map of axes for lazy evaluation of transpose. More...
 
const Matrix< C > & get_matrix () const
 distributed Matrix More...
 
Matrix< C > & get_matrix ()
 distributed Matrix More...
 
const comm_typeget_comm () const
 Communicator. More...
 
int get_comm_size () const
 Size of communicator. More...
 
int get_comm_rank () const
 Rank of process. More...
 
Index global_index (size_t i) const
 Convert local index to global index. More...
 
void global_index_fast (size_t i, Index &idx) const
 Convert local index to global index fast. More...
 
void local_position (const Index &idx, int &comm_rank, size_t &local_idx) const
 Calculate rank which has the given global index and its local index. More...
 
const C & operator[] (size_t local_idx) const
 Const array subscript operator. More...
 
C & operator[] (size_t local_idx)
 Array subscript operator. More...
 
bool get_value (const Index &idx, C &val) const
 Get an element. More...
 
void set_value (const Index &idx, C val)
 Set an element. More...
 
void print_info (std::ostream &out, const std::string &tag="") const
 Output information of tensor. More...
 
void print_info_mpi (std::ostream &, const std::string &tag="") const
 Output information of tensor. More...
 
void save (const std::string &filename) const
 Save a tensor to files. More...
 
void load (const std::string &filename)
 Load a tensor from files. More...
 
Tensor< Matrix, C > & transpose (const Axes &axes)
 Transposition of tensor with lazy evaluation. More...
 
template<typename D >
Tensor< Matrix, C > & multiply_vector (const std::vector< D > &vec, size_t n_axes)
 Element-wise vector multiplication. More...
 
template<typename D0 , typename D1 >
Tensor< Matrix, C > & multiply_vector (const std::vector< D0 > &vec0, size_t n_axes0, const std::vector< D1 > &vec1, size_t n_axes1)
 Element-wise vector multiplication. More...
 
template<typename D0 , typename D1 , typename D2 >
Tensor< Matrix, C > & multiply_vector (const std::vector< D0 > &vec0, size_t n_axes0, const std::vector< D1 > &vec1, size_t n_axes1, const std::vector< D2 > &vec2, size_t n_axes2)
 Element-wise vector multiplication. More...
 
template<typename D0 , typename D1 , typename D2 , typename D3 >
Tensor< Matrix, C > & multiply_vector (const std::vector< D0 > &vec0, size_t n_axes0, const std::vector< D1 > &vec1, size_t n_axes1, const std::vector< D2 > &vec2, size_t n_axes2, const std::vector< D3 > &vec3, size_t n_axes3)
 Element-wise vector multiplication. More...
 
Tensor< Matrix, C > & set_slice (const Tensor &a, size_t n_axes, size_t i_begin, size_t i_end)
 Inverse of slice(). More...
 
Tensor< Matrix, C > & set_slice (const Tensor &a, const Index &index_begin, const Index &index_end)
 Inverse of slice(). More...
 
Tensor< lapack::Matrix, C > gather ()
 Gather all elements into a non-distributed tensor. More...
 
std::vector< C > flatten ()
 Return a copy of the flattened vector. More...
 
Tensor< Matrix, C > & operator+= (const Tensor &rhs)
 Addition assignment. More...
 
Tensor< Matrix, C > & operator-= (const Tensor &rhs)
 Subtraction assignment. More...
 
Tensor< Matrix, C > & operator*= (C rhs)
 Scalar-multiplication assignment. More...
 
Tensor< Matrix, C > & operator/= (C rhs)
 Scalar-division assignment. More...
 
Tensor< Matrix, C > & operator= (C rhs)
 Initialize all elements by rhs. More...
 
template<typename UnaryOperation >
Tensor< Matrix, C > & map (UnaryOperation op)
 Apply a unary operation to each element. More...
 
void prep_global_to_local () const
 Preprocess for fast conversion from global index to local one. More...
 
void prep_local_to_global () const
 Preprocess for fast conversion from local index to global one. More...
 
void make_l2g_map () const
 Preprocess for fast conversion from local index to global one. More...
 
void global_index_l2g_map (size_t lindex, size_t gindex[]) const
 Convert local index to global index using l2g_map. More...
 
void global_index_l2g_map_transpose (size_t lindex, const size_t axes_trans[], size_t index_new[]) const
 Convert local index to global index of transposed tensor using l2g_map. More...
 
void local_position_fast (size_t g_row, size_t g_col, int &comm_rank, size_t &local_idx) const
 
 Tensor ()
 Default constructor of tensor. More...
 
 Tensor (const Shape &)
 Constructor of tensor. More...
 
 Tensor (const comm_type &)
 Constructor of tensor. More...
 
 Tensor (const comm_type &, const Shape &)
 Constructor of tensor. More...
 
 Tensor (const comm_type &, const Shape &, size_t upper_rank)
 Constructor of tensor. More...
 
 Tensor (const comm_type &, const Tensor< lapack::Matrix, C > &)
 Constructor of tensor from non-distributed tensor. More...
 
 Tensor (const comm_type &, const std::vector< C > &)
 Constructor of tensor from non-distributed vector. More...
 

Detailed Description

template<template< typename > class Matrix, typename C>
class mptensor::Tensor< Matrix, C >

Tensor class. The main object of mptensor.

Member Typedef Documentation

◆ comm_type

template<template< typename > class Matrix, typename C >
typedef Matrix<C>::comm_type mptensor::Tensor< Matrix, C >::comm_type

type of communicator.

MPI_Comm or int.

◆ matrix_type

template<template< typename > class Matrix, typename C >
typedef Matrix<C> mptensor::Tensor< Matrix, C >::matrix_type

type of Matrix class

◆ value_type

template<template< typename > class Matrix, typename C >
typedef C mptensor::Tensor< Matrix, C >::value_type

double or complex

Member Function Documentation

◆ flatten()

template<template< typename > class Matrix, typename C >
std::vector< C > mptensor::Tensor< Matrix, C >::flatten

Return a copy of the flattened vector.

Returns
A flattened vector, which is global (not distriuted).
Attention
A flattened vector may require huge memory.

◆ gather()

template<template< typename > class Matrix, typename C >
Tensor< lapack::Matrix, C > mptensor::Tensor< Matrix, C >::gather

Gather all elements into a non-distributed tensor.

Returns
A non-distributed global tensor.
Attention
A non-distributed global tensor may require huge memory.

◆ get_axes_map()

template<template< typename > class Matrix, typename C >
const Axes & mptensor::Tensor< Matrix, C >::get_axes_map
inline

Map of axes for lazy evaluation of transpose.

axes_map stores an index mapping from a before-transposed tensor to an after-transposed tensor.

Let V a 3-leg tensor with shape=[10, 20, 30] and axes_map=[0, 1, 2]. When we transpose V as T = V.transpose(Axes(1, 2, 0));, T has shape=[20, 30, 10] and axes_map=[2, 0, 1]. Thus, axes_map satisfies axes_map[iV] = iT.

◆ get_comm()

template<template< typename > class Matrix, typename C >
const Tensor< Matrix, C >::comm_type & mptensor::Tensor< Matrix, C >::get_comm
inline

Communicator.

◆ get_comm_rank()

template<template< typename > class Matrix, typename C >
int mptensor::Tensor< Matrix, C >::get_comm_rank
inline

Rank of process.

◆ get_comm_size()

template<template< typename > class Matrix, typename C >
int mptensor::Tensor< Matrix, C >::get_comm_size
inline

Size of communicator.

◆ get_matrix() [1/2]

template<template< typename > class Matrix, typename C >
Matrix< C > & mptensor::Tensor< Matrix, C >::get_matrix
inline

distributed Matrix

◆ get_matrix() [2/2]

template<template< typename > class Matrix, typename C >
const Matrix< C > & mptensor::Tensor< Matrix, C >::get_matrix
inline

distributed Matrix

◆ get_upper_rank()

template<template< typename > class Matrix, typename C >
size_t mptensor::Tensor< Matrix, C >::get_upper_rank
inline

Upper rank for matrix representation.

◆ get_value()

template<template< typename > class Matrix, typename C >
bool mptensor::Tensor< Matrix, C >::get_value ( const Index idx,
C &  val 
) const

Get an element.

Parameters
[in]idxGlobal index.
[out]valValue of the element.
Returns
True if my process has the element specified by the global index.

◆ global_index()

template<template< typename > class Matrix, typename C >
Index mptensor::Tensor< Matrix, C >::global_index ( size_t  lindex) const

Convert local index to global index.

Parameters
[in]lindexLocal index
Returns
Global index

◆ global_index_fast()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::global_index_fast ( size_t  lindex,
Index gindex 
) const

Convert local index to global index fast.

Parameters
[in]lindexLocal index
[out]gindexGlobal index.
Warning
The size of gindex should be larger than the rank of tensor.

◆ global_index_l2g_map()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::global_index_l2g_map ( size_t  lindex,
size_t  gindex[] 
) const
inline

Convert local index to global index using l2g_map.

Parameters
[in]lindexLocal index
[out]gindexGlobal index.
Warning
The size of gindex should be larger than the rank of tensor.

◆ global_index_l2g_map_transpose()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::global_index_l2g_map_transpose ( size_t  lindex,
const size_t  axes_trans[],
size_t  index_new[] 
) const
inline

Convert local index to global index of transposed tensor using l2g_map.

Parameters
[in]lindexLocal index.
[in]axes_transaxes mapping (see below).
[out]index_newGlobal index of transposed tensor.
axes_trans[i] = axes_inv[axes_map[i]]

Here axes_inv is inverse of axes in transpose().

Warning
The size of axes_trans and index_new should be larger than the rank of tensor.

◆ load()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::load ( const std::string &  filename)

Load a tensor from files.

Rank-0 process reads an ASCII file with shape information whose name is given by filename . Every process restores tensor elements from a binary file filename.[rank_no].bin and an ASCII file filename.[rank_no].idx .

Parameters
filenameName of the base file.
Note
[rank_no] in the name of binary files has at least 4 digit, (ex. filename.0001.bin).

◆ local_position()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::local_position ( const Index index,
int &  comm_rank,
size_t &  local_idx 
) const

Calculate rank which has the given global index and its local index.

Parameters
[in]indexGlobal index.
[out]comm_rankRank which has the element at the global index.
[out]local_idxLocal index in comm_rank.

◆ local_position_fast()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::local_position_fast ( size_t  g_row,
size_t  g_col,
int &  comm_rank,
size_t &  local_idx 
) const
inline

◆ local_size()

template<template< typename > class Matrix, typename C >
size_t mptensor::Tensor< Matrix, C >::local_size
inline

Size of local storage.

◆ make_l2g_map()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::make_l2g_map
inline

Preprocess for fast conversion from local index to global one.

◆ map()

template<template< typename > class Matrix, typename C >
template<typename UnaryOperation >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::map ( UnaryOperation  op)

Apply a unary operation to each element.

Parameters
opUnary operation.
Returns
Result.

◆ multiply_vector() [1/4]

template<template< typename > class Matrix, typename C >
template<typename D >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector ( const std::vector< D > &  vec,
size_t  n_axes 
)

Element-wise vector multiplication.

For example, T.multiply_vector(v,1) is equivalent to \( T_{ijk} := v_j T_{ijk}. \)

Parameters
vecVector
n_axesAxes to multiply the vector
Attention
The size of vec should be larger than the bond dimension of n_axes.

◆ multiply_vector() [2/4]

template<template< typename > class Matrix, typename C >
template<typename D0 , typename D1 >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector ( const std::vector< D0 > &  vec0,
size_t  n_axes0,
const std::vector< D1 > &  vec1,
size_t  n_axes1 
)

Element-wise vector multiplication.

For example, T.multiply_vector(v,1,w,0) is equivalent to \( T_{ijk} := v_j w_i T_{ijk}. \)

Parameters
vec0Vector
n_axes0Axes to multiply the vector vec0.
vec1Vector
n_axes1Axes to multiply the vector vec1.
Attention
The size of vecX should be larger than the bond dimension of n_axesX.

◆ multiply_vector() [3/4]

template<template< typename > class Matrix, typename C >
template<typename D0 , typename D1 , typename D2 >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector ( const std::vector< D0 > &  vec0,
size_t  n_axes0,
const std::vector< D1 > &  vec1,
size_t  n_axes1,
const std::vector< D2 > &  vec2,
size_t  n_axes2 
)

Element-wise vector multiplication.

For example, T.multiply_vector(v,1,w,0,x,2) is equivalent to \( T_{ijkl} := v_j w_i x_k T_{ijkl}. \)

Parameters
vec0Vector
n_axes0Axes to multiply the vector vec0.
vec1Vector
n_axes1Axes to multiply the vector vec1.
vec2Vector
n_axes2Axes to multiply the vector vec2.
Attention
The size of vecX should be larger than the bond dimension of n_axesX.

◆ multiply_vector() [4/4]

template<template< typename > class Matrix, typename C >
template<typename D0 , typename D1 , typename D2 , typename D3 >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::multiply_vector ( const std::vector< D0 > &  vec0,
size_t  n_axes0,
const std::vector< D1 > &  vec1,
size_t  n_axes1,
const std::vector< D2 > &  vec2,
size_t  n_axes2,
const std::vector< D3 > &  vec3,
size_t  n_axes3 
)

Element-wise vector multiplication.

For example, T.multiply_vector(v,1,w,0,x,2,y,4) is equivalent to \( T_{ijklm} := v_j w_i x_k y_m T_{ijklm}. \)

Parameters
vec0Vector
n_axes0Axes to multiply the vector vec0.
vec1Vector
n_axes1Axes to multiply the vector vec1.
vec2Vector
n_axes2Axes to multiply the vector vec2.
vec3Vector
n_axes3Axes to multiply the vector vec3.
Attention
The size of vecX should be larger than the bond dimension of n_axesX.

◆ ndim()

template<template< typename > class Matrix, typename C >
size_t mptensor::Tensor< Matrix, C >::ndim
inline

Rank of tensor.

Note
Same as rank(). This function is for numpy compatibility with numpy.

◆ operator*=()

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator*= ( rhs)

Scalar-multiplication assignment.

◆ operator+=()

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator+= ( const Tensor< Matrix, C > &  rhs)

Addition assignment.

◆ operator-=()

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator-= ( const Tensor< Matrix, C > &  rhs)

Subtraction assignment.

◆ operator/=()

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator/= ( rhs)

Scalar-division assignment.

◆ operator=()

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::operator= ( rhs)

Initialize all elements by rhs.

◆ operator[]() [1/2]

template<template< typename > class Matrix, typename C >
C & mptensor::Tensor< Matrix, C >::operator[] ( size_t  local_idx)
inline

Array subscript operator.

Attention
This function does not check validity of local index.

◆ operator[]() [2/2]

template<template< typename > class Matrix, typename C >
const C & mptensor::Tensor< Matrix, C >::operator[] ( size_t  local_idx) const
inline

Const array subscript operator.

Attention
This function does not check validity of local index.

◆ prep_global_to_local()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::prep_global_to_local
inline

Preprocess for fast conversion from global index to local one.

◆ prep_local_to_global()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::prep_local_to_global
inline

Preprocess for fast conversion from local index to global one.

◆ print_info()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::print_info ( std::ostream &  out,
const std::string &  tag = "" 
) const

Output information of tensor.

Every process outputs information to output stream.

Parameters
outOutput stream.
tag(optional) A tag which is inserted at the head of the line.

◆ print_info_mpi()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::print_info_mpi ( std::ostream &  out,
const std::string &  tag = "" 
) const

Output information of tensor.

All processes output information of a tensor one by one.

Note
Since this function uses MPI_Barrier, all the processes should call this function at the same time.
Parameters
outOutput stream.
tag(optional) A tag which is inserted at the head of each line.

◆ rank()

template<template< typename > class Matrix, typename C >
size_t mptensor::Tensor< Matrix, C >::rank
inline

Rank of tensor.

◆ save()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::save ( const std::string &  filename) const

Save a tensor to files.

Rank-0 process creates an ASCII file with shape information whose name is given by filename . Every process saves tensor elements to a binary file filename.[rank_no].bin and an ASCII file filename.[rank_no].idx .

Parameters
filenameName of the base file.
Note
rank_no in the name of binary files has at least 4 digit, (ex. filename.0001.bin).

◆ set_slice() [1/2]

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::set_slice ( const Tensor< Matrix, C > &  a,
const Index index_begin,
const Index index_end 
)

Inverse of slice().

If index_begin[r]==index_end[r], this rank is not sliced. T.set_slice( slice(T,i,j), i,j) does nothing. For example, T.set_slice(A,Index(1,0,3),Index(4,0,6)); is equal to T[1:4,:,3:6]=A[:,:,:] in Python.

Parameters
[in]aA sliced tensor to be set.
[in]index_beginStart indices.
[in]index_endEnd indices.

◆ set_slice() [2/2]

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::set_slice ( const Tensor< Matrix, C > &  a,
size_t  n_axes,
size_t  i_begin,
size_t  i_end 
)

Inverse of slice().

T.set_slice( slice(T,r,i,j), r,i,j) does nothing. For example, T.set_slice(A,1,4,10); is equal to T[:,4:10,:]=A[:,:,:] in Python.

Parameters
[in]aA sliced tensor to be set.
[in]n_axessliced Axes.
[in]i_beginStart index of slice.
[in]i_endEnd index of slice.

◆ set_value()

template<template< typename > class Matrix, typename C >
void mptensor::Tensor< Matrix, C >::set_value ( const Index idx,
val 
)

Set an element.

If my process does not have the element, this function does nothing.

Parameters
[in]idxGlobal index.
[in]valValue of the element.

◆ shape()

template<template< typename > class Matrix, typename C >
const Shape & mptensor::Tensor< Matrix, C >::shape
inline

Shape of tensor.

◆ transpose()

template<template< typename > class Matrix, typename C >
Tensor< Matrix, C > & mptensor::Tensor< Matrix, C >::transpose ( const Axes axes)

Transposition of tensor with lazy evaluation.

When axes=[1,2,0], \( T_{ijk} \) is transformed into \( T_{jki} \).

Parameters
[in]axesOrder of axes.

The documentation for this class was generated from the following files: