Site

Inheritance Diagram

Inheritance diagram of tenpy.networks.site.Site

Methods

Site.__init__(leg[, state_labels, sort_charge])

Site.add_op(name, op[, need_JW, hc, ...])

Add one on-site operators.

Site.change_charge([new_leg_charge, permute])

Change the charges of the site (in place).

Site.charge_to_JW_signs(charges)

Convert charge values to Jordan-Wigner parity.

Site.from_hdf5(hdf5_loader, h5gr, subpath)

Load instance from a HDF5 file.

Site.get_hc_op_name(name)

Return the hermitian conjugate of a given operator.

Site.get_op(name)

Return operator of given name.

Site.multiply_op_names(names)

Multiply operator names together.

Site.multiply_operators(operators)

Multiply local operators (possibly given by their names) together.

Site.op_needs_JW(name)

Whether an (composite) onsite operator is fermionic and needs a Jordan-Wigner string.

Site.remove_op(name)

Remove an added operator.

Site.rename_op(old_name, new_name)

Rename an added operator.

Site.save_hdf5(hdf5_saver, h5gr, subpath)

Export self into a HDF5 file.

Site.sort_charge([bunch])

Sort the leg charges (in place).

Site.state_index(label)

Return index of a basis state from its label.

Site.state_indices(labels)

Same as state_index(), but for multiple labels.

Site.test_sanity()

Sanity check, raises ValueErrors, if something is wrong.

Site.valid_opname(name)

Check whether 'name' labels a valid onsite-operator.

Class Attributes and Properties

Site.dim

Dimension of the local Hilbert space.

Site.onsite_ops

Dictionary of on-site operators for iteration.

class tenpy.networks.site.Site(leg, state_labels=None, sort_charge=False, **site_ops)[source]

Bases: Hdf5Exportable

Collects necessary information about a single local site of a lattice.

This class defines what the local basis states are: it provides the leg defining the charges of the physical leg for this site. Moreover, it stores (local) on-site operators, which are directly available as attribute, e.g., self.Sz is the Sz operator for the SpinSite. Alternatively, operators can be obtained with get_op(). The operator names Id and JW are reserved for the identity and Jordan-Wigner strings.

Warning

The order of the local basis can change depending on the charge conservation! This is a necessary feature since we need to sort the basis by charges for efficiency. We use the state_labels and perm to keep track of these permutations.

Changed in version 0.10: Add the option sort_charge. Right now the default behavior is False for backwards compatibility, but we will change it for Version 1.0 to True. For now, we raise a warning in cases where it can lead to changes. If you see this warning, just set the value explicitly to avoid breaking compatibility of existing data with future releases. Set it to False, if you already have data (for your particular model), that you want to be able to load/compare to. If you start a new project and don’t have data yet, set it to True. See also the breaking changes section in the release notes.

Parameters:
  • leg (LegCharge) – Charges of the physical states, to be used for the physical leg of MPS.

  • state_labels (None | list of str) – Optionally a label for each local basis states. None entries are ignored / not set.

  • **site_ops – Additional keyword arguments of the form name=op given to add_op(). The identity operator 'Id' is automatically included. If no 'JW' for the Jordan-Wigner string is given, 'JW' is set as an alias to 'Id'.

  • sort_charge (bool | None) – Whether sort_charge() should be called at the end of initialization. This is usually a good idea to reduce potential overhead when using charge conservation. Note that this might permute the order of the local basis states! For backwards compatibility with existing data, it is not (yet) enabled by default, but we started to warn about the behavior. Explicitly set sort_charge=False to disable the warning.

leg

Charges of the local basis states.

Type:

LegCharge

state_labels

(Optional) labels for the local basis states.

Type:

{str: int}

opnames

Labels of all onsite operators (i.e. self.op exists if 'op' in self.opnames). Note that get_op() allows arbitrary concatenations of them.

Type:

set

need_JW_string

Labels of all onsite operators that need a Jordan-Wigner string. Used in op_needs_JW() to determine whether an operator anticommutes or commutes with operators on other sites.

Type:

set

ops

Onsite operators are added directly as attributes to self. For example after self.add_op('Sz', Sz) you can use self.Sz for the Sz operator. All onsite operators have labels 'p', 'p*'.

Type:

Array

perm

Index permutation of the physical leg compared to conserve=None, i.e. OP_conserved = OP_nonconserved[np.ix_(perm,perm)] and perm[state_labels_conserved["some_state"]] == state_labels_nonconserved["some_state"].

Type:

1D array

JW_exponent

Exponents of the 'JW' operator, such that self.JW.to_ndarray() = np.diag(np.exp(1.j*np.pi* JW_exponent))

Type:

1D array

hc_ops

Mapping from operator names to their hermitian conjugates. Use get_hc_op_name() to obtain entries.

Type:

dict(str->str)

charge_to_JW_parity

If set, it is a list of factors, one per charge, such that (-1)**np.mod(np.sum(charges * charge_to_JW_parity, axis=-1), 2) is the Jordan-Wigner sign associated to a given set of charges. See charge_to_JW_signs() for more details. Often not defined at all or None, which indicates that charge information is not enough to extract the Jordan-Wigner signs, i.e., we might not have total fermion number as well-defined charge.

Type:

None | 1D array

used_sort_charge

Whether sort_charge() was called. Note that the default argument for permute_dense in add_op() changes to True in that case, to ensure a consistent use.

Type:

bool

Examples

The following generates a site for spin-1/2 with Sz conservation. Note that Sx = (Sp + Sm)/2 violates Sz conservation and is thus not a valid on-site operator.

>>> chinfo = npc.ChargeInfo([1], ['2*Sz'])
>>> ch = npc.LegCharge.from_qflat(chinfo, [1, -1])
>>> Sp = [[0, 1.], [0, 0]]
>>> Sm = [[0, 0], [1., 0]]
>>> Sz = [[0.5, 0], [0, -0.5]]
>>> site = tenpy.networks.site.Site(ch, ['up', 'down'], Splus=Sp, Sminus=Sm, Sz=Sz)
>>> print(site.Splus.to_ndarray())
[[0. 1.]
 [0. 0.]]
>>> print(site.get_op('Sminus').to_ndarray())
[[0. 0.]
 [1. 0.]]
>>> print(site.get_op('Splus Sminus').to_ndarray())
[[1. 0.]
 [0. 0.]]
change_charge(new_leg_charge=None, permute=None)[source]

Change the charges of the site (in place).

Parameters:
  • new_leg_charge (LegCharge | None) – The new charges to be used. If None, use trivial charges.

  • permute (ndarray | None) – The permutation applied to the physical leg, which also gets used to adjust state_labels and perm. If you sorted the previous leg with perm_qind, new_leg_charge = leg.sort(), use old_leg.perm_flat_from_perm_qind(perm_qind). Ignored if None.

sort_charge(bunch=True)[source]

Sort the leg charges (in place).

Parameters:

bunch (bool) – Whether to also group equal charges into larger blocks (usually a good idea).

Returns:

perm – The permutation

Return type:

1D ndarray

test_sanity()[source]

Sanity check, raises ValueErrors, if something is wrong.

property dim

Dimension of the local Hilbert space.

property onsite_ops

Dictionary of on-site operators for iteration.

Single operators are accessible as attributes.

add_op(name, op, need_JW=False, hc=None, permute_dense=None)[source]

Add one on-site operators.

Parameters:
  • name (str) – A valid python variable name, used to label the operator. The name under which op is added as attribute to self.

  • op (np.ndarray | Array) – A matrix acting on the local hilbert space representing the local operator. Dense numpy arrays are automatically converted to Array. LegCharges have to be [leg, leg.conj()]. We set labels 'p', 'p*'.

  • need_JW (bool) – Whether the operator needs a Jordan-Wigner string. If True, add name to need_JW_string.

  • hc (None | False | str) – The name for the hermitian conjugate operator, to be used for hc_ops. By default (None), try to auto-determine it. If False, disable adding entries to hc_ops.

  • permute_dense (bool | None) – Flag to enable/disable permutations when converting op from numpy to np_conserved arrays. If True, the operator is permuted with perm to account for permutations induced by sorting charges; False disables the permutations. By default (None), the value of used_sort_charge is used.

rename_op(old_name, new_name)[source]

Rename an added operator.

Parameters:
  • old_name (str) – The old name of the operator.

  • new_name (str) – The new name of the operator.

remove_op(name)[source]

Remove an added operator.

Parameters:

name (str) – The name of the operator to be removed.

state_index(label)[source]

Return index of a basis state from its label.

Parameters:

label (int | string) – either the index directly or a label (string) set before.

Returns:

state_index – the index of the basis state associated with the label.

Return type:

int

state_indices(labels)[source]

Same as state_index(), but for multiple labels.

get_op(name)[source]

Return operator of given name.

Parameters:

name (str) – The name of the operator to be returned. In case of multiple operator names separated by whitespace, we multiply them together to a single on-site operator (with the one on the right acting first).

Returns:

op – The operator given by name, with labels 'p', 'p*'. If name already was an npc Array, it’s directly returned.

Return type:

np_conserved

get_hc_op_name(name)[source]

Return the hermitian conjugate of a given operator.

Parameters:

name (str) – The name of the operator to be conjugated. Multiple operators separated by whitespace are interpreted as an operator product, exactly as get_op() does.

Returns:

hc_op_name – Operator name for the hermitian conjugate operator.

Return type:

str

op_needs_JW(name)[source]

Whether an (composite) onsite operator is fermionic and needs a Jordan-Wigner string.

Parameters:

name (str) – The name of the operator, as in get_op().

Returns:

needs_JW – Whether the operator needs a Jordan-Wigner string, judging from need_JW_string.

Return type:

bool

valid_opname(name)[source]

Check whether ‘name’ labels a valid onsite-operator.

Parameters:

name (str) – Label for the operator. Can be multiple operator(labels) separated by whitespace, indicating that they should be multiplied together.

Returns:

validTrue if name is a valid argument to get_op().

Return type:

bool

multiply_op_names(names)[source]

Multiply operator names together.

Join the operator names in names such that get_op returns the product of the corresponding operators.

Parameters:

names (list of str) – List of valid operator labels.

Returns:

combined_opname – A valid operator name Operator name representing the product of operators in names.

Return type:

str

multiply_operators(operators)[source]

Multiply local operators (possibly given by their names) together.

Parameters:

operators (list of {str | Array}) – List of valid operator names (to be translated with get_op()) or directly on-site operators in the form of npc arrays with 'p', 'p*' label. The operators are multiplied left-to-right.

Returns:

combined_operator – The product of the given operators in a left-to-right multiplication following the usual mathematical convention. For example, if operators=['Sz', 'Sp', 'Sx'], the final operator is equivalent to site.get_op('Sz Sp Sx'), with the 'Sx' operator acting first on any physical state.

Return type:

Array

charge_to_JW_signs(charges)[source]

Convert charge values to Jordan-Wigner parity.

Often, charge conservation contains the (parity of) the total fermion number. This information is enough to lift a Jordan-Wigner string applied on the left of a given bond to the virtual leg of an MPS: given the total parity number of fermions parity[alpha] = N_fermions[alpha] % 2 in each Schmidt state |alpha>, simply send |alpha> --> (-1)**parity[alpha] |alpha>. Given the charges values of the Schmidt states |alpha>, this function returns the corresponding (-1)**parity Jordan-Wigner signs.

Parameters:

charges (2D or 1D array) – Charge values, last dimension is len chinfo.qnumber. We choose the convention that these charge values correspond to an “incoming” leg with qconj=+1.

Returns:

Should only have values +1 or -1.

Return type:

JW_signs

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

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

This implementation saves the content of __dict__ with save_dict_content(), storing the format under the attribute 'format'.

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.