Exemple #1
0
    def with_measurement_key_mapping(
            self, key_map: Dict[str, str]) -> 'cirq.CircuitOperation':
        """Returns a copy of this operation with an updated key mapping.

        Args:
            key_map: A mapping of old measurement keys to new measurement keys.
                This map will be composed with any existing key mapping.
                The keys and values of the map should be unindexed (i.e. without
                repetition_ids).

        Returns:
            A copy of this operation with measurement keys updated as specified
                by key_map.

        Raises:
            ValueError: The new operation has a different number of measurement
                keys than this operation.
        """
        new_map = {}
        for k_obj in protocols.measurement_keys_touched(self.circuit):
            k = k_obj.name
            k_new = self.measurement_key_map.get(k, k)
            k_new = key_map.get(k_new, k_new)
            if k_new != k:
                new_map[k] = k_new
        new_op = self.replace(measurement_key_map=new_map)
        if len(protocols.measurement_keys_touched(new_op)) != len(
                protocols.measurement_keys_touched(self)):
            raise ValueError(
                f'Collision in measurement key map composition. Original map:\n'
                f'{self.measurement_key_map}\nApplied changes: {key_map}')
        return new_op
def _op_info_with_fallback(
        op: 'cirq.Operation',
        args: 'cirq.CircuitDiagramInfoArgs') -> 'cirq.CircuitDiagramInfo':
    info = protocols.circuit_diagram_info(op, args, None)
    rows: List[LabelEntity] = list(op.qubits)
    if args.label_map is not None:
        rows += protocols.measurement_keys_touched(op) & args.label_map.keys()
    if info is not None:
        if max(1, len(rows)) != len(info.wire_symbols):
            raise ValueError(
                f'Wanted diagram info from {op!r} for {rows!r}) but got {info!r}'
            )
        return info

    # Use the untagged operation's __str__.
    name = str(op.untagged)

    # Representation usually looks like 'gate(qubit1, qubit2, etc)'.
    # Try to cut off the qubit part, since that would be redundant.
    redundant_tail = f"({', '.join(str(e) for e in op.qubits)})"
    if name.endswith(redundant_tail):
        name = name[:-len(redundant_tail)]

    # Add tags onto the representation, if they exist
    if op.tags:
        name += f'{list(op.tags)}'

    # Include ordering in the qubit labels.
    symbols = (name, ) + tuple(f'#{i + 1}' for i in range(1, len(op.qubits)))

    return protocols.CircuitDiagramInfo(wire_symbols=symbols)
Exemple #3
0
 def _can_be_in_run_prefix(self, val: Any):
     return not protocols.measurement_keys_touched(val)