






import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY




@is_pipeline_test
class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase							:				Any 				= MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
       lowerCamelCase							:				List[str] 				= TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING



       def 					__lowercase						(							self			,						a__			,						a__			,						a__    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						AudioClassificationPipeline(model=a__			,						feature_extractor=a__    )

            # test with a raw waveform
            __snake_case			:int          =						np.zeros((3_40_00,)    )
            __snake_case			:Optional[Any]          =						np.zeros((1_40_00,)    )
            return audio_classifier, [audioa, audio]



       def 					__lowercase						(							self			,						a__			,						a__    ) ->      str:






            '''simple docstring'''





            __snake_case       , __snake_case			:int          =						examples
            __snake_case			:Tuple          =						audio_classifier(a__    )
            # by default a model is initialized with num_labels=2
            self.assertEqual(
                a__			,						[
                    {"""score""": ANY(a__    ), """label""": ANY(a__    )},
                    {"""score""": ANY(a__    ), """label""": ANY(a__    )},
                ]			,						)
            __snake_case			:List[Any]          =						audio_classifier(a__			,						top_k=1    )
            self.assertEqual(
                a__			,						[
                    {"""score""": ANY(a__    ), """label""": ANY(a__    )},
                ]			,						)

            self.run_torchaudio(a__    )



       @require_torchaudio
       def 					__lowercase						(							self			,						a__    ) ->      Union[str, Any]:






            '''simple docstring'''





            import datasets

            # test with a local file
            __snake_case			:List[str]          =						datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy"""			,						"""clean"""			,						split="""validation"""    )
            __snake_case			:Any          =						dataset[0]["""audio"""]["""array"""]
            __snake_case			:Union[str, Any]          =						audio_classifier(a__    )
            self.assertEqual(
                a__			,						[
                    {"""score""": ANY(a__    ), """label""": ANY(a__    )},
                    {"""score""": ANY(a__    ), """label""": ANY(a__    )},
                ]			,						)



       @require_torch
       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Optional[Any]          =						"""anton-l/wav2vec2-random-tiny-classifier"""

            __snake_case			:Union[str, Any]          =						pipeline("""audio-classification"""			,						model=a__    )

            __snake_case			:List[Any]          =						np.ones((80_00,)    )
            __snake_case			:Optional[int]          =						audio_classifier(a__			,						top_k=4    )

            __snake_case			:Tuple          =						[
                {"""score""": 0.08_42, """label""": """no"""},
                {"""score""": 0.08_38, """label""": """up"""},
                {"""score""": 0.08_37, """label""": """go"""},
                {"""score""": 0.08_34, """label""": """right"""},
            ]
            __snake_case			:str          =						[
                {"""score""": 0.08_45, """label""": """stop"""},
                {"""score""": 0.08_44, """label""": """on"""},
                {"""score""": 0.08_41, """label""": """right"""},
                {"""score""": 0.08_34, """label""": """left"""},
            ]
            self.assertIn(nested_simplify(a__			,						decimals=4    )			,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )

            __snake_case			:List[Any]          =						{"""array""": np.ones((80_00,)    ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
            __snake_case			:List[str]          =						audio_classifier(a__			,						top_k=4    )
            self.assertIn(nested_simplify(a__			,						decimals=4    )			,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )



       @require_torch
       @slow
       def 					__lowercase						(							self    ) ->      Any:






            '''simple docstring'''





            import datasets

            __snake_case			:str          =						"""superb/wav2vec2-base-superb-ks"""

            __snake_case			:Union[str, Any]          =						pipeline("""audio-classification"""			,						model=a__    )
            __snake_case			:Dict          =						datasets.load_dataset("""anton-l/superb_dummy"""			,						"""ks"""			,						split="""test"""    )

            __snake_case			:Tuple          =						np.array(dataset[3]["""speech"""]			,						dtype=np.floataa    )
            __snake_case			:Optional[Any]          =						audio_classifier(a__			,						top_k=4    )
            self.assertEqual(
                nested_simplify(a__			,						decimals=3    )			,						[
                    {"""score""": 0.9_81, """label""": """go"""},
                    {"""score""": 0.0_07, """label""": """up"""},
                    {"""score""": 0.0_06, """label""": """_unknown_"""},
                    {"""score""": 0.0_01, """label""": """down"""},
                ]			,						)



       @require_tf
       @unittest.skip("""Audio classification is not implemented for TF"""    )
       def 					__lowercase						(							self    ) ->      str:






            '''simple docstring'''





            pass










import numpy as np
import qiskit






def   UpperCamelCase  (       snake_case__     :   int = 8	,snake_case__     :   int | None = None  ):




     '''simple docstring'''






     __snake_case			:Tuple          =						np.random.default_rng(seed=snake_case__  )

     # Roughly 25% of the qubits will contribute to the key.
     # So we take more than we need.
     __snake_case			:Union[str, Any]          =						6 * key_len
     # Measurement basis for Alice's qubits.
     __snake_case			:int          =						rng.integers(2	,size=snake_case__  )
     # The set of states Alice will prepare.
     __snake_case			:Dict          =						rng.integers(2	,size=snake_case__  )
     # Measurement basis for Bob's qubits.
     __snake_case			:str          =						rng.integers(2	,size=snake_case__  )

     # Quantum Circuit to simulate BB84
     __snake_case			:Optional[int]          =						qiskit.QuantumCircuit(snake_case__	,name="""BB84"""  )

     # Alice prepares her qubits according to rules above.
     for index, _ in enumerate(snake_case__  ):
          if alice_state[index] == 1:
               bbaa_circ.x(snake_case__  )
          if alice_basis[index] == 1:
               bbaa_circ.h(snake_case__  )
     bbaa_circ.barrier()

     # Bob measures the received qubits according to rules above.
     for index, _ in enumerate(snake_case__  ):
          if bob_basis[index] == 1:
               bbaa_circ.h(snake_case__  )

     bbaa_circ.barrier()
     bbaa_circ.measure_all()

     # Simulate the quantum circuit.
     __snake_case			:Optional[int]          =						qiskit.Aer.get_backend("""aer_simulator"""  )
     # We only need to run one shot because the key is unique.
     # Multiple shots will produce the same key.
     __snake_case			:List[Any]          =						qiskit.execute(snake_case__	,snake_case__	,shots=1	,seed_simulator=snake_case__  )
     # Returns the result of measurement.
     __snake_case			:Tuple          =						job.result().get_counts(snake_case__  ).most_frequent()

     # Extracting the generated key from the simulation results.
     # Only keep measurement results where Alice and Bob chose the same basis.
     __snake_case			:Any          =						"""""".join(
         [
             result_bit
             for alice_basis_bit, bob_basis_bit, result_bit in zip(
                 snake_case__	,snake_case__	,snake_case__  )
             if alice_basis_bit == bob_basis_bit
         ]  )

     # Get final key. Pad with 0 if too short, otherwise truncate.
     __snake_case			:Dict          =						gen_key[:key_len] if len(snake_case__  ) >= key_len else gen_key.ljust(snake_case__	,"""0"""  )
     return key


if __name__ == "__main__":
     print(f'''The generated key is : {bbaa(8, seed=0)}''')
     from doctest import testmod

     testmod()


