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 reindex! by returing the final index replace mapping #178

Merged
merged 16 commits into from
Aug 6, 2024
Merged

Conversation

jofrevalles
Copy link
Member

Summary

This PR resolves #177 by correctly fixing the reindex of the site indices. To do that, we modified the replace! function by returning the final index mapping. I believe this is the most efficient way to do this since we could also get the mapping by comparing the tensor before and after the replace!, but this seems to do unnecessary work.

Example

Now the following code works fine:

julia> mps = rand(Chain, Open, State; n=5, p=2, χ=20)
MPS (inputs=0, outputs=5)

julia> mpo = rand(Chain, Open, Operator; n=5, χ=2, p=2)
MPO (inputs=5, outputs=5)

julia> inds(mpo, at = Site(1))
:A

julia> inds(mpo, at = Site(1; dual=true))
:F

julia> Tenet.@reindex! outputs(mps) => inputs(mpo)
MPO (inputs=5, outputs=5)

julia> inds(mpo, at = Site(1))
:A

julia> inds(mpo, at = Site(1; dual=true))
:O

julia> inds.(mpo)
5-element Vector{Vector{Symbol}}:
 [:K, :V, :B, :L]
 [:L, :U, :C, :M]
 [:M, :P, :D, :N]
 [:N, :J, :E]
 [:O, :A, :K]
 
julia> contract(merge(TensorNetwork(mps), TensorNetwork(mpo))) # We get a 5-dimensional tensor as expected
2×2×2×2×2 Tensor{Float64, 5, Array{Float64, 5}}: ...


Tenet.@reindex! outputs(qtn) => inputs(qtn2)

@test issetequal([qtn2.sites[i] for i in inputs(qtn2)], [qtn.sites[i] for i in outputs(qtn)])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@test issetequal([qtn2.sites[i] for i in inputs(qtn2)], [qtn.sites[i] for i in outputs(qtn)])
@test issetequal([inds(qtn2, at=i) for i in inputs(qtn2)], [inds(qtn, at=i) for i in outputs(qtn)])


Tenet.@reindex! outputs(qtn) => inputs(qtn2)

@test issetequal([qtn2.sites[i] for i in inputs(qtn2)], [qtn.sites[i] for i in outputs(qtn)])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@test issetequal([qtn2.sites[i] for i in inputs(qtn2)], [qtn.sites[i] for i in outputs(qtn)])
@test issetequal([inds(qtn2, at=i) for i in inputs(qtn2)], [inds(qtn, at=i) for i in outputs(qtn)])

@@ -528,6 +528,27 @@
end
end

@testset "complicated replacement" begin
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@testset "complicated replacement" begin
@testset "overlapping replacement" begin

# [:L, :U, :C, :M]
# [:M, :P, :D, :N]
# [:N, :J, :E]
@test issetequal(vcat(collect(values(tn.tensormap))...), vcat([[:O, :A, :K], [:K, :V, :B, :L], [:L, :U, :C, :M], [:M, :P, :D, :N], [:N, :J, :E]]...))
Copy link
Member

Choose a reason for hiding this comment

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

Use inds.(tensors(tn)) or another way to test this.

@mofeing mofeing merged commit b067885 into master Aug 6, 2024
6 checks passed
@mofeing mofeing deleted the fix/reindex branch August 6, 2024 07:19
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.

Tenet.@reindex odd behavior for mps-mpo
2 participants