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

Fix replace! function when replacing a Tensor with itself in pair argument #227

Merged
merged 5 commits into from
Nov 4, 2024

Conversation

jofrevalles
Copy link
Member

@jofrevalles jofrevalles commented Oct 31, 2024

Summary

This PR resolves #225 by fixing the problem with replace! when the same Tensor appears in both positions of the pair argument. Previously, since we did not have any specific check for this case, the replace! function had a push! that did not add the tensor since it was already present, but then, because we had a delete! after the push!, the Tensor got deleted.

We fixed this problem by adding a condition in the replace! function that checks if the two tensors are equal, and if they are, the function returns the TensorNetwork without changes.

Additionally (and importantly @bsc-quantic/software !), this PR also fixes the commented-out tests for the replace! function, and we have extended these tests to cover the specific case that this PR addresses.

@@ -437,6 +437,9 @@ end
function Base.replace!(tn::AbstractTensorNetwork, pair::Pair{<:Tensor,<:Tensor})
tn = TensorNetwork(tn)
old_tensor, new_tensor = pair

old_tensor == new_tensor && return tn
Copy link
Member

Choose a reason for hiding this comment

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

mmm shouldn't this be === to check for egality, not equality?

Suggested change
old_tensor == new_tensor && return tn
old_tensor === new_tensor && return tn

Copy link
Member Author

Choose a reason for hiding this comment

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

No! Since for example, now it failed when we did replace!(tn, A => conj(A)), when A is a Tensor with a parent Array that are Real.

Copy link
Member

@mofeing mofeing Nov 1, 2024

Choose a reason for hiding this comment

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

Exactly! If eltype(A) <: Real, then A === conj(A)!

Copy link
Member Author

Choose a reason for hiding this comment

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

conj(A) returns a new object with is equal to A. It would be like you say if it is conj! (not conj).

Copy link
Member

@mofeing mofeing Nov 1, 2024

Choose a reason for hiding this comment

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

You're right with this last point, but the bug in #225 is when it's replaced with the same tensor (i.e. old_tensor === new_tensor). new_tensor = copy(old_tensor) should work and there's nothing bad with it even if it really doesn't change the TN.

I try to avoid == if posible because == iterates over the whole arrays and checks for equality element-wise, while === just checks the objectids and you're done. Thus it avoids the overhead of iterating over everything, which can be costly on large arrays (specially when we start using distributed arrays).

Copy link
Member Author

Choose a reason for hiding this comment

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

but the bug in #225 is when it's replaced with the same tensor (i.e. old_tensor === new_tensor)

Yes, but I tried replace!(tn, A => conj(A)) and we got also the same error. I think this is because if you do conj(A) you still get the same objectid, which for me this is strange:

julia> objectid(A)
0xc7798e05eba779fe

julia> objectid(conj(A))
0xc7798e05eba779fe

But whatever, okay! If that is the case I agree that we can have ===, I will push that commit now.

test/TensorNetwork_test.jl Outdated Show resolved Hide resolved
test/TensorNetwork_test.jl Outdated Show resolved Hide resolved
test/TensorNetwork_test.jl Outdated Show resolved Hide resolved
@mofeing
Copy link
Member

mofeing commented Oct 31, 2024

Awesome! I'm leaving one import thing (egality vs equality) and some grammatical fix requests.

@jofrevalles
Copy link
Member Author

@mofeing I am merging this PR on monday. Let's enjoy the weekend :)

@jofrevalles jofrevalles merged commit db86848 into master Nov 4, 2024
5 checks passed
@jofrevalles jofrevalles deleted the fix/replace! branch November 4, 2024 09:02
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.

Incorrect replace! of a Tensor in a TensorNetwork with the same Tensor
2 participants