-
Notifications
You must be signed in to change notification settings - Fork 38
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.sample()
without specifying the observable
#266
Open
king-p3nguin
wants to merge
5
commits into
amazon-braket:main
Choose a base branch
from
king-p3nguin:shot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import pennylane as qml | ||
from braket.aws import AwsDevice | ||
from braket.circuits import FreeParameter, Gate, ResultType, gates, noises, observables | ||
from braket.circuits.observables import Observable as BraketObservable | ||
from braket.circuits.result_types import ( | ||
AdjointGradient, | ||
DensityMatrix, | ||
|
@@ -530,7 +531,7 @@ | |
return AdjointGradient(observable=braket_observable, target=targets, parameters=parameters) | ||
|
||
|
||
def translate_result_type( | ||
def translate_result_type( # noqa: C901 | ||
measurement: MeasurementProcess, targets: list[int], supported_result_types: frozenset[str] | ||
) -> Union[ResultType, tuple[ResultType, ...]]: | ||
"""Translates a PennyLane ``MeasurementProcess`` into the corresponding Braket ``ResultType``. | ||
|
@@ -547,6 +548,7 @@ | |
then this will return a result type for each term. | ||
""" | ||
return_type = measurement.return_type | ||
observable = measurement.obs | ||
|
||
if return_type is ObservableReturnTypes.Probability: | ||
return Probability(targets) | ||
|
@@ -558,14 +560,21 @@ | |
return DensityMatrix(targets) | ||
raise NotImplementedError(f"Unsupported return type: {return_type}") | ||
|
||
if isinstance(measurement.obs, (Hamiltonian, qml.Hamiltonian)): | ||
if isinstance(observable, (Hamiltonian, qml.Hamiltonian)): | ||
if return_type is ObservableReturnTypes.Expectation: | ||
return tuple( | ||
Expectation(_translate_observable(term), term.wires) for term in measurement.obs.ops | ||
Expectation(_translate_observable(term), term.wires) for term in observable.ops | ||
) | ||
raise NotImplementedError(f"Return type {return_type} unsupported for Hamiltonian") | ||
|
||
braket_observable = _translate_observable(measurement.obs) | ||
if return_type is ObservableReturnTypes.Sample and observable is None: | ||
if isinstance(measurement, qml.measurements.SampleMeasurement): | ||
return tuple( | ||
Sample(BraketObservable.Z(), target) for target in targets or measurement.wires | ||
) | ||
raise NotImplementedError(f"Unsupported measurement type: {type(measurement)}") | ||
|
||
braket_observable = _translate_observable(observable) | ||
if return_type is ObservableReturnTypes.Expectation: | ||
return Expectation(braket_observable, targets) | ||
elif return_type is ObservableReturnTypes.Variance: | ||
|
@@ -698,6 +707,14 @@ | |
ag_result.value["gradient"][f"p_{i}"] | ||
for i in sorted(key_indices) | ||
] | ||
|
||
if measurement.return_type is ObservableReturnTypes.Sample and observable is None: | ||
if isinstance(measurement, qml.measurements.SampleMeasurement): | ||
if targets: | ||
return [m[targets] for m in braket_result.measurements] | ||
return braket_result.measurements | ||
raise NotImplementedError(f"Unsupported measurement type: {type(measurement)}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same question here, would it be possible to embed this logic inside |
||
|
||
translated = translate_result_type(measurement, targets, supported_result_types) | ||
if isinstance(observable, (Hamiltonian, qml.Hamiltonian)): | ||
coeffs, _ = observable.terms() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of embedding this logic here, would it be possible to have
_translate_observable
return both an observable and targets, and have it fall back to these defaults ifobservable
is none and/ortargets
is none? Might be slightly cleaner (and then maybe wouldn't require theC901
suppression above either, since the logic would be outside this function).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about that. The target cannot be specified if an observable is specified, so the branching will be based on whether the observable is None. Therefore, I believe that the overall logic will remain the same, and it will only be divided into more functions.