jcmgray / quimb

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

Compressed contraction with hyperedges #203

Closed jjcmoon closed 1 year ago

jjcmoon commented 1 year ago

What happened?

The compressed contraction crashes in some cases when using multiple hyperedges. See a minimal example below. It only seems to occur when the hyperedges have an odd number of inputs. The exact contraction works fine.

Minimal Complete Verifiable Example

import quimb.tensor as qtn
import numpy as np

n = 3
tensors = [qtn.Tensor(np.random.random((2, 2)), inds=("a", "b"))
           for _ in range(n)]
tensor_network = qtn.TensorNetwork(tensors)

# works fine
tensor_network.contract(output_inds=())
# errors when n is odd (3, 5, 7, ...)
tensor_network.contract(max_bond=12, output_inds=())

Relevant log output

Traceback (most recent call last):
  File "my_script.py", line 12, in <module>
    tensor_network.contract(max_bond=12, output_inds=())
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/tensor_core.py", line 7857, in contract
    return self.contract_compressed(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/tensor_core.py", line 7379, in contract_compressed
    return self._contract_compressed_tid_sequence(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/tensor_core.py", line 7171, in _contract_compressed_tid_sequence
    tid_new, t_new = _do_contraction(tid1, tid2)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/tensor_core.py", line 7028, in _do_contraction
    tn._contract_between_tids(
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/tensor_core.py", line 5125, in _contract_between_tids
    t12 = tensor_contract(
          ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/tensor_core.py", line 221, in tensor_contract
    expression = get_contractor(eq, *shapes, **contract_opts)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/contraction.py", line 288, in get_contractor
    expr = expr_fn(eq, *shapes, optimize=path, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/quimb/tensor/contraction.py", line 136, in _get_contract_expr
    oe.contract_expression(eq, *shapes, **kwargs)
  File "/opt/homebrew/lib/python3.11/site-packages/opt_einsum/contract.py", line 882, in contract_expression
    return contract(subscripts, *dummy_arrays, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/opt_einsum/contract.py", line 497, in contract
    operands, contraction_list = contract_path(*operands,
                                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/opt_einsum/contract.py", line 238, in contract_path
    raise ValueError("Size of label '{}' for operand {} ({}) does not match previous "
ValueError: Size of label 'a' for operand 1 (2) does not match previous terms (4).

Environment

quimb==1.6.0 cotengra==0.5.0

jcmgray commented 1 year ago

Hi @jjcmoon, yes compressed contraction for hyper tensor networks is not currently defined / supported. This should probably be caught and raise an error. Some options to proceed are:

  1. call hyperinds_resolve with e.g. args ('tree', 'clustering') to convert the hyperindices to COPY tensor(s)

Or longer term, more usefully:

  1. update the compressed contraction functions to ignore hyper indices / treat them as local batch indices
  2. implement a method to compress hyperindices (basically a CP to compressed CP decomposition)

Neither of which is trivial..

In general, the compressed contraction functionality is still very experimental, and I'd note for example, that you likely need to use a contraction optimizer specifically for compressed contractions to get any savings over exact contraction. See https://cotengra.readthedocs.io/en/latest/examples/ex_compressed_contraction.html.