Upgrading your TensorNetwork code

Chase Roberts
2 min readOct 3, 2019

During development, we’ve received user feedback that the TensorNetwork object in the tensornetwork library was more of a burden than helpful, and we have decided to remove it in favor of a “free node” design. Upgrading your code is fairly straight forward, and in many cases makes your code much cleaner than before.

Simple Upgrade

If you are just doing simple network creation and contraction, you can likely just upgrade your code with a simple find-and-replace. Here we have a very basic example.

Here is what your old code would look like.

Here is what your new code would look like.

For the most part, any time you would do net.<OPERTATION> (Such as net.split_node, or net.flatten_edges)you can simply replace it with tn.<OPERTATION> and it should just work. No more need to pass around the net object between functions anymore. Woohoo!

Backends

If you are using one of the non-default backend (Tensorflow, JAX, or PyTorch), then you can upgrade your code in one of two ways.

First, if you just used tn.set_default_backend("jax") , then no changes are required (this is the preferred way). However, if you set the backend using tn.TensorNetwork(backend="jax"), then you would need to do tn.Node(tensor, backend="jax") for each node you create.

Unless you really need to use multiple backends for the same program, we highly recommend just changing the default using set_default_backend.

Merging networks

If you ever used the net.add_subnetwork or tensornetwork.merge_networks operations, you can simply delete these lines as they are not longer needed.

Contractors

We have removed the naive and stochastic contractors in favor of the opt_einsum contractors optimal branch and greedy , each of which have their own search time/contraction performance trade offs.

When using these contractors with the new API, instead of passing the TensorNetwork object, simply pass a list/set of your nodes. If a collection of nodes is passed instead of TensorNetwork, the contractor will return a Node object. This makes get_final_node unneeded.

If keeping track of your nodes in a set is too burdensome, we have a tn.reachable(node) method that will return a set of all of the nodes reachable from a given node or set of nodes. All of our contractors require connected networks anyway, so adding this is a minor change.

And that’s it! Happy tensor networking. :-)

--

--