Exemple #1
0
    def __init__(
        self,
        circuit: cirq.AbstractCircuit,
        measurement: BitstringsMeasurement,
        params: Union[Sequence[TParamPair], cirq.ParamResolverOrSimilarType] = None,
        spec: Optional[ExecutableSpec] = None,
        problem_topology: Optional[cirq.NamedTopology] = None,
        initial_state: Optional[cirq.ProductState] = None,
    ):
        """Initialize the quantum executable.

        The actual fields in this class are immutable, but we allow more liberal input types
        which will be frozen in this __init__ method.

        Args:
            circuit: The circuit. This will be frozen before being set as an attribute.
            measurement: A description of the measurement properties or process.
            params: A cirq.ParamResolverOrSimilarType which will be frozen into a tuple of
                key value pairs.
            spec: Specification metadata about this executable that is not used by the quantum
                runtime, but is persisted in result objects to associate executables with results.
            problem_topology: Description of the multiqubit gate topology present in the circuit.
                If not specified, the circuit must be compatible with the device topology.
            initial_state: How to initialize the quantum system before running `circuit`. If not
                specified, the device will be initialized into the all-zeros state.
        """

        # We care a lot about mutability in this class. No object is truly immutable in Python,
        # but we can get pretty close by following the example of dataclass(frozen=True), which
        # deletes this class's __setattr__ magic method. To set values ever, we use
        # object.__setattr__ in this __init__ function.
        #
        # We write our own __init__ function to be able to accept a wider range of input formats
        # that can be easily converted to our native, immutable format.
        object.__setattr__(self, 'circuit', circuit.freeze())
        object.__setattr__(self, 'measurement', measurement)

        if isinstance(params, tuple) and all(
            isinstance(param_kv, tuple) and len(param_kv) == 2 for param_kv in params
        ):
            frozen_params = params
        elif isinstance(params, Sequence) and all(
            isinstance(param_kv, Sequence) and len(param_kv) == 2 for param_kv in params
        ):
            frozen_params = tuple((k, v) for k, v in params)
        elif study.resolver._is_param_resolver_or_similar_type(params):
            param_resolver = cirq.ParamResolver(cast(cirq.ParamResolverOrSimilarType, params))
            frozen_params = tuple(param_resolver.param_dict.items())
        else:
            raise ValueError(f"`params` should be a ParamResolverOrSimilarType, not {params}.")
        object.__setattr__(self, 'params', frozen_params)

        object.__setattr__(self, 'spec', spec)
        object.__setattr__(self, 'problem_topology', problem_topology)
        object.__setattr__(self, 'initial_state', initial_state)

        # Hash may be expensive to compute, especially for large circuits.
        # This should be safe since this class should be immutable. This line will
        # also check for hashibility of members at construction time.
        object.__setattr__(self, '_hash', hash(dataclasses.astuple(self)))
 def func(
     circuit: cirq.AbstractCircuit,
     *,
     context: Optional[cirq.TransformerContext] = cirq.TransformerContext(),
     atol: float = 1e-4,
     custom_arg: CustomArg = CustomArg(),
 ) -> cirq.FrozenCircuit:
     my_mock(circuit, context, atol, custom_arg)
     return circuit.freeze()