LegCharge¶
full name: tenpy.linalg.charges.LegCharge
parent module:
tenpy.linalg.charges
type: class
Inheritance Diagram
Methods

Initialize self. 
Return a copy with bunched self.charges: form blocks for contiguous equal charges. 

Return unique rows of self.charges. 

Return a (shallow) copy with opposite 

Return a (shallow) copy of self. 


Return a new 
Return a copy with both negative qconj and charges. 


Add the (independent) charges of two or more legs to get larger qnumber. 

Remove a charge from a LegCharge. 

Remove a charge from a LegCharge. 

Load instance from a HDF5 file. 

Create a LegCharge from qdict form. 

Create a LegCharge from qflat form. 

Just a wrapper around self.__init__(), see class docstring for parameters. 

Create trivial (qnumber=0) LegCharge for given len of indices ind_len. 

Return charge 

Find qindex containing a flat index. 

Return the slice selecting the block for given charge values. 

Return slice selecting the block for a given qindex. 
Returns whether self is blocked, i.e. 

Checks whether 

Returns whether self.charges is sorted lexiographically. 


Convert a permutation of qind (acting on self) into a flat permutation. 

Convert flat permutation into qind permutation. 

Return copy keeping only the indices specified by mask. 

Export self into a HDF5 file. 

Return a copy of self sorted by charges (but maybe not bunched). 

Raises a ValueError if charges are incompatible for contraction with other. 

Test if charges are equal including qconj. 
Sanity check, raises ValueErrors, if something is wrong. 

Return charges in qdict form. 

Return charges in qflat form. 

class
tenpy.linalg.charges.
LegCharge
(chargeinfo, slices, charges, qconj=1)[source]¶ Bases:
object
Save the charge data associated to a leg of a tensor.
This class is more or less a wrapper around a 2D numpy array charges and a 1D array slices. See Charge conservation with np_conserved for more details.
(This class is implemented in
tenpy.linalg.charges
but also imported intenpy.linalg.np_conserved
for convenience.) Parameters
chargeinfo (
ChargeInfo
) – The nature of the charge.slices (1D array_like, len(block_number+1)) – A block with ‘qindex’
qi
correspondes to the leg indices inslice(slices[qi], slices[qi+1])
.charges (2D array_like, shape(block_number, chargeinfo.qnumber)) –
charges[qi]
gives the charges for a block with ‘qindex’qi
.qconj ({+1, 1}) – A flag telling whether the charge points inwards (+1, default) or outwards (1).

block_number
¶ The number of blocks, i.e., a ‘qindex’ for this leg is in
range(block_number)
.

chinfo
¶ The nature of the charge. Can be shared between LegCharges.
 Type
ChargeInfo
instance

slices
¶ A block with ‘qindex’
qi
correspondes to the leg indices inslice(self.slices[qi], self.slices[qi+1])
. Seeget_slice()
. Type
ndarray[np.intp_t,ndim=1] (block_number+1)

charges
¶ charges[qi]
gives the charges for a block with ‘qindex’qi
. Note: the sign might be changed by qconj. See alsoget_charge()
. Type
ndarray[QTYPE_t,ndim=1] (block_number, chinfo.qnumber)

qconj
¶ A flag telling whether the charge points inwards (+1) or outwards (1). Whenever charges are added, they should be multiplied with their qconj value.
 Type
{1, 1}
Notes
Instances of this class can be shared between different npc.Array. Thus, functions changing
self.slices
orself.charges
must always make copies. Further they must set sorted and bunched toFalse
(if they might not preserve them).
save_hdf5
(hdf5_saver, h5gr, subpath)[source]¶ Export self into a HDF5 file.
This method saves all the data it needs to reconstruct self with
from_hdf5()
.Checks
format
for an ouput format key"LegCharge"
. Possible choices are:"blocks"
(default)Store
slices
andcharges
directly as datasets, andblock_number
,sorted
,bunched
as further attributes."compact"
A single array
np.hstack([self.slices[:1], self.slices[1:], self.charges])
as dataset"blockcharges"
, andblock_number
,sorted
,bunched
as further attributes."flat"
Insufficient (!) to recover the exact blocks; saves only the array returned by
to_flat()
as dataset'charges'
.
The
ind_len
,qconj
, and the format parameter are saved as group attributes under the same names.chinfo
is always saved as subgroup.

classmethod
from_hdf5
(hdf5_loader, h5gr, subpath)[source]¶ Load instance from a HDF5 file.
This method reconstructs a class instance from the data saved with
save_hdf5()
. Parameters
hdf5_loader (
Hdf5Loader
) – Instance of the loading engine.h5gr (
Group
) – HDF5 group which is represent the object to be constructed.subpath (str) – The name of h5gr with a
'/'
in the end.
 Returns
obj – Newly generated class instance containing the required data.
 Return type
cls

classmethod
from_trivial
(ind_len, chargeinfo=None, qconj=1)[source]¶ Create trivial (qnumber=0) LegCharge for given len of indices ind_len.

classmethod
from_qflat
(chargeinfo, qflat, qconj=1)[source]¶ Create a LegCharge from qflat form.
Does neither bunch nor sort. We recommend to sort (and bunch) afterwards, if you expect that tensors using the LegCharge have entries at all positions compatible with the charges.
 Parameters
chargeinfo (
ChargeInfo
) – The nature of the charge.qflat (array_like (ind_len, qnumber)) – qnumber charges for each index of the leg on entry.
qconj ({1, 1}) – A flag telling whether the charge points inwards (+1) or outwards (1).

classmethod
from_qind
(chargeinfo, slices, charges, qconj=1)[source]¶ Just a wrapper around self.__init__(), see class docstring for parameters.

classmethod
from_qdict
(chargeinfo, qdict, qconj=1)[source]¶ Create a LegCharge from qdict form.
 Parameters
chargeinfo (
ChargeInfo
) – The nature of the charge.qdict (dict) – A dictionary mapping a tuple of charges to slices.

classmethod
from_add_charge
(legs, chargeinfo=None)[source]¶ Add the (independent) charges of two or more legs to get larger qnumber.
 Parameters
legs (iterable of
LegCharge
) – The legs for which the charges are to be combined/added.chargeinfo (
ChargeInfo
) – The ChargeInfo for all charges; create new ifNone
.
 Returns
combined – A LegCharge with the charges of both legs. Is neither sorted nor bunched!
 Return type

classmethod
from_drop_charge
(leg, charge=None, chargeinfo=None)[source]¶ Remove a charge from a LegCharge.
 Parameters
leg (
LegCharge
) – The leg from which to drop/remove a charge.charge (int  str) – Number or name of the charge (within chinfo) which is to be dropped.
None
means dropping all charges.chargeinfo (
ChargeInfo
) – The ChargeInfo with charge dropped; create new ifNone
.
 Returns
dropped – A LegCharge with the specified charge dropped. Is neither sorted nor bunched!
 Return type

classmethod
from_change_charge
(leg, charge, new_qmod, new_name='', chargeinfo=None)[source]¶ Remove a charge from a LegCharge.
 Parameters
leg (
LegCharge
) – The leg from which to drop/remove a charge.charge (int  str) – Number or name of the charge (within chinfo) for which mod is to be changed.
new_qmod (int) – The new mod to be set for charge in the
ChargeInfo
.new_name (str) – The new name for charge.
chargeinfo (
ChargeInfo
) – The ChargeInfo with charge changed; create new ifNone
.
 Returns
leg – A LegCharge with the specified charge changed. Is neither sorted nor bunched!
 Return type

conj
()[source]¶ Return a (shallow) copy with opposite
self.qconj
. Returns
conjugated – Shallow copy of self with flipped
qconj
.test_contractible()
of self with conjugated will not raise an error. Return type

flip_charges_qconj
()[source]¶ Return a copy with both negative qconj and charges.
 Returns
conj_charges – (Shallow) copy of self with negative qconj and charges, thus representing the very same charges.
test_equal()
of self with conj_charges will not raise an error. Return type

test_contractible
(other)[source]¶ Raises a ValueError if charges are incompatible for contraction with other.
 Parameters
other (
LegCharge
) – The LegCharge of the other leg condsidered for contraction. Raises
ValueError – If the charges are incompatible for direct contraction.
Notes
This function checks that two legs are ready for contraction. This is the case, if all of the following conditions are met:
the
ChargeInfo
is equalthe slices are equal
the charges are the same up to opposite signs
qconj
:self.charges * self.qconj =  other.charges * other.qconj
In general, there could also be a change of the total charge, see Charge conservation with np_conserved This special case is not considered here  instead use
gauge_total_charge()
, if a change of the charge is desired.If you are sure that the legs should be contractable, check whether the charges are actually valid or whether
self
andother
are blocked or should be sorted.See also
test_equal()
self.test_contractible(other)
just performsself.test_equal(other.conj())
.

test_equal
(other)[source]¶ Test if charges are equal including qconj.
Check that all of the following conditions are met:
the
ChargeInfo
is equalthe slices are equal
the charges are the same up to the signs
qconj
:self.charges * self.qconj = other.charges * other.qconj
See also
test_contractible()
self.test_equal(other)
is equivalent toself.test_contractible(other.conj())
.

get_qindex
(flat_index)[source]¶ Find qindex containing a flat index.
Given a flat index, to find the corresponding entry in an Array, we need to determine the block it is saved in. For example, if
slices = [[0, 3], [3, 7], [7, 12]]
, the flat index5
corresponds to the second entry,qindex = 1
(since 5 is in [3:7]), and the index within the block would be2 = 5  3
. Parameters
flat_index (int) – A flat index of the leg. Negative index counts from behind.
 Returns
qindex (int) – The qindex, i.e. the index of the block containing flat_index.
index_within_block (int) – The index of flat_index within the block given by qindex.

get_qindex_of_charges
(charges)[source]¶ Return the slice selecting the block for given charge values.
Inverse function of
get_charge()
. Parameters
charges (1D array_like) – Charge values for which the slice of the block is to be determined.
 Returns
slice(i, j) – Slice of the charge values for
 Return type
:raises ValueError : if the answer is not unique (because self is not blocked).:

sort
(bunch=True)[source]¶ Return a copy of self sorted by charges (but maybe not bunched).
If bunch=True, the returned copy is completely blocked by charge.
 Parameters
bunch (bool) – Whether self.bunch is called after sorting. If True, the leg is guaranteed to be fully blocked by charge.
 Returns
perm_qind (array (self.block_len,)) – The permutation of the qindices (before bunching) used for the sorting. To obtain the flat permuation such that
sorted_array[..., :] = unsorted_array[..., perm_flat]
, useperm_flat = unsorted_leg.perm_flat_from_perm_qind(perm_qind)
sorted_copy (
LegCharge
) – A shallow copy of self, with new qind sorted (and thus blocked if bunch) by charges.
See also
bunch()
enlarge blocks for contiguous qind of the same charges.
numpy.take()
can apply perm_flat to a given axis
tenpy.tools.misc.inverse_permutation()
returns inverse of a permutation

bunch
()[source]¶ Return a copy with bunched self.charges: form blocks for contiguous equal charges.
 Returns
idx (1D array) –
idx[:1]
are the indices of the old qind which are kept,idx[1] = old_block_number
.cp (
LegCharge
) – A new LegCharge with the same charges at given indices of the leg, but (possibly) shorterself.charges
andself.slices
.
See also
sort()
sorts by charges, thus enforcing complete blocking in combination with bunch.

project
(mask)[source]¶ Return copy keeping only the indices specified by mask.
 Parameters
mask (1D array(bool)) – Whether to keep of the indices.
 Returns
map_qind (1D array) – Map of qindices, such that
qind_new = map_qind[qind_old]
, andmap_qind[qind_old] = 1
for qindices projected out.block_masks (1D array) – The bool mask for each of the remaining blocks.
projected_copy (
LegCharge
) – Copy of self with the qind projected by mask.

extend
(extra)[source]¶ Return a new
LegCharge
, which extends self with futher charges.This is needed to formally increase the dimension of an Array.
 Parameters
extra (
LegCharge
 int) – By what to extend, i.e. the charges to be appended to self. An int stands for extending the length of the array by a single new block of that size and zero charges. Returns
extended_leg – Copy of self extended by the charge blocks of the extra leg.
 Return type

charge_sectors
()[source]¶ Return unique rows of self.charges.
 Returns
charges – Rows are the rows of self.charges lexsorted and without duplicates.
 Return type
array[QTYPE, ndim=2]

perm_flat_from_perm_qind
(perm_qind)[source]¶ Convert a permutation of qind (acting on self) into a flat permutation.

perm_qind_from_perm_flat
(perm_flat)[source]¶ Convert flat permutation into qind permutation.
 Parameters
perm_flat (1D array) – A permutation acting on self, which doesn’t mix the blocks of qind.
 Returns
perm_qind – The permutation of self.qind described by perm_flat.
 Return type
1D array
 Raises
ValueError – If perm_flat mixes blocks of different qindex.