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

Add `Base.replace!` function for `Tensor`s in a `TensorNetwork` #50

Closed jofrevalles closed 1 year ago

jofrevalles commented 1 year ago

This PR introduces a new Base.replace! function for the TensorNetwork type (issue #48). The function allows users to replace an existing Tensor within a TensorNetwork with a new Tensor, provided that the new Tensor has labels that are a permutation of the old Tensor's labels.

The implementation ensures that the TensorNetwork's inds field is properly updated to maintain consistency within the network.

Example:

julia> using Tenet; using Test

julia> ψ = rand(MatrixProductState{Open}, 16, 2, 8) # Create a random tensor network
TensorNetwork{MatrixProductState{Open}}(#tensors=16, #inds=31)

julia> old_tensor = tensors(ψ, 2) # Retrieve a tensor from the network
2×4×2 Tensor{Float64, 3, Array{Float64, 3}}:
[:, :, 1] =
 0.429373   0.473947  0.475461   0.0111029
 0.165485  -0.148758  0.0920096  0.794718

[:, :, 2] =
 0.120982    0.506526   0.29635    0.0761409
 0.0279856  -0.149644  -0.0713865  0.531198

julia> new_tensor = Tensor(rand(size(old_tensor)...), labels(old_tensor)) # Create a new tensor with permuted labels
2×4×2 Tensor{Float64, 3, Array{Float64, 3}}:
[:, :, 1] =
 0.230909  0.981709  0.891462  0.833955
 0.558508  0.269235  0.520971  0.1338

[:, :, 2] =
 0.430268  0.695382  0.454548  0.702221
 0.296282  0.457037  0.207451  0.647955

julia> replace!(ψ, old_tensor, new_tensor) # Replace the old tensor with the new tensor in the network
TensorNetwork{MatrixProductState{Open}}(#tensors=16, #inds=31)

julia> @test tensors(ψ, 2) == new_tensor # Verify that the old tensor has been replaced
Test Passed

This addition enhances the flexibility of the TensorNetwork type and allows users to perform in-place modifications of tensor networks more efficiently.

codecov[bot] commented 1 year ago

Codecov Report

Merging #50 (4ba28a8) into master (cee74dc) will increase coverage by 0.39%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #50      +/-   ##
==========================================
+ Coverage   79.96%   80.36%   +0.39%     
==========================================
  Files          11       11              
  Lines         594      606      +12     
==========================================
+ Hits          475      487      +12     
  Misses        119      119              
Impacted Files Coverage Δ
src/TensorNetwork.jl 88.59% <100.00%> (+0.99%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

jofrevalles commented 1 year ago

The function is ready to merge. I added one test for a TensorNetwork with tensors that have the same labels and works fine. However, I don't know why Julia nightly is failing now in all our runs.

mofeing commented 1 year ago

Great! Just some small requests and I'll approve it.

mmm strange error, but nightly is unstable. I'm gonna see if this still happens with v1.9.