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

Detecting mismatching operations with Catalyst

Đang mở
#257 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
47
Fork
41
Merge trung bình
4 ngày 1 giờ
Pull request đã merge (30 ngày)
2

Mô tả

**Describe the bug**
Hi,

I am working on [Catalyst](https://github.com/PennyLaneAI/catalyst/) which is JIT compiler for PennyLane. [We have an integration with Amazon Braket](https://pennylane.ai/blog/2023/07/catalyst-release-0.2/#amazon-braket-integration) that allows users to write quantum circuits expressed as PennyLane's tapes and generate OpenQASM which is then sent to the Amazon Braket for execution.

Catalyst differs a little bit from PennyLane. In particular, Catalyst uses a TOML file to specify which gates and observables are available on the target device. For example, [this is a TOML file we use for testing a test device.](https://github.com/PennyLaneAI/catalyst/blob/main/runtime/lib/backend/dummy/dummy_device.toml). PennyLane, on the other hand uses the `operations` and `observables` property on the instance of the device being executed. This sometimes leads to a conflict that may result in errors.

In particular `amazon-braket-pennylane-plugin`s 1.27.1 release contained a [modification to `BraketQubitDevice`'s observables property](https://github.com/amazon-braket/amazon-braket-pennylane-plugin-python/commit/2aec11c2d67a828d60c8055f1676ef9495ccd76c) (patch inlined below):

```patch
diff --git a/src/braket/pennylane_plugin/braket_device.py b/src/braket/pennylane_plugin/braket_device.py
index a45de88..f3866c3 100644
--- a/src/braket/pennylane_plugin/braket_device.py
+++ b/src/braket/pennylane_plugin/braket_device.py
@@ -162,7 +162,7 @@ class BraketQubitDevice(QubitDevice):

@property
def observables(self) -> frozenset[str]:
- base_observables = frozenset(super().observables)
+ base_observables = frozenset(super().observables - {"SProd", "Sum"})
# This needs to be here bc expectation(ax+by)== a*expectation(x)+b*expectation(y)
# is only true when shots=0
if not self.shots:

```

The commit removes `SProd` and `Sum` observables from the list of supported observables by the device. However, [`SProd` ](https://github.com/PennyLaneAI/catalyst/blob/main/runtime/lib/backend/openqasm/braket_local_qubit.toml#L62)and [`Sum`](https://github.com/PennyLaneAI/catalyst/blob/main/runtime/lib/backend/openqasm/braket_local_qubit.toml#L64) were not removed Catalyst's TOML file which corresponds to the local braket simulator. And this lead to errors.

The error went away with the release of `amazon-braket-pennylane-plugin` version 1.27.2 which added back the `Sum` and `SProd` observables to the list of supported observables. However, it would be nice if we could guarantee that `amazon-braket-pennylane-plugin`'s list of supported observables and operations doesn't go out of sync with Catalyst's.

**To reproduce**
pip install pennylane-catalyst amazon-braket-pennylane-plugin==1.27.1

```python
import pennylane as qml

@qml.qjit
@qml.qnode(qml.device("braket.local.qubit", wires=2))
def all_measurements(x):
qml.RY(x, wires=0)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.var(qml.PauliZ(1) @ qml.PauliZ(0)),
)

all_measurements(3.14)
```

will produce the following error:

```
catalyst.utils.exceptions.CompileError: Observables in qml.device.observables and specification file do not match.
Observables that present only in spec: {'Sum', 'SProd'}
```

**Expected behavior**
It should not throw the `CompileError` and return instead the following values:

```
(array(-0.99999873), array(2.53654331e-06))
```

**System information**
A description of your system. Please provide:
- **Amazon Braket Python PennyLane Plugin version**: 1.27.1
- **Amazon Braket Python SDK version**: amazon-braket-sdk==1.78.0
- **Amazon Braket Python Schemas version**: amazon-braket-sdk==1.78.0
- **Amazon Braket Python Default Simulator version**:
- **Python version**: 3.10

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.