LegCharge

Inheritance Diagram

Inheritance diagram of tenpy.linalg.charges.LegCharge

Methods

LegCharge.__init__(chargeinfo, slices, charges)

LegCharge.bunch()

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

LegCharge.charge_sectors()

Return unique rows of self.charges.

LegCharge.conj()

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

LegCharge.copy()

Return a (shallow) copy of self.

LegCharge.extend(extra)

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

LegCharge.flip_charges_qconj()

Return a copy with both negative qconj and charges.

LegCharge.from_add_charge(legs[, chargeinfo])

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

LegCharge.from_change_charge(leg, charge, ...)

Remove a charge from a LegCharge.

LegCharge.from_drop_charge(leg[, charge, ...])

Remove a charge from a LegCharge.

LegCharge.from_hdf5(hdf5_loader, h5gr, subpath)

Load instance from a HDF5 file.

LegCharge.from_qdict(chargeinfo, qdict[, qconj])

Create a LegCharge from qdict form.

LegCharge.from_qflat(chargeinfo, qflat[, qconj])

Create a LegCharge from qflat form.

LegCharge.from_qind(chargeinfo, slices, charges)

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

LegCharge.from_trivial(ind_len[, ...])

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

LegCharge.get_block_sizes()

Return the sizes of the individual blocks.

LegCharge.get_charge(qindex)

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

LegCharge.get_qindex(flat_index)

Find qindex containing a flat index.

LegCharge.get_qindex_of_charges(charges)

Return the slice selecting the block for given charge values.

LegCharge.get_slice(qindex)

Return slice selecting the block for a given qindex.

LegCharge.is_blocked()

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

LegCharge.is_bunched()

Checks whether bunch() would change something.

LegCharge.is_sorted()

Returns whether self.charges is sorted lexicographically.

LegCharge.perm_flat_from_perm_qind(perm_qind)

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

LegCharge.perm_qind_from_perm_flat(perm_flat)

Convert flat permutation into qind permutation.

LegCharge.project(mask)

Return copy keeping only the indices specified by mask.

LegCharge.save_hdf5(hdf5_saver, h5gr, subpath)

Export self into a HDF5 file.

LegCharge.sort([bunch])

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

LegCharge.test_contractible(other)

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

LegCharge.test_equal(other)

Test if charges are equal including qconj.

LegCharge.test_sanity()

Sanity check, raises ValueErrors, if something is wrong.

LegCharge.to_qdict()

Return charges in qdict form.

LegCharge.to_qflat()

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 in tenpy.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 corresponds to the leg indices in slice(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).

ind_len

The number of indices for this leg.

Type:

int

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 corresponds to the leg indices in slice(self.slices[qi], self.slices[qi+1]). See get_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 also get_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}

sorted

Whether the charges are guaranteed to be sorted.

Type:

bool

bunched

Whether the charges are guaranteed to be bunched.

Type:

bool

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

copy()[source]

Return a (shallow) copy of self.

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 output format key "LegCharge". Possible choices are:

"blocks" (default)

Store slices and charges directly as datasets, and block_number, sorted, bunched as further attributes.

"compact"

A single array np.hstack([self.slices[:-1], self.slices[1:], self.charges]) as dataset "blockcharges", and block_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.

Parameters:
  • hdf5_saver (Hdf5Saver) – Instance of the saving engine.

  • h5gr (:class`Group`) – HDF5 group which is supposed to represent self.

  • subpath (str) – The name of h5gr with a '/' in the end.

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

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:
  • 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 if None.

Returns:

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

Return type:

LegCharge

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 if None.

Returns:

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

Return type:

LegCharge

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 if None.

Returns:

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

Return type:

LegCharge

test_sanity()[source]

Sanity check, raises ValueErrors, if something is wrong.

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:

LegCharge

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:

LegCharge

to_qflat()[source]

Return charges in qflat form.

to_qdict()[source]

Return charges in qdict form.

Raises ValueError, if not blocked.

is_blocked()[source]

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

is_sorted()[source]

Returns whether self.charges is sorted lexicographically.

is_bunched()[source]

Checks whether bunch() would change something.

test_contractible(other)[source]

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

Parameters:

other (LegCharge) – The LegCharge of the other leg considered 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 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 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 and other are blocked or should be sorted.

See also

test_equal

self.test_contractible(other) just performs self.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 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_block_sizes()[source]

Return the sizes of the individual blocks.

Returns:

sizes – The sizes of the individual blocks; sizes[i] = slices[i+1] - slices[i].

Return type:

ndarray, shape (block_number,)

get_slice(qindex)[source]

Return slice selecting the block for a given qindex.

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 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_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:

slice

:raises ValueError : if the answer is not unique (because self is not blocked).:

get_charge(qindex)[source]

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

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 permutation such that sorted_array[..., :] = unsorted_array[..., perm_flat], use perm_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) shorter self.charges and self.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], and map_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 further 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:

LegCharge

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.