Skip to content

Commit 3b1fa88

Browse files
authored
[ITensors] Remove usage of get on keyword arguments (#1241)
1 parent 4e0ef92 commit 3b1fa88

21 files changed

Lines changed: 81 additions & 792 deletions

src/ITensorChainRules/mps/mpo.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ function rrule(::typeof(-), x1::MPO, x2::MPO; kwargs...)
3434
return rrule(+, x1, -x2; kwargs...)
3535
end
3636

37-
function rrule(::typeof(tr), x::MPO; kwargs...)
38-
y = tr(x; kwargs...)
37+
function rrule(::typeof(tr), x::MPO; plev=(0 => 1), kwargs...)
38+
y = tr(x; plev, kwargs...)
3939
function tr_pullback(ȳ)
4040
s = noprime(firstsiteinds(x))
4141
n = length(s)
4242
= MPO(s, "Id")
43-
44-
plev = get(kwargs, :plev, 0 => 1)
4543
for j in 1:n
4644
x̄[j] = mapprime(x̄[j], 0 => first(plev), 1 => last(plev))
4745
end

src/index.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct Index{T}
3535
dir::Arrow
3636
tags::TagSet
3737
plev::Int
38-
function Index{T}(id, space::T, dir, tags, plev) where {T}
38+
function Index{T}(id, space::T, dir::Arrow, tags, plev) where {T}
3939
return new{T}(id, space, dir, tags, plev)
4040
end
4141
end
@@ -48,6 +48,11 @@ end
4848
# mostly for internal usage
4949
Index{T}(dim::T) where {T} = Index(dim)
5050

51+
# `Nothing` direction gets converted to `Neither`.
52+
function Index{T}(id, space::T, dir::Nothing, tags, plev) where {T}
53+
return Index{T}(id, space, Neither, tags, plev)
54+
end
55+
5156
function Index(id, space::T, dir, tags, plev) where {T}
5257
return Index{T}(id, space, dir, tags, plev)
5358
end
@@ -101,7 +106,7 @@ julia> tags(i)
101106
```
102107
"""
103108
function Index(dim::Number, tags::Union{AbstractString,TagSet}; plev::Int=0)
104-
return Index(dim; tags=tags, plev=plev)
109+
return Index(dim; tags, plev)
105110
end
106111

107112
# This is so that when IndexSets are converted
@@ -657,8 +662,7 @@ function Base.show(io::IO, i::Index)
657662
end
658663
end
659664

660-
function readcpp(io::IO, ::Type{Index}; kwargs...)
661-
format = get(kwargs, :format, "v3")
665+
function readcpp(io::IO, ::Type{Index}; format="v3")
662666
if format != "v3"
663667
throw(ArgumentError("read Index: format=$format not supported"))
664668
end

src/indexset.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,13 +910,12 @@ block(inds::Indices, vals::Integer...) = blockindex(inds, vals...)[2]
910910
# Read and write
911911
#
912912

913-
function readcpp(io::IO, ::Type{<:Indices}; kwargs...)
914-
format = get(kwargs, :format, "v3")
913+
function readcpp(io::IO, ::Type{<:Indices}; format="v3")
915914
is = IndexSet()
916915
if format == "v3"
917916
size = read(io, Int)
918917
function readind(io, n)
919-
i = readcpp(io, Index; kwargs...)
918+
i = readcpp(io, Index; format)
920919
stride = read(io, UInt64)
921920
return i
922921
end

src/itensor.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,8 +2073,7 @@ function show(io::IO, mime::MIME"text/plain", T::ITensor)
20732073
return summary(io, T)
20742074
end
20752075

2076-
function readcpp(io::IO, ::Type{Dense{ValT}}; kwargs...) where {ValT}
2077-
format = get(kwargs, :format, "v3")
2076+
function readcpp(io::IO, ::Type{Dense{ValT}}; format="v3") where {ValT}
20782077
if format == "v3"
20792078
size = read(io, UInt64)
20802079
data = Vector{ValT}(undef, size)
@@ -2087,8 +2086,7 @@ function readcpp(io::IO, ::Type{Dense{ValT}}; kwargs...) where {ValT}
20872086
end
20882087
end
20892088

2090-
function readcpp(io::IO, ::Type{ITensor}; kwargs...)
2091-
format = get(kwargs, :format, "v3")
2089+
function readcpp(io::IO, ::Type{ITensor}; format="v3")
20922090
if format == "v3"
20932091
# TODO: use Vector{Index} here?
20942092
inds = readcpp(io, IndexSet; kwargs...)

src/iterativesolvers.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function expand_krylov_space(M::Matrix{elT}, V, AV, ni) where {elT}
4343
return newM
4444
end
4545

46-
function davidson(A, phi0::ITensorT; kwargs...) where {ITensorT<:ITensor}
46+
function davidson(
47+
A, phi0::ITensorT; maxiter=2, miniter=1, errgoal=1e-14, Northo_pass=1
48+
) where {ITensorT<:ITensor}
4749
elTA = eltype(A)
4850
elTphi = eltype(phi0)
4951

@@ -56,12 +58,6 @@ function davidson(A, phi0::ITensorT; kwargs...) where {ITensorT<:ITensor}
5658
else
5759
phi = copy(phi0)
5860
end
59-
60-
maxiter = get(kwargs, :maxiter, 2)
61-
miniter = get(kwargs, :miniter, 1)
62-
errgoal = get(kwargs, :errgoal, 1E-14)
63-
Northo_pass = get(kwargs, :Northo_pass, 1)
64-
6561
approx0 = 1E-12
6662

6763
nrm = norm(phi)

src/mps/abstractmps.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,8 +1966,7 @@ end
19661966
19671967
Swap the sites `b` and `b+1`.
19681968
"""
1969-
function swapbondsites::AbstractMPS, b::Integer; kwargs...)
1970-
ortho = get(kwargs, :ortho, "right")
1969+
function swapbondsites::AbstractMPS, b::Integer; ortho="right", kwargs...)
19711970
ψ = copy(ψ)
19721971
if ortho == "left"
19731972
orthocenter = b + 1

src/mps/mpo.jl

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -674,19 +674,25 @@ contract(ψ::MPS, A::MPO; kwargs...) = contract(A, ψ; kwargs...)
674674

675675
#@doc (@doc contract(::MPO, ::MPS)) *(::MPO, ::MPS)
676676

677-
function contract(::Algorithm"densitymatrix", A::MPO, ψ::MPS; kwargs...)::MPS
677+
function contract(
678+
::Algorithm"densitymatrix",
679+
A::MPO,
680+
ψ::MPS;
681+
cutoff=1e-13,
682+
maxdim=maxlinkdim(A) * maxlinkdim(ψ),
683+
mindim=1,
684+
normalize=false,
685+
kwargs...,
686+
)::MPS
678687
n = length(A)
679688
n != length(ψ) &&
680689
throw(DimensionMismatch("lengths of MPO ($n) and MPS ($(length(ψ))) do not match"))
681690
if n == 1
682691
return MPS([A[1] * ψ[1]])
683692
end
684-
693+
mindim = max(mindim, 1)
694+
requested_maxdim = maxdim
685695
ψ_out = similar(ψ)
686-
cutoff::Float64 = get(kwargs, :cutoff, 1e-13)
687-
requested_maxdim::Int = get(kwargs, :maxdim, maxlinkdim(A) * maxlinkdim(ψ))
688-
mindim::Int = max(get(kwargs, :mindim, 1), 1)
689-
normalize::Bool = get(kwargs, :normalize, false)
690696

691697
any(i -> isempty(i), siteinds(commoninds, A, ψ)) &&
692698
error("In `contract(A::MPO, x::MPS)`, `A` and `x` must share a set of site indices")
@@ -719,7 +725,7 @@ function contract(::Algorithm"densitymatrix", A::MPO, ψ::MPS; kwargs...)::MPS
719725
ts = isnothing(l) ? "" : tags(l)
720726
Lis = siteinds(uniqueinds, A, ψ, n)
721727
Ris = siteinds(uniqueinds, simA_c, ψ_c, n)
722-
F = eigen(ρ, Lis, Ris; ishermitian=true, tags=ts, kwargs...)
728+
F = eigen(ρ, Lis, Ris; ishermitian=true, tags=ts, cutoff, maxdim, mindim, kwargs...)
723729
D, U, Ut = F.D, F.V, F.Vt
724730
l_renorm, r_renorm = F.l, F.r
725731
ψ_out[n] = Ut
@@ -739,7 +745,7 @@ function contract(::Algorithm"densitymatrix", A::MPO, ψ::MPS; kwargs...)::MPS
739745
ts = isnothing(l) ? "" : tags(l)
740746
Lis = IndexSet(s..., l_renorm)
741747
Ris = IndexSet(s̃..., r_renorm)
742-
F = eigen(ρ, Lis, Ris; ishermitian=true, maxdim=maxdim, tags=ts, kwargs...)
748+
F = eigen(ρ, Lis, Ris; ishermitian=true, tags=ts, cutoff, maxdim, mindim, kwargs...)
743749
D, U, Ut = F.D, F.V, F.Vt
744750
l_renorm, r_renorm = F.l, F.r
745751
ψ_out[j] = Ut
@@ -755,9 +761,7 @@ function contract(::Algorithm"densitymatrix", A::MPO, ψ::MPS; kwargs...)::MPS
755761
return ψ_out
756762
end
757763

758-
function _contract(::Algorithm"naive", A, ψ; kwargs...)
759-
truncate = get(kwargs, :truncate, true)
760-
764+
function _contract(::Algorithm"naive", A, ψ; truncate=true, kwargs...)
761765
A = sim(linkinds, A)
762766
ψ = sim(linkinds, ψ)
763767

@@ -801,16 +805,20 @@ function contract(alg::Algorithm"naive", A::MPO, B::MPO; kwargs...)
801805
return _contract(alg, A, B; kwargs...)
802806
end
803807

804-
function contract(::Algorithm"zipup", A::MPO, B::MPO; kwargs...)
808+
function contract(
809+
::Algorithm"zipup",
810+
A::MPO,
811+
B::MPO;
812+
cutoff=1e-14,
813+
maxdim=maxlinkdim(A) * maxlinkdim(B),
814+
mindim=1,
815+
kwargs...,
816+
)
805817
if hassameinds(siteinds, A, B)
806818
error(
807819
"In `contract(A::MPO, B::MPO)`, MPOs A and B have the same site indices. The indices of the MPOs in the contraction are taken literally, and therefore they should only share one site index per site so the contraction results in an MPO. You may want to use `replaceprime(contract(A', B), 2 => 1)` or `apply(A, B)` which automatically adjusts the prime levels assuming the input MPOs have pairs of primed and unprimed indices.",
808820
)
809821
end
810-
cutoff::Float64 = get(kwargs, :cutoff, 1e-14)
811-
resp_degen::Bool = get(kwargs, :respect_degenerate, true)
812-
maxdim::Int = get(kwargs, :maxdim, maxlinkdim(A) * maxlinkdim(B))
813-
mindim::Int = max(get(kwargs, :mindim, 1), 1)
814822
N = length(A)
815823
N != length(B) &&
816824
throw(DimensionMismatch("lengths of MPOs A ($N) and B ($(length(B))) do not match"))
@@ -832,9 +840,9 @@ function contract(::Algorithm"zipup", A::MPO, B::MPO; kwargs...)
832840
left_inds;
833841
ortho="left",
834842
tags=commontags(linkinds(A, i)),
835-
cutoff=cutoff,
836-
maxdim=maxdim,
837-
mindim=mindim,
843+
cutoff,
844+
maxdim,
845+
mindim,
838846
kwargs...,
839847
)
840848
lCᵢ = dag(commoninds(C[i], R))
@@ -847,9 +855,9 @@ function contract(::Algorithm"zipup", A::MPO, B::MPO; kwargs...)
847855
left_inds;
848856
ortho="right",
849857
tags=commontags(linkinds(A, i)),
850-
cutoff=cutoff,
851-
maxdim=maxdim,
852-
mindim=mindim,
858+
cutoff,
859+
maxdim,
860+
mindim,
853861
kwargs...,
854862
)
855863
truncate!(C; kwargs...)

src/mps/mps.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -970,17 +970,15 @@ dens = expect(psi, "Ntot")
970970
updens, dndens = expect(psi, "Nup", "Ndn") # pass more than one operator
971971
```
972972
"""
973-
function expect(psi::MPS, ops; kwargs...)
973+
function expect(psi::MPS, ops; sites=1:length(psi), site_range=nothing)
974974
psi = copy(psi)
975975
N = length(psi)
976976
ElT = promote_itensor_eltype(psi)
977977
s = siteinds(psi)
978978

979-
if haskey(kwargs, :site_range)
979+
if !isnothing(site_range)
980980
@warn "The `site_range` keyword arg. to `expect` is deprecated: use the keyword `sites` instead"
981-
sites = kwargs[:site_range]
982-
else
983-
sites = get(kwargs, :sites, 1:N)
981+
sites = site_range
984982
end
985983

986984
site_range = (sites isa AbstractRange) ? sites : collect(sites)

src/mps/observer.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ function measure!(obs::DMRGObserver; kwargs...)
176176
end
177177
end
178178

179-
function checkdone!(o::DMRGObserver; kwargs...)
180-
outputlevel = get(kwargs, :outputlevel, false)
179+
function checkdone!(
180+
o::DMRGObserver; outputlevel=false, energy=nothing, psi=nothing, sweep=nothing
181+
)
181182
if (
182183
length(real(energies(o))) > o.minsweeps &&
183184
abs(real(energies(o))[end] - real(energies(o))[end - 1]) < o.etol

src/physics/autompo/opsum_to_mpo.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# `ValType::Type{<:Number}` is used instead of `ValType::Type` for efficiency, possibly due to increased method specialization.
22
# See https://github.com/ITensor/ITensors.jl/pull/1183.
3-
function svdMPO(ValType::Type{<:Number}, os::OpSum{C}, sites; kwargs...)::MPO where {C}
4-
mindim::Int = get(kwargs, :mindim, 1)
5-
maxdim::Int = get(kwargs, :maxdim, 10000)
6-
cutoff::Float64 = get(kwargs, :cutoff, 1E-15)
7-
3+
function svdMPO(
4+
ValType::Type{<:Number}, os::OpSum{C}, sites; mindim=1, maxdim=typemax(Int), cutoff=1e-15
5+
)::MPO where {C}
86
N = length(sites)
97

108
# Specifying the element type with `Matrix{ValType}[...]` improves type inference and therefore efficiency.

0 commit comments

Comments
 (0)