Accessing YASTN tensors#
Direct access to blocks#
Blocks of YASTN tensor can be simply accessed in the same way as standard dictionary. For example,
def test_syntax_block_access(self):
legs = [yastn.Leg(config_U1, s=-1, t=(-1, 0, 1), D=(1, 2, 3)),
yastn.Leg(config_U1, s=1, t=(-1, 1, 2), D=(4, 5, 6)),
yastn.Leg(config_U1, s=-1, t=(-1, 1, 2), D=(7, 8, 9))]
a = yastn.rand(config=config_U1, legs=legs)
#
# directly access block with charges (1, 2, 1).
#
a[(1, 2, 1)]
- Tensor.__getitem__(key) numpy.ndarray | torch.tensor #
Block corresponding to a given charge combination.
The type of the returned tensor corresponds to specified backend, e.g.,
numpy.ndarray
ortorch.Tensor
for NumPy and PyTorch respectively. In case of diagonal tensor, the output is a 1D array.- Parameters:
key (Sequence[int] | Sequence[Sequence[int]]) – charges of the block.
Converting to dense tensors, scalars#
- Tensor.to_dense(legs=None, native=False, reverse=False) numpy.ndarray | torch.tensor #
Create dense tensor corresponding to the symmetric tensor.
The type of the returned tensor depends on the backend, i.e.
numpy.ndarray
ortorch.tensor
. Blocks are ordered according to increasing charges on each leg. It is possible to supply a list of additional charge sectors to be included by explictly specifyinglegs
. Specifiedlegs
should be consistent with current structure of the tensor. This allows to fill in extra zero blocks.- Parameters:
legs (dict[int, yastn.Leg]) – specify extra charge sectors on the legs by adding desired
yastn.Leg
under legs’s index into dictionary.native (bool) – output native tensor (ignoring meta-fusion of legs).
reverse (bool) – reverse the order in which blocks are sorted. Default order is ascending in values of block’s charges.
- Tensor.to_nonsymmetric(legs=None, native=False, reverse=False) yastn.Tensor #
Create equivalent
yastn.Tensor
with no explict symmetry. All blocks of the original tensor are accummulated into a single block.Blocks are ordered according to increasing charges on each leg. It is possible to supply a list of additional charge sectors to be included by explictly specifying
legs
. These legs should be consistent with current structure of the tensor. This allows to fill in extra zero blocks.Note
YASTN structure is redundant since resulting tensor is effectively just a single dense block. To obtain this single dense block directly, use
yastn.Tensor.to_dense()
.- Parameters:
legs (dict[int, yastn.Leg]) – specify extra charge sectors on the legs by adding desired
yastn.Leg
under legs’s index into dictionary.native (bool) – output native tensor (ignoring meta-fusion of legs).
reverse (bool) – reverse the order in which blocks are sorted. Default order is ascending in values of block’s charges.
- Tensor.to_numpy(legs=None, native=False, reverse=False) numpy.ndarray #
Create dense
numpy.ndarray`
corresponding to the symmetric tensor. Seeyastn.to_dense()
.
- Tensor.to_raw_tensor() numpy.ndarray | torch.tensor #
If the symmetric tensor has just a single non-empty block, return raw tensor representing that block.
The type of the returned tensor depends on the backend, i.e.
numpy.ndarray
ortorch.tensor
.
- Tensor.to_number(part=None) number #
Assuming the symmetric tensor has just a single non-empty block of total dimension one, return this element as a scalar.
The type of the scalar is given by the backend. For empty tensor returns 0.
Note
This operation preserves autograd.
- Parameters:
part (str) – if
'real'
, returns real part only.
- Tensor.item() float #
Assuming the symmetric tensor has just a single non-empty block of total dimension one, return this element as standard Python scalar.
For empty tensor returns \(0\).