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

braket.aws.qubit returns incorrect expval from a ProgramSetQuantumTaskResult (measurements are correct)

Abierto
#333 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Python
Estrellas
47
Forks
41
Merge medio
4 d 1 h
PR fusionados (30 d)
2

Descripción

### Summary
When a `qml.expval` QNode runs on `braket.aws.qubit` against a backend that returns a **`ProgramSetQuantumTaskResult`** (observed on Rigetti Cepheus-1-108Q, where the plugin submits a parametric program set), the returned expectation values are wrong. The underlying measurement bitstrings are **correct**; only the plugin's computed expectation is wrong, and a `UserWarning: No observable was measured` is emitted.

### Steps to reproduce
```python
import numpy as np, pennylane as qml
ARN = "arn:aws:braket:us-west-1::device/qpu/rigetti/Cepheus-1-108Q"
dev = qml.device("braket.aws.qubit", device_arn=ARN, wires=3, shots=200)

@qml.qnode(dev)
def circuit(t):
for i in range(3):
qml.RY(t[i], wires=i)
return [qml.expval(qml.PauliZ(i)) for i in range(3)]

print(circuit(np.array([0.4, 1.2, 2.0])))
```
A minimal parametric circuit is enough to trigger the program set on a QPU with parametric compilation. **SV1 does not reproduce** (it returns a `GateModelQuantumTaskResult` with correct expvals), so a QPU is required.

### Observed (Cepheus, 3 qubits, 200 shots, `t = [0.4, 1.2, 2.0]`)
| | wire 0 | wire 1 | wire 2 |
|---|---:|---:|---:|
| **plugin `expval`** | 0.77 | **0.77** | **0.77** |
| **correct** (sample mean `1 − 2·mean(bit)` from the same result's measurements) | 0.77 | 0.41 | −0.25 |

The plugin returns **the first observable's value for all observables** three different angles all report 0.77, though the true per-qubit expectations differ. On a 7-qubit workload the divergence reaches ~1.5.

### Expected
For diagonal Pauli-Z observables, `expval` should equal the sample mean `1 − 2·mean(bit)` per qubit, recoverable directly from `result.entries[0].entries[0].measurements`.

### Root cause
For program-set-capable devices the plugin submits **without attaching observables**, in `src/braket/pennylane_plugin/braket_device.py` (present on current `main`):
```python
add_observables=not self._supports_program_sets,
```
So the program-set entries come back with no observable attached, and `amazon-braket-sdk`'s `MeasuredEntry.expectation()` then returns `None` (unimplemented `# TODO`, see amazon-braket/amazon-braket-sdk-python#1316); the plugin produces an incorrect expectation instead of computing the sample mean from `measurements`.

### Workaround
```python
res = dev._task.result() # or recover the task; ProgramSetQuantumTaskResult
meas = np.asarray(res.entries[0].entries[0].measurements) # shots x wires
z = 1.0 - 2.0 * meas.mean(axis=0) # correct diagonal expectations
```

### Suggested fix
When a program-set entry lacks an attached observable, compute the requested expectation from the entry's `measurements` rather than trusting `MeasuredEntry.expectation()`.

### Environment
- `pennylane==0.44.1`
- `amazon-braket-pennylane-plugin==1.34.0` — **reproduced on this version.** Not re-run on 1.34.2, but the responsible code path (`add_observables=not self._supports_program_sets`) is present on current `main`, and the 1.34.1 / 1.34.2 changelogs show no expectation-handling fix.
- `amazon-braket-sdk==1.113.1` — the SDK `# TODO` is also still present on `main` (1.121.0).
- Device: Rigetti Cepheus-1-108Q.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.