


import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
						from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

						from .test_feature_extraction_speechta import floats_list


_UpperCAmelCase      :	List[str]  =get_tests_dir("""fixtures/test_sentencepiece_bpe_char.model""")







@require_torch
class 					snake_case__(				unittest.TestCase				):







						'''simple docstring'''




						def     lowercase_			(					self				)       ->     str:
										lowerCAmelCase_			:   List[str]     					=					tempfile.mkdtemp()

										lowerCAmelCase_			:   Dict     					=					SpeechTaTokenizer(__lowercase				)
										tokenizer.save_pretrained(self.tmpdirname				)

										lowerCAmelCase_			:   int     					=					{
										    '''feature_size''': 1,
										    '''padding_value''': 0.0,
										    '''sampling_rate''': 1_6_0_0_0,
										    '''do_normalize''': False,
										    '''num_mel_bins''': 8_0,
										    '''hop_length''': 1_6,
										    '''win_length''': 6_4,
										    '''win_function''': '''hann_window''',
										    '''fmin''': 8_0,
										    '''fmax''': 7_6_0_0,
										    '''mel_floor''': 1e-10,
										    '''reduction_factor''': 2,
										    '''return_attention_mask''': True,
										}

										lowerCAmelCase_			:   int     					=					os.path.join(self.tmpdirname ,    __lowercase				)
										with open(self.feature_extraction_file ,    '''w''' ,    encoding='''utf-8'''				) as fp:
														fp.write(json.dumps(__lowercase				) + '''\n'''				)




						def     lowercase_			(					self ,    **__lowercase				)       ->     Tuple:
										return SpeechTaTokenizer.from_pretrained(self.tmpdirname ,    **__lowercase				)




						def     lowercase_			(					self ,    **__lowercase				)       ->     Dict:
										return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname ,    **__lowercase				)




						def     lowercase_			(					self				)       ->     List[str]:
										shutil.rmtree(self.tmpdirname				)




						def     lowercase_			(					self				)       ->     Any:
										lowerCAmelCase_			:   List[str]     					=					self.get_tokenizer()
										lowerCAmelCase_			:   Union[str, Any]     					=					self.get_feature_extractor()

										lowerCAmelCase_			:   Optional[Any]     					=					SpeechTaProcessor(tokenizer=__lowercase ,    feature_extractor=__lowercase				)

										processor.save_pretrained(self.tmpdirname				)
										lowerCAmelCase_			:   Tuple     					=					SpeechTaProcessor.from_pretrained(self.tmpdirname				)

										self.assertEqual(processor.tokenizer.get_vocab() ,    tokenizer.get_vocab()				)
										self.assertIsInstance(processor.tokenizer ,    __lowercase				)

										self.assertEqual(processor.feature_extractor.to_json_string() ,    feature_extractor.to_json_string()				)
										self.assertIsInstance(processor.feature_extractor ,    __lowercase				)




						def     lowercase_			(					self				)       ->     List[str]:
										lowerCAmelCase_			:   Optional[int]     					=					SpeechTaProcessor(tokenizer=self.get_tokenizer() ,    feature_extractor=self.get_feature_extractor()				)
										processor.save_pretrained(self.tmpdirname				)

										lowerCAmelCase_			:   Optional[int]     					=					self.get_tokenizer(bos_token='''(BOS)''' ,    eos_token='''(EOS)'''				)
										lowerCAmelCase_			:   Union[str, Any]     					=					self.get_feature_extractor(do_normalize=__lowercase ,    padding_value=1.0				)

										lowerCAmelCase_			:   Dict     					=					SpeechTaProcessor.from_pretrained(
										    self.tmpdirname ,    bos_token='''(BOS)''' ,    eos_token='''(EOS)''' ,    do_normalize=__lowercase ,    padding_value=1.0				)

										self.assertEqual(processor.tokenizer.get_vocab() ,    tokenizer_add_kwargs.get_vocab()				)
										self.assertIsInstance(processor.tokenizer ,    __lowercase				)

										self.assertEqual(processor.feature_extractor.to_json_string() ,    feature_extractor_add_kwargs.to_json_string()				)
										self.assertIsInstance(processor.feature_extractor ,    __lowercase				)




						def     lowercase_			(					self				)       ->     Optional[Any]:
										lowerCAmelCase_			:   str     					=					self.get_feature_extractor()
										lowerCAmelCase_			:   Optional[int]     					=					self.get_tokenizer()

										lowerCAmelCase_			:   Union[str, Any]     					=					SpeechTaProcessor(tokenizer=__lowercase ,    feature_extractor=__lowercase				)

										lowerCAmelCase_			:   Dict     					=					floats_list((3, 1_0_0_0)				)

										lowerCAmelCase_			:   int     					=					feature_extractor(audio=__lowercase ,    return_tensors='''np'''				)
										lowerCAmelCase_			:   Any     					=					processor(audio=__lowercase ,    return_tensors='''np'''				)

										for key in input_feat_extract.keys():
														self.assertAlmostEqual(input_feat_extract[key].sum() ,    input_processor[key].sum() ,    delta=1e-2				)




						def     lowercase_			(					self				)       ->     Optional[Any]:
										lowerCAmelCase_			:   Optional[Any]     					=					self.get_feature_extractor()
										lowerCAmelCase_			:   Tuple     					=					self.get_tokenizer()

										lowerCAmelCase_			:   Union[str, Any]     					=					SpeechTaProcessor(tokenizer=__lowercase ,    feature_extractor=__lowercase				)

										lowerCAmelCase_			:   Tuple     					=					floats_list((3, 1_0_0_0)				)

										lowerCAmelCase_			:   Optional[Any]     					=					feature_extractor(audio_target=__lowercase ,    return_tensors='''np'''				)
										lowerCAmelCase_			:   List[Any]     					=					processor(audio_target=__lowercase ,    return_tensors='''np'''				)

										for key in input_feat_extract.keys():
														self.assertAlmostEqual(input_feat_extract[key].sum() ,    input_processor[key].sum() ,    delta=1e-2				)




						def     lowercase_			(					self				)       ->     Union[str, Any]:
										lowerCAmelCase_			:   Optional[Any]     					=					self.get_feature_extractor()
										lowerCAmelCase_			:   str     					=					self.get_tokenizer()

										lowerCAmelCase_			:   Tuple     					=					SpeechTaProcessor(tokenizer=__lowercase ,    feature_extractor=__lowercase				)

										lowerCAmelCase_			:   Optional[int]     					=					'''This is a test string'''

										lowerCAmelCase_			:   Union[str, Any]     					=					processor(text=__lowercase				)
										lowerCAmelCase_			:   int     					=					tokenizer(__lowercase				)

										for key in encoded_tok.keys():
														self.assertListEqual(encoded_tok[key] ,    encoded_processor[key]				)




						def     lowercase_			(					self				)       ->     Dict:
										lowerCAmelCase_			:   int     					=					self.get_feature_extractor()
										lowerCAmelCase_			:   Union[str, Any]     					=					self.get_tokenizer()

										lowerCAmelCase_			:   List[Any]     					=					SpeechTaProcessor(tokenizer=__lowercase ,    feature_extractor=__lowercase				)

										lowerCAmelCase_			:   Optional[int]     					=					'''This is a test string'''

										lowerCAmelCase_			:   int     					=					processor(text_target=__lowercase				)
										lowerCAmelCase_			:   str     					=					tokenizer(__lowercase				)

										for key in encoded_tok.keys():
														self.assertListEqual(encoded_tok[key] ,    encoded_processor[key]				)




						def     lowercase_			(					self				)       ->     Tuple:
										lowerCAmelCase_			:   Tuple     					=					self.get_feature_extractor()
										lowerCAmelCase_			:   List[str]     					=					self.get_tokenizer()

										lowerCAmelCase_			:   Any     					=					SpeechTaProcessor(tokenizer=__lowercase ,    feature_extractor=__lowercase				)

										lowerCAmelCase_			:   List[Any]     					=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

										lowerCAmelCase_			:   Dict     					=					processor.batch_decode(__lowercase				)
										lowerCAmelCase_			:   int     					=					tokenizer.batch_decode(__lowercase				)

										self.assertListEqual(__lowercase ,    __lowercase				)




						def     lowercase_			(					self				)       ->     Optional[int]:
										lowerCAmelCase_			:   List[str]     					=					self.get_feature_extractor()
										lowerCAmelCase_			:   int     					=					self.get_tokenizer()

										lowerCAmelCase_			:   Dict     					=					SpeechTaProcessor(tokenizer=__lowercase ,    feature_extractor=__lowercase				)

										self.assertListEqual(
										    processor.model_input_names ,    feature_extractor.model_input_names ,    msg='''`processor` and `feature_extractor` model input names do not match''' ,    )



import doctest

import projectq
from projectq.ops import H, Measure



def       lowerCAmelCase			(		lowerCAmelCase_			)->			int:
				lowerCAmelCase_			:   List[Any]     					=					quantum_engine.allocate_qubit()
				H | qubit
				Measure | qubit
				return int(lowerCAmelCase_			)


if __name__ == "__main__":
						doctest.testmod()

						# initialises a new quantum backend
						_UpperCAmelCase      :	str  =projectq.MainEngine()

						# Generate a list of 10 random numbers
						_UpperCAmelCase      :	Optional[int]  =[get_random_number(quantum_engine) for _ in range(10)]

						# Flushes the quantum engine from memory
						quantum_engine.flush()

						print("""Random numbers""", random_numbers_list)