API Reference

This section contains the API documentation for pyQuTree.

Tree Tensor Network Optimization

class qutree.ttn.ttnopt.Logger[source]

Bases: object

class qutree.ttn.ttnopt.Objective(Err, transformer=<function Objective.<lambda>>)[source]

Bases: object

qutree.ttn.ttnopt.nparray_to_tuple(arr, precision=8)[source]
qutree.ttn.ttnopt.tn_CUR(G, O)[source]
qutree.ttn.ttnopt.ttnopt(G: DiGraph, O: Objective, nsweep: int = 6, primitive_grid: list[array] = None, start_grid=None)[source]

Run tensor network optimization on objective function using provided graph and grids.

Arguments: G (nx.DiGraph): tensor network (graph) O (Objective): objective function primitive grids (list[np.array[int]]): list of numpy arrays of

qutree.ttn.ttnopt.ttnopt_step(G, O, sweep_id)[source]

Network Structures

qutree.ttn.network.add_layer_index(G, root=None)[source]
qutree.ttn.network.add_leaves(G, f)[source]
qutree.ttn.network.back_permutation(edges, edge)[source]
qutree.ttn.network.balanced_tree(f, r=2, N=8)[source]

Generate a close-to balanced tree tensor network Node: odd f is implemented manually:

avoids adding unnecessary node by added code. See # odd-leaf tag

Parameters:
  • f (int) – Number of dimensions

  • r (int) – Bond dimension

  • N (int or list of int) – Number of grid points. Can be: - int: Same number of points for all dimensions - list of int: Per-dimension grid points

qutree.ttn.network.build_tree(f)[source]

create edges for a (close-to) balanced tree with f leaves

qutree.ttn.network.children(G, node)[source]
qutree.ttn.network.collect(G, edges, key)[source]

Graph objects from edges that correspond to ‘key’ as a list

qutree.ttn.network.flatten_back(A, shape)[source]
qutree.ttn.network.flip(edge)[source]
qutree.ttn.network.is_leaf(edge, G)[source]
qutree.ttn.network.is_leaf_node(node, G)[source]
qutree.ttn.network.pre_edges(G, edge, remove_flipped=False)[source]
qutree.ttn.network.remove_edge(G, edge)[source]
qutree.ttn.network.root(G)[source]

Return the root of a tree

qutree.ttn.network.rsweep(G)[source]
qutree.ttn.network.star_sweep(G, exclude_leafs=False)[source]
qutree.ttn.network.sweep(G, include_leaves=True)[source]
qutree.ttn.network.tensor_train_graph(f, r=2, primitive_grid: int | list[int] = 8)[source]

Generate a tensor train network

qutree.ttn.network.tensor_train_operator_graph(f, r=2, N=8)[source]

Generate a tensor train network

qutree.ttn.network.up_edge(edge, G)[source]
qutree.ttn.network.up_edges_by_distance_to_root(G, root)[source]
qutree.ttn.network.up_leaves(G)[source]

Return the leaves of a tree

qutree.ttn.network.up_sweep(G, include_leaves=True)[source]

Tensor Operations

class qutree.ttn.tensor.quTensor(array, edges, flattened_to=None)[source]

Bases: ndarray

Decorated tensor that keeps track of corresponding edges edges: those correspond to the tensor legs flattened_to: None or edge that the current tensor is flattened to. expanded_shape: shape if not permuted and flattened

Note: edges & expanded_shape is not permuted. Only

flatten(order='C')[source]

Return a copy of the array collapsed into one dimension.

Parameters:

order ({'C', 'F', 'A', 'K'}, optional) – ‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten a in the order the elements occur in memory. The default is ‘C’.

Returns:

y – A copy of the input array, flattened to one dimension.

Return type:

ndarray

See also

ravel

Return a flattened array.

flat

A 1-D flat iterator over the array.

Examples

>>> import numpy as np
>>> a = np.array([[1,2], [3,4]])
>>> a.flatten()
array([1, 2, 3, 4])
>>> a.flatten('F')
array([1, 3, 2, 4])
transpose(*axes)[source]

Returns a view of the array with axes transposed.

Refer to numpy.transpose for full documentation.

Parameters:

axes (None, tuple of ints, or n ints) –

  • None or no argument: reverses the order of the axes.

  • tuple of ints: i in the j-th place in the tuple means that the array’s i-th axis becomes the transposed array’s j-th axis.

  • n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form).

Returns:

p – View of the array with its axes suitably permuted.

Return type:

ndarray

See also

transpose

Equivalent function.

ndarray.T

Array property returning the array transposed.

ndarray.reshape

Give a new shape to an array without changing its data.

Examples

>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> a.transpose((1, 0))
array([[1, 3],
       [2, 4]])
>>> a.transpose(1, 0)
array([[1, 3],
       [2, 4]])
>>> a = np.array([1, 2, 3, 4])
>>> a
array([1, 2, 3, 4])
>>> a.transpose()
array([1, 2, 3, 4])
qutree.ttn.tensor.tensordot(A, B, edge)[source]

Tensor Network

class qutree.ttn.tensor_network.TensorNetwork(incoming_graph_data=None, **attr)[source]

Bases: DiGraph

qutree.ttn.tensor_network.contract(G, contraction_path=None)[source]
qutree.ttn.tensor_network.create_tensors(G, generator=<built-in function zeros>, key='A')[source]
qutree.ttn.tensor_network.extract_root_tensor(G)[source]

Small helper function that allows to extract a single-tensor from a tn after contraction

qutree.ttn.tensor_network.tn_to_tensor(G)[source]

Grid Operations

class qutree.ttn.grid.Grid(grid, coords)[source]

Bases: object

evaluate(func, **kwargs)[source]
num_coords()[source]
num_points()[source]
permute()[source]
random_subset(n)[source]
shape()[source]
transform(func)[source]
qutree.ttn.grid.build_node_grid(G)[source]
qutree.ttn.grid.cartesian_product(grids)[source]
qutree.ttn.grid.direct_sum(grids)[source]
qutree.ttn.grid.maxvol_grids(A, G, edge)[source]
qutree.ttn.grid.regularized_inverse(A: ndarray, lambda_reg: float, eps: float = 1e-15) ndarray[source]
Tikhonov-regularized inverse via SVD:

sigma_inv = s / (s^2 + alpha), with alpha = (lambda_eff * s_max)^2

where lambda_eff = max(lambda_reg, eps). Denominator is clamped to >= eps. This prevents divide-by-zero/overflow and stays stable even if s_max <= 0.

qutree.ttn.grid.tn_grid(G, primitive_grid, start_grid=None)[source]

Initialize a random tn grid G: tensor network grids: list of grids for each coordinate

qutree.ttn.grid.transform_node_grid(G, q_to_x)[source]

Visualization

qutree.plot.concat_pandas(dfs)[source]
qutree.plot.grid_animation(df, color='f')[source]
qutree.plot.grid_animation_to_gif(df, color='f', gif_filename='animation.gif', frames_folder='.frames')[source]
qutree.plot.plot_tn_xyz(tn, fun, q_to_x=None)[source]
qutree.plot.plot_tree(G, draw_ranks=True)[source]
qutree.plot.plot_tt_diagram(tn, draw_ranks=True)[source]
qutree.plot.plot_xyz(xyz, f, ranges=None)[source]
qutree.plot.tn_to_df(tn, fun)[source]
qutree.plot.tngrid_to_df(G, O)[source]

Matrix Factorizations