def __init__(
        self,
        observable: Union['cirq.BaseDensePauliString', Iterable['cirq.Pauli']],
        key: Union[str, 'cirq.MeasurementKey'] = '',
    ) -> None:
        """Inits PauliMeasurementGate.

        Args:
            observable: Pauli observable to measure. Any `Iterable[cirq.Pauli]`
                is a valid Pauli observable (with a +1 coefficient by default).
                If you wish to measure pauli observables with coefficient -1,
                then pass a `cirq.DensePauliString` as observable.
            key: The string key of the measurement.

        Raises:
            ValueError: If the observable is empty.
        """
        if not observable:
            raise ValueError(f'Pauli observable {observable} is empty.')
        if not all(
                isinstance(p, pauli_gates.Pauli)
                for p in cast(Iterable['cirq.Gate'], observable)):
            raise ValueError(
                f'Pauli observable {observable} must be Iterable[`cirq.Pauli`].'
            )
        coefficient = (observable.coefficient if isinstance(
            observable, dps.BaseDensePauliString) else 1)
        if coefficient not in [+1, -1]:
            raise ValueError(
                f'`cirq.DensePauliString` observable {observable} must have coefficient +1/-1.'
            )

        self._observable = dps.DensePauliString(observable,
                                                coefficient=coefficient)
        self.key = key  # type: ignore
 def with_observable(
     self, observable: Union['cirq.BaseDensePauliString',
                             Iterable['cirq.Pauli']]
 ) -> 'PauliMeasurementGate':
     """Creates a pauli measurement gate with the new observable and same key."""
     if (observable if isinstance(observable, dps.BaseDensePauliString) else
             dps.DensePauliString(observable)) == self._observable:
         return self
     return PauliMeasurementGate(observable, key=self.key)
Esempio n. 3
0
 def observable(self) -> 'cirq.DensePauliString':
     """Pauli observable which should be measured by the gate."""
     return dense_pauli_string.DensePauliString(self._observable)
Esempio n. 4
0
    def dense_pauli_string(self, pauli: Pauli) -> 'cirq.DensePauliString':
        from cirq.ops import dense_pauli_string

        pauli_tuple = self.pauli_tuple(pauli)
        coefficient = -1 if pauli_tuple[1] else 1
        return dense_pauli_string.DensePauliString(str(pauli_tuple[0]), coefficient=coefficient)