Skip to content

Commit

Permalink
Fixes to alternating_update default printing (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
emstoudenmire authored Sep 26, 2024
1 parent 902ea39 commit fc6fac2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/solvers/alternating_update/alternating_update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function alternating_update(
nsites, # define default for each level of solver implementation
updater, # this specifies the update performed locally
outputlevel=default_outputlevel(),
region_printer=nothing,
sweep_printer=nothing,
region_printer=default_region_printer,
sweep_printer=default_sweep_printer,
(sweep_observer!)=nothing,
(region_observer!)=nothing,
root_vertex=GraphsExtensions.default_root_vertex(init_state),
Expand Down Expand Up @@ -59,7 +59,7 @@ function alternating_update(
(sweep_observer!)=nothing,
sweep_printer=default_sweep_printer,#?
(region_observer!)=nothing,
region_printer=nothing,
region_printer=default_region_printer,
)
state = copy(init_state)
@assert !isnothing(sweep_plans)
Expand Down
18 changes: 11 additions & 7 deletions src/solvers/defaults.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Printf: @printf
using Printf: @printf, @sprintf
using ITensorMPS: maxlinkdim
default_outputlevel() = 0
default_nsites() = 2
Expand All @@ -7,10 +7,12 @@ default_extracter() = default_extracter
default_inserter() = default_inserter
default_checkdone() = (; kws...) -> false
default_transform_operator() = nothing

format(x) = @sprintf("%s", x)
format(x::AbstractFloat) = @sprintf("%.1E", x)

function default_region_printer(;
cutoff,
maxdim,
mindim,
inserter_kwargs,
outputlevel,
state,
sweep_plan,
Expand All @@ -23,9 +25,11 @@ function default_region_printer(;
region = first(sweep_plan[which_region_update])
@printf("Sweep %d, region=%s \n", which_sweep, region)
print(" Truncated using")
@printf(" cutoff=%.1E", cutoff)
@printf(" maxdim=%d", maxdim)
@printf(" mindim=%d", mindim)
for key in [:cutoff, :maxdim, :mindim]
if haskey(inserter_kwargs, key)
print(" ", key, "=", format(inserter_kwargs[key]))
end
end
println()
if spec != nothing
@printf(
Expand Down
1 change: 1 addition & 0 deletions test/test_treetensornetworks/test_solvers/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
28 changes: 27 additions & 1 deletion test/test_treetensornetworks/test_solvers/test_dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using KrylovKit: eigsolve
using NamedGraphs.NamedGraphGenerators: named_comb_tree
using Observers: observer
using StableRNGs: StableRNG
using Suppressor: @capture_out
using Test: @test, @test_broken, @testset

# This is needed since `eigen` is broken
Expand Down Expand Up @@ -76,6 +77,31 @@ ITensors.disable_auto_fermion()
new_E = inner(psi', H, psi)
@test new_E ≈ orig_E
=#

#
# Test outputlevels are working
#
prev_output = ""
for outputlevel in 0:2
output = @capture_out begin
e, psi = dmrg(
H,
psi;
outputlevel,
nsweeps,
maxdim,
cutoff,
nsites,
updater_kwargs=(; krylovdim=3, maxiter=1),
)
end
if outputlevel == 0
@test length(output) == 0
else
@test length(output) > length(prev_output)
end
prev_output = output
end
end

@testset "Observers" begin
Expand Down Expand Up @@ -139,7 +165,7 @@ end
nsweeps,
maxdim,
cutoff,
outputlevel=2,
outputlevel=0,
transform_operator=ITensorNetworks.cache_operator_to_disk,
transform_operator_kwargs=(; write_when_maxdim_exceeds=11),
)
Expand Down

0 comments on commit fc6fac2

Please sign in to comment.