quimb.tensor.tensor_1d_compress¶
Generic methods for compressing 1Dlike tensor networks, where the tensor network can locally have arbitrary structure and outer indices.
[x] the direct method
[x] the density matrix method
[x] the zipup method
[x] the zipup first method
[x] the 1site variational fit method, including sums of tensor networks
[x] the 2site variational fit method, including sums of tensor networks
[x] the local projector method (CTMRG and HOTRG style)
[x] the autofit method (via non1d specific ALS or autodiff)
Attributes¶
Classes¶
A labelled, tagged ndimensional array. The index labels are used 

A collection of (as yet uncontracted) Tensors. 
Functions¶

Apply a general a general tensor network representing an operator (has 

Compress an arbitrary geometry tensor network, with potentially multiple 

Create a tensor network with the same outer indices as 

Make sure 

Return a guaranteed unique, shortish identifier, optional appended 

Contract a collection of tensors into a scalar or tensor, automatically 

Check that 



Compress a 1Dlike tensor network using the 'direct' or 'naive' method, 

Compress any 1Dlike tensor network using the 'density matrix' method 

Compress a 1Dlike tensor network using the 'zipup' algorithm due to 

Compress this 1Dlike tensor network using the 'zipup first' algorithm, 

Core sweep of the 1site 1D fit algorithm. 

Core sweep of the 2site 1D fit algorithm. 

Compress any 1Dlike (can have multiple tensors per site) tensor network 

Compress a 1Dlike tensor network using the specified method. 

Apply an MPO to an MPS lazily, i.e. nothing is contracted, but the new 

Apply an MPO to an MPS using the boundary compression method, that is, 

Gate this MPS with an MPO, using the density matrix compression method. 

Apply an MPO to an MPS using the 'zipup' algorithm due to 

Apply an MPO to an MPS by first using the zipup method with a larger 

Gate an MPS with an MPO using the variational fitting or DMRGstyle 

Fit a MPS to a MPO applied to an MPS using geometry generic versions 

Apply an MPO to an MPS using local projectors, in the style of CTMRG 
Module Contents¶
 quimb.tensor.tensor_1d_compress.tensor_network_apply_op_vec(A, x, which_A='lower', contract=False, fuse_multibonds=True, compress=False, inplace=False, inplace_A=False, **compress_opts)[source]¶
Apply a general a general tensor network representing an operator (has
upper_ind_id
andlower_ind_id
) to a tensor network representing a vector (hassite_ind_id
), by contracting each pair of tensors at each site then compressing the resulting tensor network. How the compression takes place is determined by the type of tensor network passed in. The returned tensor network has the same site indices asx
, and it is thelower_ind_id
ofA
that is contracted.This is like performing
A.to_dense() @ x.to_dense()
, or the transpose thereof, depending on the value ofwhich_A
. Parameters:
A (TensorNetworkGenOperator) – The tensor network representing the operator.
x (TensorNetworkGenVector) – The tensor network representing the vector.
which_A ({"lower", "upper"}, optional) – Whether to contract the lower or upper indices of
A
with the site indices ofx
.contract (bool) – Whether to contract the tensors at each site after applying the operator, yielding a single tensor at each site.
fuse_multibonds (bool) – If
contract=True
, whether to fuse any multibonds after contracting the tensors at each site.compress (bool) – Whether to compress the resulting tensor network.
inplace (bool) – Whether to modify
x
, the input vector tensor network inplace.inplace_A (bool) – Whether to modify
A
, the operator tensor network inplace.compress_opts – Options to pass to
tn.compress
, wheretn
is the resulting tensor network, ifcompress=True
.
 Returns:
The same type as
x
. Return type:
 quimb.tensor.tensor_1d_compress.tensor_network_ag_compress(tn, max_bond, cutoff=1e10, method='localearly', site_tags=None, canonize=True, optimize='autohq', equalize_norms=False, inplace=False, **kwargs)[source]¶
Compress an arbitrary geometry tensor network, with potentially multiple tensors per site.
 Parameters:
tn (TensorNetwork) – The tensor network to compress. Every tensor should have exactly one of the site tags. Each site can have multiple tensors and output indices.
max_bond (int) – The maximum bond dimension to compress to.
cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing.
method ({'localearly', 'locallate', 'projector', 'superorthogonal', 'l2bp'}, optional) –
The compression method to use:
’localearly’: explicitly contract each site and interleave with immediate compression, see
tensor_network_ag_compress_local_early()
.’locallate’: explicitly contract all sites and then compress, see
tensor_network_ag_compress_local_late()
.’projector’: use locally computed projectors, see
tensor_network_ag_compress_projector()
.’superorthogonal’: use the ‘superorthogonal’ gauge, see
tensor_network_ag_compress_superorthogonal()
.’l2bp’: use lazy 2norm belief propagation, see
tensor_network_ag_compress_l2bp()
.
site_tags (sequence of str, optional) – The tags to use to group the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site.canonize (bool, optional) – Whether to perform canonicalization, pseudo or otherwise depending on the method, before compressing.
optimize (str, optional) – The contraction path optimizer to use.
equalize_norms (bool or float, optional) – Whether to equalize the norms of the tensors after compression. If an explicit value is give, then the norms will be set to that value, and the overall scaling factor will be accumulated into .exponent.
inplace (bool, optional) – Whether to perform the compression inplace.
kwargs – Supplied to the chosen compression method.
 quimb.tensor.tensor_1d_compress.TN_matching(tn, max_bond, site_tags=None, fill_fn=None, dtype=None, **randn_opts)[source]¶
Create a tensor network with the same outer indices as
tn
but with a single tensor per site with bond dimensionmax_bond
between each connected site. Generally to be used as an initial guess for fitting. Parameters:
tn (TensorNetwork) – The tensor network to match, it can have arbitrary local structure and output indices, as long as
site_tags
effectively partitions it.max_bond (int) – The bond dimension to use between each site.
site_tags (sequence of str, optional) – The tags to use to select the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.dtype (dtype, optional) – The data type to use for the new tensors, if not given uses the same as the original tensors.
randn_opts – Supplied to
randn()
.
 Return type:
 class quimb.tensor.tensor_1d_compress.Tensor(data=1.0, inds=(), tags=None, left_inds=None)[source]¶
A labelled, tagged ndimensional array. The index labels are used instead of axis numbers to identify dimensions, and are preserved through operations. The tags are used to identify the tensor within networks, and are combined when tensors are contracted together.
 Parameters:
data (numpy.ndarray) – The ndimensional data.
inds (sequence of str) – The index labels for each dimension. Must match the number of dimensions of
data
.tags (sequence of str, optional) – Tags with which to identify and group this tensor. These will be converted into a
oset
.left_inds (sequence of str, optional) – Which, if any, indices to group as ‘left’ indices of an effective matrix. This can be useful, for example, when automatically applying unitary constraints to impose a certain flow on a tensor network but at the atomistic (Tensor) level.
Examples
Basic construction:
>>> from quimb import randn >>> from quimb.tensor import Tensor >>> X = Tensor(randn((2, 3, 4)), inds=['a', 'b', 'c'], tags={'X'}) >>> Y = Tensor(randn((3, 4, 5)), inds=['b', 'c', 'd'], tags={'Y'})
Indices are automatically aligned, and tags combined, when contracting:
>>> X @ Y Tensor(shape=(2, 5), inds=('a', 'd'), tags={'Y', 'X'})
 __slots__ = ('_data', '_inds', '_tags', '_left_inds', '_owners')¶
 get_params()[source]¶
A simple function that returns the ‘parameters’ of the underlying data array. This is mainly for providing an interface for ‘structured’ arrays e.g. with block sparsity to interact with optimization.
 set_params(params)[source]¶
A simple function that sets the ‘parameters’ of the underlying data array. This is mainly for providing an interface for ‘structured’ arrays e.g. with block sparsity to interact with optimization.
 copy(deep=False, virtual=False)[source]¶
Copy this tensor.
Note
By default (
deep=False
), the underlying array will not be copied.
 property data¶
 property inds¶
 property tags¶
 property left_inds¶
 property owners¶
 add_owner(tn, tid)[source]¶
Add
tn
as owner of this Tensor  it’s tag and ind maps will be updated whenever this tensor is retagged or reindexed.
 check_owners()[source]¶
Check if this tensor is ‘owned’ by any alive TensorNetworks. Also trim any weakrefs to dead TensorNetworks.
 modify(**kwargs)[source]¶
Overwrite the data of this tensor in place.
 Parameters:
data (array, optional) – New data.
apply (callable, optional) – A function to apply to the current data. If data is also given this is applied subsequently.
inds (sequence of str, optional) – New tuple of indices.
tags (sequence of str, optional) – New tags.
left_inds (sequence of str, optional) – New grouping of indices to be ‘on the left’.
 apply_to_arrays(fn)[source]¶
Apply the function
fn
to the underlying data array(s). This is meant for changing how the raw arrays are backed (e.g. converting between dtypes or libraries) but not their ‘numerical meaning’.
 isel(selectors, inplace=False)[source]¶
Select specific values for some dimensions/indices of this tensor, thereby removing them. Analogous to
X[:, :, 3, :, :]
with arrays. The indices to select from can be specified either by integer, in which case the correspoding index is removed, or by a slice. Parameters:
 Return type:
Examples
>>> T = rand_tensor((2, 3, 4), inds=('a', 'b', 'c')) >>> T.isel({'b': 1}) Tensor(shape=(2, 4), inds=('a', 'c'), tags=())
See also
 add_tag(tag)[source]¶
Add a tag or multiple tags to this tensor. Unlike
self.tags.add
this also updates anyTensorNetwork
objects viewing thisTensor
.
 expand_ind(ind, size, mode=None, rand_strength=None, rand_dist='normal')[source]¶
Inplace increase the size of the dimension of
ind
, the new array entries will be filled with zeros by default. Parameters:
name (str) – Name of the index to expand.
size (int, optional) – Size of the expanded index.
mode ({None, 'zeros', 'repeat', 'random'}, optional) – How to fill any new array entries. If
'zeros'
then fill with zeros, if'repeat'
then repeatedly tile the existing entries. If'random'
then fill with random entries drawn fromrand_dist
, multiplied byrand_strength
. IfNone
then select from zeros or random depening on nonzerorand_strength
.rand_strength (float, optional) – If
mode='random'
, a multiplicative scale for the random entries, defaulting to 1.0. Ifmode is None
then supplying a nonzero value here triggersmode='random'
.rand_dist ({'normal', 'uniform', 'exp'}, optional) – If
mode='random'
, the distribution to draw the random entries from.
 new_ind(name, size=1, axis=0, mode=None, rand_strength=None, rand_dist='normal')[source]¶
Inplace add a new index  a named dimension. If
size
is specified to be greater than one then the new array entries will be filled with zeros. Parameters:
name (str) – Name of the new index.
size (int, optional) – Size of the new index.
axis (int, optional) – Position of the new index.
mode ({None, 'zeros', 'repeat', 'random'}, optional) – How to fill any new array entries. If
'zeros'
then fill with zeros, if'repeat'
then repeatedly tile the existing entries. If'random'
then fill with random entries drawn fromrand_dist
, multiplied byrand_strength
. IfNone
then select from zeros or random depening on nonzerorand_strength
.rand_strength (float, optional) – If
mode='random'
, a multiplicative scale for the random entries, defaulting to 1.0. Ifmode is None
then supplying a nonzero value here triggersmode='random'
.rand_dist ({'normal', 'uniform', 'exp'}, optional) – If
mode='random'
, the distribution to draw the random entries from.
See also
 new_ind_with_identity(name, left_inds, right_inds, axis=0)[source]¶
Inplace add a new index, where the newly stacked array entries form the identity from
left_inds
toright_inds
. Selecting 0 or 1 for the new indexname
thus is like ‘turning off’ this tensor if viewed as an operator. Parameters:
name (str) – Name of the new index.
left_inds (tuple[str]) – Names of the indices forming the left hand side of the operator.
right_inds (tuple[str]) – Names of the indices forming the right hand side of the operator. The dimensions of these must match those of
left_inds
.axis (int, optional) – Position of the new index.
 new_ind_pair_with_identity(new_left_ind, new_right_ind, d, inplace=False)[source]¶
Expand this tensor with two new indices of size
d
, by taking an (outer) tensor product with the identity operator. The two new indices are added as axes at the start of the tensor.
 property H¶
 Conjugate this tensors data (does nothing to indices).
 property shape¶
 The size of each dimension.
 property ndim¶
 The number of dimensions.
 property size¶
 The total number of array elements.
 property dtype¶
 The data type of the array elements.
 property backend¶
 The backend inferred from the data.
Get the total size of the shared index(es) with
other
.
 transpose(*output_inds, inplace=False)[source]¶
Transpose this tensor  permuting the order of both the data and the indices. This operation is mainly for ensuring a certain data layout since for most operations the specific order of indices doesn’t matter.
Note to compute the tranditional ‘transpose’ of an operator within a contraction for example, you would just use reindexing not this.
 Parameters:
 Returns:
tt – The transposed tensor.
 Return type:
See also
 transpose_like(other, inplace=False)[source]¶
Transpose this tensor to match the indices of
other
, allowing for one index to be different. E.g. ifself.inds = ('a', 'b', 'c', 'x')
andother.inds = ('b', 'a', 'd', 'c')
then ‘x’ will be aligned with ‘d’ and the output inds will be('b', 'a', 'x', 'c')
 Parameters:
 Returns:
tt – The transposed tensor.
 Return type:
See also
 moveindex(ind, axis, inplace=False)[source]¶
Move the index
ind
to positionaxis
. Liketranspose
, this permutes the order of both the data and the indices and is mainly for ensuring a certain data layout since for most operations the specific order of indices doesn’t matter.
 trace(left_inds, right_inds, preserve_tensor=False, inplace=False)[source]¶
Trace index or indices
left_inds
withright_inds
, removing them. Parameters:
left_inds (str or sequence of str) – The left indices to trace, order matching
right_inds
.right_inds (str or sequence of str) – The right indices to trace, order matching
left_inds
.preserve_tensor (bool, optional) – If
True
, a tensor will be returned even if no indices remain.inplace (bool, optional) – Perform the trace inplace.
 Returns:
z
 Return type:
Tensor or scalar
 vector_reduce(ind, v, inplace=False)[source]¶
Contract the vector
v
with the indexind
of this tensor, removing it.
 collapse_repeated(inplace=False)[source]¶
Take the diagonals of any repeated indices, such that each index only appears once.
 gate(G, ind, preserve_inds=True, inplace=False)[source]¶
Gate this tensor  contract a matrix into one of its indices without changing its indices. Unlike
contract
,G
is a raw array and the tensor remains with the same set of indices. Parameters:
G (2D array_like) – The matrix to gate the tensor index with.
ind (str) – Which index to apply the gate to.
 Return type:
Examples
Create a random tensor of 4 qubits:
>>> t = qtn.rand_tensor( ... shape=[2, 2, 2, 2], ... inds=['k0', 'k1', 'k2', 'k3'], ... )
Create another tensor with an X gate applied to qubit 2:
>>> Gt = t.gate(qu.pauli('X'), 'k2')
The contraction of these two tensors is now the expectation of that operator:
>>> t.H @ Gt 4.108910576149794
 singular_values(left_inds, method='svd')[source]¶
Return the singular values associated with splitting this tensor according to
left_inds
. Parameters:
left_inds (sequence of str) – A subset of this tensors indices that defines ‘left’.
method ({'svd', 'eig'}) – Whether to use the SVD or eigenvalue decomposition to get the singular values.
 Returns:
The singular values.
 Return type:
1darray
 entropy(left_inds, method='svd')[source]¶
Return the entropy associated with splitting this tensor according to
left_inds
.
 retag(retag_map, inplace=False)[source]¶
Rename the tags of this tensor, optionally, inplace.
 Parameters:
retag_map (dictlike) – Mapping of pairs
{old_tag: new_tag, ...}
.inplace (bool, optional) – If
False
(the default), a copy of this tensor with the changed tags will be returned.
 reindex(index_map, inplace=False)[source]¶
Rename the indices of this tensor, optionally inplace.
 Parameters:
index_map (dictlike) – Mapping of pairs
{old_ind: new_ind, ...}
.inplace (bool, optional) – If
False
(the default), a copy of this tensor with the changed inds will be returned.
 fuse(fuse_map, inplace=False)[source]¶
Combine groups of indices into single indices.
 Parameters:
fuse_map (dict_like or sequence of tuples.) – Mapping like:
{new_ind: sequence of existing inds, ...}
or an ordered mapping like[(new_ind_1, old_inds_1), ...]
in which case the output tensor’s fused inds will be ordered. In both cases the new indices are created at the minimum axis of any of the indices that will be fused. Returns:
The transposed, reshaped and relabeled tensor.
 Return type:
 unfuse(unfuse_map, shape_map, inplace=False)[source]¶
Reshape single indices into groups of multiple indices
 Parameters:
unfuse_map (dict_like or sequence of tuples.) – Mapping like:
{existing_ind: sequence of new inds, ...}
or an ordered mapping like[(old_ind_1, new_inds_1), ...]
in which case the output tensor’s new inds will be ordered. In both cases the new indices are created at the old index’s position of the tensor’s shapeshape_map (dict_like or sequence of tuples) – Mapping like:
{old_ind: new_ind_sizes, ...}
or an ordered mapping like[(old_ind_1, new_ind_sizes_1), ...]
.
 Returns:
The transposed, reshaped and relabeled tensor
 Return type:
 to_dense(*inds_seq, to_qarray=False)[source]¶
Convert this Tensor into an dense array, with a single dimension for each of inds in
inds_seqs
. E.g. to convert several sites into a density matrix:T.to_dense(('k0', 'k1'), ('b0', 'b1'))
.
 squeeze(include=None, exclude=None, inplace=False)[source]¶
Drop any singlet dimensions from this tensor.
 Parameters:
inplace (bool, optional) – Whether modify the original or return a new tensor.
include (sequence of str, optional) – Only squeeze dimensions with indices in this list.
exclude (sequence of str, optional) – Squeeze all dimensions except those with indices in this list.
inplace – Whether to perform the squeeze inplace or not.
 Return type:
 largest_element()[source]¶
Return the largest element, in terms of absolute magnitude, of this tensor.
 idxmin(f=None)[source]¶
Get the index configuration of the minimum element of this tensor, optionally applying
f
first.
 idxmax(f=None)[source]¶
Get the index configuration of the maximum element of this tensor, optionally applying
f
first.
 norm()[source]¶
Frobenius norm of this tensor:
\[\t\_F = \sqrt{\mathrm{Tr} \left(t^{\dagger} t\right)}\]where the trace is taken over all indices. Equivalent to the square root of the sum of squared singular values across any partition.
 symmetrize(ind1, ind2, inplace=False)[source]¶
Hermitian symmetrize this tensor for indices
ind1
andind2
. I.e.T = (T + T.conj().T) / 2
, where the transpose is taken only over the specified indices.
 isometrize(left_inds=None, method='qr', inplace=False)[source]¶
Make this tensor unitary (or isometric) with respect to
left_inds
. The underlying method is set bymethod
. Parameters:
left_inds (sequence of str) – The indices to group together and treat as the left hand side of a matrix.
method (str, optional) –
The method used to generate the isometry. The options are:
”qr”: use the Q factor of the QR decomposition of
x
with the constraint that the diagonal ofR
is positive.”svd”: uses
U @ VH
of the SVD decomposition ofx
. This is useful for finding the ‘closest’ isometric matrix tox
, such as when it has been expanded with noise etc. But is less stable for differentiation / optimization.”exp”: use the matrix exponential of
x  dag(x)
, first completingx
with zeros if it is rectangular. This is a good parametrization for optimization, but more expensive for nonsquarex
.”cayley”: use the Cayley transform of
x  dag(x)
, first completingx
with zeros if it is rectangular. This is a good parametrization for optimization (one the few compatible with HIPS/autograd e.g.), but more expensive for nonsquarex
.”householder”: use the Householder reflection method directly. This requires that the backend implements “linalg.householder_product”.
”torch_householder”: use the Householder reflection method directly, using the
torch_householder
package. This requires that the package is installed and that the backend is"torch"
. This is generally the best parametrizing method for “torch” if available.”mgs”: use a python implementation of the modified Gram Schmidt method directly. This is slow if not compiled but a useful reference.
Not all backends support all methods or differentiating through all methods.
inplace (bool, optional) – Whether to perform the unitization inplace.
 Return type:
 unitize_¶
 randomize(dtype=None, inplace=False, **randn_opts)[source]¶
Randomize the entries of this tensor.
 Parameters:
 Return type:
 flip(ind, inplace=False)[source]¶
Reverse the axis on this tensor corresponding to
ind
. Like performing e.g.X[:, :, ::1, :]
.
 multiply_index_diagonal(ind, x, inplace=False)[source]¶
Multiply this tensor by 1D array
x
as if it were a diagonal tensor being contracted into indexind
.
 filter_bonds(other)[source]¶
Sort this tensor’s indices into a list of those that it shares and doesn’t share with another tensor.
 __or__(other)[source]¶
Combine virtually (no copies made) with another
Tensor
orTensorNetwork
into a newTensorNetwork
.
 __matmul__(other)[source]¶
Explicitly contract with another tensor. Avoids some slight overhead of calling the full
tensor_contract()
.
 _repr_info()[source]¶
General info to show in various reprs. Sublasses can add more relevant info to this dict.
 class quimb.tensor.tensor_1d_compress.TensorNetwork(ts=(), *, virtual=False, check_collisions=True)[source]¶
Bases:
object
A collection of (as yet uncontracted) Tensors.
 Parameters:
ts (sequence of Tensor or TensorNetwork) – The objects to combine. The new network will copy these (but not the underlying data) by default. For a view set
virtual=True
.virtual (bool, optional) – Whether the TensorNetwork should be a view onto the tensors it is given, or a copy of them. E.g. if a virtual TN is constructed, any changes to a Tensor’s indices or tags will propagate to all TNs viewing that Tensor.
check_collisions (bool, optional) – If True, the default, then
TensorNetwork
instances with double indices which match anotherTensorNetwork
instances double indices will have those indices’ names mangled. Can be explicitly turned off when it is known that no collisions will take place – i.e. when not adding any new tensors.
 tensor_map¶
Mapping of unique ids to tensors, like``{tensor_id: tensor, …}``. I.e. this is where the tensors are ‘stored’ by the network.
 Type:
 tag_map¶
Mapping of tags to a set of tensor ids which have those tags. I.e.
{tag: {tensor_id_1, tensor_id_2, ...}}
. Thus to select those tensors could do:map(tensor_map.__getitem__, tag_map[tag])
. Type:
 ind_map¶
Like
tag_map
but for indices. Soind_map[ind]]
returns the tensor ids of those tensors withind
. Type:
 exponent¶
A scalar prefactor for the tensor network, stored in base 10 like
10**exponent
. This is mostly for conditioning purposes and will be0.0
unless you use useequalize_norms(value)
ortn.strip_exponent(tid_or_tensor)
. Type:
 _EXTRA_PROPS = ()¶
 _CONTRACT_STRUCTURED = False¶
 combine(other, *, virtual=False, check_collisions=True)[source]¶
Combine this tensor network with another, returning a new tensor network. This can be overriden by subclasses to check for a compatible structured type.
 Parameters:
other (TensorNetwork) – The other tensor network to combine with.
virtual (bool, optional) – Whether the new tensor network should copy all the incoming tensors (
False
, the default), or view them as virtual (True
).check_collisions (bool, optional) – Whether to check for index collisions between the two tensor networks before combining them. If
True
(the default), any inner indices that clash will be mangled.
 Return type:
 __and__(other)[source]¶
Combine this tensor network with more tensors, without contracting. Copies the tensors.
 __or__(other)[source]¶
Combine this tensor network with more tensors, without contracting. Views the constituent tensors.
 classmethod new(like=None, **kwargs)[source]¶
Create a new tensor network, without any tensors, of type
cls
, with all the requisite properties specified bykwargs
or inherited fromlike
.
 classmethod from_TN(tn, like=None, inplace=False, **kwargs)[source]¶
Construct a specific tensor network subclass (i.e. one with some promise about structure/geometry and tags/inds such as an MPS) from a generic tensor network which should have that structure already.
 Parameters:
cls (class) – The TensorNetwork subclass to convert
tn
to.tn (TensorNetwork) – The TensorNetwork to convert.
like (TensorNetwork, optional) – If specified, try and retrieve the neccesary attribute values from this tensor network.
inplace (bool, optional) – Whether to perform the conversion inplace or not.
kwargs – Extra properties of the TN subclass that should be specified.
 view_like(like, inplace=False, **kwargs)[source]¶
View this tensor network as the same subclass
cls
aslike
inheriting its extra properties as well.
 copy(virtual=False, deep=False)[source]¶
Copy this
TensorNetwork
. Ifdeep=False
, (the default), then everything but the actual numeric data will be copied.
 set_params(params)[source]¶
Take a pytree of the ‘parameters’, i.e. all underlying data arrays, as returned by
get_params
and set them.
 add_tensor(tensor, tid=None, virtual=False)[source]¶
Add a single tensor to this network  mangle its tid if neccessary.
 add(t, virtual=False, check_collisions=True)[source]¶
Add Tensor, TensorNetwork or sequence thereof to self.
 make_tids_consecutive(tid0=0)[source]¶
Reset the tids  node identifies  to be consecutive integers.
 __iand__(tensor)[source]¶
Inplace, but nonvirtual, addition of a Tensor or TensorNetwork to this network. It should not have any conflicting indices.
 __ior__(tensor)[source]¶
Inplace, virtual, addition of a Tensor or TensorNetwork to this network. It should not have any conflicting indices.
 property num_tensors¶
 The total number of tensors in the tensor network.
 property num_indices¶
 The total number of indices in the tensor network.
 add_tag(tag, where=None, which='all')[source]¶
Add tag to every tensor in this network, or if
where
is specified, the tensors matching those tags – i.e. adds the tag to all tensors inself.select_tensors(where, which=which)
.
 drop_tags(tags=None)[source]¶
Remove a tag or tags from this tensor network, defaulting to all. This is an inplace operation.
 retag(tag_map, inplace=False)[source]¶
Rename tags for all tensors in this network, optionally inplace.
 Parameters:
tag_map (dictlike) – Mapping of pairs
{old_tag: new_tag, ...}
.inplace (bool, optional) – Perform operation inplace or return copy (default).
 reindex(index_map, inplace=False)[source]¶
Rename indices for all tensors in this network, optionally inplace.
 Parameters:
index_map (dictlike) – Mapping of pairs
{old_ind: new_ind, ...}
.
 mangle_inner_(append=None, which=None)[source]¶
Generate new index names for internal bonds, meaning that when this tensor network is combined with another, there should be no collisions.
 conj(mangle_inner=False, inplace=False)[source]¶
Conjugate all the tensors in this network (leaves all indices).
 property H¶
 Conjugate all the tensors in this network (leaves all indices).
 largest_element()[source]¶
Return the ‘largest element’, in terms of absolute magnitude, of this tensor network. This is defined as the product of the largest elements of each tensor in the network, which would be the largest single term occuring if the TN was summed explicitly.
 norm(**contract_opts)[source]¶
Frobenius norm of this tensor network. Computed by exactly contracting the TN with its conjugate:
\[\T\_F = \sqrt{\mathrm{Tr} \left(T^{\dagger} T\right)}\]where the trace is taken over all indices. Equivalent to the square root of the sum of squared singular values across any partition.
 make_norm(mangle_append='*', layer_tags=('KET', 'BRA'), return_all=False)[source]¶
Make the norm tensor network of this tensor network
tn.H & tn
.
 multiply(x, inplace=False, spread_over=8)[source]¶
Scalar multiplication of this tensor network with
x
. Parameters:
x (scalar) – The number to multiply this tensor network by.
inplace (bool, optional) – Whether to perform the multiplication inplace.
spread_over (int, optional) – How many tensors to try and spread the multiplication over, in order that the effect of multiplying by a very large or small scalar is not concentrated.
 multiply_each(x, inplace=False)[source]¶
Scalar multiplication of each tensor in this tensor network with
x
. If trying to spread a multiplicative factorfac
uniformly over all tensors in the network and the number of tensors is large, then callingmultiply(fac)
can be inaccurate due to precision loss. If one has a routine that can precisely compute thex
to be applied to each tensor, then this function avoids the potential inaccuracies inmultiply()
. Parameters:
x (scalar) – The number that multiplies each tensor in the network
inplace (bool, optional) – Whether to perform the multiplication inplace.
 property tensors¶
 Get the tuple of tensors in this tensor network.
 property arrays¶
 Get the tuple of raw arrays containing all the tensor network data.
 get_symbol_map()[source]¶
Get the mapping of the current indices to
einsum
style single unicode characters. The symbols are generated in the order they appear on the tensors.See also
 get_equation(output_inds=None)[source]¶
Get the ‘equation’ describing this tensor network, in
einsum
style with a single unicode letter per index. The symbols are generated in the order they appear on the tensors. Parameters:
output_inds (None or sequence of str, optional) – Manually specify which are the output indices.
 Returns:
eq
 Return type:
Examples
>>> tn = qtn.TN_rand_reg(10, 3, 2) >>> tn.get_equation() 'abc,dec,fgb,hia,jke,lfk,mnj,ing,omd,ohl>'
See also
 get_inputs_output_size_dict(output_inds=None)[source]¶
Get a tuple of
inputs
,output
andsize_dict
suitable for e.g. passing to path optimizers. The symbols are generated in the order they appear on the tensors. Parameters:
output_inds (None or sequence of str, optional) – Manually specify which are the output indices.
 Returns:
inputs (tuple[str])
output (str)
size_dict (dict[str, ix])
See also
 geometry_hash(output_inds=None, strict_index_order=False)[source]¶
A hash of this tensor network’s shapes & geometry. A useful check for determinism. Moreover, if this matches for two tensor networks then they can be contracted using the same tree for the same cost. Order of tensors matters for this  two isomorphic tensor networks with shuffled tensor order will not have the same hash value. Permuting the indices of individual of tensors or the output does not matter unless you set
strict_index_order=True
. Parameters:
output_inds (None or sequence of str, optional) – Manually specify which indices are output indices and their order, otherwise assumed to be all indices that appear once.
strict_index_order (bool, optional) – If
False
, then the permutation of the indices of each tensor and the output does not matter.
 Return type:
Examples
If we transpose some indices, then only the strict hash changes:
>>> tn = qtn.TN_rand_reg(100, 3, 2, seed=0) >>> tn.geometry_hash() '18c702b2d026dccb1a69d640b79d22f3e706b6ad'
>>> tn.geometry_hash(strict_index_order=True) 'c109fdb43c5c788c0aef7b8df7bb83853cf67ca1'
>>> t = tn['I0'] >>> t.transpose_(t.inds[2], t.inds[1], t.inds[0]) >>> tn.geometry_hash() '18c702b2d026dccb1a69d640b79d22f3e706b6ad'
>>> tn.geometry_hash(strict_index_order=True) '52c32c1d4f349373f02d512f536b1651dfe25893'
 tensors_sorted()[source]¶
Return a tuple of tensors sorted by their respective tags, such that the tensors of two networks with the same tag structure can be iterated over pairwise.
 apply_to_arrays(fn)[source]¶
Modify every tensor’s array inplace by applying
fn
to it. This is meant for changing how the raw arrays are backed (e.g. converting between dtypes or libraries) but not their ‘numerical meaning’.
 _get_tids_from_tags(tags, which='all')[source]¶
Return the set of tensor ids that match
tags
. Parameters:
tags (seq or str, str, None, ..., int, slice) – Tag specifier(s).
which ({'all', 'any', '!all', '!any'}) –
How to select based on the tags, if:
’all’: get ids of tensors matching all tags
’any’: get ids of tensors matching any tags
’!all’: get ids of tensors not matching all tags
’!any’: get ids of tensors not matching any tags
 Return type:
 select_tensors(tags, which='all')[source]¶
Return the sequence of tensors that match
tags
. Ifwhich='all'
, each tensor must contain every tag. Ifwhich='any'
, each tensor can contain any of the tags. Parameters:
 Returns:
tagged_tensors – The tagged tensors.
 Return type:
See also
 _select_tids(tids, virtual=True)[source]¶
Get a copy or a virtual copy (doesn’t copy the tensors) of this
TensorNetwork
, only with the tensors corresponding totids
.
 _select_without_tids(tids, virtual=True)[source]¶
Get a copy or a virtual copy (doesn’t copy the tensors) of this
TensorNetwork
, without the tensors corresponding totids
.
 select(tags, which='all', virtual=True)[source]¶
Get a TensorNetwork comprising tensors that match all or any of
tags
, inherit the network properties/structure fromself
. This returns a view of the tensors not a copy. Parameters:
 Returns:
tagged_tn – A tensor network containing the tagged tensors.
 Return type:
See also
select_tensors
,select_neighbors
,partition
,partition_tensors
 select_neighbors(tags, which='any')[source]¶
Select any neighbouring tensors to those specified by
tags
.self Parameters:
 Returns:
The neighbouring tensors.
 Return type:
See also
 _select_local_tids(tids, max_distance=1, fillin=False, reduce_outer=None, inwards=False, virtual=True, include=None, exclude=None)[source]¶
 select_local(tags, which='all', max_distance=1, fillin=False, reduce_outer=None, virtual=True, include=None, exclude=None)[source]¶
Select a local region of tensors, based on graph distance
max_distance
to any tagged tensors. Parameters:
tags (str or sequence of str) – The tag or tag sequence defining the initial region.
which ({'all', 'any', '!all', '!any'}, optional) – Whether to require matching all or any of the tags.
max_distance (int, optional) – The maximum distance to the initial tagged region.
fillin (bool or int, optional) –
Once the local region has been selected based on graph distance, whether and how many times to ‘fillin’ corners by adding tensors connected multiple times. For example, if
R
is an initially tagged tensor andx
are locally selected tensors:fillin=0 fillin=1 fillin=2                ooxoo oxxxo xxxxx                oxxxo xxxxx xxxxx                xxRxx xxRxx xxRxx
reduce_outer ({'sum', 'svd', 'svdsum', 'reflect'}, optional) – Whether and how to reduce any outer indices of the selected region.
virtual (bool, optional) – Whether the returned tensor network should be a view of the tensors or a copy (
virtual=False
).include (sequence of int, optional) – Only include tensor with these
tids
.exclude (sequence of int, optional) – Only include tensor without these
tids
.
 Return type:
 partition_tensors(tags, inplace=False, which='any')[source]¶
Split this TN into a list of tensors containing any or all of
tags
and aTensorNetwork
of the the rest. Parameters:
 Returns:
(u_tn, t_ts) – The untagged tensor network, and the sequence of tagged Tensors.
 Return type:
(TensorNetwork, tuple of Tensors)
See also
 partition(tags, which='any', inplace=False)[source]¶
Split this TN into two, based on which tensors have any or all of
tags
. Unlikepartition_tensors
, both results are TNs which inherit the structure of the initial TN. Parameters:
 Returns:
untagged_tn, tagged_tn – The untagged and tagged tensor networs.
 Return type:
See also
 split_tensor(tags, left_inds, **split_opts)[source]¶
Split the single tensor uniquely identified by
tags
, adding the resulting tensors from the decomposition back into the network. Inplace operation.
 replace_with_identity(where, which='any', inplace=False)[source]¶
Replace all tensors marked by
where
with an identity. E.g. ifX
denotewhere
tensors:1 XX2 12     ==>  XXX  
 Parameters:
where (tag or seq of tags) – Tags specifying the tensors to replace.
which ({'any', 'all'}) – Whether to replace tensors matching any or all the tags
where
.inplace (bool) – Perform operation in place.
 Returns:
The TN, with section replaced with identity.
 Return type:
See also
 replace_with_svd(where, left_inds, eps, *, which='any', right_inds=None, method='isvd', max_bond=None, absorb='both', cutoff_mode='rel', renorm=None, ltags=None, rtags=None, keep_tags=True, start=None, stop=None, inplace=False)[source]¶
Replace all tensors marked by
where
with an iteratively constructed SVD. E.g. ifX
denotewhere
tensors::__ ___: X XX X : \ / :     ==> : U~s~VH: XXXX :__/ \ :  + : \__: X left_inds : right_inds
 Parameters:
where (tag or seq of tags) – Tags specifying the tensors to replace.
left_inds (ind or sequence of inds) – The indices defining the left hand side of the SVD.
eps (float) – The tolerance to perform the SVD with, affects the number of singular values kept. See
quimb.linalg.rand_linalg.estimate_rank()
.which ({'any', 'all', '!any', '!all'}, optional) – Whether to replace tensors matching any or all the tags
where
, prefix with ‘!’ to invert the selection.right_inds (ind or sequence of inds, optional) – The indices defining the right hand side of the SVD, these can be automatically worked out, but for hermitian decompositions the order is important and thus can be given here explicitly.
method (str, optional) – How to perform the decomposition, if not an iterative method the subnetwork dense tensor will be formed first, see
tensor_split()
for options.max_bond (int, optional) – The maximum bond to keep, defaults to no maximum (1).
ltags (sequence of str, optional) – Tags to add to the left tensor.
rtags (sequence of str, optional) – Tags to add to the right tensor.
keep_tags (bool, optional) – Whether to propagate tags found in the subnetwork to both new tensors or drop them, defaults to
True
.start (int, optional) – If given, assume can use
TNLinearOperator1D
.stop (int, optional) – If given, assume can use
TNLinearOperator1D
.inplace (bool, optional) – Perform operation in place.
 Return type:
See also
 replace_section_with_svd(start, stop, eps, **replace_with_svd_opts)[source]¶
Take a 1D tensor network, and replace a section with a SVD. See
replace_with_svd()
. Parameters:
start (int) – Section start index.
stop (int) – Section stop index, not included itself.
eps (float) – Precision of SVD.
replace_with_svd_opts – Supplied to
replace_with_svd()
.
 Return type:
 _contract_between_tids(tid1, tid2, equalize_norms=False, gauges=None, output_inds=None, **contract_opts)[source]¶
 contract_between(tags1, tags2, **contract_opts)[source]¶
Contract the two tensors specified by
tags1
andtags2
respectively. This is an inplace operation. Noop if the tensor specified bytags1
andtags2
is the same tensor. Parameters:
tags1 – Tags uniquely identifying the first tensor.
tags2 (str or sequence of str) – Tags uniquely identifying the second tensor.
contract_opts – Supplied to
tensor_contract()
.
 gate_inds_with_tn(inds, gate, gate_inds_inner, gate_inds_outer, inplace=False)[source]¶
Gate some indices of this tensor network with another tensor network. That is, rewire and then combine them such that the new tensor network has the same outer indices as before, but now includes gate:
gate_inds_outer : : gate_inds_inner : : : : inds inds : ┌────┐ : : ┌────┬─── : ┌───────┬─── ───┤ ├── a──┤ │ a──┤ │ │ │ │ ├─── │ ├─── ───┤gate├── b──┤self│ > b──┤ new │ │ │ │ ├─── │ ├─── ───┤ ├── c──┤ │ c──┤ │ └────┘ └────┴─── └───────┴───
Where there can be arbitrary structure of tensors within both
self
andgate
.The case where some of target
inds
are not present is handled as so (here ‘c’ is missing so ‘x’ and ‘y’ are kept):gate_inds_outer : : gate_inds_inner : : : : inds inds : ┌────┐ : : ┌────┬─── : ┌───────┬─── ───┤ ├── a──┤ │ a──┤ │ │ │ │ ├─── │ ├─── ───┤gate├── b──┤self│ > b──┤ new │ │ │ │ ├─── │ ├─── x───┤ ├──y └────┘ x──┤ ┌──┘ └────┘ └────┴───y
Which enables convinient construction of various tensor networks, for example propagators, from scratch.
 Parameters:
inds (str or sequence of str) – The current indices to gate. If an index is not present on the target tensor network, it is ignored and instead the resulting tensor network will have both the corresponding inner and outer index of the gate tensor network.
gate (Tensor or TensorNetwork) – The tensor network to gate with.
gate_inds_inner (sequence of str) – The indices of
gate
to join to the oldinds
, must be the same length asinds
.gate_inds_outer (sequence of str) – The indices of
gate
to make the new outerinds
, must be the same length asinds
.
 Returns:
tn_gated
 Return type:
See also
 _compute_tree_gauges(tree, outputs)[source]¶
Given a
tree
of connected tensors, absorb the gauges from outside inwards, finally outputing the gauges associated with theoutputs
. Parameters:
tree (sequence of (tid_outer, tid_inner, distance)) – The tree of connected tensors, see
get_tree_span()
.outputs (sequence of (tid, ind)) – Each output is specified by a tensor id and an index, such that having absorbed all gauges in the tree, the effective reduced factor of the tensor with respect to the index is returned.
 Returns:
Gouts – The effective reduced factors of the tensor index pairs specified in
outputs
, each a matrix. Return type:
sequence of array
 _compress_between_virtual_tree_tids(tidl, tidr, max_bond, cutoff, r, absorb='both', include=None, exclude=None, span_opts=None, **compress_opts)[source]¶
 _compute_bond_env(tid1, tid2, select_local_distance=None, select_local_opts=None, max_bond=None, cutoff=None, method='contract_around', contract_around_opts=None, contract_compressed_opts=None, optimize='autohq', include=None, exclude=None)[source]¶
Compute the local tensor environment of the bond(s), if cut, between two tensors.
 _compress_between_full_bond_tids(tid1, tid2, max_bond, cutoff=0.0, absorb='both', renorm=False, method='eigh', select_local_distance=None, select_local_opts=None, env_max_bond='max_bond', env_cutoff='cutoff', env_method='contract_around', contract_around_opts=None, contract_compressed_opts=None, env_optimize='autohq', include=None, exclude=None)[source]¶
 _compress_between_local_fit(tid1, tid2, max_bond, cutoff=0.0, absorb='both', method='als', select_local_distance=1, select_local_opts=None, include=None, exclude=None, **fit_opts)[source]¶
 _compress_between_tids(tid1, tid2, max_bond=None, cutoff=1e10, absorb='both', canonize_distance=None, canonize_opts=None, canonize_after_distance=None, canonize_after_opts=None, mode='basic', equalize_norms=False, gauges=None, gauge_smudge=1e06, callback=None, **compress_opts)[source]¶
 compress_between(tags1, tags2, max_bond=None, cutoff=1e10, absorb='both', canonize_distance=0, canonize_opts=None, equalize_norms=False, **compress_opts)[source]¶
Compress the bond between the two single tensors in this network specified by
tags1
andtags2
usingtensor_compress_bond()
:        ==●====●====●====●== ==●====●====●====●== / / / / / / / /         ==●====1====2====●== ==> ==●====LR====●== / / / / / / / /         ==●====●====●====●== ==●====●====●====●== / / / / / / / /
This is an inplace operation. The compression is unlikely to be optimal with respect to the frobenius norm, unless the TN is already canonicalized at the two tensors. The
absorb
kwarg can be specified to yield an isometry on either the left or right resulting tensors. Parameters:
tags1 – Tags uniquely identifying the first (‘left’) tensor.
tags2 (str or sequence of str) – Tags uniquely identifying the second (‘right’) tensor.
max_bond (int or None, optional) – The maxmimum bond dimension.
cutoff (float, optional) – The singular value cutoff to use.
canonize_distance (int, optional) – How far to locally canonize around the target tensors first.
canonize_opts (None or dict, optional) – Other options for the local canonization.
equalize_norms (bool or float, optional) – If set, rescale the norms of all tensors modified to this value, stripping the rescaling factor into the
exponent
attribute.compress_opts – Supplied to
tensor_compress_bond()
.
See also
 compress_all(max_bond=None, cutoff=1e10, canonize=True, tree_gauge_distance=None, canonize_distance=None, canonize_after_distance=None, mode='auto', inplace=False, **compress_opts)[source]¶
Compress all bonds one by one in this network.
 Parameters:
max_bond (int or None, optional) – The maxmimum bond dimension to compress to.
cutoff (float, optional) – The singular value cutoff to use.
tree_gauge_distance (int, optional) – How far to include local tree gauge information when compressing. If the local geometry is a tree, then each compression will be locally optimal up to this distance.
canonize_distance (int, optional) – How far to locally canonize around the target tensors first, this is set automatically by
tree_gauge_distance
if not specified.canonize_after_distance (int, optional) – How far to locally canonize around the target tensors after, this is set automatically by
tree_gauge_distance
, depending onmode
if not specified.mode ({'auto', 'basic', 'virtualtree'}, optional) – The mode to use for compressing the bonds. If ‘auto’, will use ‘basic’ if
tree_gauge_distance == 0
else ‘virtualtree’.inplace (bool, optional) – Whether to perform the compression inplace.
compress_opts – Supplied to
compress_between()
.
 Return type:
See also
compress_between
,canonize_all
 compress_all_tree(inplace=False, **compress_opts)[source]¶
Canonically compress this tensor network, assuming it to be a tree. This generates a tree spanning out from the most central tensor, then compresses all bonds inwards in a depthfirst manner, using an infinite
canonize_distance
to shift the orthogonality center.
 compress_all_1d(max_bond=None, cutoff=1e10, canonize=True, inplace=False, **compress_opts)[source]¶
Compress a tensor network that you know has a 1D topology, this proceeds by generating a spanning ‘tree’ from around the least central tensor, then optionally canonicalizing all bonds outwards and compressing inwards.
 Parameters:
max_bond (int, optional) – The maximum bond dimension to compress to.
cutoff (float, optional) – The singular value cutoff to use.
canonize (bool, optional) – Whether to canonize all bonds outwards first.
inplace (bool, optional) – Whether to perform the compression inplace.
compress_opts – Supplied to
tensor_compress_bond()
.
 Return type:
 compress_all_simple(max_bond=None, cutoff=1e10, gauges=None, max_iterations=5, tol=0.0, smudge=1e12, power=1.0, inplace=False, **gauge_simple_opts)[source]¶
 _canonize_between_tids(tid1, tid2, absorb='right', gauges=None, gauge_smudge=1e06, equalize_norms=False, **canonize_opts)[source]¶
 canonize_between(tags1, tags2, absorb='right', **canonize_opts)[source]¶
‘Canonize’ the bond between the two single tensors in this network specified by
tags1
andtags2
usingtensor_canonize_bond
:        ●●●● ●●●● / / / / / / / /         ●12● ==> ●>~~~~R● / / / / / / / /         ●●●● ●●●● / / / / / / / /
This is an inplace operation. This can only be used to put a TN into truly canonical form if the geometry is a tree, such as an MPS.
 Parameters:
tags1 – Tags uniquely identifying the first (‘left’) tensor, which will become an isometry.
tags2 (str or sequence of str) – Tags uniquely identifying the second (‘right’) tensor.
absorb ({'left', 'both', 'right'}, optional) – Which side of the bond to absorb the nonisometric operator.
canonize_opts – Supplied to
tensor_canonize_bond()
.
See also
 reduce_inds_onto_bond(inda, indb, tags=None, drop_tags=False, combine=True, ndim_cutoff=3)[source]¶
Use QR factorization to ‘pull’ the indices
inda
andindb
off of their respective tensors and onto the bond between them. This is an inplace operation.
 _get_neighbor_tids(tids, exclude_inds=())[source]¶
Get the tids of tensors connected to the tensor(s) at
tids
.
 _get_subgraph_tids(tids)[source]¶
Get the tids of tensors connected, by any distance, to the tensor or region of tensors
tids
.
 _ind_to_subgraph_tids(ind)[source]¶
Get the tids of tensors connected, by any distance, to the index
ind
.
 istree()[source]¶
Check if this tensor network has a tree structure, (treating multibonds as a single edge).
Examples
>>> MPS_rand_state(10, 7).istree() True
>>> MPS_rand_state(10, 7, cyclic=True).istree() False
 isconnected()[source]¶
Check whether this tensor network is connected, i.e. whether there is a path between any two tensors, (including size 1 indices).
 subgraphs(virtual=False)[source]¶
Split this tensor network into disconneceted subgraphs.
 Parameters:
virtual (bool, optional) – Whether the tensor networks should view the original tensors or not  by default take copies.
 Return type:
 get_tree_span(tids, min_distance=0, max_distance=None, include=None, exclude=None, ndim_sort='max', distance_sort='min', sorter=None, weight_bonds=True, inwards=True)[source]¶
Generate a tree on the tensor network graph, fanning out from the tensors identified by
tids
, up to a maximum ofmax_distance
away. The tree can be visualized withdraw_tree_span()
. Parameters:
tids (sequence of str) – The nodes that define the region to span out of.
min_distance (int, optional) – Don’t add edges to the tree until this far from the region. For example,
1
will not include the last merges from neighboring tensors in the region defined bytids
.max_distance (None or int, optional) – Terminate branches once they reach this far away. If
None
there is no limit,include (sequence of str, optional) – If specified, only
tids
specified here can be part of the tree.exclude (sequence of str, optional) – If specified,
tids
specified here cannot be part of the tree.ndim_sort ({'min', 'max', 'none'}, optional) – When expanding the tree, how to choose what nodes to expand to next, once connectivity to the current surface has been taken into account.
distance_sort ({'min', 'max', 'none'}, optional) – When expanding the tree, how to choose what nodes to expand to next, once connectivity to the current surface has been taken into account.
weight_bonds (bool, optional) – Whether to weight the ‘connection’ of a candidate tensor to expand out to using bond size as well as number of bonds.
 Returns:
The ordered list of merges, each given as tuple
(tid1, tid2, d)
indicating mergetid1 > tid2
at distanced
. Return type:
See also
 _draw_tree_span_tids(tids, span=None, min_distance=0, max_distance=None, include=None, exclude=None, ndim_sort='max', distance_sort='min', sorter=None, weight_bonds=True, color='order', colormap='Spectral', **draw_opts)[source]¶
 draw_tree_span(tags, which='all', min_distance=0, max_distance=None, include=None, exclude=None, ndim_sort='max', distance_sort='min', weight_bonds=True, color='order', colormap='Spectral', **draw_opts)[source]¶
Visualize a generated tree span out of the tensors tagged by
tags
. Parameters:
tags (str or sequence of str) – Tags specifiying a region of tensors to span out of.
which ({'all', 'any': '!all', '!any'}, optional) – How to select tensors based on the tags.
min_distance (int, optional) – See
get_tree_span()
.max_distance (None or int, optional) – See
get_tree_span()
.include (sequence of str, optional) – See
get_tree_span()
.exclude (sequence of str, optional) – See
get_tree_span()
.distance_sort ({'min', 'max'}, optional) – See
get_tree_span()
.color ({'order', 'distance'}, optional) – Whether to color nodes based on the order of the contraction or the graph distance from the specified region.
colormap (str) – The name of a
matplotlib
colormap to use.
See also
 _canonize_around_tids(tids, min_distance=0, max_distance=None, include=None, exclude=None, span_opts=None, absorb='right', gauge_links=False, link_absorb='both', inwards=True, gauges=None, gauge_smudge=1e06, **canonize_opts)[source]¶
 canonize_around(tags, which='all', min_distance=0, max_distance=None, include=None, exclude=None, span_opts=None, absorb='right', gauge_links=False, link_absorb='both', equalize_norms=False, inplace=False, **canonize_opts)[source]¶
Expand a locally canonical region around
tags
:●●     ●vv●       ●>vv<●         ●>>OO<<●         ●>^^^●       ●^^●     ●● <=====> max_distance = 2 e.g.
Shown on a grid here but applicable to arbitrary geometry. This is a way of gauging a tensor network that results in a canonical form if the geometry is described by a tree (e.g. an MPS or TTN). The canonizations proceed inwards via QR decompositions.
The sequence generated by roundrobin expanding the boundary of the originally specified tensors  it will only be unique for trees.
 Parameters:
tags (str, or sequence or str) – Tags defining which set of tensors to locally canonize around.
which ({'all', 'any', '!all', '!any'}, optional) – How select the tensors based on tags.
min_distance (int, optional) – How close, in terms of graph distance, to canonize tensors away. See
get_tree_span()
.max_distance (None or int, optional) – How far, in terms of graph distance, to canonize tensors away. See
get_tree_span()
.include (sequence of str, optional) – How to build the spanning tree to canonize along. See
get_tree_span()
.exclude (sequence of str, optional) – How to build the spanning tree to canonize along. See
get_tree_span()
.{'min' (distance_sort) – How to build the spanning tree to canonize along. See
get_tree_span()
.'max'} – How to build the spanning tree to canonize along. See
get_tree_span()
.optional – How to build the spanning tree to canonize along. See
get_tree_span()
.absorb ({'right', 'left', 'both'}, optional) – As we canonize inwards from tensor A to tensor B which to absorb the singular values into.
gauge_links (bool, optional) – Whether to gauge the links between branches of the spanning tree generated (in a Simple Update like fashion).
link_absorb ({'both', 'right', 'left'}, optional) – If performing the link gauging, how to absorb the singular values.
equalize_norms (bool or float, optional) – Scale the norms of tensors acted on to this value, accumulating the log10 scaled factors in
self.exponent
.inplace (bool, optional) – Whether to perform the canonization inplace.
 Return type:
See also
 gauge_all_canonize(max_iterations=5, absorb='both', gauges=None, gauge_smudge=1e06, equalize_norms=False, inplace=False, **canonize_opts)[source]¶
Iterative gauge all the bonds in this tensor network with a basic ‘canonization’ strategy.
 gauge_all_simple(max_iterations=5, tol=0.0, smudge=1e12, power=1.0, gauges=None, equalize_norms=False, progbar=False, inplace=False)[source]¶
Iterative gauge all the bonds in this tensor network with a ‘simple update’ like strategy.
 gauge_all_random(max_iterations=1, unitary=True, seed=None, inplace=False)[source]¶
Gauge all the bonds in this network randomly. This is largely for testing purposes.
 gauge_all(method='canonize', **gauge_opts)[source]¶
Gauge all bonds in this network using one of several strategies.
 Parameters:
See also
 _gauge_local_tids(tids, max_distance=1, max_iterations='max_distance', method='canonize', inwards=False, include=None, exclude=None, **gauge_local_opts)[source]¶
Iteratively gauge all bonds in the local tensor network defined by
tids
according to one of several strategies.
 gauge_local(tags, which='all', max_distance=1, max_iterations='max_distance', method='canonize', inplace=False, **gauge_local_opts)[source]¶
Iteratively gauge all bonds in the tagged sub tensor network according to one of several strategies.
 gauge_simple_insert(gauges, remove=False, smudge=0.0, power=1.0)[source]¶
Insert the simple update style bond gauges found in
gauges
if they are present in this tensor network. The gauges inserted are also returned so that they can be removed later. Parameters:
gauges (dict[str, array_like]) – The store of bond gauges, the keys being indices and the values being the vectors. Only bonds present in this dictionary will be gauged.
remove (bool, optional) – Whether to remove the gauges from the store after inserting them.
smudge (float, optional) – A small value to add to the gauge vectors to avoid singularities.
 Returns:
outer (list[(Tensor, str, array_like)]) – The sequence of gauges applied to outer indices, each a tuple of the tensor, the index and the gauge vector.
inner (list[((Tensor, Tensor), str, array_like)]) – The sequence of gauges applied to inner indices, each a tuple of the two inner tensors, the inner bond and the gauge vector applied.
 static gauge_simple_remove(outer=None, inner=None)[source]¶
Remove the simple update style bond gauges inserted by
gauge_simple_insert
.
 gauge_simple_temp(gauges, smudge=1e12, ungauge_outer=True, ungauge_inner=True)[source]¶
Context manager that temporarily inserts simple update style bond gauges into this tensor network, before optionally ungauging them.
 Parameters:
self (TensorNetwork) – The TensorNetwork to be gaugebonded.
gauges (dict[str, array_like]) – The store of gauge bonds, the keys being indices and the values being the vectors. Only bonds present in this dictionary will be gauged.
ungauge_outer (bool, optional) – Whether to ungauge the outer bonds.
ungauge_inner (bool, optional) – Whether to ungauge the inner bonds.
 Yields:
outer (list[(Tensor, int, array_like)]) – The tensors, indices and gauges that were performed on outer indices.
inner (list[((Tensor, Tensor), int, array_like)]) – The tensors, indices and gauges that were performed on inner bonds.
Examples
>>> tn = TN_rand_reg(10, 4, 3) >>> tn ^ all 51371.66630218866
>>> gauges = {} >>> tn.gauge_all_simple_(gauges=gauges) >>> len(gauges) 20
>>> tn ^ all 28702551.673767876
>>> with gauged_bonds(tn, gauges): ... # temporarily insert gauges ... print(tn ^ all) 51371.66630218887
>>> tn ^ all 28702551.67376789
 _contract_compressed_tid_sequence(seq, max_bond=None, cutoff=1e10, output_inds=None, tree_gauge_distance=1, canonize_distance=None, canonize_opts=None, canonize_after_distance=None, canonize_after_opts=None, gauge_boundary_only=True, compress_opts=None, compress_late=True, compress_mode='auto', compress_min_size=None, compress_span=False, compress_matrices=True, compress_exclude=None, equalize_norms=False, gauges=None, gauge_smudge=1e06, callback_pre_contract=None, callback_post_contract=None, callback_pre_compress=None, callback_post_compress=None, callback=None, preserve_tensor=False, progbar=False, inplace=False)[source]¶
 _contract_around_tids(tids, seq=None, min_distance=0, max_distance=None, span_opts=None, max_bond=None, cutoff=1e10, canonize_opts=None, **kwargs)[source]¶
Contract around
tids
, by following a greedily generated spanning tree, and compressing whenever two tensors in the outer ‘boundary’ share more than one index.
 contract_around(tags, which='all', min_distance=0, max_distance=None, span_opts=None, max_bond=None, cutoff=1e10, tree_gauge_distance=1, canonize_distance=None, canonize_opts=None, canonize_after_distance=None, canonize_after_opts=None, gauge_boundary_only=True, compress_late=True, compress_min_size=None, compress_opts=None, compress_span=False, compress_matrices=True, equalize_norms=False, gauges=None, gauge_smudge=1e06, callback_pre_contract=None, callback_post_contract=None, callback_pre_compress=None, callback_post_compress=None, callback=None, inplace=False, **kwargs)[source]¶
Perform a compressed contraction inwards towards the tensors identified by
tags
.
 contract_compressed(optimize, output_inds=None, max_bond=None, cutoff=1e10, tree_gauge_distance=1, canonize_distance=None, canonize_opts=None, canonize_after_distance=None, canonize_after_opts=None, gauge_boundary_only=True, compress_late=True, compress_min_size=None, compress_opts=None, compress_span=True, compress_matrices=True, compress_exclude=None, equalize_norms=False, gauges=None, gauge_smudge=1e06, callback_pre_contract=None, callback_post_contract=None, callback_pre_compress=None, callback_post_compress=None, callback=None, progbar=False, **kwargs)[source]¶
 new_bond(tags1, tags2, **opts)[source]¶
Inplace addition of a dummmy (size 1) bond between the single tensors specified by by
tags1
andtags2
. Parameters:
tags1 (sequence of str) – Tags identifying the first tensor.
tags2 (sequence of str) – Tags identifying the second tensor.
opts – Supplied to
new_bond()
.
See also
 cut_between(left_tags, right_tags, left_ind, right_ind)[source]¶
Cut the bond between the tensors specified by
left_tags
andright_tags
, giving them the new indsleft_ind
andright_ind
respectively.
 cut_bond(bond, new_left_ind=None, new_right_ind=None)[source]¶
Cut the bond index specified by
bond
between the tensors it connects. Usecut_between
for control over which tensor gets which new indexnew_left_ind
ornew_right_ind
. The index must connect exactly two tensors.
 drape_bond_between(tagsa, tagsb, tags_target, left_ind=None, right_ind=None, inplace=False)[source]¶
Take the bond(s) connecting the tensors tagged at
tagsa
andtagsb
, and ‘drape’ it through the tensor tagged attags_target
, effectively adding an identity tensor between the two and contracting it with the third:┌─┐ ┌─┐ ┌─┐ ┌─┐ ─┤A├─Id─┤B├─ ─┤A├─┐ ┌─┤B├─ └─┘ └─┘ └─┘ │ │ └─┘ left_ind│ │right_ind ┌─┐ > ├─┤ ─┤C├─ ─┤D├─ └┬┘ └┬┘ where D = C ⊗ Id │ │
This increases the size of the target tensor by
d**2
, and disconnects the tensors attagsa
andtagsb
. Parameters:
tagsa (str or sequence of str) – The tag(s) identifying the first tensor.
tagsb (str or sequence of str) – The tag(s) identifying the second tensor.
tags_target (str or sequence of str) – The tag(s) identifying the target tensor.
left_ind (str, optional) – The new index to give to the left tensor.
right_ind (str, optional) – The new index to give to the right tensor.
inplace (bool, optional) – Whether to perform the draping inplace.
 Return type:
 isel(selectors, inplace=False)[source]¶
Select specific values for some dimensions/indices of this tensor network, thereby removing them.
 Parameters:
 Return type:
See also
 sum_reduce(ind, inplace=False)[source]¶
Sum over the index
ind
of this tensor network, removing it. This is like contracting a vector of ones in, or marginalizing a classical probability distribution.
 vector_reduce(ind, v, inplace=False)[source]¶
Contract the vector
v
with the indexind
of this tensor network, removing it. Parameters:
 Return type:
 cut_iter(*inds)[source]¶
Cut and iterate over one or more indices in this tensor network. Each network yielded will have that index removed, and the sum of all networks will equal the original network. This works by iterating over the product of all combinations of each bond supplied to
isel
. As such, the number of networks produced is exponential in the number of bonds cut. Parameters:
inds (sequence of str) – The bonds to cut.
 Yields:
TensorNetwork
Examples
Here we’ll cut the two extra bonds of a cyclic MPS and sum the contraction of the resulting 49 OBC MPS norms:
>>> psi = MPS_rand_state(10, bond_dim=7, cyclic=True) >>> norm = psi.H & psi >>> bnds = bonds(norm[0], norm[1]) >>> sum(tn ^ all for tn in norm.cut_iter(*bnds)) 1.0
See also
 insert_operator(A, where1, where2, tags=None, inplace=False)[source]¶
Insert an operator on the bond between the specified tensors, e.g.:
    12 > 1A2  
 Parameters:
A (array) – The operator to insert.
where1 (str, sequence of str, or int) – The tags defining the ‘left’ tensor.
where2 (str, sequence of str, or int) – The tags defining the ‘right’ tensor.
tags (str or sequence of str) – Tags to add to the new operator’s tensor.
inplace (bool, optional) – Whether to perform the insertion inplace.
 insert_gauge(U, where1, where2, Uinv=None, tol=1e10)[source]¶
Insert the gauge transformation
U^1 @ U
into the bond between the tensors,T1
andT2
, defined bywhere1
andwhere2
. The resulting tensors at those locations will beT1 @ U^1
andU @ T2
. Parameters:
U (array) – The gauge to insert.
where1 (str, sequence of str, or int) – Tags defining the location of the ‘left’ tensor.
where2 (str, sequence of str, or int) – Tags defining the location of the ‘right’ tensor.
Uinv (array) – The inverse gauge,
U @ Uinv == Uinv @ U == eye
, to insert. If not given will be calculated usingnumpy.linalg.inv()
.
 contract_tags(tags, which='any', output_inds=None, optimize=None, get=None, backend=None, preserve_tensor=False, inplace=False, **contract_opts)[source]¶
Contract the tensors that match any or all of
tags
. Parameters:
tags (sequence of str) – The list of tags to filter the tensors by. Use
all
or...
(Ellipsis
) to contract all tensors.which ({'all', 'any'}) – Whether to require matching all or any of the tags.
output_inds (sequence of str, optional) – The indices to specify as outputs of the contraction. If not given, and the tensor network has no hyperindices, these are computed automatically as every index appearing once.
optimize ({None, str, path_like, PathOptimizer}, optional) –
The contraction path optimization strategy to use.
None
: use the default strategy,str: use the preset strategy with the given name,
path_like: use this exact path,
cotengra.HyperOptimizer
: find the contraction using this optimizer, supports slicing,cotengra.ContractionTree
: use this exact tree, supports
slicing, 
opt_einsum.PathOptimizer
: find the path using thisoptimizer.
Contraction with
cotengra
might be a bit more efficient but the main reason would be to handle sliced contraction automatically.get (str, optional) –
What to return. If:
None
(the default)  return the resulting scalar or Tensor.'expression'
 return a callbable expression that performs the contraction and operates on the raw arrays.'tree'
 return thecotengra.ContractionTree
describing the contraction.'path'
 return the raw ‘path’ as a list of tuples.'symbolmap'
 return the dict mapping indices to ‘symbols’ (single unicode letters) used internally bycotengra
'pathinfo'
 return theopt_einsum.PathInfo
path object with detailed information such as flop cost. The symbolmap is also added to thequimb_symbol_map
attribute.
backend ({'auto', 'numpy', 'jax', 'cupy', 'tensorflow', ...}, optional) – Which backend to use to perform the contraction. Supplied to cotengra.
preserve_tensor (bool, optional) – Whether to return a tensor regardless of whether the output object is a scalar (has no indices) or not.
inplace (bool, optional) – Whether to perform the contraction inplace.
contract_opts – Passed to
tensor_contract()
.
 Returns:
The result of the contraction, still a
TensorNetwork
if the contraction was only partial. Return type:
TensorNetwork, Tensor or scalar
See also
 contract(tags=..., output_inds=None, optimize=None, get=None, backend=None, preserve_tensor=False, max_bond=None, inplace=False, **opts)[source]¶
Contract some, or all, of the tensors in this network. This method dispatches to
contract_tags
,contract_structured
, orcontract_compressed
based on the various arguments. Parameters:
tags (sequence of str, all, or Ellipsis, optional) – Any tensors with any of these tags with be contracted. Use
all
or...
(Ellipsis
) to contract all tensors....
will try and use a ‘structured’ contract method if possible.output_inds (sequence of str, optional) – The indices to specify as outputs of the contraction. If not given, and the tensor network has no hyperindices, these are computed automatically as every index appearing once.
optimize ({None, str, path_like, PathOptimizer}, optional) –
The contraction path optimization strategy to use.
None
: use the default strategy,str: use the preset strategy with the given name,
path_like: use this exact path,
cotengra.HyperOptimizer
: find the contraction using this optimizer, supports slicing,cotengra.ContractionTree
: use this exact tree, supports slicing,opt_einsum.PathOptimizer
: find the path using this optimizer.
Contraction with
cotengra
might be a bit more efficient but the main reason would be to handle sliced contraction automatically.get (str, optional) –
What to return. If:
None
(the default)  return the resulting scalar or Tensor.'expression'
 return a callbable expression that performs the contraction and operates on the raw arrays.'tree'
 return thecotengra.ContractionTree
describing the contraction.'path'
 return the raw ‘path’ as a list of tuples.'symbolmap'
 return the dict mapping indices to ‘symbols’ (single unicode letters) used internally bycotengra
'pathinfo'
 return theopt_einsum.PathInfo
path object with detailed information such as flop cost. The symbolmap is also added to thequimb_symbol_map
attribute.
backend ({'auto', 'numpy', 'jax', 'cupy', 'tensorflow', ...}, optional) – Which backend to use to perform the contraction. Supplied to cotengra.
preserve_tensor (bool, optional) – Whether to return a tensor regardless of whether the output object is a scalar (has no indices) or not.
inplace (bool, optional) – Whether to perform the contraction inplace. This is only valid if not all tensors are contracted (which doesn’t produce a TN).
opts – Passed to
tensor_contract()
,contract_compressed()
.
 Returns:
The result of the contraction, still a
TensorNetwork
if the contraction was only partial. Return type:
TensorNetwork, Tensor or scalar
See also
 contract_cumulative(tags_seq, output_inds=None, preserve_tensor=False, equalize_norms=False, inplace=False, **opts)[source]¶
Cumulative contraction of tensor network. Contract the first set of tags, then that set with the next set, then both of those with the next and so forth. Could also be described as an manually ordered contraction of all tags in
tags_seq
. Parameters:
tags_seq (sequence of sequence of str) – The list of taggroups to cumulatively contract.
output_inds (sequence of str, optional) – The indices to specify as outputs of the contraction. If not given, and the tensor network has no hyperindices, these are computed automatically as every index appearing once.
preserve_tensor (bool, optional) – Whether to return a tensor regardless of whether the output object is a scalar (has no indices) or not.
inplace (bool, optional) – Whether to perform the contraction inplace.
opts – Passed to
tensor_contract()
.
 Returns:
The result of the contraction, still a
TensorNetwork
if the contraction was only partial. Return type:
TensorNetwork, Tensor or scalar
See also
 contraction_path(optimize=None, **contract_opts)[source]¶
Compute the contraction path, a sequence of (int, int), for the contraction of this entire tensor network using path optimizer
optimize
.
 contraction_info(optimize=None, **contract_opts)[source]¶
Compute the
opt_einsum.PathInfo
object decsribing the contraction of this entire tensor network using path optimizeroptimize
.
 contraction_tree(optimize=None, output_inds=None, **kwargs)[source]¶
Return the
cotengra.ContractionTree
corresponding to contracting this entire tensor network with path finderoptimize
.
 contraction_width(optimize=None, **contract_opts)[source]¶
Compute the ‘contraction width’ of this tensor network. This is defined as log2 of the maximum tensor size produced during the contraction sequence. If every index in the network has dimension 2 this corresponds to the maximum rank tensor produced.
 contraction_cost(optimize=None, **contract_opts)[source]¶
Compute the ‘contraction cost’ of this tensor network. This is defined as log10 of the total number of scalar operations during the contraction sequence.
 as_network(virtual=True)[source]¶
Matching method (for ensuring object is a tensor network) to
as_network()
, which simply returnsself
ifvirtual=True
.
 aslinearoperator(left_inds, right_inds, ldims=None, rdims=None, backend=None, optimize=None)[source]¶
View this
TensorNetwork
as aTNLinearOperator
.
 split(left_inds, right_inds=None, **split_opts)[source]¶
Decompose this tensor network across a bipartition of outer indices.
This method matches
Tensor.split
by converting to aTNLinearOperator
first. Note unless an iterative method is passed tomethod
, the full dense tensor will be contracted.
 to_dense(*inds_seq, to_qarray=False, **contract_opts)[source]¶
Convert this network into an dense array, with a single dimension for each of inds in
inds_seqs
. E.g. to convert several sites into a density matrix:TN.to_dense(('k0', 'k1'), ('b0', 'b1'))
.
 compute_reduced_factor(side, left_inds, right_inds, optimize='autohq', **contract_opts)[source]¶
Compute either the left or right ‘reduced factor’ of this tensor network. I.e., view as an operator,
X
, mappingleft_inds
toright_inds
and computeL
orR
such thatX = U_R @ R
orX = L @ U_L
, withU_R
andU_L
unitary operators that are not computed. Onlydag(X) @ X
orX @ dag(X)
is contracted, which is generally cheaper than contractingX
itself. Parameters:
self (TensorNetwork) – The tensor network to compute the reduced factor of.
side ({'left', 'right'}) – Whether to compute the left or right reduced factor. If ‘right’ then
dag(X) @ X
is contracted, otherwiseX @ dag(X)
.left_inds (sequence of str) – The indices forming the left side of the operator.
right_inds (sequence of str) – The indices forming the right side of the operator.
contract_opts (dict, optional) – Options to pass to
to_dense()
.
 Return type:
array_like
 insert_compressor_between_regions(ltags, rtags, max_bond=None, cutoff=1e10, select_which='any', insert_into=None, new_tags=None, new_ltags=None, new_rtags=None, bond_ind=None, optimize='autohq', inplace=False, **compress_opts)[source]¶
Compute and insert a pair of ‘oblique’ projection tensors (see for example https://arxiv.org/abs/1905.02351) that effectively compresses between two regions of the tensor network. Useful for various approximate contraction methods such as HOTRG and CTMRG.
 Parameters:
ltags (sequence of str) – The tags of the tensors in the left region.
rtags (sequence of str) – The tags of the tensors in the right region.
max_bond (int or None, optional) – The maximum bond dimension to use for the compression (i.e. shared by the two projection tensors). If
None
then the maximum is controlled bycutoff
.cutoff (float, optional) – The cutoff to use for the compression.
select_which ({'any', 'all', 'none'}, optional) – How to select the regions based on the tags, see
select()
.insert_into (TensorNetwork, optional) – If given, insert the new tensors into this tensor network, assumed to have the same relevant indices as
self
.new_tags (str or sequence of str, optional) – The tag(s) to add to both the new tensors.
new_ltags (str or sequence of str, optional) – The tag(s) to add to the new left projection tensor.
new_rtags (str or sequence of str, optional) – The tag(s) to add to the new right projection tensor.
optimize (str or PathOptimizer, optional) – How to optimize the contraction of the projection tensors.
inplace (bool, optional) – Whether perform the insertion inplace. If
insert_into
is supplied then this doesn’t matter, and that tensor network will be modified and returned.
 Return type:
See also
 fit(tn_target, method='als', tol=1e09, inplace=False, progbar=False, **fitting_opts)[source]¶
Optimize the entries of this tensor network with respect to a least squares fit of
tn_target
which should have the same outer indices. Depending onmethod
this callstensor_network_fit_als()
ortensor_network_fit_autodiff()
. The quantity minimized is:\[D(A, B) =  A  B _{\mathrm{fro}} = \mathrm{Tr} [(A  B)^{\dagger}(A  B)]^{1/2} = ( \langle A  A \rangle  2 \mathrm{Re} \langle A  B \rangle + \langle B  B \rangle ) ^{1/2}\] Parameters:
tn_target (TensorNetwork) – The target tensor network to try and fit the current one to.
method ({'als', 'autodiff'}, optional) – Whether to use alternating least squares (ALS) or automatic differentiation to perform the optimization. Generally ALS is better for simple geometries, autodiff better for complex ones.
tol (float, optional) – The target norm distance.
inplace (bool, optional) – Update the current tensor network in place.
progbar (bool, optional) – Show a live progress bar of the fitting process.
fitting_opts – Supplied to either
tensor_network_fit_als()
ortensor_network_fit_autodiff()
.
 Returns:
tn_opt – The optimized tensor network.
 Return type:
See also
tensor_network_fit_als
,tensor_network_fit_autodiff
,tensor_network_distance
 property tags¶
 inner_inds()[source]¶
Tuple of interior indices, assumed to be any indices that appear twice or more (this only holds generally for nonhyper tensor networks).
 outer_inds()[source]¶
Tuple of exterior indices, assumed to be any lone indices (this only holds generally for nonhyper tensor networks).
 outer_dims_inds()[source]¶
Get the ‘outer’ pairs of dimension and indices, i.e. as if this tensor network was fully contracted.
 outer_size()[source]¶
Get the total size of the ‘outer’ indices, i.e. as if this tensor network was fully contracted.
 get_multibonds(include=None, exclude=None)[source]¶
Get a dict of ‘multibonds’ in this tensor network, i.e. groups of two or more indices that appear on exactly the same tensors and thus could be fused, for example.
 Parameters:
 Returns:
A dict mapping the tuple of indices that could be fused to the tuple of tensor ids they appear on.
 Return type:
 get_hyperinds(output_inds=None)[source]¶
Get a tuple of all ‘hyperinds’, defined as those indices which don’t appear exactly twice on either the tensors or in the ‘outer’ (i.e. output) indices.
Note the default set of ‘outer’ indices is calculated as only those indices that appear once on the tensors, so these likely need to be manually specified, otherwise, for example, an index that appears on two tensors and the output will incorrectly be identified as nonhyper.
 compute_contracted_inds(*tids, output_inds=None)[source]¶
Get the indices describing the tensor contraction of tensors corresponding to
tids
.
 squeeze(fuse=False, include=None, exclude=None, inplace=False)[source]¶
Drop singlet bonds and dimensions from this tensor network. If
fuse=True
also fuse all multibonds between tensors. Parameters:
fuse (bool, optional) – Whether to fuse multibonds between tensors as well as squeezing.
include (sequence of str, optional) – Only squeeze these indices, by default all indices.
exclude (sequence of str, optional) – Ignore these indices, by default the outer indices of this TN.
inplace (bool, optional) – Whether to perform the squeeze and optional fuse inplace.
 Return type:
 isometrize(method='qr', allow_no_left_inds=False, inplace=False)[source]¶
Project every tensor in this network into an isometric form, assuming they have
left_inds
marked. Parameters:
method (str, optional) –
The method used to generate the isometry. The options are:
”qr”: use the Q factor of the QR decomposition of
x
with the constraint that the diagonal ofR
is positive.”svd”: uses
U @ VH
of the SVD decomposition ofx
. This is useful for finding the ‘closest’ isometric matrix tox
, such as when it has been expanded with noise etc. But is less stable for differentiation / optimization.”exp”: use the matrix exponential of
x  dag(x)
, first completingx
with zeros if it is rectangular. This is a good parametrization for optimization, but more expensive for nonsquarex
.”cayley”: use the Cayley transform of
x  dag(x)
, first completingx
with zeros if it is rectangular. This is a good parametrization for optimization (one the few compatible with HIPS/autograd e.g.), but more expensive for nonsquarex
.”householder”: use the Householder reflection method directly. This requires that the backend implements “linalg.householder_product”.
”torch_householder”: use the Householder reflection method directly, using the
torch_householder
package. This requires that the package is installed and that the backend is"torch"
. This is generally the best parametrizing method for “torch” if available.”mgs”: use a python implementation of the modified Gram Schmidt method directly. This is slow if not compiled but a useful reference.
Not all backends support all methods or differentiating through all methods.
allow_no_left_inds (bool, optional) – If
True
then allow tensors with noleft_inds
to be left alone, rather than raising an error.inplace (bool, optional) – If
True
then perform the operation inplace.
 Return type:
 unitize_¶
 randomize(dtype=None, seed=None, inplace=False, **randn_opts)[source]¶
Randomize every tensor in this TN  see
quimb.tensor.tensor_core.Tensor.randomize()
. Parameters:
dtype ({None, str}, optional) – The data type of the random entries. If left as the default
None
, then the data type of the current array will be used.seed (None or int, optional) – Seed for the random number generator.
inplace (bool, optional) – Whether to perform the randomization inplace, by default
False
.randn_opts – Supplied to
randn()
.
 Return type:
 strip_exponent(tid_or_tensor, value=None)[source]¶
Scale the elements of tensor corresponding to
tid
so that the norm of the array is some value, which defaults to1
. The log of the scaling factor, base 10, is then accumulated in theexponent
attribute.
 distribute_exponent()[source]¶
Distribute the exponent
p
of this tensor network (i.e. corresponding totn * 10**p
) equally among all tensors.
 equalize_norms(value=None, inplace=False)[source]¶
Make the Frobenius norm of every tensor in this TN equal without changing the overall value if
value=None
, or set the norm of every tensor tovalue
by scalar multiplication only. Parameters:
 Return type:
 balance_bonds(inplace=False)[source]¶
Apply
tensor_balance_bond()
to all bonds in this tensor network. Parameters:
inplace (bool, optional) – Whether to perform the bond balancing inplace or not.
 Return type:
 fuse_multibonds(gauges=None, include=None, exclude=None, inplace=False)[source]¶
Fuse any multibonds (more than one index shared by the same pair of tensors) into a single bond.
 Parameters:
gauges (None or dict[str, array_like], optional) – If supplied, also fuse the gauges contained in this dict.
include (sequence of str, optional) – Only consider these indices, by default all indices.
exclude (sequence of str, optional) – Ignore these indices, by default the outer indices of this TN.
 expand_bond_dimension(new_bond_dim, mode=None, rand_strength=None, rand_dist='normal', inds_to_expand=None, inplace=False)[source]¶
Increase the dimension of all or some of the bonds in this tensor network to at least
new_bond_dim
, optinally adding some random noise to the new entries. Parameters:
new_bond_dim (int) – The minimum bond dimension to expand to, if the bond dimension is already larger than this it will be left unchanged.
rand_strength (float, optional) – The strength of random noise to add to the new array entries, if any. The noise is drawn from a normal distribution with standard deviation
rand_strength
.inds_to_expand (sequence of str, optional) – The indices to expand, if not all.
inplace (bool, optional) – Whether to expand this tensor network in place, or return a new one.
 Return type:
 flip(inds, inplace=False)[source]¶
Flip the dimension corresponding to indices
inds
on all tensors that share it.
 rank_simplify(output_inds=None, equalize_norms=False, cache=None, max_combinations=500, inplace=False)[source]¶
Simplify this tensor network by performing contractions that don’t increase the rank of any tensors.
 Parameters:
output_inds (sequence of str, optional) – Explicitly set which indices of the tensor network are output indices and thus should not be modified.
equalize_norms (bool or float) – Actively renormalize the tensors during the simplification process. Useful for very large TNs. The scaling factor will be stored as an exponent in
tn.exponent
.cache (None or set) – Persistent cache used to mark already checked tensors.
inplace (bool, optional) – Whether to perform the rand reduction inplace.
 Return type:
See also
 diagonal_reduce(output_inds=None, atol=1e12, cache=None, inplace=False)[source]¶
Find tensors with diagonal structure and collapse those axes. This will create a tensor ‘hyper’ network with indices repeated 2+ times, as such, output indices should be explicitly supplied when contracting, as they can no longer be automatically inferred. For example:
>>> tn_diag = tn.diagonal_reduce() >>> tn_diag.contract(all, output_inds=[])
 Parameters:
output_inds (sequence of str, optional) – Which indices to explicitly consider as outer legs of the tensor network and thus not replace. If not given, these will be taken as all the indices that appear once.
atol (float, optional) – When identifying diagonal tensors, the absolute tolerance with which to compare to zero with.
cache (None or set) – Persistent cache used to mark already checked tensors.
inplace – Whether to perform the diagonal reduction inplace.
bool – Whether to perform the diagonal reduction inplace.
optional – Whether to perform the diagonal reduction inplace.
 Return type:
See also
 antidiag_gauge(output_inds=None, atol=1e12, cache=None, inplace=False)[source]¶
Flip the order of any bonds connected to antidiagonal tensors. Whilst this is just a gauge fixing (with the gauge being the flipped identity) it then allows
diagonal_reduce
to then simplify those indices. Parameters:
output_inds (sequence of str, optional) – Which indices to explicitly consider as outer legs of the tensor network and thus not flip. If not given, these will be taken as all the indices that appear once.
atol (float, optional) – When identifying antidiagonal tensors, the absolute tolerance with which to compare to zero with.
cache (None or set) – Persistent cache used to mark already checked tensors.
inplace – Whether to perform the antidiagonal gauging inplace.
bool – Whether to perform the antidiagonal gauging inplace.
optional – Whether to perform the antidiagonal gauging inplace.
 Return type:
See also
full_simplify
,rank_simplify
,diagonal_reduce
,column_reduce
 column_reduce(output_inds=None, atol=1e12, cache=None, inplace=False)[source]¶
Find bonds on this tensor network which have tensors where all but one column (of the respective index) is nonzero, allowing the ‘cutting’ of that bond.
 Parameters:
output_inds (sequence of str, optional) – Which indices to explicitly consider as outer legs of the tensor network and thus not slice. If not given, these will be taken as all the indices that appear once.
atol (float, optional) – When identifying singlet column tensors, the absolute tolerance with which to compare to zero with.
cache (None or set) – Persistent cache used to mark already checked tensors.
inplace – Whether to perform the column reductions inplace.
bool – Whether to perform the column reductions inplace.
optional – Whether to perform the column reductions inplace.
 Return type:
See also
full_simplify
,rank_simplify
,diagonal_reduce
,antidiag_gauge
 split_simplify(atol=1e12, equalize_norms=False, cache=None, inplace=False, **split_opts)[source]¶
Find tensors which have low rank SVD decompositions across any combination of bonds and perform them.
 Parameters:
atol (float, optional) – Cutoff used when attempting low rank decompositions.
equalize_norms (bool or float) – Actively renormalize the tensors during the simplification process. Useful for very large TNs. The scaling factor will be stored as an exponent in
tn.exponent
.cache (None or set) – Persistent cache used to mark already checked tensors.
inplace – Whether to perform the split simplification inplace.
bool – Whether to perform the split simplification inplace.
optional – Whether to perform the split simplification inplace.
 gen_loops(max_loop_length=None)[source]¶
Generate sequences of tids that represent loops in the TN.
 Parameters:
max_loop_length (None or int) – Set the maximum number of tensors that can appear in a loop. If
None
, wait until any loop is found and set that as the maximum length. Yields:
tuple[int]
See also
 gen_inds_loops(max_loop_length=None)[source]¶
Generate all sequences of indices, up to a specified length, that represent loops in this tensor network. Unlike
gen_loops
this function will return the indices of the tensors in the loop rather than the tensor ids, allowing one to differentiate between e.g. a double loop and a ‘figure of eight’ loop. Parameters:
max_loop_length (None or int) – Set the maximum number of indices that can appear in a loop. If
None
, wait until any loop is found and set that as the maximum length. Yields:
tuple[str]
See also
 gen_inds_connected(max_length)[source]¶
Generate all index ‘patches’ of size up to
max_length
. Parameters:
max_length (int) – The maximum number of indices in the patch.
 Yields:
tuple[str]
See also
 compute_shortest_distances(tids=None, exclude_inds=())[source]¶
Compute the minimum graph distances between all or some nodes
tids
.
 compute_hierarchical_linkage(tids=None, method='weighted', optimal_ordering=True, exclude_inds=())[source]¶
 compute_hierarchical_ssa_path(tids=None, method='weighted', optimal_ordering=True, exclude_inds=(), are_sorted=False, linkage=None)[source]¶
Compute a hierarchical grouping of
tids
, as assa_path
.
 compute_hierarchical_ordering(tids=None, method='weighted', optimal_ordering=True, exclude_inds=(), linkage=None)[source]¶
 compute_hierarchical_grouping(max_group_size, tids=None, method='weighted', optimal_ordering=True, exclude_inds=(), linkage=None)[source]¶
Group
tids
(by default, all tensors) into groups of sizemax_group_size
or less, using a hierarchical clustering.
 pair_simplify(cutoff=1e12, output_inds=None, max_inds=10, cache=None, equalize_norms=False, max_combinations=500, inplace=False, **split_opts)[source]¶
 loop_simplify(output_inds=None, max_loop_length=None, max_inds=10, cutoff=1e12, loops=None, cache=None, equalize_norms=False, inplace=False, **split_opts)[source]¶
Try and simplify this tensor network by identifying loops and checking for lowrank decompositions across groupings of the loops outer indices.
 Parameters:
max_loop_length (None or int, optional) – Largest length of loop to search for, if not set, the size will be set to the length of the first (and shortest) loop found.
cutoff (float, optional) – Cutoff to use for the operator decomposition.
loops (None, sequence or callable) – Loops to check, or a function that generates them.
cache (set, optional) – For performance reasons can supply a cache for already checked loops.
inplace (bool, optional) – Whether to replace the loops inplace.
split_opts – Supplied to
tensor_split()
.
 Return type:
 full_simplify(seq='ADCR', output_inds=None, atol=1e12, equalize_norms=False, cache=None, inplace=False, progbar=False, rank_simplify_opts=None, loop_simplify_opts=None, split_simplify_opts=None, custom_methods=(), split_method='svd')[source]¶
Perform a series of tensor network ‘simplifications’ in a loop until there is no more reduction in the number of tensors or indices. Note that apart from rankreduction, the simplification methods make use of the nonzero structure of the tensors, and thus changes to this will potentially produce different simplifications.
 Parameters:
seq (str, optional) –
Which simplifications and which order to perform them in.
'A'
: stands forantidiag_gauge
'D'
: stands fordiagonal_reduce
'C'
: stands forcolumn_reduce
'R'
: stands forrank_simplify
'S'
: stands forsplit_simplify
'L'
: stands forloop_simplify
If you want to keep the tensor network ‘simple’, i.e. with no hyperedges, then don’t use
'D'
(moreover'A'
is redundant).output_inds (sequence of str, optional) – Explicitly set which indices of the tensor network are output indices and thus should not be modified. If not specified the tensor network is assumed to be a ‘standard’ one where indices that only appear once are the output indices.
atol (float, optional) – The absolute tolerance when indentifying zero entries of tensors and performing lowrank decompositions.
equalize_norms (bool or float) – Actively renormalize the tensors during the simplification process. Useful for very large TNs. If True, the norms, in the formed of stripped exponents, will be redistributed at the end. If an actual number, the final tensors will all have this norm, and the scaling factor will be stored as a base10 exponent in
tn.exponent
.cache (None or set) – A persistent cache for each simplification process to mark already processed tensors.
progbar (bool, optional) – Show a live progress bar of the simplification process.
inplace (bool, optional) – Whether to perform the simplification inplace.
 Return type:
 hyperinds_resolve(mode='dense', sorter=None, output_inds=None, inplace=False)[source]¶
Convert this into a regular tensor network, where all indices appear at most twice, by inserting COPY tensor or tensor networks for each hyper index.
 Parameters:
mode ({'dense', 'mps', 'tree'}, optional) – What type of COPY tensor(s) to insert.
sorter (None or callable, optional) – If given, a function to sort the indices that a single hyperindex will be turned into. Th function is called like
tids.sort(key=sorter)
.inplace (bool, optional) – Whether to insert the COPY tensors inplace.
 Return type:
 compress_simplify(output_inds=None, atol=1e06, simplify_sequence_a='ADCRS', simplify_sequence_b='RPL', hyperind_resolve_mode='tree', hyperind_resolve_sort='clustering', final_resolve=False, split_method='svd', max_simplification_iterations=100, converged_tol=0.01, equalize_norms=True, progbar=False, inplace=False, **full_simplify_opts)[source]¶
 property shape¶
 Actual, i.e. exterior, shape of this TensorNetwork.
 property dtype¶
 The dtype of this TensorNetwork, this is the minimal common type
 of all the tensors data.
 quimb.tensor.tensor_1d_compress.ensure_dict(x)[source]¶
Make sure
x
is adict
, creating an empty one ifx is None
.
 quimb.tensor.tensor_1d_compress.rand_uuid(base='')[source]¶
Return a guaranteed unique, shortish identifier, optional appended to
base
.Examples
>>> rand_uuid() '_2e1dae1b'
>>> rand_uuid('virtbond') 'virtbond_bf342e68'
 quimb.tensor.tensor_1d_compress.tensor_contract(*tensors, output_inds=None, optimize=None, get=None, backend=None, preserve_tensor=False, drop_tags=False, **contract_opts)[source]¶
Contract a collection of tensors into a scalar or tensor, automatically aligning their indices and computing an optimized contraction path. The output tensor will have the union of tags from the input tensors.
 Parameters:
tensors (sequence of Tensor) – The tensors to contract.
output_inds (sequence of str) – The output indices. These can be inferred if the contraction has no ‘hyper’ indices, in which case the output indices are those that appear only once in the input indices, and ordered as they appear in the inputs. For hyper indices or a specific ordering, these must be supplied.
optimize ({None, str, path_like, PathOptimizer}, optional) –
The contraction path optimization strategy to use.
None
: use the default strategy,str: use the preset strategy with the given name,
path_like: use this exact path,
cotengra.HyperOptimizer
: find the contraction using this optimizer, supports slicing,cotengra.ContractionTree
: use this exact tree, supports slicing,opt_einsum.PathOptimizer
: find the path using this optimizer.
Contraction with
cotengra
might be a bit more efficient but the main reason would be to handle sliced contraction automatically, as well as the fact that it usesautoray
internally.get (str, optional) –
What to return. If:
None
(the default)  return the resulting scalar or Tensor.'expression'
 return a callbable expression that performs the contraction and operates on the raw arrays.'tree'
 return thecotengra.ContractionTree
describing the contraction.'path'
 return the raw ‘path’ as a list of tuples.'symbolmap'
 return the dict mapping indices to ‘symbols’ (single unicode letters) used internally bycotengra
'pathinfo'
 return theopt_einsum.PathInfo
path object with detailed information such as flop cost. The symbolmap is also added to thequimb_symbol_map
attribute.
backend ({'auto', 'numpy', 'jax', 'cupy', 'tensorflow', ...}, optional) – Which backend to use to perform the contraction. Supplied to cotengra.
preserve_tensor (bool, optional) – Whether to return a tensor regardless of whether the output object is a scalar (has no indices) or not.
drop_tags (bool, optional) – Whether to drop all tags from the output tensor. By default the output tensor will keep the union of all tags from the input tensors.
contract_opts – Passed to
cotengra.array_contract
.
 Return type:
scalar or Tensor
 quimb.tensor.tensor_1d_compress.enforce_1d_like(tn, site_tags=None, fix_bonds=True, inplace=False)[source]¶
Check that
tn
is 1Dlike with OBC, i.e. 1) that each tensor has exactly one of the givensite_tags
. If not, raise a ValueError. 2) That there are no hyper indices. And 3) that there are only bonds within sites or between nearest neighbor sites. This issue can be optionally automatically fixed by inserting a string of identity tensors. Parameters:
tn (TensorNetwork) – The tensor network to check.
site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
.fix_bonds (bool, optional) – Whether to fix the bond structure by inserting identity tensors.
inplace (bool, optional) – Whether to perform the fix inplace or not.
 Raises:
ValueError – If the tensor network is not 1Dlike.
 quimb.tensor.tensor_1d_compress.tensor_network_1d_compress_direct(tn, max_bond=None, cutoff=1e10, site_tags=None, normalize=False, canonize=True, cutoff_mode='rsum2', permute_arrays=True, optimize='autohq', sweep_reverse=False, equalize_norms=False, inplace=False, **compress_opts)[source]¶
Compress a 1Dlike tensor network using the ‘direct’ or ‘naive’ method, that is, explicitly contracting sitewise to form a MPSlike TN, canonicalizing in one direction, then compressing in the other. This has the same scaling as the density matrix (dm) method, but a larger prefactor. It can still be faster for small bond dimensions however, and is potentially higher precision since it works in the space of singular values directly rather than singular values squared. It is not quite optimal in terms of error due to the compounding errors of the SVDs.
 Parameters:
tn (TensorNetwork) – The tensor network to compress. Every tensor should have exactly one of the site tags. Each site can have multiple tensors and output indices.
max_bond (int) – The maximum bond dimension to compress to.
cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing.
site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.normalize (bool, optional) – Whether to normalize the final tensor network, making use of the fact that the output tensor network is in right canonical form.
canonize (bool, optional) – Whether to canonicalize the network in one direction before compressing in the other.
cutoff_mode ({"rsum2", "rel", ...}, optional) – The mode to use when truncating the singular values of the decomposed tensors. See
tensor_split()
.permute_arrays (bool or str, optional) – Whether to permute the array indices of the final tensor network into canonical order. If
True
will use the default order, otherwise if a string this specifies a custom order.optimize (str, optional) – The contraction path optimizer to use.
sweep_reverse (bool, optional) – Whether to sweep in the reverse direction, resulting in a left canonical form instead of right canonical.
equalize_norms (bool, optional) – Whether to renormalize the tensors during the compression procedure. If
True
the gathered exponent will be redistributed equally among the tensors. If a float, all tensors will be renormalized to this value, and the gathered exponent is tracked intn.exponent
of the returned tensor network.inplace (bool, optional) – Whether to perform the compression inplace or not.
compress_opts – Supplied to
tensor_split()
.
 Returns:
The compressed tensor network, with canonical center at
site_tags[0]
(‘right canonical’ form) orsite_tags[1]
(‘left canonical’ form) ifsweep_reverse
. Return type:
 quimb.tensor.tensor_1d_compress.tensor_network_1d_compress_dm(tn, max_bond=None, cutoff=1e10, site_tags=None, normalize=False, cutoff_mode='rsum1', permute_arrays=True, optimize='autohq', sweep_reverse=False, canonize=True, equalize_norms=False, inplace=False, **compress_opts)[source]¶
Compress any 1Dlike tensor network using the ‘density matrix’ method (https://tensornetwork.org/mps/algorithms/denmat_mpo_mps/).
While this has the same scaling as the direct method, in practice it can often be faster, especially at large bond dimensions. Potentially there are some situations where the direct method is more stable with regard to precision, since the density matrix method works in the ‘squared’ picture.
 Parameters:
tn (TensorNetwork) – The tensor network to compress. Every tensor should have exactly one of the site tags. Each site can have multiple tensors and output indices.
max_bond (int) – The maximum bond dimension to compress to.
cutoff (float, optional) – The truncation error to use when compressing the double layer tensor network.
site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.normalize (bool, optional) – Whether to normalize the final tensor network, making use of the fact that the output tensor network is in right canonical form.
cutoff_mode ({"rsum1", "rel", ...}, optional) – The mode to use when truncating the singular values of the decomposed tensors. See
tensor_split()
. Note for the density matrix method the default ‘rsum1’ mode acts like ‘rsum2’ for the direct method due to truncating in the squared space.permute_arrays (bool or str, optional) – Whether to permute the array indices of the final tensor network into canonical order. If
True
will use the default order, otherwise if a string this specifies a custom order.optimize (str, optional) – The contraction path optimizer to use.
sweep_reverse (bool, optional) – Whether to sweep in the reverse direction, resulting in a left canonical form instead of right canonical.
canonize (bool, optional) – Dummy argument to match the signature of other compression methods.
equalize_norms (bool or float, optional) – Whether to equalize the norms of the tensors after compression. If an explicit value is give, then the norms will be set to that value, and the overall scaling factor will be accumulated into .exponent.
inplace (bool, optional) – Whether to perform the compression inplace or not.
compress_opts – Supplied to
tensor_split()
.
 Returns:
The compressed tensor network, with canonical center at
site_tags[0]
(‘right canonical’ form) orsite_tags[1]
(‘left canonical’ form) ifsweep_reverse
. Return type:
 quimb.tensor.tensor_1d_compress.tensor_network_1d_compress_zipup(tn, max_bond=None, cutoff=1e10, site_tags=None, canonize=True, normalize=False, cutoff_mode='rsum2', permute_arrays=True, optimize='autohq', sweep_reverse=False, equalize_norms=False, inplace=False, **compress_opts)[source]¶
Compress a 1Dlike tensor network using the ‘zipup’ algorithm due to ‘Minimally Entangled Typical Thermal State Algorithms’, E.M. Stoudenmire & Steven R. White (https://arxiv.org/abs/1002.1305). The returned tensor network will have one tensor per site, in the order given by
site_tags
, with canonical center atsite_tags[0]
(‘right’ canonical form).The zipup algorithm scales better than the direct and density matrix methods when multiple tensors are present at each site (such as MPOMPS multiplication), but is less accurate due to the compressions taking place in an only pseudocanonical gauge. It generally also only makes sense in the fixed bond dimension case, as opposed to relying on a specific cutoff only.
 Parameters:
tn (TensorNetwork) – The tensor network to compress. Every tensor should have exactly one of the site tags. Each site can have multiple tensors and output indices.
max_bond (int) – The maximum bond dimension to compress to.
cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing.
site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.canonize (bool, optional) – Whether to pseudo canonicalize the initial tensor network.
normalize (bool, optional) – Whether to normalize the final tensor network, making use of the fact that the output tensor network is in right canonical form.
cutoff_mode ({"rsum2", "rel", ...}, optional) – The mode to use when truncating the singular values of the decomposed tensors. See
tensor_split()
.permute_arrays (bool or str, optional) – Whether to permute the array indices of the final tensor network into canonical order. If
True
will use the default order, otherwise if a string this specifies a custom order.optimize (str, optional) – The contraction path optimizer to use.
sweep_reverse (bool, optional) – Whether to sweep in the reverse direction, resulting in a left canonical form instead of right canonical.
equalize_norms (bool or float, optional) – Whether to equalize the norms of the tensors after compression. If an explicit value is give, then the norms will be set to that value, and the overall scaling factor will be accumulated into .exponent.
inplace (bool, optional) – Whether to perform the compression inplace or not.
compress_opts – Supplied to
tensor_split()
.
 Returns:
The compressed tensor network, with canonical center at
site_tags[0]
(‘right canonical’ form) orsite_tags[1]
(‘left canonical’ form) ifsweep_reverse
. Return type:
 quimb.tensor.tensor_1d_compress.tensor_network_1d_compress_zipup_first(tn, max_bond=None, max_bond_zipup=None, cutoff=1e10, cutoff_zipup=None, site_tags=None, canonize=True, normalize=False, cutoff_mode='rsum2', permute_arrays=True, optimize='autohq', sweep_reverse=False, equalize_norms=False, inplace=False, **compress_opts)[source]¶
Compress this 1Dlike tensor network using the ‘zipup first’ algorithm, that is, first compressing the tensor network to a larger bond dimension using the ‘zipup’ algorithm, then compressing to the desired bond dimension using a direct sweep.
Depending on the value of
max_bond
andmax_bond_zipup
, this can be scale better than the direct and density matrix methods, but reach close to the same accuracy. As with the ‘zipup’ method, there is no advantage unless there are multiple tensors per site, and it generally only makes sense in the fixed bond dimension case, as opposed to relying on a specific cutoff only. Parameters:
tn (TensorNetwork) – The tensor network to compress. Every tensor should have exactly one of the site tags. Each site can have multiple tensors and output indices.
max_bond (int) – The final maximum bond dimension to compress to.
max_bond_zipup (int, optional) – The intermediate maximum bond dimension to compress to using the ‘zipup’ algorithm. If not given and max_bond is, this is set as twice the target bond dimension,
2 * max_bond
.cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing.
cutoff_zipup (float, optional) – A dynamic threshold for discarding singular values when compressing to the intermediate bond dimension using the ‘zipup’ algorithm. If not given, this is set to the same as
cutoff
if a maximum bond is given, elsecutoff / 10
.site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.canonize (bool, optional) – Whether to pseudo canonicalize the initial tensor network.
normalize (bool, optional) – Whether to normalize the final tensor network, making use of the fact that the output tensor network is in right canonical form.
cutoff_mode ({"rsum2", "rel", ...}, optional) – The mode to use when truncating the singular values of the decomposed tensors. See
tensor_split()
.permute_arrays (bool or str, optional) – Whether to permute the array indices of the final tensor network into canonical order. If
True
will use the default order, otherwise if a string this specifies a custom order.optimize (str, optional) – The contraction path optimizer to use.
sweep_reverse (bool, optional) – Whether to sweep in the reverse direction, resulting in a left canonical form instead of right canonical.
equalize_norms (bool or float, optional) – Whether to equalize the norms of the tensors after compression. If an explicit value is give, then the norms will be set to that value, and the overall scaling factor will be accumulated into .exponent.
inplace (bool, optional) – Whether to perform the compression inplace or not.
compress_opts – Supplied to
tensor_split()
.
 Returns:
The compressed tensor network, with canonical center at
site_tags[0]
(‘right canonical’ form) orsite_tags[1]
(‘left canonical’ form) ifsweep_reverse
. Return type:
 quimb.tensor.tensor_1d_compress._tn1d_fit_sum_sweep_1site(tn_fit, tn_overlaps, site_tags, max_bond=None, cutoff=0.0, envs=None, prepare=True, reverse=False, compute_tdiff=True, optimize='autohq')[source]¶
Core sweep of the 1site 1D fit algorithm.
 quimb.tensor.tensor_1d_compress._tn1d_fit_sum_sweep_2site(tn_fit, tn_overlaps, site_tags, max_bond=None, cutoff=1e10, envs=None, prepare=True, reverse=False, optimize='autohq', compute_tdiff=True, **compress_opts)[source]¶
Core sweep of the 2site 1D fit algorithm.
 quimb.tensor.tensor_1d_compress.tensor_network_1d_compress_fit(tns, max_bond=None, cutoff=None, tn_fit=None, bsz='auto', initial_bond_dim=8, max_iterations=10, tol=0.0, site_tags=None, cutoff_mode='rsum2', sweep_sequence='RL', normalize=False, permute_arrays=True, optimize='autohq', canonize=True, sweep_reverse=False, equalize_norms=False, inplace_fit=False, inplace=False, progbar=False, **compress_opts)[source]¶
Compress any 1Dlike (can have multiple tensors per site) tensor network or sum of tensor networks to an exactly 1D (one tensor per site) tensor network of bond dimension max_bond using the 1site or 2site variational fitting (or ‘DMRGstyle’) method. The tensor network(s) can have arbitrary inner and outer structure.
This method has the lowest scaling of the standard 1D compression methods and can also provide the most accurate compression, but the actual speed and accuracy depend on the number of iterations required and initial guess, making it a more ‘handson’ method.
It’s also the only method to support fitting to a sum of tensor networks directly, rather than having to forming the explicitly summed TN first.
 Parameters:
tns (TensorNetwork or Sequence[TensorNetwork]) – The tensor network or tensor networks to compress. Each tensor network should have the same outer index structure, and within each tensor network every tensor should have exactly one of the site tags.
max_bond (int) – The maximum bond dimension to compress to. If not given, this is set as the maximum bond dimension of the initial guess tensor network, if any, else infinite for
bsz=2
.cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing. This is only relevant for the 2site sweeping algorithm (
bsz=2
), where it defaults to 1e10.tn_fit (TensorNetwork, dict, or str, optional) – An initial guess for the compressed tensor network. It should matching outer indices and site tags with
tn
. If a dict, this is assumed to be options to supply to tensor_network_1d_compress to construct the initial guess, inheriting various defaults like initial_bond_dim. If a string, e.g."zipup"
, this is shorthand for that compression method with default settings. If not given, a random 1D tensor network will be used.bsz ({"auto", 1, 2}, optional) – The size of the block to optimize while sweeping. If
"auto"
, this will be inferred from the value ofmax_bond
andcutoff
.initial_bond_dim (int, optional) – The initial bond dimension to use when creating the initial guess. This is only relevant if
tn_fit
is not given. For each sweep the allowed bond dimension is doubled, up tomax_bond
. For 1site this occurs via explicit bond expansion, while for 2site it occurs during the 2site tensor decomposition.max_iterations (int, optional) – The maximum number of variational sweeps to perform.
tol (float, optional) – The convergence tolerance, in terms of local tensor distance normalized. If zero, there will be exactly
max_iterations
sweeps.site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.cutoff_mode ({"rsum2", "rel", ...}, optional) – The mode to use when truncating the singular values of the decomposed tensors. See
tensor_split()
, if using the 2site sweeping algorithm.sweep_sequence (str, optional) – The sequence of sweeps to perform, e.g.
"LR"
means first sweep left to right, then right to left. The sequence is cycled.normalize (bool, optional) – Whether to normalize the final tensor network, making use of the fact that the output tensor network is in left or right canonical form.
permute_arrays (bool or str, optional) – Whether to permute the array indices of the final tensor network into canonical order. If
True
will use the default order, otherwise if a string this specifies a custom order.optimize (str, optional) – The contraction path optimizer to use.
canonize (bool, optional) – Dummy argument to match the signature of other compression methods.
sweep_reverse (bool, optional) – Whether to sweep in the reverse direction, swapping whether the final tensor network is in right or left canonical form, which also depends on the last sweep direction.
equalize_norms (bool or float, optional) – Whether to equalize the norms of the tensors after compression. If an explicit value is give, then the norms will be set to that value, and the overall scaling factor will be accumulated into .exponent.
inplace_fit (bool, optional) – Whether to perform the compression inplace on the initial guess tensor network,
tn_fit
, if supplied.inplace (bool, optional) – Whether to perform the compression inplace on the target tensor network supplied, or
tns[0]
if a sequence to sum is supplied.progbar (bool, optional) – Whether to show a progress bar. Note the progress bar shows the maximum change of any single tensor norm, not the global change in norm or truncation error.
compress_opts – Supplied to
tensor_split()
, if using the 2site sweeping algorithm.
 Returns:
The compressed tensor network. Depending on
sweep_reverse
and the last sweep direction, the canonical center will be at either L:site_tags[0]
or R:site_tags[1]
, or the opposite ifsweep_reverse
. Return type:
 quimb.tensor.tensor_1d_compress._TN1D_COMPRESS_METHODS¶
 quimb.tensor.tensor_1d_compress.tensor_network_1d_compress(tn, max_bond=None, cutoff=1e10, method='dm', site_tags=None, canonize=True, permute_arrays=True, optimize='autohq', sweep_reverse=False, equalize_norms=False, compress_opts=None, inplace=False, **kwargs)[source]¶
Compress a 1Dlike tensor network using the specified method.
 Parameters:
tn (TensorNetwork) – The tensor network to compress. Every tensor should have exactly one of the site tags. Each site can have multiple tensors and output indices.
max_bond (int) – The maximum bond dimension to compress to.
cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing.
method ({"direct", "dm", "zipup", "zipupfirst", "fit", "projector"}) – The compression method to use.
site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.canonize (bool, optional) – Whether to perform canonicalization, pseudo or otherwise depending on the method, before compressing. Ignored for
method='dm'
andmethod='fit'
.permute_arrays (bool or str, optional) – Whether to permute the array indices of the final tensor network into canonical order. If
True
will use the default order, otherwise if a string this specifies a custom order.optimize (str, optional) – The contraction path optimizer to use.
sweep_reverse (bool, optional) – Whether to sweep in the reverse direction, resulting in a left canonical form instead of right canonical (for the fit method, this also depends on the last sweep direction).
equalize_norms (bool or float, optional) – Whether to equalize the norms of the tensors after compression. If an explicit value is give, then the norms will be set to that value, and the overall scaling factor will be accumulated into .exponent.
inplace (bool, optional) – Whether to perform the compression inplace.
kwargs – Supplied to the chosen compression method.
 Return type:
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_lazy(mps, mpo, inplace=False)[source]¶
Apply an MPO to an MPS lazily, i.e. nothing is contracted, but the new TN object has the same outer indices as the original MPS.
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_direct(mps, mpo, max_bond=None, cutoff=1e10, inplace=False, **compress_opts)[source]¶
Apply an MPO to an MPS using the boundary compression method, that is, explicitly contracting sitewise to form a MPSlike TN, canonicalizing in one direction, then compressing in the other. This has the same scaling as the density matrix (dm) method, but a larger prefactor. It can still be faster for small bond dimensions however, and is potentially higher precision since it works in the space of singular values directly rather than singular values squared. It is not quite optimal in terms of error due to the compounding errors of the SVDs.
 Parameters:
mps (MatrixProductState) – The MPS to gate.
mpo (MatrixProductOperator) – The MPO to gate with.
max_bond (int) – The maximum bond dimension to compress to.
cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing.
compress_opts – Supplied to
tensor_split()
.
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_dm(mps, mpo, max_bond=None, cutoff=1e10, inplace=False, **compress_opts)[source]¶
Gate this MPS with an MPO, using the density matrix compression method.
 Parameters:
mps (MatrixProductState) – The MPS to gate.
mpo (MatrixProductOperator) – The MPO to gate with.
max_bond (int, optional) – The maximum bond dimension to keep when compressing the double layer tensor network, if any.
cutoff (float, optional) – The truncation error to use when compressing the double layer tensor network, if any.
compress_opts – Supplied to
tensor_split()
.
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_zipup(mps, mpo, max_bond=None, cutoff=1e10, canonize=True, optimize='autohq', **compress_opts)[source]¶
Apply an MPO to an MPS using the ‘zipup’ algorithm due to ‘Minimally Entangled Typical Thermal State Algorithms’, E.M. Stoudenmire & Steven R. White (https://arxiv.org/abs/1002.1305).
 Parameters:
mps (MatrixProductState) – The MPS to gate.
mpo (MatrixProductOperator) – The MPO to gate with.
max_bond (int) – The maximum bond dimension to compress to.
cutoff (float, optional) – A dynamic threshold for discarding singular values when compressing.
site_tags (sequence of str, optional) – The tags to use to group and order the tensors from
tn
. If not given, usestn.site_tags
. The tensor network built will have one tensor per site, in the order given bysite_tags
.canonize (bool, optional) – Whether to pseudo canonicalize the initial tensor network.
normalize (bool, optional) – Whether to normalize the final tensor network, making use of the fact that the output tensor network is in right canonical form.
permute_arrays (bool or str, optional) – Whether to permute the array indices of the final tensor network into canonical order. If
True
will use the default order, otherwise if a string this specifies a custom order.optimize (str, optional) – The contraction path optimizer to use.
compress_opts – Supplied to
tensor_split()
.
 Returns:
The compressed MPS, in right canonical form.
 Return type:
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_zipup_first(mps, mpo, max_bond=None, max_bond_zipup=None, cutoff=1e10, cutoff_zipup=None, canonize=True, optimize='autohq', **compress_opts)[source]¶
Apply an MPO to an MPS by first using the zipup method with a larger bond dimension, then doing a regular compression sweep to the target final bond dimension. This avoids forming an intermediate MPS with bond dimension
mps.max_bond() * mpo.max_bond()
. Parameters:
mps (MatrixProductState) – The MPS to gate.
mpo (MatrixProductOperator) – The MPO to gate with.
max_bond (int) – The target final bond dimension.
max_bond_zipup (int, optional) – The maximum bond dimension to use when zipup compressing the double layer tensor network. If not given, defaults to
2 * max_bond
. Needs to be smaller thanmpo.max_bond()
for any savings.cutoff (float, optional) – The truncation error to use when performing the final regular compression sweep.
cutoff_zipup (float, optional) – The truncation error to use when performing the zipup compression.
canonize (bool, optional) – Whether to pseudo canonicalize the initial tensor network.
optimize (str, optional) – The contraction path optimizer to use.
compress_opts – Supplied to
tensor_split()
(both the zipup and final sweep).
 Returns:
The compressed MPS, in right canonical form.
 Return type:
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_fit(mps, mpo, max_bond, **kwargs)[source]¶
Gate an MPS with an MPO using the variational fitting or DMRGstyle method.
 Parameters:
mps (MatrixProductState) – The MPS to gate.
mpo (MatrixProductOperator) – The MPO to gate with.
max_bond (int) – The maximum bond dimension to compress to.
 Returns:
The gated MPS.
 Return type:
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_autofit(self, mpo, max_bond, cutoff=0.0, init_guess=None, **fit_opts)[source]¶
Fit a MPS to a MPO applied to an MPS using geometry generic versions of either ALS or autodiff. This is usually much less efficient that using the 1D specific methods.
Some nice alternatives to the default fit_opts:
method=”autodiff”
method=”als”, solver=”lstsq”
 quimb.tensor.tensor_1d_compress.mps_gate_with_mpo_projector(self, mpo, max_bond, cutoff=1e10, canonize=True, canonize_opts=None, inplace=False, **compress_opts)[source]¶
Apply an MPO to an MPS using local projectors, in the style of CTMRG or HOTRG, without using information beyond the neighboring 4 tensors.