amazon-braket-sdk-python icon indicating copy to clipboard operation
amazon-braket-sdk-python copied to clipboard

Bug: Chained Observables should not be allowed

Open mbeach-aws opened this issue 1 year ago • 1 comments

Describe the bug Observables should not be chained together.

In the Amazon Braket SDK, Observable.I() and Observable.X() are used to create identity and Pauli-X observables, respectively. However, these are not methods that are typically chained together. Each of these calls is intended to create a separate observable object, and chaining them like Observable.I().X() suggests applying one after the other, which doesn't align with how observables are used in the SDK.

To reproduce The following code snippet should not be valid.

from braket.circuits import Observable

Observable.I().X()

mbeach-aws avatar Mar 01 '24 15:03 mbeach-aws

This is a side effect of each observable being registered to Observable: https://github.com/amazon-braket/amazon-braket-sdk-python/blob/0370766feca0be3a5a3128b44f65cf5132ca1099/src/braket/circuits/observables.py#L73

Since they're registered as class methods, they can also be called on any instance of Observable; unfortunately, I don't think there's a simple way to restrict instances from calling the class method. Ideally, we would get rid of the registrations altogether, but that would be a pretty far-reaching breaking change.

speller26 avatar Mar 01 '24 22:03 speller26