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

Fixed a bug in MPS from string. #1338

Closed

Conversation

corbett5
Copy link
Contributor

Fixes what I believe to be a bug in the construction of a QN MPS from a list of strings. Specifically I think the direction of the link indices was backwards.

using ITensors

sites = siteinds("Qubit", 2; conserve_qns=true)

os = OpSum()
os .+= 1, "Z", 1
os .+= 1, "Z", 2
H = MPO(os, sites)

psi = MPS(sites, ["1", "1"])
@show linkinds(psi)

Hpsi = apply(H, psi)
@show linkinds(Hpsi)

add(psi, Hpsi; alg="directsum")
Demonstration of previous behavior

linkinds(psi) = Index{Vector{Pair{QN, Int64}}}[(dim=1|id=823|"Link,l=1") <In>
 1: QN("Parity",1,2) => 1]
linkinds(Hpsi) = Index{Vector{Pair{QN, Int64}}}[(dim=1|id=961|"Link,l=1") <Out>
 1: QN("Parity",1,2) => 1]
ERROR: LoadError: To direct sum two indices, they must have the same direction. Trying to direct sum indices (dim=1|id=823|"Link,l=1") <In>
 1: QN("Parity",1,2) => 1 and (dim=1|id=961|"Link,l=1") <Out>
 1: QN("Parity",1,2) => 1.

Demonstration of new behavior

linkinds(psi) = Index{Vector{Pair{QN, Int64}}}[(dim=1|id=124|"Link,l=1") <Out>
 1: QN("Parity",1,2) => 1]
linkinds(Hpsi) = Index{Vector{Pair{QN, Int64}}}[(dim=1|id=906|"Link,l=1") <Out>
 1: QN("Parity",1,2) => 1]
MPS
[1] ((dim=2|id=724|"Qubit,Site,n=1") <Out>
 1: QN("Parity",0,2) => 1
 2: QN("Parity",1,2) => 1, (dim=2|id=806|"Link,l=1") <Out>
 1: QN("Parity",1,2) => 1
 2: QN("Parity",1,2) => 1)
[2] ((dim=2|id=269|"Qubit,Site,n=2") <Out>
 1: QN("Parity",0,2) => 1
 2: QN("Parity",1,2) => 1, (dim=2|id=806|"Link,l=1") <In>
 1: QN("Parity",1,2) => 1
 2: QN("Parity",1,2) => 1)

  • 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.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that verify the behavior of the changes I made.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • Any dependent changes have been merged and published in downstream modules.

@codecov-commenter
Copy link

codecov-commenter commented Feb 20, 2024

Codecov Report

Attention: 12 lines in your changes are missing coverage. Please review.

Comparison is base (2fd3e0a) 84.32% compared to head (3768e33) 53.79%.
Report is 3 commits behind head on main.

❗ Current head 3768e33 differs from pull request most recent head 6ea1852. Consider uploading reports for the commit 6ea1852 to get more accurate results

Files Patch % Lines
src/ITensorChainRules/mps/abstractmps.jl 0.00% 7 Missing ⚠️
src/ITensorChainRules/mps/mpo.jl 0.00% 4 Missing ⚠️
src/mps/mps.jl 0.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1338       +/-   ##
===========================================
- Coverage   84.32%   53.79%   -30.54%     
===========================================
  Files         100       99        -1     
  Lines        8568     8525       -43     
===========================================
- Hits         7225     4586     -2639     
- Misses       1343     3939     +2596     

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

@emstoudenmire
Copy link
Collaborator

Thanks Ben, but let me take a close look at this before we decide to merge. I remember needing to reverse the link indices from the usual direction in order to make this function work properly with our "auto fermion" system.

@emstoudenmire
Copy link
Collaborator

Is this reverse order causing an issue later on in your code? Or is it just not the ordering you expected when printing the tensors?

@corbett5
Copy link
Contributor Author

It produces an error when trying to add two MPSs using the "directsum" method (see above). I think it's just a consistency issue, that the direction from apply is one way and the direction from this MPS function is another. Now that I think about it there's probably an easyish way to reverse the direction of the links of an MPS and I could just do that in my code, but it seems like a hack.

@mtfishman
Copy link
Member

I think a better solution would be to make add(::MPS, ::MPS; alg="directsum") work with MPS where the link indices have mismatched arrow directions between the MPS, the fact that it doesn't is the real issue here and should be considered a bug. We shouldn't need to change the arrow direction in the MPS constructor. The arrow directions of link indices are just an internal detail that shouldn't affect external code (and as Miles said, we may choose the arrow directions to be one way or another for internal reasons, like the automatic fermion sign system).

@mtfishman
Copy link
Member

I raised an issue about add(::MPS, ::MPS; alg="directsum") in #1340, I'll close this in favor of that.

@mtfishman mtfishman closed this Feb 21, 2024
@corbett5 corbett5 deleted the bugfix/corbett/mps-construction branch February 21, 2024 02:29
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.

None yet

4 participants