RuntimeWarning and how to fix it

How do I use this algorithm? What does that parameter do?
Post Reply
rubenv
Posts: 6
Joined: 03 Mar 2021, 16:15

RuntimeWarning and how to fix it

Post by rubenv »

Hi guys,

I just joined the forum :) I wanted to share an issue I was having (I am using version 0.7.2). When running DMRG (or time evolution for that matter), I would sometimes get the error

TeNPy/tenpy/networks/mps.py:3374: RuntimeWarning: divide by zero encountered in reciprocal
S = S**form_diff

However, the output was fine, i.e., there did not actually seem to be an error. This was confirmed by printing out S (before applying the problematic line of code) and seeing that there are no small values. I realize there is another post on this forum about this same error, but it is not clear to me whether it was caused by the the same issue (in that case, the issue was solved by setting update_env = 0 in the DMRG parameters, which did not help for me), so I am posting this separately.

It simply turns out that

Code: Select all

S = S**form_diff

with form_diff = -1 gives the above error. If I instead use "S = 1./S", I get no error, although it is doing exactly the same thing. I guess that this if-clause only ever gets called if form_diff = -1, correct? If so, replacing this line of code by "S = 1./S" seems to have no downsides and gets ride of the issue.

Am I the only one who was getting this fake error message?

Cheers,
Ruben

User avatar
Johannes
Site Admin
Posts: 413
Joined: 21 Jul 2018, 12:52
Location: TU Munich

Re: RuntimeWarning and how to fix it

Post by Johannes »

Hi Ruben,

I have seen this warning before as well, but not so much lately. I vaguely remember that I also tried printing the S values and/or checking that none of them was 0 or negative, but couldn't quite pinpoint it.When testing S=S**-1. for different values like S = np.array([1.e-2, 1.e-15, 1.e-203, -1.e-203, -1.e-14]) manually, I could only see the warning pop up when there is an exact 0 in the array, in which case the warning appears as well for S = 1./S.
In other words, I don't really understand why using 1./S should help to avoid the warning. Maybe it's only an issue in specific numpy versions?

form_diff can in principle also be something like 0.5, in case you use the symmetric `C` form for your MPS. However, in practice at least none of the algorithms inside TeNPy use that form, such that -1, 0, and 1 are indeed the only remaining options.
Following your suggestion, I've updated it in d9821b821795233b9175e88df2bea3ab15ca1841 anyways.
User avatar
Johannes
Site Admin
Posts: 413
Joined: 21 Jul 2018, 12:52
Location: TU Munich

Re: RuntimeWarning and how to fix it

Post by Johannes »

Hmmm. I'm still a bit worried, because after all we should usually avoid taking inverses of small singular values all-together for numerical stability.
I tried to track down where it came from and found that it basically stems from this line in MPS.compress_svd() not truncating tiny singualar values, which gets called during MPO evolution with SVD compression (for infinite systems).
Is this where it occured for you as well?
User avatar
Johannes
Site Admin
Posts: 413
Joined: 21 Jul 2018, 12:52
Location: TU Munich

Re: RuntimeWarning and how to fix it

Post by Johannes »

Update:
when trying to install TeNPy on a cluster with the Intel Python distribution, we encountered the same runtime warnings.
Looking into this now...
Post Reply