bsc-quantic / Tenet.jl

Composable Tensor Network library in Julia
https://bsc-quantic.github.io/Tenet.jl/
Apache License 2.0
17 stars 1 forks source link

Fixed issue in `contract` function for `TensorNetwork` #59

Closed jofrevalles closed 1 year ago

jofrevalles commented 1 year ago

The contract function for TensorNetworks raised an error when trying to contract a tensor network defined with a list of Tensors. In this PR we have fixed this small problem and added tests covering the previous error.

To illustrate the issue, we provide an example of the previous error:

julia> using Tenet

julia> A = Tensor(rand(2, 2, 2), (:i, :j, :k))
2×2×2 Tensor{Float64, 3, Array{Float64, 3}}: ...

julia> B = Tensor(rand(2, 2, 2), (:j, :l, :m))
2×2×2 Tensor{Float64, 3, Array{Float64, 3}}: ...

julia> tn = TensorNetwork([A, B])
TensorNetwork{Arbitrary}(#tensors=2, #inds=5)

julia> contract(tn)
ERROR: MethodError: Cannot `convert` an object of type Index to an object of type Symbol
Closest candidates are:
  convert(::Type{T}, ::T) where T at Base.jl:61
  Symbol(::Any...) at strings/basic.jl:229

With this PR the problem no longer occurs:

julia> using Tenet
...
julia> contract(tn)
2×2×2×2 Tensor{Float64, 4, Array{Float64, 4}}:
[:, :, 1, 1] =
 0.718756  0.317039
 0.665384  0.576727

[:, :, 2, 1] =
 0.182248  0.224855
 0.350426  0.479966

[:, :, 1, 2] =
 0.644568  0.404929
 0.748414  0.795829

[:, :, 2, 2] =
 0.438436  0.199706
 0.413822  0.366387
codecov[bot] commented 1 year ago

Codecov Report

Merging #59 (0891760) into master (8c4c5b9) will not change coverage. The diff coverage is n/a.

@@           Coverage Diff           @@
##           master      #59   +/-   ##
=======================================
  Coverage   82.14%   82.14%           
=======================================
  Files          12       12           
  Lines         616      616           
=======================================
  Hits          506      506           
  Misses        110      110           
Impacted Files Coverage Δ
src/TensorNetwork.jl 86.27% <ø> (ø)