<commit_before># Copyright 2019 Google LLC. All Rights Reserved.
#
# 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 warnings
from typing import Dict, Union

import cirq
import numpy as np

from . import qsim


# List of parameter names that appear in valid Cirq protos.
GATE_PARAMS = [
    "exponent",
    "phase_exponent",
    "global_shift",
    "x_exponent",
    "z_exponent",
    "axis_phase_exponent",
    "phi",
    "theta",
]


def _translate_ControlledGate(gate: cirq.ControlledGate):
    return _cirq_gate_kind(gate.sub_gate)


def _translate_XPowGate(gate: cirq.XPowGate):
    # cirq.rx also uses this path.
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kX
    return qsim.kXPowGate


def _translate_YPowGate(gate: cirq.YPowGate):
    # cirq.ry also uses this path.
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kY
    return qsim.kYPowGate


def _translate_ZPowGate(gate: cirq.ZPowGate):
    # cirq.rz also uses this path.
    if gate.global_shift == 0:
        if gate.exponent == 1:
            return qsim.kZ
        if gate.exponent == 0.5:
            return qsim.kS
        if gate.exponent == 0.25:
            return qsim.kT
    return qsim.kZPowGate


def _translate_HPowGate(gate: cirq.HPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kH
    return qsim.kHPowGate


def _translate(gate: cirq.CZPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCZ
    return qsim.kCZPowGate


def _translate_CXPowGate(gate: cirq.CXPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCX
    return qsim.kCXPowGate


def _translate_PhasedXPowGate(gate: cirq.PhasedXPowGate):
    return qsim.kPhasedXPowGate


def _translate_PhasedXZGate(gate: cirq.PhasedXZGate):
    return qsim.kPhasedXZGate


def _translate_XXPowGate(gate: cirq.XXPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kXX
    return qsim.kXXPowGate


def _translate_YYPowGate(gate: cirq.YYPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kYY
    return qsim.kYYPowGate


def _translate_ZZPowGate(gate: cirq.ZZPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kZZ
    return qsim.kZZPowGate


def _translate_SwapPowGate(gate: cirq.SwapPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kSWAP
    return qsim.kSwapPowGate


def _translate_ISwapPowGate(gate: cirq.ISwapPowGate):
    # cirq.riswap also uses this path.
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kISWAP
    return qsim.kISwapPowGate


def _translate_PhasedISwapPowGate(gate: cirq.PhasedISwapPowGate):
    # cirq.givens also uses this path.
    return qsim.kPhasedISwapPowGate


def _translate_FSimGate(gate: cirq.FSimGate):
    return qsim.kFSimGate


def _translate_TwoQubitDiagonalGate(gate: cirq.TwoQubitDiagonalGate):
    return qsim.kTwoQubitDiagonalGate


def _translate_ThreeQubitDiagonalGate(gate: cirq.ThreeQubitDiagonalGate):
    return qsim.kThreeQubitDiagonalGate


def _translate_CCZPowGate(gate: cirq.CCZPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCCZ
    return qsim.kCCZPowGate


def _translate_CCXPowGate(gate: cirq.CCXPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCCX
    return qsim.kCCXPowGate


def _translate_CSwapGate(gate: cirq.CSwapGate):
    return qsim.kCSwapGate


def _translate_MatrixGate(gate: cirq.MatrixGate):
    if gate.num_qubits() <= 6:
        return qsim.kMatrixGate
    raise NotImplementedError(
        f"Received matrix on {gate.num_qubits()} qubits; "
        + "only up to 6-qubit gates are supported."
    )


def _translate_MeasurementGate(gate: cirq.MeasurementGate):
    # needed to inherit SimulatesSamples in sims
    return qsim.kMeasurement


TYPE_TRANSLATOR = {
    cirq.ControlledGate: _translate_ControlledGate,
    cirq.XPowGate: _translate_XPowGate,
    cirq.YPowGate: _translate_YPowGate,
    cirq.ZPowGate: _translate_ZPowGate,
    cirq.HPowGate: _translate_HPowGate,
    cirq.CXPowGate: _translate_CXPowGate,
    cirq.PhasedXPowGate: _translate_PhasedXPowGate,
    cirq.PhasedXZGate: _translate_PhasedXZGate,
    cirq.XXPowGate: _translate_XXPowGate,
    cirq.YYPowGate: _translate_YYPowGate,
    cirq.ZZPowGate: _translate_ZZPowGate,
    cirq.SwapPowGate: _translate_SwapPowGate,
    cirq.ISwapPowGate: _translate_ISwapPowGate,
    cirq.PhasedISwapPowGate: _translate_PhasedISwapPowGate,
    cirq.FSimGate: _translate_FSimGate,
    cirq.TwoQubitDiagonalGate: _translate_TwoQubitDiagonalGate,
    cirq.ThreeQubitDiagonalGate: _translate_ThreeQubitDiagonalGate,
    cirq.CCZPowGate: _translate_CCZPowGate,
    cirq.CCXPowGate: _translate_CCXPowGate,
    cirq.CSwapGate: _translate_CSwapGate,
    cirq.MatrixGate: _translate_MatrixGate,<commit_msg>Include CZ<commit_after># Copyright 2019 Google LLC. All Rights Reserved.
#
# 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 warnings
from typing import Dict, Union

import cirq
import numpy as np

from . import qsim


# List of parameter names that appear in valid Cirq protos.
GATE_PARAMS = [
    "exponent",
    "phase_exponent",
    "global_shift",
    "x_exponent",
    "z_exponent",
    "axis_phase_exponent",
    "phi",
    "theta",
]


def _translate_ControlledGate(gate: cirq.ControlledGate):
    return _cirq_gate_kind(gate.sub_gate)


def _translate_XPowGate(gate: cirq.XPowGate):
    # cirq.rx also uses this path.
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kX
    return qsim.kXPowGate


def _translate_YPowGate(gate: cirq.YPowGate):
    # cirq.ry also uses this path.
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kY
    return qsim.kYPowGate


def _translate_ZPowGate(gate: cirq.ZPowGate):
    # cirq.rz also uses this path.
    if gate.global_shift == 0:
        if gate.exponent == 1:
            return qsim.kZ
        if gate.exponent == 0.5:
            return qsim.kS
        if gate.exponent == 0.25:
            return qsim.kT
    return qsim.kZPowGate


def _translate_HPowGate(gate: cirq.HPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kH
    return qsim.kHPowGate


def _translate_CZPowGate(gate: cirq.CZPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCZ
    return qsim.kCZPowGate


def _translate_CXPowGate(gate: cirq.CXPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCX
    return qsim.kCXPowGate


def _translate_PhasedXPowGate(gate: cirq.PhasedXPowGate):
    return qsim.kPhasedXPowGate


def _translate_PhasedXZGate(gate: cirq.PhasedXZGate):
    return qsim.kPhasedXZGate


def _translate_XXPowGate(gate: cirq.XXPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kXX
    return qsim.kXXPowGate


def _translate_YYPowGate(gate: cirq.YYPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kYY
    return qsim.kYYPowGate


def _translate_ZZPowGate(gate: cirq.ZZPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kZZ
    return qsim.kZZPowGate


def _translate_SwapPowGate(gate: cirq.SwapPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kSWAP
    return qsim.kSwapPowGate


def _translate_ISwapPowGate(gate: cirq.ISwapPowGate):
    # cirq.riswap also uses this path.
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kISWAP
    return qsim.kISwapPowGate


def _translate_PhasedISwapPowGate(gate: cirq.PhasedISwapPowGate):
    # cirq.givens also uses this path.
    return qsim.kPhasedISwapPowGate


def _translate_FSimGate(gate: cirq.FSimGate):
    return qsim.kFSimGate


def _translate_TwoQubitDiagonalGate(gate: cirq.TwoQubitDiagonalGate):
    return qsim.kTwoQubitDiagonalGate


def _translate_ThreeQubitDiagonalGate(gate: cirq.ThreeQubitDiagonalGate):
    return qsim.kThreeQubitDiagonalGate


def _translate_CCZPowGate(gate: cirq.CCZPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCCZ
    return qsim.kCCZPowGate


def _translate_CCXPowGate(gate: cirq.CCXPowGate):
    if gate.exponent == 1 and gate.global_shift == 0:
        return qsim.kCCX
    return qsim.kCCXPowGate


def _translate_CSwapGate(gate: cirq.CSwapGate):
    return qsim.kCSwapGate


def _translate_MatrixGate(gate: cirq.MatrixGate):
    if gate.num_qubits() <= 6:
        return qsim.kMatrixGate
    raise NotImplementedError(
        f"Received matrix on {gate.num_qubits()} qubits; "
        + "only up to 6-qubit gates are supported."
    )


def _translate_MeasurementGate(gate: cirq.MeasurementGate):
    # needed to inherit SimulatesSamples in sims
    return qsim.kMeasurement


TYPE_TRANSLATOR = {
    cirq.ControlledGate: _translate_ControlledGate,
    cirq.XPowGate: _translate_XPowGate,
    cirq.YPowGate: _translate_YPowGate,
    cirq.ZPowGate: _translate_ZPowGate,
    cirq.HPowGate: _translate_HPowGate,
    cirq.CZPowGate: _translate_CZPowGate,
    cirq.CXPowGate: _translate_CXPowGate,
    cirq.PhasedXPowGate: _translate_PhasedXPowGate,
    cirq.PhasedXZGate: _translate_PhasedXZGate,
    cirq.XXPowGate: _translate_XXPowGate,
    cirq.YYPowGate: _translate_YYPowGate,
    cirq.ZZPowGate: _translate_ZZPowGate,
    cirq.SwapPowGate: _translate_SwapPowGate,
    cirq.ISwapPowGate: _translate_ISwapPowGate,
    cirq.PhasedISwapPowGate: _translate_PhasedISwapPowGate,
    cirq.FSimGate: _translate_FSimGate,
    cirq.TwoQubitDiagonalGate: _translate_TwoQubitDiagonalGate,
    cirq.ThreeQubitDiagonalGate: _translate_ThreeQubitDiagonalGate,
    cirq.CCZPowGate: _translate_CCZPowGate,
    cirq.CCXPowGate: _translate_CCXPowGate,
    cirq.CSwapGate: _translate_CSwapGate,
    cirq.MatrixGate: _translate_MatrixGate,
{'fraction_non_alphanumeric': 0.07922873069542173, 'fraction_numerical': 0.007493374760120625, 'mean_word_length': 3.6669509594882728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4705582', 'n_tokens_mistral': 4095, 'n_tokens_neox': 3841, 'n_words': 884}