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

Fix the AngularMomentum (again) #1292

Merged

Conversation

mrossinek
Copy link
Member

Summary

Fixes #1291

Details and comments

See also #1291 (comment) for some more details.

Prior to this fix, the `AngularMomentum` operator was working on the
assumption that the number of alpha-spin particles would always exceed
the number of beta-spin particles (for non-singlet systems). However,
this is not guaranteed to be the case. This commit fixes this problem by
using a symmetric formula for S^2 which is the average of the two cases
(alpha- vs. beta-spin particles exceeding the other).
When dealing with active spaces obtained from unrestricted-spin
orbitals, the alpha-beta overlap matrix which is used to construct the
S^2 operator can become non-unitary (for example, when the active set of
alpha- and beta-spin orbitals do not span the same space). This can
result in an <S^2> value measured on the active space that is largely
different from (e.g.) the UHF <S^2> value. More importantly, when this
is the case, the difference between these two <S^2> values is "hidden"
in the inactive+inactive and inactive+active interactions. Especially
when spin contamination is present, this can lead to vastly unexpected
results and may indicate a poor choice of active space.
@mrossinek mrossinek force-pushed the fix-overlap-anti-commutation branch from 528f24b to 531bb81 Compare December 1, 2023 16:50
@mrossinek mrossinek marked this pull request as ready for review December 1, 2023 16:50
@coveralls
Copy link

coveralls commented Dec 1, 2023

Pull Request Test Coverage Report for Build 7083799155

  • 19 of 19 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 86.765%

Totals Coverage Status
Change from base Build 7083375275: 0.02%
Covered Lines: 8785
Relevant Lines: 10125

💛 - Coveralls

"""
self.num_spatial_orbitals = num_spatial_orbitals
self._overlap: np.ndarray | None = None
self.overlap = overlap
Copy link
Collaborator

Choose a reason for hiding this comment

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

For my education: isn't this overridden by the overlap property?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see it now (I think! 😺) this line will call the setter and apply the initialization logic. But then is there a point to type-hint self._overlap a union type? Couldn't it be just np.ndarray?

Copy link
Member Author

Choose a reason for hiding this comment

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

The first line here makes sure that pylint does not complain about the private _overlap attribute not being defined inside the __init__ (because otherwise it would be defined inside the property method.
I am typing it for the sake of completeness 👍

self.overlap = overlap

@property
def overlap(self) -> np.ndarray | None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it ever possible for this to return None?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this can be left as None in which case the methods will internally treat it as an identity matrix of the correct size (but there is no need to store that square matrix so I left it as None 👍)

@mrossinek mrossinek added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Dec 4, 2023
@mrossinek mrossinek merged commit 55e6776 into qiskit-community:main Dec 4, 2023
15 checks passed
@mrossinek mrossinek deleted the fix-overlap-anti-commutation branch December 4, 2023 08:33
mergify bot pushed a commit that referenced this pull request Dec 4, 2023
* fix: use the symmetric formula for S^2

Prior to this fix, the `AngularMomentum` operator was working on the
assumption that the number of alpha-spin particles would always exceed
the number of beta-spin particles (for non-singlet systems). However,
this is not guaranteed to be the case. This commit fixes this problem by
using a symmetric formula for S^2 which is the average of the two cases
(alpha- vs. beta-spin particles exceeding the other).

* feat: log a warning when the alpha-beta overlap is non-unitary

When dealing with active spaces obtained from unrestricted-spin
orbitals, the alpha-beta overlap matrix which is used to construct the
S^2 operator can become non-unitary (for example, when the active set of
alpha- and beta-spin orbitals do not span the same space). This can
result in an <S^2> value measured on the active space that is largely
different from (e.g.) the UHF <S^2> value. More importantly, when this
is the case, the difference between these two <S^2> values is "hidden"
in the inactive+inactive and inactive+active interactions. Especially
when spin contamination is present, this can lead to vastly unexpected
results and may indicate a poor choice of active space.

* Add reno

* test: add a regression test

* fix: update warning tolerance

* Linting and formatting

* docs: fix verbatim in RST

(cherry picked from commit 55e6776)
mergify bot added a commit that referenced this pull request Dec 4, 2023
* fix: use the symmetric formula for S^2

Prior to this fix, the `AngularMomentum` operator was working on the
assumption that the number of alpha-spin particles would always exceed
the number of beta-spin particles (for non-singlet systems). However,
this is not guaranteed to be the case. This commit fixes this problem by
using a symmetric formula for S^2 which is the average of the two cases
(alpha- vs. beta-spin particles exceeding the other).

* feat: log a warning when the alpha-beta overlap is non-unitary

When dealing with active spaces obtained from unrestricted-spin
orbitals, the alpha-beta overlap matrix which is used to construct the
S^2 operator can become non-unitary (for example, when the active set of
alpha- and beta-spin orbitals do not span the same space). This can
result in an <S^2> value measured on the active space that is largely
different from (e.g.) the UHF <S^2> value. More importantly, when this
is the case, the difference between these two <S^2> values is "hidden"
in the inactive+inactive and inactive+active interactions. Especially
when spin contamination is present, this can lead to vastly unexpected
results and may indicate a poor choice of active space.

* Add reno

* test: add a regression test

* fix: update warning tolerance

* Linting and formatting

* docs: fix verbatim in RST

(cherry picked from commit 55e6776)

Co-authored-by: Max Rossmannek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The orbital overlap is still not handled correctly by the AngularMomentum operator
3 participants