diff --git a/cirq/ops/measure_util.py b/cirq/ops/measure_util.py index 82713ef80c5..f9521efb98d 100644 --- a/cirq/ops/measure_util.py +++ b/cirq/ops/measure_util.py @@ -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()) diff --git a/cirq/ops/measure_util_test.py b/cirq/ops/measure_util_test.py index ef8084ea9b7..8967c1ac736 100644 --- a/cirq/ops/measure_util_test.py +++ b/cirq/ops/measure_util_test.py @@ -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