bsc-quantic / Qrochet.jl

Quantum Tensor Networks
https://bsc-quantic.github.io/Qrochet.jl/
Apache License 2.0
1 stars 0 forks source link

Add `canonize` function #27

Closed jofrevalles closed 4 months ago

jofrevalles commented 4 months ago

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
codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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

Additional details and impacted files ```diff @@ 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 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

jofrevalles commented 4 months ago

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.

jofrevalles commented 4 months ago

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

jofrevalles commented 4 months ago

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