tensorcircuit.mps_base

FiniteMPS from tensornetwork with bug fixed

class tensorcircuit.mps_base.FiniteMPS(tensors: List[Any], center_position: int | None = None, canonicalize: bool | None = True, backend: str | AbstractBackend | None = None)[source]

Bases: FiniteMPS

__init__(tensors: List[Any], center_position: int | None = None, canonicalize: bool | None = True, backend: str | AbstractBackend | None = None) None[source]
Initialize a FiniteMPS. If canonicalize is True the state

is brought into canonical form, with BaseMPS.center_position at center_position. if center_position is None and canonicalize = True, BaseMPS.center_position is set to 0.

Parameters:
  • tensors – A list of Tensor objects.

  • center_position – The initial position of the center site.

  • canonicalize – If True the mps is canonicalized at initialization.

  • backend – The name of the backend that should be used to perform contractions. Available backends are currently ‘numpy’, ‘tensorflow’, ‘pytorch’, ‘jax’

apply_one_site_gate(gate: Any, site: int) None

Apply a one-site gate to an MPS. This routine will in general destroy any canonical form of the state. If a canonical form is needed, the user can restore it using FiniteMPS.position :param gate: a one-body gate :param site: the site where the gate should be applied

apply_transfer_operator(site: int, direction: str | int, matrix: Any) Any

Compute the action of the MPS transfer-operator at site site.

Parameters:
  • site – A site of the MPS

  • direction

    • if 1, ‘l’ or ‘left’: compute the left-action of the MPS transfer-operator at site on the input matrix.

    • if -1, ‘r’ or ‘right’: compute the right-action of the MPS transfer-operator at site on the input matrix

  • matrix – A rank-2 tensor or matrix.

Returns:

The result of applying the MPS transfer-operator to matrix

Return type:

Tensor

apply_two_site_gate(gate: Any, site1: int, site2: int, max_singular_values: int | None = None, max_truncation_err: float | None = None, center_position: int | None = None, relative: bool = False) Any[source]

Apply a two-site gate to an MPS. This routine will in general destroy any canonical form of the state. If a canonical form is needed, the user can restore it using FiniteMPS.position.

Parameters:
  • gate (Tensor) – A two-body gate.

  • site1 (int) – The first site where the gate acts.

  • site2 (int) – The second site where the gate acts.

  • max_singular_values (Optional[float], optional) – The maximum number of singular values to keep.

  • max_truncation_err (Optional[float], optional) – The maximum allowed truncation error.

  • center_position (Optional[int],optional) – An optional value to choose the MPS tensor at center_position to be isometric after the application of the gate. Defaults to site1. If the MPS is canonical (i.e.`BaseMPS.center_position != None`), and if the orthogonality center coincides with either site1 or site2, the orthogonality center will be shifted to center_position (site1 by default). If the orthogonality center does not coincide with (site1, site2) then MPS.center_position is set to None.

  • relative (bool) – Multiply max_truncation_err with the largest singular value.

Raises:

ValueError – “rank of gate is {} but has to be 4”, “site1 = {} is not between 0 <= site < N - 1 = {}”, “site2 = {} is not between 1 <= site < N = {}”,”Found site2 ={}, site1={}. Only nearest neighbor gates are currently supported”, “f center_position = {center_position} not f in {(site1, site2)} “, or “center_position = {}, but gate is applied at sites {}, {}. Truncation should only be done if the gate is applied at the center position of the MPS.”

Returns:

A scalar tensor containing the truncated weight of the truncation.

Return type:

Tensor

bond_dimension(bond) List

The bond dimension of bond

property bond_dimensions: List

A list of bond dimensions of BaseMPS

canonicalize(normalize: bool = True) number[source]

Bring the MPS into canonical form according to center_position. If center_position is None, the MPS is canonicalized with center_position = 0.

Parameters:

normalize – If True, normalize matrices when shifting the orthogonality center.

Returns:

The norm of the MPS.

Return type:

Tensor

center_position: int | None
check_canonical() Any[source]

Check whether the MPS is in the expected canonical form.

Returns:

The L2 norm of the vector of local deviations.

check_orthonormality(which: str, site: int) Any

Check orthonormality of tensor at site site.

Parameters:
  • which

    • if ‘l’ or ‘left’: check left orthogonality

    • if ‘r’ or ‘right’: check right orthogonality

  • site – The site of the tensor.

Returns:

The L2 norm of the deviation from identity.

Return type:

scalar Tensor

Raises:

ValueError – If which is different from ‘l’,’left’, ‘r’ or ‘right’.

conj() FiniteMPS[source]
copy() FiniteMPS[source]
property dtype: Type[number]
get_tensor(site: int) Any

Returns the Tensor object at site.

If site==len(self) - 1 BaseMPS.connector_matrix is absorbed fromt the right-hand side into the returned Tensor object.

Parameters:

site – The site for which to return the Node.

Returns:

The tensor at site.

Return type:

Tensor

left_envs(sites: Sequence[int]) Dict[source]

Compute left reduced density matrices for site sites. This returns a dict left_envs mapping sites (int) to Tensors. left_envs[site] is the left-reduced density matrix to the left of site site.

Parameters:

sites (list of int) – A list of sites of the MPS.

Returns:

The left-reduced density matrices

at each site in sites.

Return type:

dict mapping int to Tensor

left_transfer_operator(A, l, Abar)
measure_local_operator(ops: List[Any], sites: Sequence[int]) List[Any][source]

Measure the expectation value of local operators ops site sites.

Parameters:
  • ops (List[Tensor]) – A list Tensors of rank 2; the local operators to be measured.

  • sites (Sequence[int]) – Sites where ops act.

Returns:

measurements \(\langle\) ops[n]\(\rangle\) for n in sites

Return type:

List[Tensor]

measure_two_body_correlator(op1: Any, op2: Any, site1: int, sites2: Sequence[int]) List[Any][source]

Compute the correlator \(\langle\) op1[site1], op2[s]\(\rangle\) between site1 and all sites s in sites2. If s == site1, op2[s] will be applied first.

Parameters:
  • op1 (Tensor) – Tensor of rank 2; the local operator at site1.

  • op2 (Tensor) – Tensor of rank 2; the local operator at sites2.

  • site1 (int) – The site where op1 acts

  • sites2 (Sequence[int]) – Sites where operator op2 acts.

Returns:

Correlator \(\langle\) op1[site1], op2[s]\(\rangle\) for s \(\in\) sites2.

Return type:

List[Tensor]

property physical_dimensions: List

A list of physical Hilbert-space dimensions of BaseMPS

position(site: int, normalize: bool | None = True, D: int | None = None, max_truncation_err: float | None = None) number

Shift center_position to site.

Parameters:
  • site – The site to which FiniteMPS.center_position should be shifted

  • normalize – If True, normalize matrices when shifting.

  • D – If not None, truncate the MPS bond dimensions to D.

  • max_truncation_err – if not None, truncate each bond dimension, but keeping the truncation error below max_truncation_err.

Returns:

The norm of the tensor at FiniteMPS.center_position

Return type:

Tensor

Raises:

ValueError – If center_position is None.

classmethod random(d: List[int], D: List[int], dtype: Type[number], canonicalize: bool = True, backend: str | AbstractBackend | None = None) FiniteMPS[source]

Initialize a random FiniteMPS. The resulting state is normalized. Its center-position is at 0.

Parameters:
  • d – A list of physical dimensions.

  • D – A list of bond dimensions.

  • dtype – A numpy dtype.

  • backend – An optional backend.

Returns:

FiniteMPS

right_envs(sites: Sequence[int]) Dict[source]

Compute right reduced density matrices for site sites. This returns a dict `right_envs mapping sites (int) to Tensors. right_envs[site] is the right-reduced density matrix to the right of site site.

Parameters:

sites (list of int) – A list of sites of the MPS.

Returns:

The right-reduced density matrices

at each site in sites.

Return type:

dict mapping int to Tensor

right_transfer_operator(B, r, Bbar)
save(path: str)[source]