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

Support qml.counts for circuits #267

Merged
merged 7 commits into from
Jun 17, 2024
Merged

Conversation

king-p3nguin
Copy link
Contributor

@king-p3nguin king-p3nguin commented Jun 8, 2024

Issue #, if available:
#195

Description of changes:

  • qml.counts() is now supported.

Testing done:

Example:

import pennylane as qml
dev = qml.device("braket.local.qubit", wires=2, shots=100)

@qml.qnode(dev)
def test_circ():
    qml.PauliX(0)
    return qml.counts()

test_circ()

output:

tensor(Counter({'10': 100}), dtype=object, requires_grad=True)

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.

@rmshaffer rmshaffer linked an issue Jun 10, 2024 that may be closed by this pull request
@yitchen-tim
Copy link
Contributor

yitchen-tim commented Jun 11, 2024

Hi @king-p3nguin, thanks for starting your contribution! Could you also add unit tests to this PR?

@king-p3nguin king-p3nguin marked this pull request as ready for review June 12, 2024 08:51
@king-p3nguin king-p3nguin requested a review from a team as a code owner June 12, 2024 08:51
Copy link

codecov bot commented Jun 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (7b9f700) to head (3df7f89).
Report is 19 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #267   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines         1211      1229   +18     
  Branches       295       304    +9     
=========================================
+ Hits          1211      1229   +18     

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

@yitchen-tim
Copy link
Contributor

yitchen-tim commented Jun 12, 2024

Hi @king-p3nguin, the basic use case from the issue throws error

import pennylane as qml
dev = qml.device("braket.local.qubit", wires=2, shots=100)

@qml.qnode(dev)
def test_circ():
    qml.PauliZ(0)
    return qml.counts()

test_circ()

with error message DeviceError: Unsupported observable: <class 'NoneType'>

And another use case

@qml.qnode(dev)
def circuit():
    qml.X(wires=0)
    return qml.counts(op=qml.PauliZ(wires=[0]))

throws a different error NotImplementedError: Unsupported return type: ObservableReturnTypes.Counts

@yitchen-tim
Copy link
Contributor

Also, could you add more tests to increase the coverage? thanks

@king-p3nguin
Copy link
Contributor Author

king-p3nguin commented Jun 12, 2024

@yitchen-tim Thank you for checking. The two basic use cases did not show errors in my environment. Can you ensure the code is executed within my fork's counts branch? (The added integrated tests have a similar circuit, and those tests are working fine on CI)

(As for unit testing, please give me some time as it seems to be quite complicated to create a mock)

@yitchen-tim
Copy link
Contributor

yitchen-tim commented Jun 12, 2024

@king-p3nguin Great, thanks, I did have some dependencies issues so it was not working correctly. The basic examples works for me now. After full test coverage, we should be good to go!

Also, per UnitaryHack guideline, it's good as long as PR is opened before EOD today and merged before 6/19. So we still have a week.

return Sample(braket_observable, targets)
else:
raise NotImplementedError(f"Unsupported return type: {return_type}")
raise NotImplementedError(f"Unsupported return type: {return_type}") # pragma: no cover
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add test case for unsupported return type rather than # pragma: no cover?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that when an observable is defined, there are only four possible types (Expectation, Variance, Sample, and Counts), so I will just use the else statement here.

Copy link
Contributor

@yitchen-tim yitchen-tim Jun 16, 2024

Choose a reason for hiding this comment

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

The else statement is prone to error. Pennylane supports few other return types, and the else statement doesn't really handle all these other result types.

To add coverage and test the unsupported return type for these lines

    else:
        raise NotImplementedError(f"Unsupported return type: {return_type}")

, you could mock the measurement as something like

measurement = Mock(
    return_type = Enum('ObservableReturnTypes', {'Foo': "foo"}).Foo,
    observable = qml.PauliZ()
)

If any function that is imcompatible with the mocked measurement, you can adjust the mock or patch the imcompatible function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion! I made the change as requested.

@king-p3nguin
Copy link
Contributor Author

@yitchen-tim
Sorry for the delay. I added unit tests.

@yitchen-tim
Copy link
Contributor

@king-p3nguin thanks, coverage is 100%, woot! Just one more change around testing unsupported result types.

Copy link
Contributor

@yitchen-tim yitchen-tim left a comment

Choose a reason for hiding this comment

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

Great work! Thanks again for the contribution @king-p3nguin

@yitchen-tim yitchen-tim requested a review from rmshaffer June 17, 2024 00:42
@rmshaffer rmshaffer merged commit d7c232f into amazon-braket:main Jun 17, 2024
15 checks passed
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 qml.counts for circuits
3 participants