Skip to content

Commit

Permalink
Raise value error if pauli string passed to cirq.measure_single_pauli…
Browse files Browse the repository at this point in the history
…string does not have a coefficient of +1 (quantumlib#5623)

Fixes quantumlib#5615
  • Loading branch information
tanujkhattar authored and rht committed May 1, 2023
1 parent e7e8ef5 commit 6252ea2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cirq-core/cirq/ops/measure_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ def measure_single_paulistring(
An operation measuring the pauli observable.
Raises:
ValueError: if the observable is not an instance of PauliString.
ValueError: if the observable is not an instance of PauliString or if the coefficient
is not +1.
"""
if not isinstance(pauli_observable, pauli_string.PauliString):
raise ValueError(
f'Pauli observable {pauli_observable} should be an instance of cirq.PauliString.'
)
if pauli_observable.coefficient != 1:
raise ValueError(f"Pauli observable {pauli_observable} must have a coefficient of +1.")

if key is None:
key = _default_measurement_key(pauli_observable)
return PauliMeasurementGate(pauli_observable.values(), key).on(*pauli_observable.keys())
Expand Down
4 changes: 4 additions & 0 deletions cirq-core/cirq/ops/measure_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def test_measure_single_paulistring():
with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'):
_ = cirq.measure_single_paulistring(q)

# Coefficient != +1
with pytest.raises(ValueError, match='must have a coefficient'):
_ = cirq.measure_single_paulistring(-ps)


def test_measure_paulistring_terms():
# Correct application
Expand Down

0 comments on commit 6252ea2

Please sign in to comment.