Skip to content

Commit 0d4137a

Browse files
authored
Compact MPS/MPO printing (#166)
1 parent b94c4bc commit 0d4137a

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensorMPS"
22
uuid = "0d1a4710-d33b-49a5-8f18-73bdf49b47e2"
33
authors = ["Matthew Fishman <[email protected]>", "Miles Stoudenmire <[email protected]>"]
4-
version = "0.3.24"
4+
version = "0.3.25"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/abstractmps.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,20 +2434,21 @@ end
24342434
# Printing functions
24352435
#
24362436

2437+
# Used to print `#undef` for unassigned MPS tensors.
2438+
# Maybe there's a better way to do this?
2439+
struct Undef end
2440+
Base.show(io::IO, ::Undef) = print(io, "#undef")
2441+
24372442
function Base.show(io::IO, M::AbstractMPS)
2438-
print(io, "$(typeof(M))")
2439-
(length(M) > 0) && print(io, "\n")
2440-
for i in eachindex(M)
2441-
if !isassigned(M, i)
2442-
println(io, "#undef")
2443-
else
2444-
A = M[i]
2445-
if order(A) != 0
2446-
println(io, "[$i] $(inds(A))")
2447-
else
2448-
println(io, "[$i] ITensor()")
2449-
end
2450-
end
2443+
print(io, "$(typeof(M))($(length(M)))")
2444+
return nothing
2445+
end
2446+
function Base.show(io::IO, ::MIME"text/plain", M::AbstractMPS)
2447+
println(io, "$(length(M))-element $(typeof(M)):")
2448+
inds_vec = map(eachindex(M)) do j
2449+
!isassigned(M, j) && return Undef()
2450+
return inds(M[j])
24512451
end
2452+
Base.print_array(io, inds_vec)
24522453
return
24532454
end

test/base/test_mpo.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ end
3939
O = MPO(sites)
4040
@test length(O) == N
4141

42-
str = split(sprint(show, O), '\n')
43-
@test endswith(str[1], "MPO")
44-
@test length(str) == length(O) + 2
42+
@test sprint(show, O) in ("MPO(6)", "ITensorMPS.MPO(6)")
43+
strs = split(sprint(show, MIME"text/plain"(), O), "\n")
44+
@test length(strs) == length(O) + 1
45+
@test strs[1] in ("6-element MPO:", "6-element ITensorMPS.MPO:")
46+
for str in strs[2:end]
47+
@test startswith(str, " ((dim=")
48+
end
4549

4650
O[1] = ITensor(sites[1], prime(sites[1]))
4751
@test hasind(O[1], sites[1])

test/base/test_mps.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ include(joinpath(@__DIR__, "utils", "util.jl"))
2424
@test linkdims(psi) == fill(3, length(psi) - 1)
2525
@test isnothing(flux(psi))
2626

27-
str = split(sprint(show, psi), '\n')
28-
@test endswith(str[1], "MPS")
29-
@test length(str) == length(psi) + 2
27+
@test sprint(show, psi) in ("MPS(10)", "ITensorMPS.MPS(10)")
28+
strs = split(sprint(show, MIME"text/plain"(), psi), "\n")
29+
@test length(strs) == length(psi) + 1
30+
@test strs[1] in ("10-element MPS:", "10-element ITensorMPS.MPS:")
31+
for str in strs[2:end]
32+
@test startswith(str, " ((dim=")
33+
end
3034

3135
@test siteind(psi, 2) == sites[2]
3236
@test findfirstsiteind(psi, sites[2]) == 2

0 commit comments

Comments
 (0)