Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Add canonize function #27

Merged
merged 14 commits into from
Mar 12, 2024
Merged

Add canonize function #27

merged 14 commits into from
Mar 12, 2024

Conversation

jofrevalles
Copy link
Member

@jofrevalles jofrevalles commented Mar 11, 2024

Summary

This PR resolves #22 by implementing the canonize function, which takes an input Chain and transforms it into the canonical form (also known as Vidal form), where we have a set of Γ and Λ tensors. For now, this function is implemented for Chain with Open boundaries.

Additionally, we added some tests to cover for the new function. Thanks to @starsfordummies for the useful comments!

Example

Let's show how this function works:

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

julia> canonized = canonize(qtn)
MPS (inputs=0, outputs=5)

julia> length(tensors(canonized)) # 5 tensors + 4 singular values vectors
9

julia> isapprox(contract(transform(TensorNetwork(canonized), Tenet.HyperindConverter())),
                contract(TensorNetwork(qtn)))
true

julia> Γ₃ = select(canonized, :tensor, Site(3))
4×2×4 Tensor{Float64, 3, Array{Float64, 3}}: ...

julia> Λ₄ = pop!(TensorNetwork(canonized), select(canonized, :between, Site(3), Site(4)))
4-element Tensor{Float64, 1, Vector{Float64}}:
 0.8303349576858878
 0.4869563863594485
 0.22360345538771664
 0.15303212266448157

julia> sum((i -> i^2).(Λ₄))
1.000000000000001

julia> replace!(TensorNetwork(canonized), Γ₃ => contract(Γ₃, Λ₄; dims=()))
TensorNetwork (#tensors=8, #inds=9)

julia> @test Qrochet.isrightcanonical(canonized, Site(3))
Test Passed

Copy link

codecov bot commented Mar 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.00%. Comparing base (eb61105) to head (3c89308).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #27      +/-   ##
==========================================
+ Coverage   72.94%   75.00%   +2.05%     
==========================================
  Files           8        8              
  Lines         414      436      +22     
==========================================
+ Hits          302      327      +25     
+ Misses        112      109       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jofrevalles
Copy link
Member Author

Question for @bsc-quantic/tensor-networks: should we do that the isleftcanonical, isrightcanonical functions already process the singular values?

I think that makes sense that we do not (so like it is currently in the code), and just take the tensor at that site and do the checking. This makes the tests for this function a little bit cumbersome, but do the work. In my opinion, we should add a function that does the contraction of the singular values, since I think this will be necessary for many algorithms.


@test length(tensors(canonized)) == 9 # 5 tensors + 4 singular values vectors
@test isapprox(
contract(transform(TensorNetwork(canonized), Tenet.HyperindConverter())),
Copy link
Member

@mofeing mofeing Mar 11, 2024

Choose a reason for hiding this comment

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

I think you no longer need to do the Tenet.HyperindConverter transformation to be able to contract. Would you mind updating your Tenet installation and trying it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I now tried using Tenet v0.5.2 and it did not work without the Tenet.HyperindConverter.

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.

First batch of requests. There is one thing that you don't test and I think it's very important: the norm of the TN. The L2-norm of each of the singular value vector on each bond should give the same value, and equal to contract(tn).

src/Ansatz/Chain.jl Outdated Show resolved Hide resolved
src/Ansatz/Chain.jl Outdated Show resolved Hide resolved
test/Ansatz/Chain_test.jl Outdated Show resolved Hide resolved
src/Ansatz/Chain.jl Outdated Show resolved Hide resolved
@jofrevalles
Copy link
Member Author

@mofeing The code now will error due to the Tenet.HyperindConverter thingy. I have tested locally and everything works otherwise.

@jofrevalles
Copy link
Member Author

jofrevalles commented Mar 12, 2024

Okay @mofeing, every test now has passed correctly. I would like to merge this.

src/Ansatz.jl Outdated
@@ -84,5 +84,16 @@ function LinearAlgebra.norm(ψ::Ansatz, p::Real = 2; kwargs...)
p != 2 && throw(ArgumentError("p=$p is not implemented yet"))

# TODO: Replace with contract(hcat(ψ, ψ')...) when implemented
return contract(merge(TensorNetwork(ψ), TensorNetwork(ψ')); kwargs...) |> only |> sqrt |> abs
value = contract(merge(TensorNetwork(ψ), TensorNetwork(ψ')); kwargs...)
println("value: $(inds(value))")
Copy link
Member

Choose a reason for hiding this comment

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

??

src/Ansatz.jl Outdated Show resolved Hide resolved
src/Ansatz/Chain.jl Outdated Show resolved Hide resolved
@jofrevalles jofrevalles merged commit 11890f0 into master Mar 12, 2024
5 checks passed
@jofrevalles jofrevalles deleted the feature/implement-canonize branch March 12, 2024 09:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Vidal-form canonization
2 participants