コード例 #1
0
ファイル: measure_util.py プロジェクト: MichaelBroughton/Cirq
def measure_single_paulistring(
    pauli_observable: pauli_string.PauliString,
    key: Optional[Union[str, 'cirq.MeasurementKey']] = None,
) -> raw_types.Operation:
    """Returns a single PauliMeasurementGate which measures the pauli observable

    Args:
        pauli_observable: The `cirq.PauliString` observable to measure.
        key: Optional `str` or `cirq.MeasurementKey` that gate should use.
            If none provided, it defaults to a comma-separated list of
            `str(qubit)` for each of the target qubits.

    Returns:
        An operation measuring the pauli observable.

    Raises:
        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())
コード例 #2
0
def measure_paulistring_terms(
    pauli_basis: pauli_string.PauliString,
    key_func: Callable[[raw_types.Qid],
                       str] = str) -> List[raw_types.Operation]:
    """Returns a list of operations individually measuring qubits in the pauli basis.

    Args:
        pauli_basis: The `cirq.PauliString` basis in which each qubit should
            be measured.
        key_func: Determines the key of the measurements of each qubit. Takes
            the qubit and returns the key for that qubit. Defaults to str.

    Returns:
        A list of operations individually measuring the given qubits in the
        specified pauli basis.

    Raises:
        ValueError: if `pauli_basis` is not an instance of `cirq.PauliString`.
    """
    if not isinstance(pauli_basis, pauli_string.PauliString):
        raise ValueError(
            f'Pauli observable {pauli_basis} should be an instance of cirq.PauliString.'
        )
    return [
        PauliMeasurementGate([pauli_basis[q]], key=key_func(q)).on(q)
        for q in pauli_basis
    ]