Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement @unsafe_region macro for relaxed tensor operations #149

Merged
merged 9 commits into from
Jun 10, 2024

Conversation

jofrevalles
Copy link
Member

This PR resolves issue #147 by introducing a new macro @unsafe_region, allowing users to temporarily relax index size checks when performing tensor operations within a specified code block. This feature is particularly useful for updating tensors in a tensor network where intermediate steps might involve tensors with mismatched sizes.

Example usage

julia> using Tenet

julia> tn = TensorNetwork([
           Tensor(rand(2, 2, 2), [:i, :j, :k]),
           Tensor(rand(2, 2), [:j, :l]),
           Tensor(rand(2, 2, 2, 2), [:l, :k, :m, :n]),
           Tensor(rand(2, 2, 2), [:n, :o, :p])])
TensorNetwork (#tensors=4, #inds=8)

julia> a = Tensor(rand(3, 2), (:i, :j)); push!(tn, a)
ERROR: DimensionMismatch: size(tensor,i)=3 but should be equal to size(tn,i)=2
Stacktrace:
 [1] push!(tn::TensorNetwork, tensor::Tensor{Float64, 2, Matrix{Float64}})
   @ Tenet ~/git/Tenet.jl/src/TensorNetwork.jl:255
 [2] top-level scope
   @ REPL[11]:1

julia> Tenet.@unsafe_region tn begin
           a = Tensor(rand(3, 2), (:i, :j))
           push!(tn, a)
           pop!(tn, a)
       end
3×2 Tensor{Float64, 2, Matrix{Float64}}:
 0.638985  0.725609
 0.850249  0.315764
 0.141707  0.642034

@jofrevalles jofrevalles requested a review from mofeing June 10, 2024 07:28
Copy link
Member

@mofeing mofeing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great! Thanks @jofrevalles!

Would you mind adding some tests?

src/TensorNetwork.jl Outdated Show resolved Hide resolved
@jofrevalles
Copy link
Member Author

It looks great! Thanks @jofrevalles!

Would you mind adding some tests?

Done, @mofeing !

Copy link
Member

@mofeing mofeing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, just some small fixes and we can merge it.

test/TensorNetwork_test.jl Show resolved Hide resolved
test/TensorNetwork_test.jl Outdated Show resolved Hide resolved
test/TensorNetwork_test.jl Outdated Show resolved Hide resolved
@mofeing mofeing merged commit ec14833 into master Jun 10, 2024
6 checks passed
@mofeing mofeing deleted the feature/unsafe-region branch June 10, 2024 09:58
@mofeing mofeing linked an issue Jun 10, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Relax push! constrains under specific conditions
2 participants