tensornetwork.ChargeArray¶
-
class
tensornetwork.
ChargeArray
(data: numpy.ndarray, charges: List[tensornetwork.block_sparse.charge.BaseCharge], flows: Union[numpy.ndarray, List[bool]], order: Optional[List[List[int]]] = None, check_consistency: Optional[bool] = False)¶ Base class for BlockSparseTensor. Stores a dense tensor together with its charge data. Attributes: * _charges: A list of
BaseCharge
objects, one for each leg of the tensor. * _flows: An np.ndarray of boolean dtype, storing the flow direction of eachleg.- data: A flat np.ndarray storing the actual tensor data.
- _order: A list of list, storing information on how tensor legs are transposed.
-
__init__
(data: numpy.ndarray, charges: List[tensornetwork.block_sparse.charge.BaseCharge], flows: Union[numpy.ndarray, List[bool]], order: Optional[List[List[int]]] = None, check_consistency: Optional[bool] = False) → None¶ Initialize a
ChargeArray
object.len(data)
has to be equal tonp.prod([c.dim for c in charges])
.Parameters: - data – An np.ndarray of the data.
- charges – A list of
BaseCharge
objects. - flows – The flows of the tensor indices,
False
for inflowing,True
for outflowing. - order – An optional order argument, determining the shape and order of the tensor.
- check_consistency – Perform error checks at initialization.
Methods
__init__
(data, charges, flows, List[bool]], …)Initialize a ChargeArray
object.conj
()Complex conjugate operation. random
(indices, …)Initialize a random ChargeArray object with data from a random uniform distribution. reshape
(shape, int]])Reshape tensor
intoshape.`ChargeArray.reshape
works the same as the dense version, with the notable exception that the tensor can only be reshaped into a form compatible with its elementary shape.todense
()Map the sparse tensor to dense storage. transpose
(order, 0]), shuffle)Transpose the tensor into the new order order
.ChargeArray.transpose_data
Attributes
T
charges
A list of list of BaseCharge
.dtype
The dtype of ChargeArray
.flat_charges
flat_flows
flat_order
The flattened ChargeArray._oder
.flows
A list of list of bool
.ndim
The number of tensor dimensions. shape
The dense shape of the tensor. sparse_shape
The sparse shape of the tensor. -
charges
¶ A list of list of
BaseCharge
. The charges, in the current shape and index order as determined byChargeArray._order
. :returns: List of List of BaseCharge
-
conj
() → tensornetwork.block_sparse.blocksparsetensor.ChargeArray¶ Complex conjugate operation. :returns: The conjugated tensor :rtype: ChargeArray
-
contiguous
() → tensornetwork.block_sparse.blocksparsetensor.ChargeArray¶ Transpose the tensor data such that the linear order of the elements in
ChargeArray.data
corresponds to the current order of tensor indices. Consider a tensor with current order given by_order=[[1,2],[3],[0]]
, i.e.data
was initialized according to order [0,1,2,3], and the tensor has since been reshaped and transposed. The linear order ofdata
does not match the desired order [1,2,3,0] of the tensor.contiguous
fixes this by permutingdata
into this order, transposing_charges
and_flows
, and changing_order
to[[0,1],[2],[3]]
.
-
dtype
¶ The dtype of
ChargeArray
.
-
flat_order
¶ The flattened
ChargeArray._oder
.
-
flows
¶ A list of list of
bool
. The flows, in the current shape and index order as determined byChargeArray._order
. :returns: List of List of bool
-
ndim
¶ The number of tensor dimensions.
-
classmethod
random
(indices: Union[Tuple[tensornetwork.block_sparse.index.Index], List[tensornetwork.block_sparse.index.Index]], boundaries: Optional[Tuple[float, float]] = (0.0, 1.0), dtype: Optional[Type[numpy.number]] = None) → tensornetwork.block_sparse.blocksparsetensor.ChargeArray¶ Initialize a random ChargeArray object with data from a random uniform distribution. :param indices: List of
Index
objects. :param boundaries: Tuple of interval boundaries for the random uniformdistribution.Parameters: dtype – An optional numpy dtype. The dtype of the ChargeArray Returns: ChargeArray
-
reshape
(shape: Sequence[Union[tensornetwork.block_sparse.index.Index, int]]) → tensornetwork.block_sparse.blocksparsetensor.ChargeArray¶ Reshape
tensor
intoshape. `ChargeArray.reshape
works the same as the dense version, with the notable exception that the tensor can only be reshaped into a form compatible with its elementary shape. The elementary shape is the shape determined by ChargeArray._charges. For example, while the following reshaping is possible for regular dense numpy tensor,` A = np.random.rand(6,6,6) np.reshape(A, (2,3,6,6)) `
the same code for ChargeArray` q1 = U1Charge(np.random.randint(0,10,6)) q2 = U1Charge(np.random.randint(0,10,6)) q3 = U1Charge(np.random.randint(0,10,6)) i1 = Index(charges=q1,flow=False) i2 = Index(charges=q2,flow=True) i3 = Index(charges=q3,flow=False) A=ChargeArray.randn(indices=[i1,i2,i3]) print(A.shape) #prints (6,6,6) A.reshape((2,3,6,6)) #raises ValueError `
raises aValueError
since (2,3,6,6) is incompatible with the elementary shape (6,6,6) of the tensor.Parameters: - tensor – A symmetric tensor.
- shape – The new shape. Can either be a list of
Index
or a list ofint
.
Returns: A new tensor reshaped into
shape
Return type:
-
shape
¶ The dense shape of the tensor. :returns: A tuple of
int
. :rtype: Tuple
-
size
¶ The dense size of the tensor, i.e. the total number of elements, including those which are zero by conservation of charge. :returns: The total number of elements. :rtype: int
-
todense
() → numpy.ndarray¶ Map the sparse tensor to dense storage.
-
transpose
(order: Sequence[int] = array([1, 0]), shuffle: Optional[bool] = False) → tensornetwork.block_sparse.blocksparsetensor.ChargeArray¶ Transpose the tensor into the new order
order
. Ifshuffle=False
no data-reshuffling is done. :param order: The new order of indices. :param shuffle: IfTrue
, reshuffle data.Returns: The transposed tensor. Return type: ChargeArray