Exemple #1
0
def measure(
        *target: 'cirq.Qid',
        key: Union[str, value.MeasurementKey] = '',
        invert_mask: Tuple[bool, ...] = (),
) -> raw_types.Operation:
    """Returns a single MeasurementGate applied to all the given qubits.

    The qubits are measured in the computational basis.

    Args:
        *target: The qubits that the measurement gate should measure.
        key: The string key of the measurement. If this is empty, defaults
            to a comma-separated list of the target qubits' str values.
        invert_mask: A list of Truthy or Falsey values indicating whether
            the corresponding qubits should be flipped. None indicates no
            inverting should be done.

    Returns:
        An operation targeting the given qubits with a measurement.

    Raises:
        ValueError if the qubits are not instances of Qid.
    """
    for qubit in target:
        if isinstance(qubit, np.ndarray):
            raise ValueError(
                'measure() was called a numpy ndarray. Perhaps you meant '
                'to call measure_state_vector on numpy array?')
        elif not isinstance(qubit, raw_types.Qid):
            raise ValueError(
                'measure() was called with type different than Qid.')

    qid_shape = protocols.qid_shape(target)
    return MeasurementGate(len(target), key, invert_mask,
                           qid_shape).on(*target)
Exemple #2
0
def measure_each(
    *qubits,
    key_func: Callable[[raw_types.Qid],
                       str] = str) -> List[raw_types.Operation]:
    """Returns a list of operations individually measuring the given qubits.

    The qubits are measured in the computational basis.

    Args:
        *qubits: The qubits to measure.  These can be passed as separate
            function arguments or as a one-argument iterable of qubits.
        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.
    """
    one_iterable_arg: bool = (len(qubits) == 1
                              and isinstance(qubits[0], Iterable)
                              and not isinstance(qubits[0], (bytes, str)))
    qubitsequence = qubits[0] if one_iterable_arg else qubits
    return [
        MeasurementGate(1, key_func(q), qid_shape=(q.dimension, )).on(q)
        for q in qubitsequence
    ]
def measure(
    *target,
    key: Optional[Union[str, 'cirq.MeasurementKey']] = None,
    invert_mask: Tuple[bool, ...] = (),
    confusion_map: Optional[Dict[Tuple[int, ...], np.ndarray]] = None,
) -> raw_types.Operation:
    """Returns a single MeasurementGate applied to all the given qubits.

    The qubits are measured in the computational basis.

    Args:
        *target: The qubits that the measurement gate should measure.
            These can be specified as separate function arguments or
            with a single argument for an iterable of qubits.
        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.
        invert_mask: A list of Truthy or Falsey values indicating whether
            the corresponding qubits should be flipped. None indicates no
            inverting should be done.
        confusion_map: A map of qubit index sets (using indices in
            `target`) to the 2D confusion matrix for those qubits. Indices
            not included use the identity. Applied before invert_mask if both
            are provided.

    Returns:
        An operation targeting the given qubits with a measurement.

    Raises:
        ValueError: If the qubits are not instances of Qid.
    """
    one_iterable_arg: bool = (
        len(target) == 1
        and isinstance(target[0], Iterable)
        and not isinstance(target[0], (bytes, str, np.ndarray))
    )
    targets = tuple(target[0]) if one_iterable_arg else target
    for qubit in targets:
        if isinstance(qubit, np.ndarray):
            raise ValueError(
                'measure() was called a numpy ndarray. Perhaps you meant '
                'to call measure_state_vector on numpy array?'
            )
        elif not isinstance(qubit, raw_types.Qid):
            raise ValueError('measure() was called with type different than Qid.')

    if key is None:
        key = _default_measurement_key(targets)
    qid_shape = protocols.qid_shape(targets)
    return MeasurementGate(len(targets), key, invert_mask, qid_shape, confusion_map).on(*targets)
Exemple #4
0
def measure_each(
    *qubits: 'cirq.Qid', key_func: Callable[[raw_types.Qid], str] = str
) -> List[raw_types.Operation]:
    """Returns a list of operations individually measuring the given qubits.

    The qubits are measured in the computational basis.

    Args:
        *qubits: The qubits to measure.
        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.
    """
    return [MeasurementGate(1, key_func(q), qid_shape=(q.dimension,)).on(q) for q in qubits]
Exemple #5
0
def measure_each(
    *qubits: 'cirq.Qid',
    key_func: Callable[[raw_types.Qid], str] = lambda x: ''
) -> List[raw_types.Operation]:
    """Returns a list of operations individually measuring the given qubits.

    The qubits are measured in the computational basis.

    Args:
        *qubits: The qubits to measure.
        key_func: Determines the key of the measurements of each qubit. Takes
            the qubit and returns the key for that qubit. Defaults to empty string
            key which would result in a comma-separated list of the target qubits'
            str values.

    Returns:
        A list of operations individually measuring the given qubits.
    """
    return [
        MeasurementGate(1, key_func(q), qid_shape=(q.dimension, )).on(q)
        for q in qubits
    ]