Skip to content

Commit 84f9b80

Browse files
Fix braille and add small test (#293)
* fix `braile` and add small test * add testing for MPOs * update test * move show methods, add braille docstring * remove comment
1 parent 31f0ce1 commit 84f9b80

File tree

4 files changed

+121
-74
lines changed

4 files changed

+121
-74
lines changed

src/MPSKit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ include("operators/projection.jl")
120120
include("operators/timedependence.jl")
121121
include("operators/multipliedoperator.jl")
122122
include("operators/lazysum.jl")
123+
include("operators/show.jl")
123124

124125
include("transfermatrix/transfermatrix.jl")
125126
include("transfermatrix/transfer.jl")

src/operators/abstractmpo.jl

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -82,80 +82,6 @@ function remove_orphans!(mpo::SparseMPO; tol=eps(real(scalartype(mpo)))^(3 / 4))
8282
return mpo
8383
end
8484

85-
# Show
86-
# ----
87-
function Base.show(io::IO, ::MIME"text/plain", W::AbstractMPO)
88-
L = length(W)
89-
println(io, L == 1 ? "single site " : "$L-site ", typeof(W), ":")
90-
context = IOContext(io, :typeinfo => eltype(W), :compact => true)
91-
return show(context, W)
92-
end
93-
94-
Base.show(io::IO, mpo::AbstractMPO) = show(convert(IOContext, io), mpo)
95-
function Base.show(io::IOContext, mpo::AbstractMPO)
96-
charset = (; top="", bot="", mid="", ver="", dash="──")
97-
limit = get(io, :limit, false)::Bool
98-
half_screen_rows = limit ? div(displaysize(io)[1] - 8, 2) : typemax(Int)
99-
L = length(mpo)
100-
101-
# used to align all mposite infos regardless of the length of the mpo (100 takes up more space than 5)
102-
npad = floor(Int, log10(L))
103-
mpoletter = mpo isa MPOHamiltonian ? "W" : "O"
104-
isfinite = (mpo isa FiniteMPO) || (mpo isa FiniteMPOHamiltonian)
105-
106-
!isfinite && println(io, "╷ ⋮")
107-
for site in reverse(1:L)
108-
if site < half_screen_rows || site > L - half_screen_rows
109-
if site == L && isfinite
110-
println(io, charset.top, " $mpoletter[$site]: ",
111-
repeat(" ", npad - floor(Int, log10(site))), mpo[site])
112-
elseif (site == 1) && isfinite
113-
println(io, charset.bot, " $mpoletter[$site]: ",
114-
repeat(" ", npad - floor(Int, log10(site))), mpo[site])
115-
else
116-
println(io, charset.mid, " $mpoletter[$site]: ",
117-
repeat(" ", npad - floor(Int, log10(site))), mpo[site])
118-
end
119-
elseif site == half_screen_rows
120-
println(io, " ", "")
121-
end
122-
end
123-
!isfinite && println(io, "╵ ⋮")
124-
return nothing
125-
end
126-
127-
function braille(H::SparseMPO)
128-
isfinite = (H isa FiniteMPO) || (H isa FiniteMPOHamiltonian)
129-
dash = "🭻"
130-
stride = 2 #amount of dashes between braille
131-
L = length(H)
132-
133-
brailles = Vector{Vector{String}}(undef, L)
134-
buffer = IOBuffer()
135-
for (i, W) in enumerate(H)
136-
BlockTensorKit.show_braille(buffer, W)
137-
brailles[i] = split(String(take!(buffer)))
138-
end
139-
140-
maxheight = maximum(length.(brailles))
141-
142-
for i in 1:maxheight
143-
line = ""
144-
line *= ((i == 1 && !isfinite) ? ("... " * dash) : " ")
145-
line *= (i > 1 && !isfinite) ? " " : ""
146-
for (j, braille) in enumerate(brailles)
147-
line *= (checkbounds(Bool, braille, i) ? braille[i] :
148-
repeat(" ", length(braille[1])))
149-
if j < L
150-
line *= repeat(((i == 1) ? dash : " "), stride)
151-
end
152-
end
153-
line *= ((i == 1 && !isfinite) ? (dash * " ...") : " ")
154-
println(line)
155-
end
156-
return nothing
157-
end
158-
15985
# Linear Algebra
16086
# --------------
16187
Base.:+(mpo::AbstractMPO) = scale(mpo, One())

src/operators/show.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# AbstractMPO
2+
# -----------
3+
function Base.show(io::IO, ::MIME"text/plain", W::AbstractMPO)
4+
L = length(W)
5+
println(io, L == 1 ? "single site " : "$L-site ", typeof(W), ":")
6+
context = IOContext(io, :typeinfo => eltype(W), :compact => true)
7+
return show(context, W)
8+
end
9+
10+
Base.show(io::IO, mpo::AbstractMPO) = show(convert(IOContext, io), mpo)
11+
function Base.show(io::IOContext, mpo::AbstractMPO)
12+
charset = (; top="", bot="", mid="", ver="", dash="──")
13+
limit = get(io, :limit, false)::Bool
14+
half_screen_rows = limit ? div(displaysize(io)[1] - 8, 2) : typemax(Int)
15+
L = length(mpo)
16+
17+
# used to align all mposite infos regardless of the length of the mpo (100 takes up more space than 5)
18+
npad = floor(Int, log10(L))
19+
mpoletter = mpo isa MPOHamiltonian ? "W" : "O"
20+
isfinite = (mpo isa FiniteMPO) || (mpo isa FiniteMPOHamiltonian)
21+
22+
!isfinite && println(io, "╷ ⋮")
23+
for site in reverse(1:L)
24+
if site < half_screen_rows || site > L - half_screen_rows
25+
if site == L && isfinite
26+
println(io, charset.top, " $mpoletter[$site]: ",
27+
repeat(" ", npad - floor(Int, log10(site))), mpo[site])
28+
elseif (site == 1) && isfinite
29+
println(io, charset.bot, " $mpoletter[$site]: ",
30+
repeat(" ", npad - floor(Int, log10(site))), mpo[site])
31+
else
32+
println(io, charset.mid, " $mpoletter[$site]: ",
33+
repeat(" ", npad - floor(Int, log10(site))), mpo[site])
34+
end
35+
elseif site == half_screen_rows
36+
println(io, " ", "")
37+
end
38+
end
39+
!isfinite && println(io, "╵ ⋮")
40+
return nothing
41+
end
42+
43+
# braille
44+
# -------
45+
"""
46+
braille(io::IO, H::Union{SparseMPO, MPOHamiltonian})
47+
braille(H::Union{SparseMPO, MPOHamiltonian})
48+
49+
Prints a compact, human-readable "braille" visualization of a sparseMPO or MPOHamiltonian.
50+
Each site of the MPO is represented as a block of Unicode braille characters, with sites separated by dashes.
51+
This visualization is useful for quickly inspecting the structure and sparsity pattern of MPOs.
52+
53+
# Arguments
54+
- `io::IO`: The output stream to print to (e.g., `stdout`).
55+
- `H::Union{SparseMPO, MPOHamiltonian}`: The `SparseMPO` or `MPOHamiltonian` to visualize.
56+
57+
If called without an `io` argument, output is printed to `stdout`.
58+
"""
59+
function braille(io::IO, H::Union{SparseMPO,MPOHamiltonian})
60+
dash = "🭻"
61+
stride = 2 #amount of dashes between braille
62+
L = length(H)
63+
64+
brailles = Vector{Vector{String}}(undef, L)
65+
buffer = IOBuffer()
66+
for (i, W) in enumerate(H)
67+
BlockTensorKit.show_braille(buffer, W)
68+
brailles[i] = split(String(take!(buffer)))
69+
end
70+
71+
maxheight = maximum(length.(brailles))
72+
73+
for i in 1:maxheight
74+
line = ""
75+
line *= ((i == 1 && !isfinite(H)) ? ("... " * dash) : " ")
76+
line *= (i > 1 && !isfinite(H)) ? " " : ""
77+
for (j, braille) in enumerate(brailles)
78+
line *= (checkbounds(Bool, braille, i) ? braille[i] :
79+
repeat(" ", length(braille[1])))
80+
if j < L
81+
line *= repeat(((i == 1) ? dash : " "), stride)
82+
end
83+
end
84+
line *= ((i == 1 && !isfinite(H)) ? (dash * " ...") : " ")
85+
println(io, line)
86+
end
87+
return nothing
88+
end
89+
90+
braille(H::Union{SparseMPO,MPOHamiltonian}) = braille(stdout, H)

test/other.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,34 @@ end
6363
end
6464
end
6565

66+
@testset "braille" begin
67+
# Infinite Hamiltonians and MPOs
68+
# -------------------------------
69+
H = transverse_field_ising()
70+
buffer = IOBuffer()
71+
braille(buffer, H)
72+
output = String(take!(buffer))
73+
check = "... 🭻⎡⠉⢀⎤🭻 ...\n ⎣⠀⢀⎦ \n"
74+
@test output == check
75+
76+
O = make_time_mpo(H, 1.0, TaylorCluster(3, false, false))
77+
braille(buffer, O)
78+
output = String(take!(buffer))
79+
check = "... 🭻⎡⡏⠉⠒⠔⎤🭻 ...\n ⎣⡇⠀⠀⡂⎦ \n"
80+
@test output == check
81+
82+
# Finite Hamiltonians and MPOs
83+
# ----------------------------
84+
H = transverse_field_ising(; L=4)
85+
braille(buffer, H)
86+
output = String(take!(buffer))
87+
check = " ⎡⠉⠀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⠉⢀⎤🭻🭻⎡⡀⠀⎤ \n ⎣⠀⠀⎦ ⎣⠀⢀⎦ ⎣⠀⢀⎦ ⎣⡀⠀⎦ \n"
88+
@test output == check
89+
90+
O = make_time_mpo(H, 1.0, TaylorCluster(3, false, false))
91+
braille(buffer, O)
92+
output = String(take!(buffer))
93+
check = " ⎡⠉⠉⎤🭻🭻⎡⠍⠉⠤⠠⎤🭻🭻⎡⡏⠉⠒⠔⎤🭻🭻⎡⡇⠀⎤ \n ⎣⠀⠀⎦ ⎣⡂⠀⠀⠂⎦ ⎣⡇⠀⠀⡂⎦ ⎣⡇⠀⎦ \n"
94+
@test output == check
95+
end
6696
end

0 commit comments

Comments
 (0)