From ee86245f5783ad8f9066e50498f2fcacf9d9d844 Mon Sep 17 00:00:00 2001 From: Tanuj Khattar Date: Sat, 25 Jun 2022 21:20:44 -0700 Subject: [PATCH] Raise value error if pauli string passed to cirq.measure_single_paulistring does not have a coefficient of +1 (#5623) Fixes https://github.com/quantumlib/Cirq/issues/5615 --- cirq/ops/measure_util.py | 6 +++++- cirq/ops/measure_util_test.py | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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