|
| 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) |
0 commit comments