<commit_before># Copyright 2019 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Callable, Dict, NamedTuple, Optional, Sequence

import sympy
from google.protobuf import json_format

from cirq import devices, ops
from cirq.api.google import v2
from cirq.google import arg_func_langs


class DeserializingArg(
        NamedTuple('DeserializingArg', [
            ('serialized_name', str),
            ('constructor_arg_name', str),
            ('value_func', Optional[Callable[[arg_func_langs.ArgValue], Any]]),
            ('required', bool),
        ])):
    """Specification of the arguments to deserialize an argument to a gate.

    Attributes:
        serialized_name: The serialized name of the gate that is being
            deserialized.
        constructor_arg_name: The name of the argument in the constructor of
            the gate corresponding to this serialized argument.
        value_func: Sometimes a value from the serialized proto needs to
            converted to an appropriate type or form. This function takes the
            serialized value and returns the appropriate type. Defaults to
            None.
        required: Whether a value must be specified when constructing the
            deserialized gate. Defaults to True.
    """

    def __new__(cls,
                serialized_name,
                constructor_arg_name,
                value_func=None,
                required=True):
        return super(DeserializingArg,
                     cls).__new__(cls, serialized_name, constructor_arg_name,
                                  value_func, required)


class GateOpDeserializer:
    """Describes how to deserialize a proto to a given Gate type.

    Attributes:
        serialized_gate_id: The id used when serializing the gate.
    """

    def __init__(self,
                 serialized_gate_id: str,
                 gate_constructor: type,
                 args: Sequence[DeserializingArg],
                 num_qubits_param: Optional[str] = None):
        """Constructs a deserializer.

        Args:
            serialized_gate_id: The serialized id of the gate that is being
                deserialized.
            gate_constructor: The constructor for the deserialized gate.
            args: A list of the arguments to be read from the serialized
                gate and the information required to use this to construct
                the gate using the gate_constructor above.
            num_qubits_param: Some gate constructors require that the number
                of qubits be passed to their constructor. This is the name
                of the parameter in the constructor for this value. If None,
                no number of qubits is passed to the constructor.
        """
        self.serialized_gate_id = serialized_gate_id
        self.gate_constructor = gate_constructor
        self.args = args
        self.num_qubits_param = num_qubits_param

    def from_proto_dict(self, proto: Dict) -> ops.GateOperation:
        """Turns a cirq.api.google.v2.Operation proto into a GateOperation."""
        msg = v2.program_pb2.Operation()
        json_format.ParseDict(proto, msg)
        return self.from_proto(msg)

    def from_proto(self, proto: v2.program_pb2.Operation) -> ops.GateOperation:
        """Turns a cirq.api.google.v2.Operation proto into a GateOperation."""
        qubits = [devices.GridQubit.from_proto_id(q.id) for q in proto.qubits]
        args = self._args_from_proto(proto)
        if self.num_qubits_param is not None:
            args[self.num_qubits_param] = len(qubits)
        gate = self.gate_constructor(**args)
        return gate.on(*qubits)

    def _args_from_proto(self, proto: v2.program_pb2.Operation
                        ) -> Dict[str, arg_func_langs.ArgValue]:
        return_args = {}<commit_msg>Add type annotations in cirq/google/op_deserializer.py (#1874)<commit_after># Copyright 2019 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Callable, Dict, NamedTuple, Optional, Sequence

import sympy
from google.protobuf import json_format

from cirq import devices, ops
from cirq.api.google import v2
from cirq.google import arg_func_langs


class DeserializingArg(
        NamedTuple('DeserializingArg', [
            ('serialized_name', str),
            ('constructor_arg_name', str),
            ('value_func', Optional[Callable[[arg_func_langs.ArgValue], Any]]),
            ('required', bool),
        ])):
    """Specification of the arguments to deserialize an argument to a gate.

    Attributes:
        serialized_name: The serialized name of the gate that is being
            deserialized.
        constructor_arg_name: The name of the argument in the constructor of
            the gate corresponding to this serialized argument.
        value_func: Sometimes a value from the serialized proto needs to
            converted to an appropriate type or form. This function takes the
            serialized value and returns the appropriate type. Defaults to
            None.
        required: Whether a value must be specified when constructing the
            deserialized gate. Defaults to True.
    """

    def __new__(cls,
                *,
                serialized_name: str,
                constructor_arg_name: str,
                value_func: Optional[Callable[[Any], Any]] = None,
                required: bool = True):
        return super(DeserializingArg,
                     cls).__new__(cls,
                                  serialized_name=serialized_name,
                                  constructor_arg_name=constructor_arg_name,
                                  value_func=value_func,
                                  required=required)


class GateOpDeserializer:
    """Describes how to deserialize a proto to a given Gate type.

    Attributes:
        serialized_gate_id: The id used when serializing the gate.
    """

    def __init__(self,
                 serialized_gate_id: str,
                 gate_constructor: Callable,
                 args: Sequence[DeserializingArg],
                 num_qubits_param: Optional[str] = None):
        """Constructs a deserializer.

        Args:
            serialized_gate_id: The serialized id of the gate that is being
                deserialized.
            gate_constructor: A function that produces the deserialized gate
                given arguments from args.
            args: A list of the arguments to be read from the serialized
                gate and the information required to use this to construct
                the gate using the gate_constructor above.
            num_qubits_param: Some gate constructors require that the number
                of qubits be passed to their constructor. This is the name
                of the parameter in the constructor for this value. If None,
                no number of qubits is passed to the constructor.
        """
        self.serialized_gate_id = serialized_gate_id
        self.gate_constructor = gate_constructor
        self.args = args
        self.num_qubits_param = num_qubits_param

    def from_proto_dict(self, proto: Dict) -> ops.GateOperation:
        """Turns a cirq.api.google.v2.Operation proto into a GateOperation."""
        msg = v2.program_pb2.Operation()
        json_format.ParseDict(proto, msg)
        return self.from_proto(msg)

    def from_proto(self, proto: v2.program_pb2.Operation) -> ops.GateOperation:
        """Turns a cirq.api.google.v2.Operation proto into a GateOperation."""
        qubits = [devices.GridQubit.from_proto_id(q.id) for q in proto.qubits]
        args = self._args_from_proto(proto)
        if self.num_qubits_param is not None:
            args[self.num_qubits_param] = len(qubits)
        gate = self.gate_constructor(**args)
        return gate.on(*qubits)

    def _args_from_proto(self, proto: v2.program_pb2.Operation
                        ) -> Dict[str, arg_func_langs.ArgValue]:
        return_args = {}
{'fraction_non_alphanumeric': 0.0652414709791759, 'fraction_numerical': 0.004209127159946832, 'mean_word_length': 2.344074074074074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6587930', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2278, 'n_words': 835}