From ff7c1dd4ec4e01d9b5f7355cc99b9518d05dd739 Mon Sep 17 00:00:00 2001 From: Hiroshi Shinaoka Date: Wed, 11 Jan 2023 13:30:56 +0900 Subject: [PATCH 1/2] Implement MPO-MPO contraction --- .../solvers/contract_mpo_mps.jl | 10 +++++-- .../test_solvers/test_contract_mpo.jl | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/treetensornetworks/solvers/contract_mpo_mps.jl b/src/treetensornetworks/solvers/contract_mpo_mps.jl index ccabbc3c..e8e102aa 100644 --- a/src/treetensornetworks/solvers/contract_mpo_mps.jl +++ b/src/treetensornetworks/solvers/contract_mpo_mps.jl @@ -30,16 +30,20 @@ function ITensors.contract( init_mps = deepcopy(init_mps) init_mps = sim(linkinds, init_mps) Ai = siteinds(A) - ti = Vector{Index}(undef, n) + init_mpsi = siteinds(init_mps) for j in 1:n + ti = nothing for i in Ai[j] if !hasind(psi0[j], i) - ti[j] = i + ti = i break end end + if ti !== nothing + ci = commoninds(init_mpsi[j], A[j])[1] + replaceind!(init_mps[j], ci=>ti) + end end - replace_siteinds!(init_mps, ti) t = Inf reverse_step = false diff --git a/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl b/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl index dc3564d7..50273ad7 100644 --- a/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl +++ b/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl @@ -51,4 +51,32 @@ using Test @test inner(psit, Hpsi) ≈ inner(psit, H, psi) atol = 1E-4 end +function asMPO(M::MPS, sites) + M_ = MPO(length(sites)) + for n in eachindex(sites) + M_[n] = M[n] + end + return M_ +end + +@testset "Contract MPO-MPO" begin + nbit = 5 + sites = siteinds("Qubit", nbit) + M1 = randomMPO(sites) + randomMPO(sites) + M2 = randomMPO(sites) + randomMPO(sites) + + # The function `apply` does not work correctly with the mapping-MPO-to-MPS trick. + M1 = replaceprime(M1, 1=>2, 0=>1) + + M2_ = MPS(length(sites)) + for n in eachindex(sites) + M2_[n] = M2[n] + end + + M12_ref = contract(M1, M2; alg="naive") + M12 = asMPO(contract(M1, M2_; alg="fit"), sites) + + @test M12_ref ≈ M12 +end + nothing From 141c2c8091cab8430698e62b8304e3768849b1cc Mon Sep 17 00:00:00 2001 From: Hiroshi Shinaoka Date: Wed, 11 Jan 2023 16:30:06 +0900 Subject: [PATCH 2/2] Refactor test_contract_mpo.jl --- .../test_solvers/test_contract_mpo.jl | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl b/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl index 50273ad7..937fe7e9 100644 --- a/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl +++ b/test/test_treetensornetworks/test_solvers/test_contract_mpo.jl @@ -51,8 +51,8 @@ using Test @test inner(psit, Hpsi) ≈ inner(psit, H, psi) atol = 1E-4 end -function asMPO(M::MPS, sites) - M_ = MPO(length(sites)) +function asMPS(M::MPO, sites) + M_ = MPS(length(sites)) for n in eachindex(sites) M_[n] = M[n] end @@ -68,13 +68,10 @@ end # The function `apply` does not work correctly with the mapping-MPO-to-MPS trick. M1 = replaceprime(M1, 1=>2, 0=>1) - M2_ = MPS(length(sites)) - for n in eachindex(sites) - M2_[n] = M2[n] - end + M2_ = asMPS(M2, sites) - M12_ref = contract(M1, M2; alg="naive") - M12 = asMPO(contract(M1, M2_; alg="fit"), sites) + M12_ref = asMPS(contract(M1, M2; alg="naive"), sites) + M12 = contract(M1, M2_; alg="fit") @test M12_ref ≈ M12 end