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

Update `replace!` function for multiple `Tensor` replacements and fix `Symbol` check #52

Closed jofrevalles closed 1 year ago

jofrevalles commented 1 year ago

Summary

This PR includes several improvements to the replace! function in the TensorNetwork module:

  1. Fixed the replace! function for Symbol pairs to correctly check if target symbols are not already present in the network. The line !isdisjoint(values(old_new), labels(tn)) && throw(...) was updated to !isdisjoint(last.(old_new), labels(tn)) && throw(...).
  2. Updated the replace! function for Tensor pairs to now accept multiple replacements instead of just one. This allows for more efficient and convenient replacement of multiple tensors within a network.
  3. Added new tests to ensure the correctness of the updated replace! function for both Symbol and Tensor pairs. The new tests check the previous error in the replace! function for symbols and the functionality of multiple tensor replacements.

Example

Example of multiple replacements of Tensors:

julia> using Tenet

julia> using Test

julia> t_ij = Tensor(rand(2, 2), (:i, :j))

julia> t_ik = Tensor(rand(2, 2), (:i, :k))

julia> t_ilm = Tensor(rand(2, 2, 2), (:i, :l, :m))

julia> t_lm = Tensor(rand(2, 2), (:l, :m))

julia> tn = TensorNetwork([t_ij, t_ik, t_ilm, t_lm])
TensorNetwork{Arbitrary}(#tensors=4, #inds=5)

julia> new_tensor1 = Tensor(rand(2, 2), (:i, :k))
2×2 Tensor{Float64, 2, Matrix{Float64}}:
 0.225687  0.85958
 0.104862  0.00510741

julia> new_tensor2 = Tensor(ones(2, 2), (:i, :k))
2×2 Tensor{Float64, 2, Matrix{Float64}}:
 1.0  1.0
 1.0  1.0

julia> replace!(tn, t_ik=>new_tensor1, new_tensor1=>new_tensor2)
TensorNetwork{Arbitrary}(#tensors=4, #inds=5)

julia> tensors(tn, 2)
2×2 Tensor{Float64, 2, Matrix{Float64}}:
 1.0  1.0
 1.0  1.0
codecov[bot] commented 1 year ago

Codecov Report

Merging #52 (a035e3d) into master (641f114) will increase coverage by 0.02%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #52      +/-   ##
==========================================
+ Coverage   82.11%   82.14%   +0.02%     
==========================================
  Files          12       12              
  Lines         615      616       +1     
==========================================
+ Hits          505      506       +1     
  Misses        110      110              
Impacted Files Coverage Δ
src/TensorNetwork.jl 86.27% <100.00%> (+0.09%) :arrow_up:
mofeing commented 1 year ago

This seems like a rewrite of something that should already work (maybe with some bug fixes) and the refactor seems much more verbose.

  1. Fixed the replace! function for Symbol pairs to correctly check if target symbols are not already present in the network. The line !isdisjoint(values(old_new), labels(tn)) && throw(...) was updated to !isdisjoint(last.(old_new), labels(tn)) && throw(...).

Okay, super. My bad for using values instead of last.

  1. Updated the replace! function for Tensor pairs to now accept multiple replacements instead of just one. This allows for more efficient and convenient replacement of multiple tensors within a network.

mmm this was already working. Was there any bug?

  1. Added new tests to ensure the correctness of the updated replace! function for both Symbol and Tensor pairs. The new tests check the previous error in the replace! function for symbols and the functionality of multiple tensor replacements.

Nice.

In general, I don't understand the reasons behind the refactor. Could explain yourself a little bit more?

jofrevalles commented 1 year ago

@mofeing I think that now the branch is ready to merge.