Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension to convert ITensorMPS.MPS to Tenet.MPS and viceversa #251

Merged
merged 8 commits into from
Nov 18, 2024

Conversation

jofrevalles
Copy link
Member

@jofrevalles jofrevalles commented Nov 15, 2024

Summary

This PR resolves #247 by adding TenetITensorMPSExt extension, which adds two convert functions to convert ITensorMPS.MPS to Tenet.MPS and viceversa. Additionally, we also added some tests to cover for this function. @starsfordummies

TODO

  • Clean some code.

Example

julia> using Tenet

julia> using ITensors; using ITensorMPS

julia> tenet_mps = rand(MPS; n=5, maxdim=30)
MPS (inputs=0, outputs=5)

julia> itensor_mps = convert(ITensorMPS.MPS, tenet_mps) # Tenet.MPS to ITensorMPS conversion
ITensors.ITensorMPS.MPS
[1] ((dim=2|id=565|"Site,n=1"), (dim=2|id=954|"Link,l=1"))
[2] ((dim=2|id=557|"Site,n=2"), (dim=2|id=954|"Link,l=1"), (dim=4|id=965|"Link,l=2"))
[3] ((dim=2|id=556|"Site,n=3"), (dim=4|id=965|"Link,l=2"), (dim=4|id=651|"Link,l=3"))
[4] ((dim=2|id=195|"Site,n=4"), (dim=4|id=651|"Link,l=3"), (dim=2|id=558|"Link,l=4"))
[5] ((dim=2|id=263|"Site,n=5"), (dim=2|id=558|"Link,l=4"))

julia> @test isapprox(parent(Tenet.contract(tenet_mps)), Array(ITensorMPS.contract(itensor_mps).tensor))
Test Passed

julia> itensor_mps = ITensorMPS.random_mps(siteinds(4, 5); linkdims=7)
ITensors.ITensorMPS.MPS
[1] ((dim=4|id=51|"Site,n=1"), (dim=7|id=149|"Link,l=1"))
[2] ((dim=7|id=149|"Link,l=1"), (dim=4|id=113|"Site,n=2"), (dim=7|id=746|"Link,l=2"))
[3] ((dim=7|id=746|"Link,l=2"), (dim=4|id=227|"Site,n=3"), (dim=7|id=555|"Link,l=3"))
[4] ((dim=7|id=555|"Link,l=3"), (dim=4|id=400|"Site,n=4"), (dim=4|id=984|"Link,l=4"))
[5] ((dim=4|id=984|"Link,l=4"), (dim=4|id=459|"Site,n=5"))

julia> tenet_mps = convert(MPS, itensor_mps) # ITensorMPS to Tenet.MPS conversion
MPS (inputs=0, outputs=5)

julia> form(tenet_mps)
MixedCanonical(1)

julia> contracted = permutedims(Tenet.contract(tenet_mps), [inds(tenet_mps; at=Site(i)) for i in 1:length(tensors(tenet_mps))])
4×4×4×4×4 Tensor{Float64, 5, Array{Float64, 5}}: ...

julia> @test isapprox(parent(contracted), Array(ITensorMPS.contract(itensor_mps).tensor))
Test Passed

Copy link
Member

@mofeing mofeing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super cool!

Project.toml Outdated
@@ -66,6 +68,7 @@ EinExprs = "0.5, 0.6"
FiniteDifferences = "0.12"
GraphMakie = "0.4,0.5"
Graphs = "1.7"
ITensorMPS = "0.2.6"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm if we set the patch version, will we have problems when v0.2.7 lands?

like, will it force it to use v0.2.6 or will it be compatible (but not included) up to v0.3.0?

in principle we should only mark major and minor versions, not patch version (unless a patch is completely required for it to work)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmm I don't know, I just put the version since we also had that for ITensors and ITensorNetworks. Do you really think this is a problem?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to just set the major.minor version. If you need a higher patch version, you should just call Pkg.update() then.

Copy link
Contributor

@starsfordummies starsfordummies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nicee! It should be easy to make it work for MPO as well, yes?
so we can finally have MPO to MPS application in Tenet:

function evolve!(tenet_mpo, tenet_mps)
   itensor_mps = convert(ITensorMPS.MPS, tenet_mps)
   itensor_mpo = convert(ITensorMPS.MPS, tenet_mpo)
   o_psi = apply(itensor_mpo, itensor_mps)
   o_psi_tenet = convert(MPS, o_psi) 
   println("🙃 ")
end 

@jofrevalles
Copy link
Member Author

I want to merge this, do you approve the PR? @mofeing @starsfordummies

Copy link
Contributor

@starsfordummies starsfordummies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks!

@mofeing
Copy link
Member

mofeing commented Nov 18, 2024

The tests failing are my fault. If they pass locally, you can merge.

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 0% with 52 lines in your changes missing coverage. Please review.

Project coverage is 0.04%. Comparing base (8a576c5) to head (a510320).
Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
ext/TenetITensorMPSExt.jl 0.00% 52 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           master    #251      +/-   ##
=========================================
- Coverage    0.05%   0.04%   -0.01%     
=========================================
  Files          30      32       +2     
  Lines        1708    2036     +328     
=========================================
  Hits            1       1              
- Misses       1707    2035     +328     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jofrevalles jofrevalles merged commit 8446b75 into master Nov 18, 2024
2 of 7 checks passed
@jofrevalles jofrevalles deleted the feature/convert-itensor_mps branch November 18, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

convert to/from ITensors MPS
3 participants