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

feature: support LinearCombination as observable #246

Merged
merged 3 commits into from
May 3, 2024

Conversation

ashlhans
Copy link
Contributor

Issue #, if available:
#245

Description of changes:

  • Add support for the new LinearCombination as an observable that will be the default in the new version of PennyLane
  • Add support for the qml.opsSProd and qml.ops.Sum in the observable

Testing done:
tox and tox -e integ-tests

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

codecov bot commented Apr 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (184745c) to head (6bfb893).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #246   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines         1207      1215    +8     
  Branches       290       295    +5     
=========================================
+ Hits          1207      1215    +8     

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

@ashlhans ashlhans marked this pull request as ready for review April 30, 2024 23:53
@ashlhans ashlhans requested a review from a team as a code owner April 30, 2024 23:53
Comment on lines 48 to 53
try:
from pennylane.ops import LinearCombination
except (AttributeError, ImportError):

class LinearCombination:
pass
Copy link
Member

Choose a reason for hiding this comment

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

Rather than doing conditional imports, I'd say it's fine to wait until 0.36 is released and import normally

@speller26 speller26 linked an issue May 1, 2024 that may be closed by this pull request
@astralcai
Copy link
Contributor

astralcai commented May 1, 2024

When I proposed the solution I did not expect the issue of having to do conditional imports. In that case I would say:

isinstance(..., (qml.ops.Hamiltonian, qml.Hamiltonian))

would suffice. Depending on whether new opmath is enabled, qml.Hamiltonian will point to the correct thing. The only time this would cause an error is if the user is explicitly creating a LinearCombination with new opmath disabled, which seems like an edge case that wouldn't realistically happen (the only practical reason for disabling new opmath is typically to work with the legacy Hamiltonian).

Comment on lines 584 to 586
@_translate_observable.register(qml.ops.Hamiltonian)
@_translate_observable.register(qml.Hamiltonian)
def _(H: Union[qml.ops.Hamiltonian, qml.Hamiltonian]):
Copy link
Member

Choose a reason for hiding this comment

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

Does this work?

Suggested change
@_translate_observable.register(qml.ops.Hamiltonian)
@_translate_observable.register(qml.Hamiltonian)
def _(H: Union[qml.ops.Hamiltonian, qml.Hamiltonian]):
@_translate_observable.register
def _(H: Union[qml.ops.Hamiltonian, qml.Hamiltonian]):

Otherwise,

@_translate_observable.register(qml.ops.Hamiltonian)
@_translate_observable.register(qml.Hamiltonian)
def _(H):

Copy link
Contributor

Choose a reason for hiding this comment

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

Only since Python 3.11

src/braket/pennylane_plugin/ahs_device.py Outdated Show resolved Hide resolved
src/braket/pennylane_plugin/ahs_device.py Outdated Show resolved Hide resolved
@ashlhans ashlhans merged commit 012a3e2 into main May 3, 2024
15 checks passed
@ashlhans ashlhans deleted the supportLinearCombination branch May 3, 2024 21:53
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.

Support LinearCombination as an observable
3 participants