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

Commit

Permalink
Move isleftcanonical,isrightcanonical to source
Browse files Browse the repository at this point in the history
  • Loading branch information
mofeing committed Feb 22, 2024
1 parent deebc3d commit 754ca92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
30 changes: 30 additions & 0 deletions src/Ansatz/Chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ function canonize_site!(::Open, tn::Chain, site::Site; direction::Symbol, method
return tn
end

function isleftcanonical(qtn::Chain, site; atol::Real = 1e-12)
right_ind = rightindex(qtn, site)

# we are at right-most site, which cannot be left-canonical
if isnothing(right_ind)
return false
end

# TODO is replace(conj(A)...) copying too much?
tensor = select(qtn, :tensor, site)
contracted = contract(tensor, replace(conj(tensor), right_ind => :new_ind_name))

return isapprox(contracted, Matrix{Float64}(I, size(A, right_ind), size(A, right_ind)); atol)
end

function isrightcanonical(qtn::Chain, site; atol::Real = 1e-12)
left_ind = leftindex(qtn, site)

# we are at left-most site, which cannot be right-canonical
if isnothing(left_ind)
return false
end

#TODO is replace(conj(A)...) copying too much?
tensor = select(qtn, :tensor, site)
contracted = contract(tensor, replace(conj(tensor), left_ind => :new_ind_name))

return isapprox(contracted, Matrix{Float64}(I, size(A, left_ind), size(A, left_ind)); atol)
end

mixed_canonize(tn::Chain, args...; kwargs...) = mixed_canonize!(deepcopy(tn), args...; kwargs...)
mixed_canonize!(tn::Chain, args...; kwargs...) = mixed_canonize!(boundary(tn), tn, args...; kwargs...)

Expand Down
22 changes: 0 additions & 22 deletions test/Ansatz/Chain_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,6 @@
@testset "Canonization" begin
using Tenet

function is_left_canonical(qtn, s::Site)
label_r = rightindex(qtn, s)
A = select(qtn, :tensor, s)
try
contracted = contract(A, replace(conj(A), label_r => :new_ind_name))
return isapprox(contracted, Matrix{Float64}(I, size(A, label_r), size(A, label_r)), atol = 1e-12)
catch
return false
end
end

function is_right_canonical(qtn, s::Site)
label_l = leftindex(qtn, s)
A = select(qtn, :tensor, s)
try
contracted = contract(A, replace(conj(A), label_l => :new_ind_name))
return isapprox(contracted, Matrix{Float64}(I, size(A, label_l), size(A, label_l)), atol = 1e-12)
catch
return false
end
end

@testset "canonize_site" begin
qtn = Chain(State(), Open(), [rand(4, 4), rand(4, 4, 4), rand(4, 4)])

Expand Down

0 comments on commit 754ca92

Please sign in to comment.