<commit_before># Copyright 2021 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.
import datetime

from typing import Callable, Dict, Iterable, List, Optional, Sequence, TYPE_CHECKING, Union

import cirq

from cirq_google.api import v2
from cirq_google.engine import calibration, validating_sampler
from cirq_google.engine.abstract_local_processor import AbstractLocalProcessor
from cirq_google.engine.abstract_local_program import AbstractLocalProgram
from cirq_google.engine.abstract_program import AbstractProgram
from cirq_google.engine.local_simulation_type import LocalSimulationType
from cirq_google.engine.simulated_local_job import SimulatedLocalJob
from cirq_google.engine.simulated_local_program import SimulatedLocalProgram
from cirq_google.serialization.circuit_serializer import CIRCUIT_SERIALIZER

if TYPE_CHECKING:
    from cirq_google.serialization.serializer import Serializer

VALID_LANGUAGES = [
    'type.googleapis.com/cirq.google.api.v2.Program',
    'type.googleapis.com/cirq.google.api.v2.BatchProgram',
]

GATE_SET_VALIDATOR_TYPE = Callable[
    [Sequence[cirq.AbstractCircuit], Sequence[cirq.Sweepable], int, 'Serializer'],
    None,
]


def _date_to_timestamp(
    union_time: Optional[Union[datetime.datetime, datetime.date, int]]
) -> Optional[int]:
    if isinstance(union_time, int):
        return union_time
    elif isinstance(union_time, datetime.datetime):
        return int(union_time.timestamp())
    elif isinstance(union_time, datetime.date):
        return int(datetime.datetime.combine(union_time, datetime.datetime.min.time()).timestamp())
    return None


class SimulatedLocalProcessor(AbstractLocalProcessor):
    """A processor backed by a sampler and device.

    Intended for local simulation testing, this processor will
    create a `ValidationSampler` that will validate requests based on
    the provided device and an additional Callable (that can verify
    serialization constraints, for instance).  Jobs will then be
    executed using the provided sampler.

    This class also supports a list of calibration metrics that are
    stored in-memory to replicate Quantum Engine calibration metrics.

    This class can be used as a local emulator for the Quantum Engine
    API or for testing or mocking.

    Attributes:
        sampler: A `cirq.Sampler` that can execute the quantum jobs.
        device: An optional device, for validation of qubit connectivity.
        validator: A Callable that can validate additional characteristics
            beyond the device, such as serialization, repetition limits, etc.
        gate_set_validator:  A callable that can validate a circuit and sweeps
            based on the given serializer.
        calibrations: A dictionary of calibration metrics keyed by epoch seconds
            that can be returned by the processor.
        processor_id: Unique string id of the processor.
        engine: The parent `AbstractEngine` object, if available.<commit_msg>Add missing Arg in SimulatedLocalProcessor (#5027)<commit_after># Copyright 2021 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.
import datetime

from typing import Callable, Dict, Iterable, List, Optional, Sequence, TYPE_CHECKING, Union

import cirq

from cirq_google.api import v2
from cirq_google.engine import calibration, validating_sampler
from cirq_google.engine.abstract_local_processor import AbstractLocalProcessor
from cirq_google.engine.abstract_local_program import AbstractLocalProgram
from cirq_google.engine.abstract_program import AbstractProgram
from cirq_google.engine.local_simulation_type import LocalSimulationType
from cirq_google.engine.simulated_local_job import SimulatedLocalJob
from cirq_google.engine.simulated_local_program import SimulatedLocalProgram
from cirq_google.serialization.circuit_serializer import CIRCUIT_SERIALIZER

if TYPE_CHECKING:
    from cirq_google.serialization.serializer import Serializer

VALID_LANGUAGES = [
    'type.googleapis.com/cirq.google.api.v2.Program',
    'type.googleapis.com/cirq.google.api.v2.BatchProgram',
]

GATE_SET_VALIDATOR_TYPE = Callable[
    [Sequence[cirq.AbstractCircuit], Sequence[cirq.Sweepable], int, 'Serializer'],
    None,
]


def _date_to_timestamp(
    union_time: Optional[Union[datetime.datetime, datetime.date, int]]
) -> Optional[int]:
    if isinstance(union_time, int):
        return union_time
    elif isinstance(union_time, datetime.datetime):
        return int(union_time.timestamp())
    elif isinstance(union_time, datetime.date):
        return int(datetime.datetime.combine(union_time, datetime.datetime.min.time()).timestamp())
    return None


class SimulatedLocalProcessor(AbstractLocalProcessor):
    """A processor backed by a sampler and device.

    Intended for local simulation testing, this processor will
    create a `ValidationSampler` that will validate requests based on
    the provided device and an additional Callable (that can verify
    serialization constraints, for instance).  Jobs will then be
    executed using the provided sampler.

    This class also supports a list of calibration metrics that are
    stored in-memory to replicate Quantum Engine calibration metrics.

    This class can be used as a local emulator for the Quantum Engine
    API or for testing or mocking.

    Attributes:
        sampler: A `cirq.Sampler` that can execute the quantum jobs.
        device: An optional device, for validation of qubit connectivity.
        validator: A Callable that can validate additional characteristics
            beyond the device, such as serialization, repetition limits, etc.
        gate_set_validator:  A callable that can validate a circuit and sweeps
            based on the given serializer.
        simulation_type:  Whether sampler execution should be
            synchronous or asynchronous.
        calibrations: A dictionary of calibration metrics keyed by epoch seconds
            that can be returned by the processor.
        processor_id: Unique string id of the processor.
        engine: The parent `AbstractEngine` object, if available.
{'fraction_non_alphanumeric': 0.057323943661971834, 'fraction_numerical': 0.0036619718309859155, 'mean_word_length': 4.2561065877128055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6269855', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1795, 'n_words': 707}