amazon-braket / amazon-braket/amazon-braket-sdk-python

ProgramSet MeasuredEntry.expectation() returns None when no observable is attached (unimplemented)

Open
#1,316 1 comment 0 reactions 1 assignee Claimed by @speller26 View on GitHub
Dominant language
Python
Stars
373
Forks
195
Avg merge
4d 15h
Merged PRs (30d)
8

Description

### Summary
`MeasuredEntry.expectation()` on a `ProgramSetQuantumTaskResult` returns `None` (and warns `"No observable was measured"`) whenever the entry has no attached observable, even though the entry contains the `measurements` needed to compute the expectation. The code path is explicitly unimplemented.

### Location
`src/braket/tasks/program_set_quantum_task_result.py`, `MeasuredEntry` (verified present on `main` as of 2026-07):
```python
self._expectation = (
expectation_from_measurements(counts, measured_qubits, self.observable, self.observable.targets)
if self.observable
else None
)

def expectation(self) -> float | None:
# TODO: Use program set payload to calculate expectation
if self._expectation is None:
warnings.warn("No observable was measured", stacklevel=1)
return self._expectation
```

### Impact
Downstream consumers that request an expectation from a program-set result receive `None`/no usable value and can silently produce wrong results, despite the measurements being present and correct. We hit this via the PennyLane–Braket plugin, where `qml.expval` returns values off by up to ~1.5 for a program-set result on a QPU (filing a companion issue on that repo, will cross-link). Because the failure is silent (only a `UserWarning`) and the measurements look fine, the corruption is easy to miss, in our case it manufactured a spurious ~0.2 R² effect that looked like a hardware-fidelity difference.

### Suggested fix
Implement the `# TODO`: when `self.observable` is absent but the requested observable is a computational-basis/diagonal observable (or is recoverable from the program-set payload), compute the expectation from `measurements` instead of returning `None`.

### Environment
Reproduced on `amazon-braket-sdk==1.113.1`; the unimplemented path is still present on `main` (1.121.0). Device: Rigetti Cepheus-1-108Q. The measurement bitstrings on the entry are correct, only the computed expectation is missing.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.