Skip to content

Commit a332ac9

Browse files
committed
Updated docs and added tests.
1 parent 07f7534 commit a332ac9

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/lib/ITensorMPS/src/mpo.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -662,11 +662,16 @@ Choose the method with the `method` keyword, for example
662662
- `mindim::Int=1`: the minimal bond dimension of the resulting MPS.
663663
- `normalize::Bool=false`: whether or not to normalize the resulting MPS.
664664
- `method::String="densitymatrix"`: the algorithm to use for the contraction.
665-
Currently the options are "densitymatrix", where the network formed by the
666-
MPO and MPS is squared and contracted down to a density matrix which is
667-
diagonalized iteratively at each site, and "naive", where the MPO and MPS
668-
tensor are contracted exactly at each site and then a truncation of the
669-
resulting MPS is performed.
665+
- "densitymatrix": The network formed by the MPO and MPS is squared and contracted down to
666+
a density matrix which is diagonalized iteratively at each site.
667+
- "naive": The MPO and MPS tensor are contracted exactly at each site and then a truncation
668+
of the resulting MPS is performed.
669+
- "zipup": The MPO and MPS tensors are contracted then truncated at each site without enforcing
670+
the appropriate orthogonal gauge. Once this sweep is complete a call to `truncate!` occurs.
671+
Because the initial truncation is not locally optimal it is recommended to use a loose
672+
`cutoff` and `maxdim` and then pass the desired truncation parameters to the locally optimal
673+
`truncate!` sweep via the additional keyword argument `truncate_kwargs`.
674+
Suggested use is `contract(A, ψ; method="zipup", cutoff=cutoff / 10, maxdim=2 * maxdim, truncate_kwargs=(; cutoff, maxdim))`.
670675
671676
See also [`apply`](@ref).
672677
"""
@@ -946,14 +951,20 @@ C = apply(A, B; alg="naive", truncate=false)
946951
in general you should set a `cutoff` value.
947952
- `maxdim::Int=maxlinkdim(A) * maxlinkdim(B))`: the maximal bond dimension of the results MPS.
948953
- `mindim::Int=1`: the minimal bond dimension of the resulting MPS.
949-
- `alg="zipup"`: Either `"zipup"` or `"naive"`. `"zipup"` contracts pairs of
950-
site tensors and truncates with SVDs in a sweep across the sites, while `"naive"`
951-
first contracts pairs of tensor exactly and then truncates at the end if `truncate=true`.
952954
- `truncate=true`: Enable or disable truncation. If `truncate=false`, ignore
953955
other truncation parameters like `cutoff` and `maxdim`. This is most relevant
954956
for the `"naive"` version, if you just want to contract the tensors pairwise
955957
exactly. This can be useful if you are contracting MPOs that have diverging
956958
norms, such as MPOs originating from sums of local operators.
959+
- `alg="zipup"`: the algorithm to use for the contraction. Supported algorithms are
960+
- "naive": The MPO tensors are contracted exactly at each site and then a truncation
961+
of the resulting MPO is performed.
962+
- "zipup": The MPO and MPS tensors are contracted then truncated at each site without enforcing
963+
the appropriate orthogonal gauge. Once this sweep is complete a call to `truncate!` occurs.
964+
Because the initial truncation is not locally optimal it is recommended to use a loose
965+
`cutoff` and `maxdim` and then pass the desired truncation parameters to the locally optimal
966+
`truncate!` sweep via the additional keyword argument `truncate_kwargs`.
967+
Suggested use is `contract(A, ψ; method="zipup", cutoff=cutoff / 10, maxdim=2 * maxdim, truncate_kwargs=(; cutoff, maxdim))`.
957968
958969
See also [`apply`](@ref) for details about the arguments available.
959970
"""

src/lib/ITensorMPS/test/base/test_mpo.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,22 @@ end
240240
@test_throws DimensionMismatch error_contract(phi, K, badpsi)
241241
end
242242

243-
@testset "contract" begin
243+
@testset "contract(::MPO, ::MPS)" for method in ["densitymatrix", "naive", "zipup"]
244244
phi = random_mps(sites)
245245
K = random_mpo(sites)
246246
@test maxlinkdim(K) == 1
247247
psi = random_mps(sites)
248-
psi_out = contract(K, psi; maxdim=1)
248+
psi_out = contract(K, psi; method, maxdim=1)
249249
@test inner(phi', psi_out) inner(phi', K, psi)
250-
psi_out = contract(psi, K; maxdim=1)
250+
psi_out = contract(psi, K; method, maxdim=1)
251251
@test inner(phi', psi_out) inner(phi', K, psi)
252252
psi_out = psi * K
253253
@test inner(phi', psi_out) inner(phi', K, psi)
254254
@test_throws MethodError contract(K, psi; method="fakemethod")
255255

256256
badsites = [Index(2, "Site") for n in 1:(N + 1)]
257257
badpsi = random_mps(badsites)
258-
@test_throws DimensionMismatch contract(K, badpsi)
258+
@test_throws DimensionMismatch contract(K, badpsi; method)
259259

260260
# make bigger random MPO...
261261
for link_dim in 2:5
@@ -287,7 +287,7 @@ end
287287
orthogonalize!(psi, 1; maxdim=link_dim)
288288
orthogonalize!(K, 1; maxdim=link_dim)
289289
orthogonalize!(phi, 1; normalize=true, maxdim=link_dim)
290-
psi_out = contract(deepcopy(K), deepcopy(psi); maxdim=10 * link_dim, cutoff=0.0)
290+
psi_out = contract(deepcopy(K), deepcopy(psi); method, maxdim=10 * link_dim, cutoff=0.0)
291291
@test inner(phi', psi_out) inner(phi', K, psi)
292292
end
293293
end
@@ -354,14 +354,14 @@ end
354354
@test maxlinkdim(H) maxlinkdim(H₁) + maxlinkdim(H₂)
355355
end
356356

357-
@testset "contract(::MPO, ::MPO)" begin
357+
@testset "contract(::MPO, ::MPO)" for alg in ["naive", "zipup"]
358358
psi = random_mps(sites)
359359
K = random_mpo(sites)
360360
L = random_mpo(sites)
361361
@test maxlinkdim(K) == 1
362362
@test maxlinkdim(L) == 1
363-
KL = contract(prime(K), L; maxdim=1)
364-
psi_kl_out = contract(prime(K), contract(L, psi; maxdim=1); maxdim=1)
363+
KL = contract(prime(K), L; alg, maxdim=1)
364+
psi_kl_out = contract(prime(K), contract(L, psi; alg, maxdim=1); alg, maxdim=1)
365365
@test inner(psi'', KL, psi) inner(psi'', psi_kl_out) atol = 5e-3
366366

367367
# where both K and L have differently labelled sites
@@ -373,15 +373,15 @@ end
373373
replaceind!(K[ii], sites[ii]', othersitesk[ii])
374374
replaceind!(L[ii], sites[ii]', othersitesl[ii])
375375
end
376-
KL = contract(K, L; maxdim=1)
376+
KL = contract(K, L; alg, maxdim=1)
377377
psik = random_mps(othersitesk)
378378
psil = random_mps(othersitesl)
379-
psi_kl_out = contract(K, contract(L, psil; maxdim=1); maxdim=1)
379+
psi_kl_out = contract(K, contract(L, psil; alg, maxdim=1); alg, maxdim=1)
380380
@test inner(psik, KL, psil) inner(psik, psi_kl_out) atol = 5e-3
381381

382382
badsites = [Index(2, "Site") for n in 1:(N + 1)]
383383
badL = random_mpo(badsites)
384-
@test_throws DimensionMismatch contract(K, badL)
384+
@test_throws DimensionMismatch contract(K, badL; alg)
385385
end
386386

387387
@testset "*(::MPO, ::MPO)" begin

0 commit comments

Comments
 (0)