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

Fix `DimensionMismatch` not thrown in `TensorNetwork` constructor #72

Closed jofrevalles closed 12 months ago

jofrevalles commented 12 months ago

Summary

This PR fixes issue #69 (resolves #69). The previous implementation did not raise a DimensionMismatch error in the TensorNetwork constructor function for inconsistent dimensions across tensors sharing the same index label. This issue could lead to incorrect results when working with TensorNetwork objects.

In response, we have implemented a dimensionality check in the TensorNetwork constructor function, ensuring consistency across tensors that share an index label. This is similar to the check that already exists in the push! method.

Furthermore, to confirm that both the TensorNetwork constructor and push! methods are functioning correctly, we have added tests to ensure they throw a DimensionMismatch error when encountering inconsistent dimensions.

Example

With our fix, a DimensionMismatcherror is now correctly thrown:

julia> using Tenet

julia> A = Tensor(rand(3, 3), (:i, :j))
3×3 Tensor{Float64, 2, Matrix{Float64}}:
 0.0256405  0.746125  0.0352789
 0.932879   0.458685  0.726907
 0.129121   0.645359  0.941556

julia> B = Tensor(rand(2, 2), (:j, :k))
2×2 Tensor{Float64, 2, Matrix{Float64}}:
 0.0137305  0.132258
 0.671962   0.411074

julia> tn = TensorNetwork([A, B]) # Now an error is raised here
ERROR: DimensionMismatch: Tensors with index j do not all have the same size
Stacktrace:
...
codecov[bot] commented 12 months ago

Codecov Report

Merging #72 (f32a805) into master (6f21d05) will increase coverage by 0.23%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #72      +/-   ##
==========================================
+ Coverage   78.13%   78.37%   +0.23%     
==========================================
  Files          10       10              
  Lines         709      712       +3     
==========================================
+ Hits          554      558       +4     
+ Misses        155      154       -1     
Impacted Files Coverage Δ
src/TensorNetwork.jl 84.24% <100.00%> (+0.90%) :arrow_up:
jofrevalles commented 12 months ago

It seems that the new thrown error now shows us problems in the hcat function

mofeing commented 12 months ago

I've refactored some code and now it should be ok.

mofeing commented 12 months ago

It seems that the new thrown error now shows us problems in the hcat function

The code is actually ok. The problem was that hcat tests where connecting indices with wrong sizes, so it detected a bug!