Accessing YASTN tensors#
Direct access to blocks#
Blocks of YASTN tensor can be simply accessed in the same way as standard dictionary. See example at Direct access to blocks.
- 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.ndarrayortorch.Tensorfor 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.ndarrayortorch.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. Specifiedlegsshould 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.Legunder 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.Tensorwith 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.Legunder 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.ndarrayortorch.tensor.
- Tensor.to_number() 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.
- 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\).