
'''simple docstring'''







import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer






class 			lowerCAmelCase__       (  lowerCamelCase_					):






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Any													=	tempfile.mkdtemp()
											lowercase_     :					int													=	5

											# Realm tok
											lowercase_     :					str													=	[
											    '''[UNK]''',
											    '''[CLS]''',
											    '''[SEP]''',
											    '''[PAD]''',
											    '''[MASK]''',
											    '''test''',
											    '''question''',
											    '''this''',
											    '''is''',
											    '''the''',
											    '''first''',
											    '''second''',
											    '''third''',
											    '''fourth''',
											    '''fifth''',
											    '''record''',
											    '''want''',
											    '''##want''',
											    '''##ed''',
											    '''wa''',
											    '''un''',
											    '''runn''',
											    '''##ing''',
											    ''',''',
											    '''low''',
											    '''lowest''',
											]
											lowercase_     :					str													=	os.path.join(self.tmpdirname    ,							'''realm_tokenizer'''		)
											os.makedirs(__SCREAMING_SNAKE_CASE    ,							exist_ok=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					int													=	os.path.join(__SCREAMING_SNAKE_CASE    ,							VOCAB_FILES_NAMES['''vocab_file''']		)
											with open(self.vocab_file    ,							'''w'''    ,							encoding='''utf-8'''		) as vocab_writer:
																	vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]		)		)

											lowercase_     :					str													=	os.path.join(self.tmpdirname    ,							'''realm_block_records'''		)
											os.makedirs(__SCREAMING_SNAKE_CASE    ,							exist_ok=__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname    ,							'''realm_tokenizer'''		)		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											shutil.rmtree(self.tmpdirname		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Union[str, Any]													=	RealmConfig(num_block_records=self.num_block_records		)
											return config






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[Any]													=	Dataset.from_dict(
											    {
											        '''id''': ['''0''', '''1'''],
											        '''question''': ['''foo''', '''bar'''],
											        '''answers''': [['''Foo''', '''Bar'''], ['''Bar''']],
											    }		)
											return dataset






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Optional[int]													=	np.array(
											    [
											        b'''This is the first record''',
											        b'''This is the second record''',
											        b'''This is the third record''',
											        b'''This is the fourth record''',
											        b'''This is the fifth record''',
											        b'''This is a longer longer longer record''',
											    ]    ,							dtype=__SCREAMING_SNAKE_CASE    ,							)
											return block_records






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Optional[Any]													=	RealmRetriever(
											    block_records=self.get_dummy_block_records()    ,							tokenizer=self.get_tokenizer()    ,							)
											return retriever






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[str]													=	self.get_config()
											lowercase_     :					Optional[int]													=	self.get_dummy_retriever()
											lowercase_     :					Optional[Any]													=	retriever.tokenizer

											lowercase_     :					Optional[Any]													=	np.array([0, 3]    ,							dtype='''long'''		)
											lowercase_     :					Dict													=	tokenizer(['''Test question''']		).input_ids
											lowercase_     :					List[str]													=	tokenizer(
											    ['''the fourth''']    ,							add_special_tokens=__SCREAMING_SNAKE_CASE    ,							return_token_type_ids=__SCREAMING_SNAKE_CASE    ,							return_attention_mask=__SCREAMING_SNAKE_CASE    ,							).input_ids
											lowercase_     :					Any													=	config.reader_seq_len

											lowercase_						,			lowercase_						,			lowercase_						,			lowercase_     :					Dict													=	retriever(
											    __SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							answer_ids=__SCREAMING_SNAKE_CASE    ,							max_length=__SCREAMING_SNAKE_CASE    ,							return_tensors='''np'''		)

											self.assertEqual(len(__SCREAMING_SNAKE_CASE		)    ,							2		)
											self.assertEqual(len(__SCREAMING_SNAKE_CASE		)    ,							2		)
											self.assertEqual(len(__SCREAMING_SNAKE_CASE		)    ,							2		)
											self.assertEqual(concat_inputs.input_ids.shape    ,							(2, 10)		)
											self.assertEqual(concat_inputs.attention_mask.shape    ,							(2, 10)		)
											self.assertEqual(concat_inputs.token_type_ids.shape    ,							(2, 10)		)
											self.assertEqual(concat_inputs.special_tokens_mask.shape    ,							(2, 10)		)
											self.assertEqual(
											    tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]		)    ,							['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''first''', '''record''', '''[SEP]''']    ,							)
											self.assertEqual(
											    tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]		)    ,							['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''fourth''', '''record''', '''[SEP]''']    ,							)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Dict													=	self.get_config()
											lowercase_     :					Dict													=	self.get_dummy_retriever()
											lowercase_     :					List[Any]													=	retriever.tokenizer

											lowercase_     :					Tuple													=	np.array([0, 3, 5]    ,							dtype='''long'''		)
											lowercase_     :					int													=	tokenizer(['''Test question''']		).input_ids
											lowercase_     :					Optional[Any]													=	tokenizer(
											    ['''the fourth''', '''longer longer''']    ,							add_special_tokens=__SCREAMING_SNAKE_CASE    ,							return_token_type_ids=__SCREAMING_SNAKE_CASE    ,							return_attention_mask=__SCREAMING_SNAKE_CASE    ,							).input_ids
											lowercase_     :					str													=	config.reader_seq_len

											lowercase_						,			lowercase_						,			lowercase_						,			lowercase_     :					Tuple													=	retriever(
											    __SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							answer_ids=__SCREAMING_SNAKE_CASE    ,							max_length=__SCREAMING_SNAKE_CASE    ,							return_tensors='''np'''		)

											self.assertEqual([False, True, True]    ,							__SCREAMING_SNAKE_CASE		)
											self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]    ,							__SCREAMING_SNAKE_CASE		)
											self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[Any]													=	self.get_dummy_retriever()
											retriever.save_pretrained(os.path.join(self.tmpdirname    ,							'''realm_block_records'''		)		)

											# Test local path
											lowercase_     :					Union[str, Any]													=	retriever.from_pretrained(os.path.join(self.tmpdirname    ,							'''realm_block_records'''		)		)
											self.assertEqual(retriever.block_records[0]    ,							b'''This is the first record'''		)

											# Test mocked remote path
											with patch('''transformers.models.realm.retrieval_realm.hf_hub_download'''		) as mock_hf_hub_download:
																	lowercase_     :					Any													=	os.path.join(
																	    os.path.join(self.tmpdirname    ,							'''realm_block_records'''		)    ,							_REALM_BLOCK_RECORDS_FILENAME		)
																	lowercase_     :					Dict													=	RealmRetriever.from_pretrained('''google/realm-cc-news-pretrained-openqa'''		)

											self.assertEqual(retriever.block_records[0]    ,							b'''This is the first record'''		)





'''simple docstring'''







import qiskit
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			int				,				__SCREAMING_SNAKE_CASE :			int							):



						"""simple docstring"""





						lowercase_     :					List[Any]													=	qiskit.Aer.get_backend('''aer_simulator'''							)

						# Create a Quantum Circuit acting on the q register
						lowercase_     :					Dict													=	qiskit.QuantumCircuit(__SCREAMING_SNAKE_CASE				,				__SCREAMING_SNAKE_CASE							)

						# Map the quantum measurement to the classical bits
						circuit.measure([0]				,				[0]							)

						# Execute the circuit on the simulator
						lowercase_     :					Union[str, Any]													=	qiskit.execute(__SCREAMING_SNAKE_CASE				,				__SCREAMING_SNAKE_CASE				,				shots=1000							)

						# Return the histogram data of the results of the experiment.
						return job.result().get_counts(__SCREAMING_SNAKE_CASE							)


if __name__ == "__main__":
							print(f"""Total count for various states are: {single_qubit_measure(1, 1)}""")



