Skip to content

PauliMeasurementGate ignores the sign of the observable given to it #4814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Strilanc opened this issue Jan 8, 2022 · 1 comment · Fixed by #4836
Closed

PauliMeasurementGate ignores the sign of the observable given to it #4814

Strilanc opened this issue Jan 8, 2022 · 1 comment · Fixed by #4836
Assignees
Labels
area/measurements kind/bug-report Something doesn't seem to work. priority/p1 Fix is needed as soon as possible. Should be staffed. It is blocking some major flows for users triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@Strilanc
Copy link
Contributor

Strilanc commented Jan 8, 2022

The following code should success but it fails:

import cirq
obs = cirq.DensePauliString("Z", coefficient=-1)
c = cirq.Circuit(
    cirq.PauliMeasurementGate(obs, key='out').on(cirq.LineQubit(0)),
)
assert cirq.Simulator().sample(c)['out'][0] == 1

The issue is that the line self._observable = tuple(observable) in the init method is ignoring the coefficient. The coefficient has to be +1 or -1 to be valid.

This is a very bad bug if you are e.g. multiplying overlapping observables together to get composite measurements, as the result of doing so can be negative. For example XX * ZZ = -YY.

@Strilanc Strilanc added the kind/bug-report Something doesn't seem to work. label Jan 8, 2022
@tanujkhattar
Copy link
Collaborator

tanujkhattar commented Jan 9, 2022

Similar bugs at other places due to constructor accepting Iterable[Pauli] and ignoring the sign.

Another bug due to a similar issue, where the constructor expects iterable of a pauli and hence the sign gets ignored.

>>> cirq.DensePauliString(-1j *  cirq.Z) == cirq.DensePauliString('Z', coefficient=-1j)
False

Similarly, passing a dense pauli string to itself also resets the coefficient. i.e.

>>> obs = cirq.DensePauliString('Z', coefficient = -1j)
>>> cirq.DensePauliString(obs) == obs
False

Solution for this bug:

My proposal to fix this bug would be:

  • Expect a Union['cirq.BaseDensePauliString', Iterable['cirq.Pauli']] instead of Iterable['cirq.Pauli'] as the observable and make self_observable by default a dense pauli string so we retain the coefficient information (also assert that coefficient is +1/-1).
  • Update _decompose_ to include a invert_mask=True in the decomposed measurement gate if coefficient is -1.

Note that this would be a breaking change because

  • Json serialization would be updated (serialized observable would now include the sign).
  • dense pauli strings with coefficient != +1/-1 are currently accepted (coefficient is ignored) but would raise an error after the change.

If we wish to avoid a breaking change, then we'd have to include an additional coefficient parameter to the constructor, but then it allows us to do ugly things like cirq.PauliMeasurementGate(cirq.DensePauliString('Z', coefficient=-1), coefficient = -1) -- should this measure Z or -Z ?

LMK your thoughts. I'll also bring this up in the upcoming cirq sync and will send a PR for the agreed upon version.

@tanujkhattar tanujkhattar added area/measurements priority/p1 Fix is needed as soon as possible. Should be staffed. It is blocking some major flows for users labels Jan 9, 2022
@tanujkhattar tanujkhattar self-assigned this Jan 9, 2022
@tanujkhattar tanujkhattar added the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label Jan 9, 2022
@MichaelBroughton MichaelBroughton added triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Jan 12, 2022
CirqBot pushed a commit that referenced this issue Jan 14, 2022
)

Fixes #4814 

Note that this is a breaking change because:
- Serialization of the `PauliMeasurementGate` is now different -- the serialized observable is `DensePauliString` instead of a tuple of Pauli's.
-  A DensePauliString with coefficient != +1/-1 will now raise a `ValueError` whereas earlier the coefficient was simply ignored.
MichaelBroughton pushed a commit to MichaelBroughton/Cirq that referenced this issue Jan 22, 2022
…antumlib#4836)

Fixes quantumlib#4814 

Note that this is a breaking change because:
- Serialization of the `PauliMeasurementGate` is now different -- the serialized observable is `DensePauliString` instead of a tuple of Pauli's.
-  A DensePauliString with coefficient != +1/-1 will now raise a `ValueError` whereas earlier the coefficient was simply ignored.
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…antumlib#4836)

Fixes quantumlib#4814 

Note that this is a breaking change because:
- Serialization of the `PauliMeasurementGate` is now different -- the serialized observable is `DensePauliString` instead of a tuple of Pauli's.
-  A DensePauliString with coefficient != +1/-1 will now raise a `ValueError` whereas earlier the coefficient was simply ignored.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
…antumlib#4836)

Fixes quantumlib#4814 

Note that this is a breaking change because:
- Serialization of the `PauliMeasurementGate` is now different -- the serialized observable is `DensePauliString` instead of a tuple of Pauli's.
-  A DensePauliString with coefficient != +1/-1 will now raise a `ValueError` whereas earlier the coefficient was simply ignored.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/measurements kind/bug-report Something doesn't seem to work. priority/p1 Fix is needed as soon as possible. Should be staffed. It is blocking some major flows for users triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants