Skip to main content

A high level tensor network API for accelerated tensor network calculations.

Project description

To make the upstream of TensorCircuit-NG more robust and survive the breaking API changes from Python to Jax, this fork version of TensorNetwork will be further maintained (still at very early development stage).

A tensor network wrapper for TensorFlow, JAX, PyTorch, and Numpy.

For an overview of tensor networks please see the following:

More information can be found in our TensorNetwork papers:

Installation

pip3 install tensornetwork

Documentation

For details about the TensorNetwork API, see the reference documentation.

Tutorials

Basic API tutorial

Tensor Networks inside Neural Networks using Keras

Basic Example

Here, we build a simple 2 node contraction.

import numpy as np
import tensornetwork as tn

# Create the nodes
a = tn.Node(np.ones((10,)))
b = tn.Node(np.ones((10,)))
edge = a[0] ^ b[0] # Equal to tn.connect(a[0], b[0])
final_node = tn.contract(edge)
print(final_node.tensor) # Should print 10.0

Optimized Contractions.

Usually, it is more computationally effective to flatten parallel edges before contracting them in order to avoid trace edges. We have contract_between and contract_parallel that do this automatically for your convenience.

# Contract all of the edges between a and b
# and create a new node `c`.
c = tn.contract_between(a, b)
# This is the same as above, but much shorter.
c = a @ b

# Contract all of edges that are parallel to edge
# (parallel means connected to the same nodes).
c = tn.contract_parallel(edge)

Split Node

You can split a node by doing a singular value decomposition.

# This will return two nodes and a tensor of the truncation error.
# The two nodes are the unitary matrices multiplied by the square root of the
# singular values.
# The `left_edges` are the edges that will end up on the `u_s` node, and `right_edges`
# will be on the `vh_s` node.
u_s, vh_s, trun_error = tn.split_node(node, left_edges, right_edges)
# If you want the singular values in it's own node, you can use `split_node_full_svd`.
u, s, vh, trun_error = tn.split_node_full_svd(node, left_edges, right_edges)

Node and Edge names.

You can optionally name your nodes/edges. This can be useful for debugging, as all error messages will print the name of the broken edge/node.

node = tn.Node(np.eye(2), name="Identity Matrix")
print("Name of node: {}".format(node.name))
edge = tn.connect(node[0], node[1], name="Trace Edge")
print("Name of the edge: {}".format(edge.name))
# Adding name to a contraction will add the name to the new edge created.
final_result = tn.contract(edge, name="Trace Of Identity")
print("Name of new node after contraction: {}".format(final_result.name))

Named axes.

To make remembering what an axis does easier, you can optionally name a node's axes.

a = tn.Node(np.zeros((2, 2)), axis_names=["alpha", "beta"])
edge = a["beta"] ^ a["alpha"]

Edge reordering.

To assert that your result's axes are in the correct order, you can reorder a node at any time during computation.

a = tn.Node(np.zeros((1, 2, 3)))
e1 = a[0]
e2 = a[1]
e3 = a[2]
a.reorder_edges([e3, e1, e2])
# If you already know the axis values, you can equivalently do
# a.reorder_axes([2, 0, 1])
print(a.tensor.shape) # Should print (3, 1, 2)

NCON interface.

For a more compact specification of a tensor network and its contraction, there is ncon(). For example:

from tensornetwork import ncon
a = np.ones((2, 2))
b = np.ones((2, 2))
c = ncon([a, b], [(-1, 1), (1, -2)])
print(c)

Different backend support.

Currently, we support JAX, TensorFlow, PyTorch and NumPy as TensorNetwork backends. We also support tensors with Abelian symmetries via a symmetric backend, see the reference documentation for more details.

To change the default global backend, you can do:

tn.set_default_backend("jax") # tensorflow, pytorch, numpy, symmetric

Or, if you only want to change the backend for a single Node, you can do:

tn.Node(tensor, backend="jax")

If you want to run your contractions on a GPU, we highly recommend using JAX, as it has the closet API to NumPy.

Disclaimer

This library is in alpha and will be going through a lot of breaking changes. While releases will be stable enough for research, we do not recommend using this in any production environment yet.

TensorNetwork is not an official Google product. Copyright 2019 The TensorNetwork Developers.

Citation

If you are using TensorNetwork for your research please cite this work using the following bibtex entry:

@misc{roberts2019tensornetwork,
      title={TensorNetwork: A Library for Physics and Machine Learning},
      author={Chase Roberts and Ashley Milsted and Martin Ganahl and Adam Zalcman and Bruce Fontaine and Yijian Zou and Jack Hidary and Guifre Vidal and Stefan Leichenauer},
      year={2019},
      eprint={1905.01330},
      archivePrefix={arXiv},
      primaryClass={physics.comp-ph}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensornetwork_ng-0.5.1.tar.gz (130.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tensornetwork_ng-0.5.1-py3-none-any.whl (244.1 kB view details)

Uploaded Python 3

File details

Details for the file tensornetwork_ng-0.5.1.tar.gz.

File metadata

  • Download URL: tensornetwork_ng-0.5.1.tar.gz
  • Upload date:
  • Size: 130.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.0

File hashes

Hashes for tensornetwork_ng-0.5.1.tar.gz
Algorithm Hash digest
SHA256 b123073969c3a5440bc364f7a0c1e428b19d3b7c0c7b4c7e6aa2e936bd7dbaf3
MD5 95a73c5b8ccec4ff47cdd740f1deb75d
BLAKE2b-256 30e3738c9f253c790c0964f388711757e9f0af3b3f75e3fe2cf7427c9c34d290

See more details on using hashes here.

File details

Details for the file tensornetwork_ng-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tensornetwork_ng-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9479a8fc35c14aac327b1aba345672fe1e303ae88ed8d5c896c79a429a59afd
MD5 b915a3726c315efcc69aceebeeee180a
BLAKE2b-256 debe120e746deb786ebcb4d258c161c1863900ea76234b675053c22f64ad08b7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page