Jutho / TensorKit.jl

A Julia package for large-scale tensor computations, with a hint of category theory
MIT License
234 stars 41 forks source link

Inconsistent behaviour for tensor contraction #80

Closed philihps closed 1 year ago

philihps commented 1 year ago

Hi,

I have come across an issue after updating from TensorKit v.0.10.0 to v.0.11.0, which uses TensorOperations v.4. In the code I construct a hermitian MPO out of a direct sum of two other MPOs, both of which are non-hermitian. In TensorKit v.0.10.0 this worked fine, but after updating the resulting MPO is no longer hermitian, as is should be. Below there is a MWE for the direct sum of the MPO on a single site, which already shows a discrepancy between the two versions.

using TensorKit

# initialize MPO tensor AC and BC
AC = TensorMap(reshape([1 +2 ; -2 1], (1, 2, 2, 1)), ComplexSpace(1) ⊗ ComplexSpace(2), ComplexSpace(2) ⊗ ComplexSpace(1));
BC = TensorMap(reshape([1 -2 ; +2 1], (1, 2, 2, 1)), ComplexSpace(1) ⊗ ComplexSpace(2), ComplexSpace(2) ⊗ ComplexSpace(1));

# take sum of MPOs
isoLA = isometry(space(AC, 1) ⊕ space(BC, 1), space(AC, 1));
isoLB = leftnull(isoLA);
isoRA = isometry(space(AC, 4)' ⊕ space(BC, 4)', space(AC, 4)')';
isoRB = rightnull(isoRA);
@tensor CC[-1 -2; -3 -4] := isoLA[-1, 1] * AC[1, -2, -3, 4] * isoRA[4, -4] + isoLB[-1, 1] * BC[1, -2, -3, 4] * isoRB[4, -4];
CC = convert(Array, CC);
display(reshape(CC, size(CC, 1) * size(CC, 2), size(CC, 3) * size(CC, 4)))

Running the code with [07d1fe3e] TensorKit v0.10.0 produces the output

4×4 Matrix{Float64}:
  1.0  2.0  0.0   0.0
  0.0  0.0  1.0  -2.0
 -2.0  1.0  0.0   0.0
  0.0  0.0  2.0   1.0

while running it with [07d1fe3e] TensorKit v0.11.0 produces

4×4 Matrix{Float64}:
 0.0  0.0  0.0   0.0
 0.0  0.0  2.0  -4.0
 0.0  0.0  0.0   0.0
 0.0  0.0  4.0   2.0

I have checked that the individual tensors are the same for both versions of TensorKit, but so far I couldn't figure out what causes the difference.

lkdvos commented 1 year ago

Hi, It seems like on my machine, even with the latest version of TensorKit, the correct result is produced. My best guess is thus that this is an issue stemming from the TensorOperations rewrites that perhaps already got fixed (we did have a few bugs there). Could you try and see if updating TensorOperations to v4.0.5 (which possibly also has some updates for Strided.jl as well) fixes the problem, and if not attach a complete list of the package versions you are using?

philihps commented 1 year ago

Hi Lukas,

even with the latest version of TensorKit I get the wrong result on my machine. After a regular package update I have the following versions installed.

[07d1fe3e] TensorKit v0.11.1
[6aa20fa7] TensorOperations v4.0.2

If it works for you, the problem has probably been fixed in the meantime. However, I cannot install the latest version of TensorOperations to test it. I have tried add TensorOperations@v4.0.5, but this results in the following error.

ERROR: Unsatisfiable requirements detected for package TensorKit [07d1fe3e]:
 TensorKit [07d1fe3e] log:
 ├─possible versions are: 0.0.1-0.11.1 or uninstalled
 ├─restricted to versions * by an explicit requirement, leaving only versions: 0.0.1-0.11.1
 ├─restricted by compatibility requirements with TensorKitManifolds [11fa318c] to versions: 0.3.0-0.11.1
 │ └─TensorKitManifolds [11fa318c] log:
 │   ├─possible versions are: 0.1.0-0.6.1 or uninstalled
 │   ├─restricted to versions * by an explicit requirement, leaving only versions: 0.1.0-0.6.1
 │   └─restricted by compatibility requirements with Strided [5e0ebb24] to versions: 0.3.0-0.6.1 or uninstalled, leaving only versions: 0.3.0-0.6.1
 │     └─Strided [5e0ebb24] log:
 │       ├─possible versions are: 0.1.0-2.0.4 or uninstalled
 │       ├─restricted by compatibility requirements with TensorKit [07d1fe3e] to versions: 0.3.3-2.0.4
 │       │ └─TensorKit [07d1fe3e] log: see above
 │       └─restricted by compatibility requirements with TensorOperations [6aa20fa7] to versions: 2.0.4
 │         └─TensorOperations [6aa20fa7] log:
 │           ├─possible versions are: 0.6.1-4.0.5 or uninstalled
 │           └─restricted to versions 4.0.5 by an explicit requirement, leaving only versions: 4.0.5
 ├─restricted by compatibility requirements with TensorOperations [6aa20fa7] to versions: 0.11.0-0.11.1 or uninstalled, leaving only versions: 0.11.0-0.11.1
 │ └─TensorOperations [6aa20fa7] log: see above
 └─restricted by compatibility requirements with VectorInterface [409d34a3] to versions: 0.0.1-0.10.0 or uninstalled — no versions left
   └─VectorInterface [409d34a3] log:
     ├─possible versions are: 0.1.0-0.4.1 or uninstalled
     └─restricted by compatibility requirements with TensorOperations [6aa20fa7] to versions: 0.4.1
       └─TensorOperations [6aa20fa7] log: see above
Jutho commented 1 year ago

I guess the problem is that the latest release of TensorKit.jl is still on VectorInterface 0.2, whereas we are already at 0.4.1. Should we tag another TensorKit.jl release @lkdvos ?

Jutho commented 1 year ago

Ok, I just tagged 0.11.2, should be up in about half an hour or so. This should allow you to also upgrade TensorOperations (if it doesn't automatically).

Jutho commented 1 year ago

I hope your problem is solved, so I will close this issue. Don't hesitate to let us know if this or other problems appear.