




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      : Union[str, Any]					      =   get_tests_dir("fixtures/test_sentencepiece_bpe_char.model")

@require_torch
class 	__lowerCAmelCase			(			unittest.TestCase				):
							def    lowerCamelCase__							(		self		:Tuple       ):







								'''simple docstring'''







								a		       =							tempfile.mkdtemp()

								a		       =							SpeechTaTokenizer(__magic_name__       )
								tokenizer.save_pretrained(self.tmpdirname       )

								a		       =							{
								    """feature_size""": 1,
								    """padding_value""": 0.0,
								    """sampling_rate""": 1_6000,
								    """do_normalize""": False,
								    """num_mel_bins""": 80,
								    """hop_length""": 16,
								    """win_length""": 64,
								    """win_function""": """hann_window""",
								    """fmin""": 80,
								    """fmax""": 7600,
								    """mel_floor""": 1E-1_0,
								    """reduction_factor""": 2,
								    """return_attention_mask""": True,
								}

								a		       =							os.path.join(self.tmpdirname	,   __magic_name__       )
								with open(self.feature_extraction_file	,   """w"""	,   encoding="""utf-8"""       ) as fp:
									fp.write(json.dumps(__magic_name__       ) + """\n"""       )
							def    lowerCamelCase__							(		self		:Optional[Any]	,   **__magic_name__		:Dict       ):







								'''simple docstring'''







								return SpeechTaTokenizer.from_pretrained(self.tmpdirname	,   **__magic_name__       )
							def    lowerCamelCase__							(		self		:int	,   **__magic_name__		:List[Any]       ):







								'''simple docstring'''







								return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname	,   **__magic_name__       )
							def    lowerCamelCase__							(		self		:Optional[Any]       ):







								'''simple docstring'''







								shutil.rmtree(self.tmpdirname       )
							def    lowerCamelCase__							(		self		:List[Any]       ):







								'''simple docstring'''







								a		       =							self.get_tokenizer()
								a		       =							self.get_feature_extractor()

								a		       =							SpeechTaProcessor(tokenizer=__magic_name__	,   feature_extractor=__magic_name__       )

								processor.save_pretrained(self.tmpdirname       )
								a		       =							SpeechTaProcessor.from_pretrained(self.tmpdirname       )

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

								self.assertEqual(processor.feature_extractor.to_json_string()	,   feature_extractor.to_json_string()       )
								self.assertIsInstance(processor.feature_extractor	,   __magic_name__       )
							def    lowerCamelCase__							(		self		:str       ):







								'''simple docstring'''







								a		       =							SpeechTaProcessor(tokenizer=self.get_tokenizer()	,   feature_extractor=self.get_feature_extractor()       )
								processor.save_pretrained(self.tmpdirname       )

								a		       =							self.get_tokenizer(bos_token="""(BOS)"""	,   eos_token="""(EOS)"""       )
								a		       =							self.get_feature_extractor(do_normalize=__magic_name__	,   padding_value=1.0       )

								a		       =							SpeechTaProcessor.from_pretrained(
								    self.tmpdirname	,   bos_token="""(BOS)"""	,   eos_token="""(EOS)"""	,   do_normalize=__magic_name__	,   padding_value=1.0       )

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

								self.assertEqual(processor.feature_extractor.to_json_string()	,   feature_extractor_add_kwargs.to_json_string()       )
								self.assertIsInstance(processor.feature_extractor	,   __magic_name__       )
							def    lowerCamelCase__							(		self		:Union[str, Any]       ):







								'''simple docstring'''







								a		       =							self.get_feature_extractor()
								a		       =							self.get_tokenizer()

								a		       =							SpeechTaProcessor(tokenizer=__magic_name__	,   feature_extractor=__magic_name__       )

								a		       =							floats_list((3, 1000)       )

								a		       =							feature_extractor(audio=__magic_name__	,   return_tensors="""np"""       )
								a		       =							processor(audio=__magic_name__	,   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    lowerCamelCase__							(		self		:List[Any]       ):







								'''simple docstring'''







								a		       =							self.get_feature_extractor()
								a		       =							self.get_tokenizer()

								a		       =							SpeechTaProcessor(tokenizer=__magic_name__	,   feature_extractor=__magic_name__       )

								a		       =							floats_list((3, 1000)       )

								a		       =							feature_extractor(audio_target=__magic_name__	,   return_tensors="""np"""       )
								a		       =							processor(audio_target=__magic_name__	,   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    lowerCamelCase__							(		self		:Union[str, Any]       ):







								'''simple docstring'''







								a		       =							self.get_feature_extractor()
								a		       =							self.get_tokenizer()

								a		       =							SpeechTaProcessor(tokenizer=__magic_name__	,   feature_extractor=__magic_name__       )

								a		       =							"""This is a test string"""

								a		       =							processor(text=__magic_name__       )
								a		       =							tokenizer(__magic_name__       )

								for key in encoded_tok.keys():
									self.assertListEqual(encoded_tok[key]	,   encoded_processor[key]       )
							def    lowerCamelCase__							(		self		:List[str]       ):







								'''simple docstring'''







								a		       =							self.get_feature_extractor()
								a		       =							self.get_tokenizer()

								a		       =							SpeechTaProcessor(tokenizer=__magic_name__	,   feature_extractor=__magic_name__       )

								a		       =							"""This is a test string"""

								a		       =							processor(text_target=__magic_name__       )
								a		       =							tokenizer(__magic_name__       )

								for key in encoded_tok.keys():
									self.assertListEqual(encoded_tok[key]	,   encoded_processor[key]       )
							def    lowerCamelCase__							(		self		:Union[str, Any]       ):







								'''simple docstring'''







								a		       =							self.get_feature_extractor()
								a		       =							self.get_tokenizer()

								a		       =							SpeechTaProcessor(tokenizer=__magic_name__	,   feature_extractor=__magic_name__       )

								a		       =							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								a		       =							processor.batch_decode(__magic_name__       )
								a		       =							tokenizer.batch_decode(__magic_name__       )

								self.assertListEqual(__magic_name__	,   __magic_name__       )





							def    lowerCamelCase__							(		self		:Any       ):







								'''simple docstring'''







								a		       =							self.get_feature_extractor()
								a		       =							self.get_tokenizer()

								a		       =							SpeechTaProcessor(tokenizer=__magic_name__	,   feature_extractor=__magic_name__       )

								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 	__A	(			__lowerCamelCase					)   -> int:
	a		       =							quantum_engine.allocate_qubit()
	H | qubit
	Measure | qubit
	return int(__lowerCamelCase					)


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

		# initialises a new quantum backend
		__UpperCamelCase      : List[Any]					      =   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)

