amazon-braket / amazon-braket-pennylane-plugin-python

A plugin for allowing Xanadu PennyLane to use Amazon Braket devices
https://amazon-braket-pennylane-plugin-python.readthedocs.io/
Apache License 2.0
43 stars 38 forks source link

Support `LinearCombination` as an observable #245

Closed astralcai closed 6 months ago

astralcai commented 6 months ago

Describe the feature you'd like In v0.36.0 of PennyLane, the new LinearCombination is used in favour of the legacy Hamiltonian, which should also be supported as an observable.

Additional context There are two classes now to represent a Hamiltonian: qml.ops.Hamiltonian, and qml.ops.LinearCombination. The latter is now used by default in favour of the former. With new opmath enabled, qml.Hamiltonian will refer to LinearCombination, and with new opmath disabled, qml.Hamiltonian will refer to the old Hamiltonian class.

Solution

  1. In braket_device.py, line 169 should be updated to return base_observables.union({"Hamiltonian", "LinearCombination"})
  2. Update all isinstance(..., Hamiltonian) checks to isinstance(..., (qml.ops.Hamiltonian, qml.ops.LinearCombination)), such as line 230 of braket_device.py, line 561 and 691 of translation.py
  3. Update line 584-585 of translation.py to:
    @_translate_observable.register(qml.ops.Hamiltonian)
    @_translate_observable.register(qml.ops.LinearCombination)
    def _(H: Union[qml.ops.Hamiltonian, qml.ops.LinearCombination]):
    ...
virajvchaudhari commented 6 months ago

Thanks for pointing this out @astralcai, we will take a look into this.