LegCharge

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 Introduction to np_conserved for more details.

(This class is implemented in tenpy.linalg.charges but also imported in tenpy.linalg.np_conserved for convenience.)

Parameters
chargeinfoChargeInfo

The nature of the charge.

slices: 1D array_like, len(block_number+1)

A block with ‘qindex’ qi correspondes to the leg indices in slice(slices[qi], slices[qi+1]).

charges2D 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).

Notes

Instances of this class can be shared between different npc.Array. Thus, functions changing self.slices or self.charges must always make copies. Further they must set sorted and bunched to False (if they might not preserve them).

Attributes
ind_len: int

The number of indices for this leg.

block_number:

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

chinfoChargeInfo instance

The nature of the charge. Can be shared between LegCharges.

slicesndarray[np.intp_t,ndim=1] (block_number+1)

A block with ‘qindex’ qi correspondes to the leg indices in slice(self.slices[qi], self.slices[qi+1]). See get_slice().

chargesndarray[QTYPE_t,ndim=1] (block_number, chinfo.qnumber)

charges[qi] gives the charges for a block with ‘qindex’ qi. Note: the sign might be changed by qconj. See also get_charge().

qconj{-1, 1}

A flag telling whether the charge points inwards (+1) or outwards (-1). Whenever charges are added, they should be multiplied with their qconj value.

sortedbool

Whether the charges are guaranteed to be sorted.

bunchedbool

Whether the charges are guaranteed to be bunched.

Methods

bunch(self)

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

charge_sectors(self)

Return unique rows of self.charges.

conj(self)

Return a (shallow) copy with opposite self.qconj.

copy(self)

Return a (shallow) copy of self.

extend(self, extra)

Return a new LegCharge, which extends self with futher charges.

flip_charges_qconj(self)

Return a copy with both negative qconj and charges.

from_add_charge(legs[, chargeinfo])

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

from_change_charge(leg, charge, new_qmod[, …])

Remove a charge from a LegCharge.

from_drop_charge(leg[, charge, chargeinfo])

Remove a charge from a LegCharge.

from_qdict(chargeinfo, qdict[, qconj])

Create a LegCharge from qdict form.

from_qflat(chargeinfo, qflat[, qconj])

Create a LegCharge from qflat form.

from_qind(chargeinfo, slices, charges[, qconj])

Just a wrapper around self.__init__(), see class doc-string for parameters.

from_trivial(ind_len[, chargeinfo, qconj])

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

get_charge(self, qindex)

Return charge self.charges[qindex] * self.qconj for a given qindex.

get_qindex(self, flat_index)

Find qindex containing a flat index.

get_qindex_of_charges(self, charges)

Return the slice selecting the block for given charge values.

get_slice(self, qindex)

Return slice selecting the block for a given qindex.

is_blocked(self)

Returns whether self is blocked, i.e.

is_bunched(self)

Checks whether bunch() would change something.

is_sorted(self)

Returns whether self.charges is sorted lexiographically.

perm_flat_from_perm_qind(self, perm_qind)

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

perm_qind_from_perm_flat(self, perm_flat)

Convert flat permutation into qind permutation.

project(self, mask)

Return copy keeping only the indices specified by mask.

sort(self[, bunch])

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

test_contractible(self, other)

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

test_equal(self, other)

Test if charges are equal including qconj.

test_sanity(self)

Sanity check, raises ValueErrors, if something is wrong.

to_qdict(self)

Return charges in qdict form.

to_qflat(self)

Return charges in qflat form.

copy(self)[source]

Return a (shallow) copy of self.

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
chargeinfoChargeInfo

The nature of the charge.

qflatarray_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).

See also

sort

sorts by charges

bunch

bunches contiguous blocks of the same charge.

classmethod from_qind(chargeinfo, slices, charges, qconj=1)[source]

Just a wrapper around self.__init__(), see class doc-string for parameters.

See also

sort

sorts by charges

bunch

bunches contiguous blocks of the same charge.

classmethod from_qdict(chargeinfo, qdict, qconj=1)[source]

Create a LegCharge from qdict form.

Parameters
chargeinfoChargeInfo

The nature of the charge.

qdictdict

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
legsiterable of LegCharge

The legs for which the charges are to be combined/added.

chargeinfoChargeInfo

The ChargeInfo for all charges; create new if None.

Returns
combinedLegCharge

A LegCharge with the charges of both legs. Is neither sorted nor bunched!

classmethod from_drop_charge(leg, charge=None, chargeinfo=None)[source]

Remove a charge from a LegCharge.

Parameters
legLegCharge

The leg from which to drop/remove a charge.

chargeint | str

Number or name of the charge (within chinfo) which is to be dropped. None means dropping all charges.

chargeinfoChargeInfo

The ChargeInfo with charge dropped; create new if None.

Returns
droppedLegCharge

A LegCharge with the specified charge dropped. Is neither sorted nor bunched!

classmethod from_change_charge(leg, charge, new_qmod, new_name='', chargeinfo=None)[source]

Remove a charge from a LegCharge.

Parameters
legLegCharge

The leg from which to drop/remove a charge.

chargeint | str

Number or name of the charge (within chinfo) for which mod is to be changed.

new_qmodint

The new mod to be set for charge in the ChargeInfo.

new_namestr

The new name for charge.

chargeinfoChargeInfo

The ChargeInfo with charge changed; create new if None.

Returns
legLegCharge

A LegCharge with the specified charge changed. Is neither sorted nor bunched!

test_sanity(self)[source]

Sanity check, raises ValueErrors, if something is wrong.

conj(self)[source]

Return a (shallow) copy with opposite self.qconj.

Returns
conjugatedLegCharge

Shallow copy of self with flipped qconj. test_contractible() of self with conjugated will not raise an error.

flip_charges_qconj(self)[source]

Return a copy with both negative qconj and charges.

Returns
conj_chargesLegCharge

(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.

to_qflat(self)[source]

Return charges in qflat form.

to_qdict(self)[source]

Return charges in qdict form.

Raises ValueError, if not blocked.

is_blocked(self)[source]

Returns whether self is blocked, i.e. qindex map 1:1 to charge values.

is_sorted(self)[source]

Returns whether self.charges is sorted lexiographically.

is_bunched(self)[source]

Checks whether bunch() would change something.

test_contractible(self, other)[source]

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

Parameters
otherLegCharge

The LegCharge of the other leg condsidered for contraction.

Raises
ValueError

If the charges are incompatible for direct contraction.

See also

test_equal

self.test_contractible(other) just performs self.test_equal(other.conj()).

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 equal

  • the 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 Introduction to 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 and other are blocked or should be sorted.

test_equal(self, other)[source]

Test if charges are equal including qconj.

Check that all of the following conditions are met:

  • the ChargeInfo is equal

  • the 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 to self.test_contractible(other.conj()).

get_slice(self, qindex)[source]

Return slice selecting the block for a given qindex.

get_qindex(self, 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 index 5 corresponds to the second entry, qindex = 1 (since 5 is in [3:7]), and the index within the block would be 2 = 5 - 3.

Parameters
flat_indexint

A flat index of the leg. Negative index counts from behind.

Returns
qindexint

The qindex, i.e. the index of the block containing flat_index.

index_within_blockint

The index of flat_index within the block given by qindex.

get_qindex_of_charges(self, charges)[source]

Return the slice selecting the block for given charge values.

Inverse function of get_charge().

Parameters
charges1D array_like

Charge values for which the slice of the block is to be determined.

Returns
slice(i, j)slice

Slice of the charge values for

Raises
ValueErrorif the answer is not unique (because self is not blocked).
get_charge(self, qindex)[source]

Return charge self.charges[qindex] * self.qconj for a given qindex.

sort(self, 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
bunchbool

Whether self.bunch is called after sorting. If True, the leg is guaranteed to be fully blocked by charge.

Returns
perm_qindarray (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], use perm_flat = unsorted_leg.perm_flat_from_perm_qind(perm_qind)

sorted_copyLegCharge

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(self)[source]

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

Returns
idx1D array

idx[:-1] are the indices of the old qind which are kept, idx[-1] = old_block_number.

cpLegCharge

A new LegCharge with the same charges at given indices of the leg, but (possibly) shorter self.charges and self.slices.

See also

sort

sorts by charges, thus enforcing complete blocking in combination with bunch.

project(self, mask)[source]

Return copy keeping only the indices specified by mask.

Parameters
mask1D array(bool)

Whether to keep of the indices.

Returns
map_qind1D array

Map of qindices, such that qind_new = map_qind[qind_old], and map_qind[qind_old] = -1 for qindices projected out.

block_masks1D array

The bool mask for each of the remaining blocks.

projected_copyLegCharge

Copy of self with the qind projected by mask.

extend(self, extra)[source]

Return a new LegCharge, which extends self with futher charges.

This is needed to formally increase the dimension of an Array.

Parameters
extraLegCharge | 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_legLegCharge

Copy of self extended by the charge blocks of the extra leg.

charge_sectors(self)[source]

Return unique rows of self.charges.

Returns
chargesarray[QTYPE, ndim=2]

Rows are the rows of self.charges lexsorted and without duplicates.

perm_flat_from_perm_qind(self, perm_qind)[source]

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

perm_qind_from_perm_flat(self, perm_flat)[source]

Convert flat permutation into qind permutation.

Parameters
perm_flat1D array

A permutation acting on self, which doesn’t mix the blocks of qind.

Returns
perm_qind1D array

The permutation of self.qind described by perm_flat.

Raises
ValueError

If perm_flat mixes blocks of different qindex.