jcmgray / quimb

A python library for quantum information and many-body calculations including tensor networks.
http://quimb.readthedocs.io
Other
455 stars 107 forks source link

`contract_compressed` requires either `max_bond` or `cutoff` to be specified #143

Closed snsunx closed 1 year ago

snsunx commented 1 year ago

What happened?

I found that the TensorNetwork.contract_compressed method works on a TensorNetwork with two Tensors but does not work on a TensorNetwork with more than two Tensors.

What did you expect to happen?

I expect the method to return a Tensor object.

Minimal Complete Verifiable Example

import numpy as np
import quimb.tensor as qtn   

A = qtn.Tensor(np.random.rand(3, 4), inds=['a', 'b'], tags=['A'])
B = qtn.Tensor(np.random.rand(4, 5), inds=['b', 'c'], tags=['B'])
C = qtn.Tensor(np.random.rand(5, 4), inds=['c', 'd'], tags=['C'])
tn = A & B & C
tn.contract_compressed('greedy')

Relevant log output

Traceback (most recent call last):
  File "/central/home/ssun3/tests/test_tensor_networks.py", line 53, in <module>
    test_mps()
  File "/central/home/ssun3/tests/test_tensor_networks.py", line 49, in test_mps
    mps.contract_compressed('greedy')
  File "/home/ssun3/miniconda3/envs/tns/lib/python3.10/site-packages/quimb/tensor/tensor_core.py", line 6476, in contract_compressed
    t = tn._contract_compressed_tid_sequence(
  File "/home/ssun3/miniconda3/envs/tns/lib/python3.10/site-packages/quimb/tensor/tensor_core.py", line 6290, in _contract_compressed_tid_sequence
    _compress_neighbors(tid2, t2, d)
  File "/home/ssun3/miniconda3/envs/tns/lib/python3.10/site-packages/quimb/tensor/tensor_core.py", line 6236, in _compress_neighbors
    if bonds_size(t, t_neighb) > chi:
TypeError: '>' not supported between instances of 'int' and 'NoneType'

Anything else we need to know?

The chi variable is None, which resulted in the error.

Environment

I'm using Quimb version 1.4.0.

snsunx commented 1 year ago

If I set max_bond when calling the function, the error goes away. Is this the expected behavior? Is it possible that the user calls contract_compressed without setting max_bond, i.e. without intending to compress the tensor network?

jcmgray commented 1 year ago

Hi @snsunx, yes currently one needs to supply either max_bond or cutoff. Probably it would be useful for debugging purposes to support neither being supplied.

Cleaning up the interface and writing some docs is on my to-do list, but it is a tricky task, as this is quite an advanced function, where there is a trade-off between 'just working' with no settings, and explicitly forcing the user to engage in understanding the algorithm at some level and choosing the settings, since there is a large affect in terms of both complexity and accuracy.

I suppose I'm saying the interface is not quite finalized yet!

snsunx commented 1 year ago

That answers my question. Thank you for the clarification!