ITensors.jl icon indicating copy to clipboard operation
ITensors.jl copied to clipboard

[ITensors] Support for MPO*MPO contraction by densitymatrix method

Open shinaoka opened this issue 2 years ago • 9 comments

Description

Based on discussions with Miles.

This small patch allows contracting an MPO and an MPS with a dangling bond at each site. This is intended to be a minimal and tentative patch that will eventually be replaced by ITensorNetworks.jl.

Minimal demonstration of previous behavior

using ITensors

R = 3
sites = [Index(2, "Qubit,n=$n") for n in 1:R]
a = replaceprime(randomMPO(sites), 0 => 1, 1 => 2)
b = randomMPO(sites)
ab_ref = contract(a, b; alg="naive")
ab = contract(a, b; alg="densitymatrix")

ERROR: MethodError: no method matching contract(::NDTensors.Algorithm{:densitymatrix}, ::MPO, ::MPO)

Closest candidates are:
  contract(::NDTensors.Algorithm{:naive}, ::MPO, ::MPO; kwargs...)
   @ ITensors ~/.julia/packages/ITensors/HjjU3/src/mps/mpo.jl:801
  contract(::NDTensors.Algorithm{:densitymatrix}, ::MPO, ::MPS; kwargs...)
   @ ITensors ~/.julia/packages/ITensors/HjjU3/src/mps/mpo.jl:678
  contract(::NDTensors.Algorithm{:zipup}, ::MPO, ::MPO; kwargs...)
   @ ITensors ~/.julia/packages/ITensors/HjjU3/src/mps/mpo.jl:805
  ...

Stacktrace:
 [1] contract(A::MPO, B::MPO; alg::String, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ITensors ~/.julia/packages/ITensors/HjjU3/src/mps/mpo.jl:798
 [2] top-level scope
   @ REPL[7]:1
Minimal demonstration of new behavior

using ITensors

R = 3
sites = [Index(2, "Qubit,n=$n") for n in 1:R]
a = replaceprime(randomMPO(sites), 0 => 1, 1 => 2)
b = randomMPO(sites)
ab_ref = contract(a, b; alg="naive")
ab = contract(a, b; alg="densitymatrix")
ab_ref ≈ ab

How Has This Been Tested?

A test will be added in test_mpo.jl that compares the results of contractions of an MPO and an MPS with a dangling bond.

Checklist:

  • [x] My code follows the style guidelines of this project. Please run using JuliaFormatter; format(".") in the base directory of the repository (~/.julia/dev/ITensors) to format your code according to our style guidelines.
  • [x] I have performed a self-review of my own code.
  • [x] I have commented my code, particularly in hard-to-understand areas.
  • [x] I have added tests that verify the behavior of the changes I made.
  • [x] I have made corresponding changes to the documentation.
  • [x] My changes generate no new warnings.
  • [x] Any dependent changes have been merged and published in downstream modules.

shinaoka avatar May 11 '23 02:05 shinaoka

Thanks @shinaoka, this will be useful to have.

I would instead vote for generalizing the function signature of:

function contract(::Algorithm"densitymatrix", A::MPO, ψ::MPS; kwargs...)
  # [...]
end

to:

function contract(::Algorithm"densitymatrix", A::AbstractMPS, B::AbstractMPS; kwargs...)
  # [...]
end

and then returning an object that is typeof(B).

I'm also not such a fan of:

  function _getsimψ_c(i)
    t = copy(ψ_c[i])
    replaceinds!(t, dangling_inds_ψ[i], sim_dangling_inds_ψ[i])
    return t
  end

Why not just use:

replaceinds(ψ_c[i], dangling_inds_ψ[i], sim_dangling_inds_ψ[i])

directly?

mtfishman avatar May 25 '23 17:05 mtfishman

Codecov Report

Merging #1123 (3a4dcfd) into main (7120090) will decrease coverage by 29.29%. The diff coverage is 0.00%.

:exclamation: Current head 3a4dcfd differs from pull request most recent head aa2b347. Consider uploading reports for the commit aa2b347 to get more accurate results

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@             Coverage Diff             @@
##             main    #1123       +/-   ##
===========================================
- Coverage   82.59%   53.31%   -29.29%     
===========================================
  Files          86       85        -1     
  Lines        8319     7803      -516     
===========================================
- Hits         6871     4160     -2711     
- Misses       1448     3643     +2195     
Impacted Files Coverage Δ
src/mps/mpo.jl 0.00% <0.00%> (-90.23%) :arrow_down:

... and 64 files with indirect coverage changes

codecov-commenter avatar May 25 '23 18:05 codecov-commenter

Thank you for the suggestion. I have addressed the issues. I am a bit afraid of "Merging #1123 (3a4dcfd) into main (7120090) will decrease coverage by 29.29%.". What do you think?

shinaoka avatar May 26 '23 11:05 shinaoka

I have addressed the issues:

  • The testset has been moved into inside the testset "MPO Basics".
  • "function contract(::Algorithm"densitymatrix", M1::MPO, M2::MPO; kwargs...)::MPO" has been removed.

https://github.com/shinaoka/ITensors.jl/commit/6098a1067eb02e3bb6f9fce45a572547a83acd65

shinaoka avatar May 27 '23 13:05 shinaoka

[test ITensors mps] (this comment ought to run MPS and MPO tests also)

emstoudenmire avatar May 27 '23 14:05 emstoudenmire

Looks good to me @shinaoka. Let's see how the tests do and if Matt has more comments.

emstoudenmire avatar May 27 '23 14:05 emstoudenmire

Run ITensors mps tests from comment trigger: succeeded ✅ https://github.com/ITensor/ITensors.jl/actions/runs/5099391690

github-actions[bot] avatar May 27 '23 15:05 github-actions[bot]

Run ITensors mps tests from comment trigger: succeeded ✅ https://github.com/ITensor/ITensors.jl/actions/runs/5099391690

github-actions[bot] avatar May 27 '23 15:05 github-actions[bot]

One test on automatic differentiation failed. I got precisely the same error on my Macbook Pro with the latest commit in the main branch. The error seems to be independent of this PR.

shinaoka avatar May 27 '23 22:05 shinaoka