# MPSEnvironment¶

Inheritance Diagram

Methods

 MPSEnvironment.__init__(bra, ket[, cache]) Update short_term_keys for the cache and possibly preload tensors. Delete all partial contractions except the left-most LP and right-most RP. Delete stored part strictly to the left of site i. Delete stored part scrictly to the right of site i. MPSEnvironment.expectation_value(ops[, ...]) Expectation value  of (n-site) operator(s). Calculate the overlap by a full contraction of the network. MPSEnvironment.get_LP(i[, store]) Calculate LP at given site from nearest available one. Return number of physical sites in the contractions of get_LP(i). MPSEnvironment.get_RP(i[, store]) Calculate RP at given site from nearest available one. Return number of physical sites in the contractions of get_RP(i). Return data for (re-)initialization of the environment. Return True if LP left of site i is stored. Return True if RP right of site i is stored. MPSEnvironment.init_LP(i[, start_env_sites]) Build initial left part LP. MPSEnvironment.init_RP(i[, start_env_sites]) Build initial right part RP for an MPS/MPOEnvironment. (Re)initialize first LP and last RP from the given data. MPSEnvironment.set_LP(i, LP, age) Store part to the left of site i. MPSEnvironment.set_RP(i, RP, age) Store part to the right of site i. Sanity check, raises ValueErrors, if something is wrong.
class tenpy.networks.mps.MPSEnvironment(bra, ket, cache=None, **init_env_data)[source]

Bases: object

Stores partial contractions of $$<bra|Op|ket>$$ for local operators Op.

The network for a contraction $$<bra|Op|ket>$$ of a local operator Op, say exemplary at sites i, i+1 looks like:

|     .-----M[0]--- ... --M[1]---M[2]--- ... ->--.
|     |     |             |      |               |
|     |     |             |------|               |
|     LP[0] |             |  Op  |               RP[-1]
|     |     |             |------|               |
|     |     |             |      |               |
|     .-----N[0]*-- ... --N[1]*--N[2]*-- ... -<--.


Of course, we can also calculate the overlap <bra|ket> by using the special case Op = Id.

We use the following label convention (where arrows indicate qconj):

|    .-->- vR           vL ->-.
|    |                        |
|    LP                       RP
|    |                        |
|    .--<- vR*         vL* -<-.


To avoid recalculations of the whole network e.g. in the DMRG sweeps, we store the contractions up to some site index in this class. For bc='finite','segment', the very left and right part LP[0] and RP[L-1] are trivial and don’t change, but for bc='infinite' they are might be updated (by inserting another unit cell to the left/right).

The MPS bra and ket have to be in canonical form. All the environments are constructed without the singular values on the open bond. In other words, we contract left-canonical A to the left parts LP and right-canonical B to the right parts RP. Thus, the special case ket=bra should yield identity matrices for LP and RP.

Parameters
• bra (MPS) – The MPS to project on. Should be given in usual ‘ket’ form; we call conj() on the matrices directly. Stored in place, without making copies. If necessary to match charges, we call gauge_total_charge().

• ket (MPO | None) – The MPS on which the local operator acts. Stored in place, without making copies. If None, use bra.

• cache (DictCache | None) – Cache in which the tensors should be saved. If None, a new DictCache is generated.

• **init_env_data – Further keyword arguments with initializaiton data, as returned by get_initialization_data(). See initialize_first_LP_last_RP() for details on these parameters.

L

Number of physical sites involved into the Environment, i.e. the least common multiple of bra.L and ket.L.

Type

int

bra, ket

The two MPS for the contraction.

Type

MPS

dtype

The data type.

Type

type

_finite

Whether the boundary conditions of the MPS are finite.

Type

bool

cache

Cache for saving the environment tensors.

Type

DictCache

_LP_keys, _RP_keys

Map indices to keys for the cache.

Type

list of str

_LP

Left parts of the environment, len L. LP[i] contains the contraction strictly left of site i (or None, if we don’t have it calculated).

Type

list of {None | }

_RP

Right parts of the environment, len L. RP[i] contains the contraction strictly right of site i (or None, if we don’t have it calculated).

Type

list of {None | Array}

_LP_age

Used for book-keeping, how large the DMRG system grew: _LP_age[i] stores the number of physical sites invovled into the contraction network which yields self.get_LP(i).

Type

list of int | None

_RP_age

Used for book-keeping, how large the DMRG system grew: _RP_age[i] stores the number of physical sites invovled into the contraction network which yields self._RP[i].

Type

list of int | None

init_first_LP_last_RP(init_LP=None, init_RP=None, age_LP=0, age_RP=0, start_env_sites=0)[source]

(Re)initialize first LP and last RP from the given data.

Parameters
• init_LP (None | Array) – Initial very left part LP. If None, build one with :methinit_LP.

• init_RP (None | Array) – Initial very right part RP. If None, build one with init_RP().

• age_LP (int) – The number of physical sites involved into the contraction of init_LP.

• age_RP (int) – The number of physical sites involved into the contraction of init_RP.

• start_env_sites (int) – If init_LP and init_RP are not specified, contract each start_env_sites for them.

test_sanity()[source]

Sanity check, raises ValueErrors, if something is wrong.

init_LP(i, start_env_sites=0)[source]

Build initial left part LP.

If bra and ket are the same and in left canonical form, this is the environment you get contracting he overlaps from the left infinity up to bond left of site i.

For segment MPS, the segment_boundaries are read out (if set).

Parameters
• i (int) – Build LP left of site i.

• start_env_sites (int) – How many sites to contract to converge the init_LP; the initial age_LP.

Returns

init_LP – Identity contractible with the vL leg of ket.get_B(i), labels 'vR*', 'vR'.

Return type

Array

init_RP(i, start_env_sites=0)[source]

Build initial right part RP for an MPS/MPOEnvironment.

If bra and ket are the same and in right canonical form, this is the environment you get contracting from the right infinity up to bond right of site i.

For segment MPS, the segment_boundaries are read out (if set).

Parameters
• i (int) – Build RP right of site i.

• start_env_sites (int) – How many sites to contract to converge the init_RP; the initial age_RP.

Returns

init_RP – Identity contractible with the vR leg of ket.get_B(i), labels 'vL*', 'vL'.

Return type

Array

get_LP(i, store=True)[source]

Calculate LP at given site from nearest available one.

The returned LP_i corresponds to the following contraction, where the M’s and the N’s are in the ‘A’ form:

|     .-------M[0]--- ... --M[i-1]--->-   'vR'
|     |       |             |
|     LP[0]   |             |
|     |       |             |
|     .-------N[0]*-- ... --N[i-1]*--<-   'vR*'

Parameters
• i (int) – The returned LP will contain the contraction strictly left of site i.

• store (bool) – Wheter to store the calculated LP in self (True) or discard them (False).

Returns

LP_i – Contraction of everything left of site i, with labels 'vR*', 'vR' for bra, ket.

Return type

Array

get_RP(i, store=True)[source]

Calculate RP at given site from nearest available one.

The returned RP_i corresponds to the following contraction, where the M’s and the N’s are in the ‘B’ form:

|     'vL'  ->---M[i+1]-- ... --M[L-1]----.
|                |              |         |
|                |              |         RP[L-1]
|                |              |         |
|     'vL*' -<---N[i+1]*- ... --N[L-1]*---.

Parameters
• i (int) – The returned RP will contain the contraction strictly right of site i.

• store (bool) – Wheter to store the calculated RP in self (True) or discard them (False).

Returns

RP_i – Contraction of everything left of site i, with labels 'vL', 'vL*' for ket, bra.

Return type

Array

get_LP_age(i)[source]

Return number of physical sites in the contractions of get_LP(i).

Might be None.

get_RP_age(i)[source]

Return number of physical sites in the contractions of get_RP(i).

Might be None.

set_LP(i, LP, age)[source]

Store part to the left of site i.

set_RP(i, RP, age)[source]

Store part to the right of site i.

del_LP(i)[source]

Delete stored part strictly to the left of site i.

del_RP(i)[source]

Delete stored part scrictly to the right of site i.

clear()[source]

Delete all partial contractions except the left-most LP and right-most RP.

has_LP(i)[source]

Return True if LP left of site i is stored.

has_RP(i)[source]

Return True if RP right of site i is stored.

Update short_term_keys for the cache and possibly preload tensors.

Parameters
get_initialization_data(first=0, last=None)[source]

Return data for (re-)initialization of the environment.

Parameters
• first (int) – The first and last site, to the left and right of which we should return the environments. Defaults to 0 and L - 1.

• last (int) – The first and last site, to the left and right of which we should return the environments. Defaults to 0 and L - 1.

Returns

init_env_data – A dictionary with the following entries.

init_LP, init_RPArray

LP on the left of site first and RP on the right of site last, which can be used as init_LP and init_RP for the initialization of a new environment.

age_LP, age_RPint

The number of physical sites involved into the contraction yielding init_LP and init_RP, respectively.

Return type

dict

full_contraction(i0)[source]

Calculate the overlap by a full contraction of the network.

The full contraction of the environments gives the overlap <bra|ket>, taking into account MPS.norm of both bra and ket. For this purpose, this function contracts get_LP(i0+1, store=False) and get_RP(i0, store=False) with appropriate singular values in between.

Parameters

i0 (int) – Site index.

expectation_value(ops, sites=None, axes=None)[source]

Expectation value <bra|ops|ket> of (n-site) operator(s).

Calculates n-site expectation values of operators sandwiched between bra and ket. For examples the contraction for a two-site operator on site i would look like:

|          .--S--B[i]--B[i+1]--.
|          |     |     |       |
|          |     |-----|       |
|          LP[i] | op  |       RP[i+1]
|          |     |-----|       |
|          |     |     |       |
|          .--S--B*[i]-B*[i+1]-.


Here, the B are taken from ket, the B* from bra. The call structure is the same as for MPS.expectation_value().

Warning

In contrast to MPS.expectation_value(), this funciton does not normalize, thus it also takes into account MPS.norm of both bra and ket.

Parameters
• ops ((list of) { Array | str }) – The operators, for wich the expectation value should be taken, All operators should all have the same number of legs (namely 2 n). If less than len(sites) operators are given, we repeat them periodically. Strings (like 'Id', 'Sz') are translated into single-site operators defined by sites.

• sites (list) – List of site indices. Expectation values are evaluated there. If None (default), the entire chain is taken (clipping for finite b.c.)

• axes (None | (list of str, list of str)) – Two lists of each n leg labels giving the physical legs of the operator used for contraction. The first n legs are contracted with conjugated B, the second n legs with the non-conjugated B. None defaults to (['p'], ['p*']) for single site (n=1), or (['p0', 'p1', ... 'p{n-1}'], ['p0*', 'p1*', .... 'p{n-1}*']) for n > 1.

Returns

exp_vals – Expectation values, exp_vals[i] = <bra|ops[i]|ket>, where ops[i] acts on site(s) j, j+1, ..., j+{n-1} with j=sites[i].

Return type

1D ndarray