-
Notifications
You must be signed in to change notification settings - Fork 37
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 for SProd
, Prod
, and Sum
#253
Comments
Hi @astralcai, thank you for raising this. I shall start looking into a fix. |
The current @_translate_observable.register
def _(t: qml.operation.Tensor):
return reduce(lambda x, y: x @ y, [_translate_observable(factor) for factor in t.obs])
@_translate_observable.register
def _(t: qml.ops.Prod):
return reduce(lambda x, y: x @ y, [_translate_observable(factor) for factor in t.operands])
@_translate_observable.register
def _(t: qml.ops.SProd):
return t.scalar * _translate_observable(t.base) Shouldn't that take care of the nesting problem? |
measurements = []
for mp in circuit.measurements:
obs = mp.obs
if isinstance(obs, (Hamiltonian, LinearCombination, Sum, SProd, Prod)):
obs = obs.simplify()
mp = type(mp)(obs)
measurements.append(mp) I'm noticing that |
It should, but as I recall it didn't. I was looking into it some time ago and couldn't make it work, that's why I suggested using the same approach for all potential multi-term observables. You can give it a try. I don't remember what the issue was exactly, but I believe it has something to do with the braket backend unable to parse scalar products.
This occured when trying to parse the scalar product of an observable. See this run: https://github.com/PennyLaneAI/plugin-test-matrix/actions/runs/9018042395/job/24777766316 |
Simplify does not preserve the original order of operands. |
Sorry for the delay; I finally managed to return to this, and I think I've found the actual issues. Looking at the device test run in #264, we observe two types of failures:
This is due to attempting to run Braket
This is because we pass in the amazon-braket-pennylane-plugin-python/src/braket/pennylane_plugin/braket_device.py Lines 257 to 260 in 17dae39
This is fixed by mapping the wires of the |
Fixed in #275 |
Sum
Sum
should be handled the same way asHamiltonian
andLinearCombination
, which was partially addressed in #252, but the same treatment should be applied totranslate_result_type
andtranslate_result
intranslation.py
as well.Note:
Sum.ops
is deprecated, so instead ofmeasurement.obs.ops
, do_, ops = measurement.obs.terms()
, and then useops
.SProd and Prod
Since
SProd
andProd
could be nested, they are not guaranteed to be single-term observables. For example, anSProd
could be0.1 * (qml.Z(0) + qml.X(1))
, in which case it's actually aSum
. Similarly, aProd
could beqml.Z(0) @ (qml.X(0) + qml.Y(1))
.This means that the same treatment for
Hamiltonian
,LinearCombination
andSum
should extend toSProd
andProd
as well, including_translate_observable
, which should registerSum
,SProd
andProd
all under the same dispatch function asHamiltonian
, which usesH.terms()
.Caveat:
Prod.terms()
will resolve to itself if theProd
only contains one term. For example:This may result in infinite recursion in
_translate_observable
, so a base case should be added to returnreduce(lambda x, y: x @ y, [_translate_observable(factor) for factor in H.operands])
ifH
is aProd
with a single term.Note: The
terms()
function will unwrap any nested structures but also simplify the observable. For example:This will create a mismatch between the number of targets in the translated observable and the original observable. We do plan on addressing this issue in PennyLane and have
terms()
recursively unwraps the observable without doing any simplification, but for now, in_pl_to_braket_circuit
, do not usecircuit.measurements
directly, instead do something likeThen use
measurements
instead ofcircuit.measurements
from this point on. The list of simplified measurements should also be passed into_apply_gradient_result_type
and used there.Device
Now since
SProd
,Prod
, andSum
all could be nested, multi-term observables, they should be removed from the list of supported observables and added back if no shots are present:The text was updated successfully, but these errors were encountered: