8.3. ncon¶
Another way to contract tensors is by using ncon. This function was originally proposed for MATLAB [PESV15]. It has a syntax where the index connectivity and ordering of the indices on the resulting tensor are defined by integer values. Note that ncon requires the user to know the ordering of the bonds in a tensor. Labels are not made use of when using ncon.
For a tensor contraction with ncon, we first construct a labelled diagram of the desired network contraction. Indices are labelled by integer numbers:
Each index that shall be contracted is labelled with a positive integer (typically starting with 1, but this is not required).
External indices, which shall not be contracted, are labelled with sequential negative integers [-1,-2,-3,…]. These values define the index order of the final tensor, which has -1 as the first index, -2 as the second, …
Following this, the ncon routine is called as:
- OutputTensor = ncon(tensor_list_in, connect_list_in, cont_order)
- Parameters:
tensor_list_in (list) – 1D array containing the tensors comprising the network
connect_list_in (list) – 1D array of vectors. The kth element contains the integer index labels of the kth tensor in tensor_list_in. These integers are defined by the diagram. Their order must correspond to the ordering of indices on the corresponding tensor.
cont_order (list) – a vector containing the positive integer labels of the diagram. It is used to specify the order in which ncon contracts the indices. Note that cont_order is an optional input that can be omitted if desired, in which case ncon will contract in ascending order of the integer values.
For example, we want to contract the following tensor network (as before with Contracts()) consisting of tensors A1, A2 and M:
In the figure we labelled the internal bonds using unique positive numbers. External legs have negative integer values. This figure translates into an ncon call:
In Python:
1# Creating A1, A2, M
2A1 = cytnx.UniTensor(
3 cytnx.random.normal(
4 [2,8,8], mean=0., std=1.,
5 dtype=cytnx.Type.ComplexDouble))
6
7A2 = A1.Conj()
8M = cytnx.UniTensor(cytnx.ones([2,2,4,4]))
9
10# Calling ncon
11Res = cytnx.ncon([A1,M,A2],
12 [[1,-1,-2],[1,2,-3,-4],[2,-5,-6]])
13Res.print_diagram()
-----------------------
tensor Name :
tensor Rank : 6
block_form : False
is_diag : False
on device : cytnx device: CPU
--------
/ \
| 8 |____ -1
| |
| 8 |____ -2
| |
| 4 |____ -3
| |
| 4 |____ -4
| |
| 8 |____ -5
| |
| 8 |____ -6
\ /
--------
We see that ncon accomplishes contractions similar to Contracts or a contraction Network. While the code becomes very compact with ncon, the user must take care of the correct index order of all tensors.
Robert N. C. Pfeifer, Glen Evenbly, Sukhwinder Singh, and Guifre Vidal. Ncon: a tensor network contractor for matlab. 2015. arXiv:1402.0939.