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

The commutator methods break API by using normal_order #1298

Closed
mrossinek opened this issue Dec 7, 2023 · 0 comments · Fixed by #1299
Closed

The commutator methods break API by using normal_order #1298

mrossinek opened this issue Dec 7, 2023 · 0 comments · Fixed by #1299
Labels

Comments

@mrossinek
Copy link
Member

Environment

  • Qiskit Nature version: main @ 55e6776
  • Python version: Any
  • Operating system: Any

What is happening?

The methods provided by the qiskit_nature.second_q.operators.commutators module call .normal_order() even though they canNOT because this method is not part of the SparseLabelOp API. I am honestly very surprised how our linters did not catch this.

How can we reproduce the issue?

I found this while testing something with the MajoranaOp from #1270, but we can also reproduce it with the SpinOp which has no normal_order method.

from qiskit_nature.second_q.operators import SpinOp
from qiskit_nature.second_q.operators.commutators import commutator

op = SpinOp({"X_0": 1.0})

print(commutator(op, op))

This results in:

Traceback (most recent call last):
  File "/home/oss/Files/Dev/Qiskit/qiskit-nature/main/tmp-no.py", line 6, in <module>
    print(commutator(op, op))
  File "/home/oss/Files/Dev/Qiskit/qiskit-nature/main/qiskit_nature/second_q/operators/commutators.py", line 48, in commutator
    return (op_a @ op_b - op_b @ op_a).normal_order().simplify(atol=0)
AttributeError: 'SpinOp' object has no attribute 'normal_order'

What should happen?

The methods should work for any SparseLabelOp. This bug was introduced by #1210 which attempted to fix #1208.

@ftroisi we should figure out a different way of fixing that problem with the BosonicOp.

Any suggestions?

No response

@mrossinek mrossinek added the bug label Dec 7, 2023
mrossinek added a commit to mrossinek/qiskit-nature that referenced this issue Dec 7, 2023
mrossinek added a commit to mrossinek/qiskit-nature that referenced this issue Dec 7, 2023
mrossinek added a commit that referenced this issue Dec 7, 2023
* fix: do not use `normal_order` as part of commutator methods

Fixes #1298

* Update docs
mergify bot pushed a commit that referenced this issue Dec 7, 2023
* fix: do not use `normal_order` as part of commutator methods

Fixes #1298

* Update docs

(cherry picked from commit 978fcad)
mergify bot added a commit that referenced this issue Dec 7, 2023
…1300)

* fix: do not use `normal_order` as part of commutator methods

Fixes #1298

* Update docs

(cherry picked from commit 978fcad)

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant