From 6e72c6f1048ce2befabf64e92e11157bf84be184 Mon Sep 17 00:00:00 2001 From: victor Date: Tue, 20 May 2025 15:01:26 +0200 Subject: [PATCH 1/5] fix `braile` and add small test --- src/MPSKit.jl | 1 + src/operators/abstractmpo.jl | 32 -------------------------------- src/operators/utility.jl | 33 +++++++++++++++++++++++++++++++++ test/other.jl | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 32 deletions(-) create mode 100644 src/operators/utility.jl diff --git a/src/MPSKit.jl b/src/MPSKit.jl index 83647881f..aa0fe648d 100644 --- a/src/MPSKit.jl +++ b/src/MPSKit.jl @@ -120,6 +120,7 @@ include("operators/projection.jl") include("operators/timedependence.jl") include("operators/multipliedoperator.jl") include("operators/lazysum.jl") +include("operators/utility.jl") # extra functions that need all of the above structs to be defined and fit nowhere else include("transfermatrix/transfermatrix.jl") include("transfermatrix/transfer.jl") diff --git a/src/operators/abstractmpo.jl b/src/operators/abstractmpo.jl index 550f674d5..01d76a9ee 100644 --- a/src/operators/abstractmpo.jl +++ b/src/operators/abstractmpo.jl @@ -124,38 +124,6 @@ function Base.show(io::IOContext, mpo::AbstractMPO) return nothing end -function braille(H::SparseMPO) - isfinite = (H isa FiniteMPO) || (H isa FiniteMPOHamiltonian) - dash = "🭻" - stride = 2 #amount of dashes between braille - L = length(H) - - brailles = Vector{Vector{String}}(undef, L) - buffer = IOBuffer() - for (i, W) in enumerate(H) - BlockTensorKit.show_braille(buffer, W) - brailles[i] = split(String(take!(buffer))) - end - - maxheight = maximum(length.(brailles)) - - for i in 1:maxheight - line = "" - line *= ((i == 1 && !isfinite) ? ("... " * dash) : " ") - line *= (i > 1 && !isfinite) ? " " : "" - for (j, braille) in enumerate(brailles) - line *= (checkbounds(Bool, braille, i) ? braille[i] : - repeat(" ", length(braille[1]))) - if j < L - line *= repeat(((i == 1) ? dash : " "), stride) - end - end - line *= ((i == 1 && !isfinite) ? (dash * " ...") : " ") - println(line) - end - return nothing -end - # Linear Algebra # -------------- Base.:+(mpo::AbstractMPO) = scale(mpo, One()) diff --git a/src/operators/utility.jl b/src/operators/utility.jl new file mode 100644 index 000000000..6694af29f --- /dev/null +++ b/src/operators/utility.jl @@ -0,0 +1,33 @@ +function braille(io::IO, H::Union{SparseMPO,MPOHamiltonian}) + isfinite = (H isa FiniteMPO) || (H isa FiniteMPOHamiltonian) + dash = "🭻" + stride = 2 #amount of dashes between braille + L = length(H) + + brailles = Vector{Vector{String}}(undef, L) + buffer = IOBuffer() + for (i, W) in enumerate(H) + BlockTensorKit.show_braille(buffer, W) + brailles[i] = split(String(take!(buffer))) + end + + maxheight = maximum(length.(brailles)) + + for i in 1:maxheight + line = "" + line *= ((i == 1 && !isfinite) ? ("... " * dash) : " ") + line *= (i > 1 && !isfinite) ? " " : "" + for (j, braille) in enumerate(brailles) + line *= (checkbounds(Bool, braille, i) ? braille[i] : + repeat(" ", length(braille[1]))) + if j < L + line *= repeat(((i == 1) ? dash : " "), stride) + end + end + line *= ((i == 1 && !isfinite) ? (dash * " ...") : " ") + println(io, line) + end + return nothing +end + +braille(H::Union{SparseMPO,MPOHamiltonian}) = braille(stdout, H) diff --git a/test/other.jl b/test/other.jl index 658c2fd93..3d7bd3057 100644 --- a/test/other.jl +++ b/test/other.jl @@ -63,4 +63,20 @@ end end end +@testset "braille" begin + # Infinite Systems + H = transverse_field_ising() + buffer = IOBuffer() + braille(buffer, H) + output = String(take!(buffer)) + check = "... 🭻⎡⠉⢀⎤🭻 ...\n ⎣⠀⢀⎦ \n" + @test output == check + + # Finite Systems + H = open_boundary_conditions(H, 4) + braille(buffer, H) + output = String(take!(buffer)) + check = " ⎡⠉⠀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⡀⠀⎤ \n ⎣⠀⠀⎦ ⎣⠀⢀⎦ ⎣⠀⢀⎦ ⎣⡀⠀⎦ \n" + @test output == check +end end From 0b69bc370a97d2bb8aff95c41b858f5c3a333f52 Mon Sep 17 00:00:00 2001 From: victor Date: Tue, 20 May 2025 15:07:59 +0200 Subject: [PATCH 2/5] add testing for MPOs --- test/other.jl | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/other.jl b/test/other.jl index 3d7bd3057..08e515fe5 100644 --- a/test/other.jl +++ b/test/other.jl @@ -64,7 +64,8 @@ end end @testset "braille" begin - # Infinite Systems + # Infinite Hamiltonians and MPOs + # ------------------------------- H = transverse_field_ising() buffer = IOBuffer() braille(buffer, H) @@ -72,11 +73,24 @@ end check = "... 🭻⎡⠉⢀⎤🭻 ...\n ⎣⠀⢀⎦ \n" @test output == check - # Finite Systems + O = make_time_mpo(H, 1.0, TaylorCluster(3, false, false)) + braille(buffer, O) + output = String(take!(buffer)) + check = "... 🭻⎡⡏⠉⠒⠔⎤🭻 ...\n ⎣⡇⠀⠀⡂⎦ \n" + @test output == check + + # Finite Hamiltonians and MPOs + # ---------------------------- H = open_boundary_conditions(H, 4) braille(buffer, H) output = String(take!(buffer)) check = " ⎡⠉⠀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⡀⠀⎤ \n ⎣⠀⠀⎦ ⎣⠀⢀⎦ ⎣⠀⢀⎦ ⎣⡀⠀⎦ \n" @test output == check + + O = make_time_mpo(H, 1.0, TaylorCluster(3, false, false)) + braille(buffer, O) + output = String(take!(buffer)) + check = " ⎡⠉⠉⎤🭻🭻⎡⠍⠉⠤⠠⎤🭻🭻⎡⡏⠉⠒⠔⎤🭻🭻⎡⡇⠀⎤ \n ⎣⠀⠀⎦ ⎣⡂⠀⠀⠂⎦ ⎣⡇⠀⠀⡂⎦ ⎣⡇⠀⎦ \n" + @test output == check end end From 3b3242c1480e2ba0860635b76044a946260c5029 Mon Sep 17 00:00:00 2001 From: victor Date: Wed, 21 May 2025 13:03:15 +0200 Subject: [PATCH 3/5] update test --- test/other.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/other.jl b/test/other.jl index 08e515fe5..0306f1888 100644 --- a/test/other.jl +++ b/test/other.jl @@ -81,7 +81,7 @@ end # Finite Hamiltonians and MPOs # ---------------------------- - H = open_boundary_conditions(H, 4) + H = transverse_field_ising(; L=4) braille(buffer, H) output = String(take!(buffer)) check = " ⎡⠉⠀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⡀⠀⎤ \n ⎣⠀⠀⎦ ⎣⠀⢀⎦ ⎣⠀⢀⎦ ⎣⡀⠀⎦ \n" From 8b031bf66601a854cbe7792fd1c036581de5a3cc Mon Sep 17 00:00:00 2001 From: victor Date: Wed, 21 May 2025 13:03:58 +0200 Subject: [PATCH 4/5] move show methods, add braille docstring --- src/MPSKit.jl | 2 +- src/operators/abstractmpo.jl | 42 ----------------- src/operators/show.jl | 90 ++++++++++++++++++++++++++++++++++++ src/operators/utility.jl | 33 ------------- 4 files changed, 91 insertions(+), 76 deletions(-) create mode 100644 src/operators/show.jl delete mode 100644 src/operators/utility.jl diff --git a/src/MPSKit.jl b/src/MPSKit.jl index aa0fe648d..e311ca588 100644 --- a/src/MPSKit.jl +++ b/src/MPSKit.jl @@ -120,7 +120,7 @@ include("operators/projection.jl") include("operators/timedependence.jl") include("operators/multipliedoperator.jl") include("operators/lazysum.jl") -include("operators/utility.jl") # extra functions that need all of the above structs to be defined and fit nowhere else +include("operators/show.jl") # extra functions that need all of the above structs to be defined and fit nowhere else include("transfermatrix/transfermatrix.jl") include("transfermatrix/transfer.jl") diff --git a/src/operators/abstractmpo.jl b/src/operators/abstractmpo.jl index 01d76a9ee..5ea9ca739 100644 --- a/src/operators/abstractmpo.jl +++ b/src/operators/abstractmpo.jl @@ -82,48 +82,6 @@ function remove_orphans!(mpo::SparseMPO; tol=eps(real(scalartype(mpo)))^(3 / 4)) return mpo end -# Show -# ---- -function Base.show(io::IO, ::MIME"text/plain", W::AbstractMPO) - L = length(W) - println(io, L == 1 ? "single site " : "$L-site ", typeof(W), ":") - context = IOContext(io, :typeinfo => eltype(W), :compact => true) - return show(context, W) -end - -Base.show(io::IO, mpo::AbstractMPO) = show(convert(IOContext, io), mpo) -function Base.show(io::IOContext, mpo::AbstractMPO) - charset = (; top="┬", bot="┴", mid="┼", ver="│", dash="──") - limit = get(io, :limit, false)::Bool - half_screen_rows = limit ? div(displaysize(io)[1] - 8, 2) : typemax(Int) - L = length(mpo) - - # used to align all mposite infos regardless of the length of the mpo (100 takes up more space than 5) - npad = floor(Int, log10(L)) - mpoletter = mpo isa MPOHamiltonian ? "W" : "O" - isfinite = (mpo isa FiniteMPO) || (mpo isa FiniteMPOHamiltonian) - - !isfinite && println(io, "╷ ⋮") - for site in reverse(1:L) - if site < half_screen_rows || site > L - half_screen_rows - if site == L && isfinite - println(io, charset.top, " $mpoletter[$site]: ", - repeat(" ", npad - floor(Int, log10(site))), mpo[site]) - elseif (site == 1) && isfinite - println(io, charset.bot, " $mpoletter[$site]: ", - repeat(" ", npad - floor(Int, log10(site))), mpo[site]) - else - println(io, charset.mid, " $mpoletter[$site]: ", - repeat(" ", npad - floor(Int, log10(site))), mpo[site]) - end - elseif site == half_screen_rows - println(io, " ", "⋮") - end - end - !isfinite && println(io, "╵ ⋮") - return nothing -end - # Linear Algebra # -------------- Base.:+(mpo::AbstractMPO) = scale(mpo, One()) diff --git a/src/operators/show.jl b/src/operators/show.jl new file mode 100644 index 000000000..b3b74dff5 --- /dev/null +++ b/src/operators/show.jl @@ -0,0 +1,90 @@ +# AbstractMPO +# ----------- +function Base.show(io::IO, ::MIME"text/plain", W::AbstractMPO) + L = length(W) + println(io, L == 1 ? "single site " : "$L-site ", typeof(W), ":") + context = IOContext(io, :typeinfo => eltype(W), :compact => true) + return show(context, W) +end + +Base.show(io::IO, mpo::AbstractMPO) = show(convert(IOContext, io), mpo) +function Base.show(io::IOContext, mpo::AbstractMPO) + charset = (; top="┬", bot="┴", mid="┼", ver="│", dash="──") + limit = get(io, :limit, false)::Bool + half_screen_rows = limit ? div(displaysize(io)[1] - 8, 2) : typemax(Int) + L = length(mpo) + + # used to align all mposite infos regardless of the length of the mpo (100 takes up more space than 5) + npad = floor(Int, log10(L)) + mpoletter = mpo isa MPOHamiltonian ? "W" : "O" + isfinite = (mpo isa FiniteMPO) || (mpo isa FiniteMPOHamiltonian) + + !isfinite && println(io, "╷ ⋮") + for site in reverse(1:L) + if site < half_screen_rows || site > L - half_screen_rows + if site == L && isfinite + println(io, charset.top, " $mpoletter[$site]: ", + repeat(" ", npad - floor(Int, log10(site))), mpo[site]) + elseif (site == 1) && isfinite + println(io, charset.bot, " $mpoletter[$site]: ", + repeat(" ", npad - floor(Int, log10(site))), mpo[site]) + else + println(io, charset.mid, " $mpoletter[$site]: ", + repeat(" ", npad - floor(Int, log10(site))), mpo[site]) + end + elseif site == half_screen_rows + println(io, " ", "⋮") + end + end + !isfinite && println(io, "╵ ⋮") + return nothing +end + +# braille +# ------- +""" + braille(io::IO, H::Union{SparseMPO, MPOHamiltonian}) + braille(H::Union{SparseMPO, MPOHamiltonian}) + +Prints a compact, human-readable "braille" visualization of a sparseMPO or MPOHamiltonian. +Each site of the MPO is represented as a block of Unicode braille characters, with sites separated by dashes. +This visualization is useful for quickly inspecting the structure and sparsity pattern of MPOs. + +# Arguments +- `io::IO`: The output stream to print to (e.g., `stdout`). +- `H::Union{SparseMPO, MPOHamiltonian}`: The `SparseMPO` or `MPOHamiltonian` to visualize. + +If called without an `io` argument, output is printed to `stdout`. +""" +function braille(io::IO, H::Union{SparseMPO,MPOHamiltonian}) + dash = "🭻" + stride = 2 #amount of dashes between braille + L = length(H) + + brailles = Vector{Vector{String}}(undef, L) + buffer = IOBuffer() + for (i, W) in enumerate(H) + BlockTensorKit.show_braille(buffer, W) + brailles[i] = split(String(take!(buffer))) + end + + maxheight = maximum(length.(brailles)) + + for i in 1:maxheight + line = "" + line *= ((i == 1 && !isfinite(H)) ? ("... " * dash) : " ") + line *= (i > 1 && !isfinite(H)) ? " " : "" + for (j, braille) in enumerate(brailles) + line *= (checkbounds(Bool, braille, i) ? braille[i] : + repeat(" ", length(braille[1]))) + if j < L + line *= repeat(((i == 1) ? dash : " "), stride) + end + end + line *= ((i == 1 && !isfinite(H)) ? (dash * " ...") : " ") + println(io, line) + end + return nothing +end + +braille(H::Union{SparseMPO,MPOHamiltonian}) = braille(stdout, H) diff --git a/src/operators/utility.jl b/src/operators/utility.jl deleted file mode 100644 index 6694af29f..000000000 --- a/src/operators/utility.jl +++ /dev/null @@ -1,33 +0,0 @@ -function braille(io::IO, H::Union{SparseMPO,MPOHamiltonian}) - isfinite = (H isa FiniteMPO) || (H isa FiniteMPOHamiltonian) - dash = "🭻" - stride = 2 #amount of dashes between braille - L = length(H) - - brailles = Vector{Vector{String}}(undef, L) - buffer = IOBuffer() - for (i, W) in enumerate(H) - BlockTensorKit.show_braille(buffer, W) - brailles[i] = split(String(take!(buffer))) - end - - maxheight = maximum(length.(brailles)) - - for i in 1:maxheight - line = "" - line *= ((i == 1 && !isfinite) ? ("... " * dash) : " ") - line *= (i > 1 && !isfinite) ? " " : "" - for (j, braille) in enumerate(brailles) - line *= (checkbounds(Bool, braille, i) ? braille[i] : - repeat(" ", length(braille[1]))) - if j < L - line *= repeat(((i == 1) ? dash : " "), stride) - end - end - line *= ((i == 1 && !isfinite) ? (dash * " ...") : " ") - println(io, line) - end - return nothing -end - -braille(H::Union{SparseMPO,MPOHamiltonian}) = braille(stdout, H) From b1a222c3f95dd9a2e4a9234024115fe1003ed0fa Mon Sep 17 00:00:00 2001 From: victor Date: Wed, 21 May 2025 13:05:25 +0200 Subject: [PATCH 5/5] remove comment --- src/MPSKit.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MPSKit.jl b/src/MPSKit.jl index e311ca588..d7fd0da07 100644 --- a/src/MPSKit.jl +++ b/src/MPSKit.jl @@ -120,7 +120,7 @@ include("operators/projection.jl") include("operators/timedependence.jl") include("operators/multipliedoperator.jl") include("operators/lazysum.jl") -include("operators/show.jl") # extra functions that need all of the above structs to be defined and fit nowhere else +include("operators/show.jl") include("transfermatrix/transfermatrix.jl") include("transfermatrix/transfer.jl")