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

Update qml.generator's "hamiltonian" method to follow new operator arithmetic #5421

Merged
merged 31 commits into from
Mar 21, 2024

Conversation

lillian542
Copy link
Contributor

@lillian542 lillian542 commented Mar 20, 2024

Context:
We are generally treating "Hamiltonian" as meaning alternately qml.ops.Hamiltonian or qml.ops.LinearCombination depending on whether or not new_opmath is enabled.

The current implementation of qml.generator(op, format="hamiltonian") mostly respects that, but uses convert_to_legacy_H if the generator is a Sum, Prod or SProd even with new_opmath is enabled, breaking the convention.

Description of the Change:
We add convert_to_H, which converts any arithmetic operator to a conceptual Hamiltonian - either qml.ops.Hamiltonian or qml.ops.LinearCombination. We use the new convert_to_H function in the qml.generator method. The convert_to_legacy_H function continues to convert specifically to a legacy Hamiltonian regardless of the global setting for new_opmath.

Benefits:
Consistency, no one will ask for a generator with new_opmath enabled and get a legacy Hamiltonian.

@lillian542 lillian542 changed the base branch from master to ham-tests March 20, 2024 18:19
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@lillian542
Copy link
Contributor Author

[sc-57982]

Copy link

codecov bot commented Mar 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.48%. Comparing base (c290f28) to head (68e9e14).

Additional details and impacted files
@@            Coverage Diff             @@
##           ham-tests    #5421   +/-   ##
==========================================
  Coverage      99.47%   99.48%           
==========================================
  Files            400      400           
  Lines          37143    37146    +3     
==========================================
+ Hits           36949    36954    +5     
+ Misses           194      192    -2     

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

@lillian542 lillian542 marked this pull request as ready for review March 20, 2024 21:04
pennylane/operation.py Outdated Show resolved Hide resolved
Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

I am in favor of this change, i.e. enforcing that generators are Hamiltonian-like instances for consistency

Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
@lillian542
Copy link
Contributor Author

I am in favor of this change, i.e. enforcing that generators are Hamiltonian-like instances for consistency

I wouldn't say we're enforcing anything per se. This changes it specifically if you call qml.generator(op, format="hamiltonian"). Calls of qml.generator in general will still default to the prefactor format.

@PietropaoloFrisoni
Copy link
Contributor

Thank you so much for this @lillian542 ! Tests seem to have issues, but it is most certainly just random fluctuations

@Qottmann
Copy link
Contributor

The test failures came from ham-tests. Should be good now though 👍

@PietropaoloFrisoni
Copy link
Contributor

@Qottmann Yep, I have the same issue in my PR (which is why I am still waiting to merge).

@lillian542 lillian542 merged commit 5013e4b into ham-tests Mar 21, 2024
39 checks passed
@lillian542 lillian542 deleted the update_qml_generator branch March 21, 2024 17:48
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.

6 participants