





"""simple docstring"""
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import require_keras_nlp, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_keras_nlp_available():
  from transformers.models.gpta import TFGPTaTokenizer


lowerCamelCase_								=		["gpt2"]
lowerCamelCase_								=		"gpt2"

if is_tf_available():






  class 					_SCREAMING_SNAKE_CASE(   tf.Module  ):




         def __init__(						self		,SCREAMING_SNAKE_CASE__							)			->      List[str]:





               """simple docstring"""






               super().__init__()
               __SCREAMING_SNAKE_CASE   :int						=  tokenizer
               __SCREAMING_SNAKE_CASE   :List[str]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
               __SCREAMING_SNAKE_CASE   :Optional[int]						=  TFGPTaLMHeadModel.from_config(SCREAMING_SNAKE_CASE__							)




         @tf.function(input_signature=(tf.TensorSpec((None,)		,tf.string		,name='''text'''							),)							)
         def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__							)			->      int:





               """simple docstring"""






               __SCREAMING_SNAKE_CASE   :Optional[int]						=  self.tokenizer(SCREAMING_SNAKE_CASE__							)
               __SCREAMING_SNAKE_CASE   :int						=  tokenized['''input_ids'''].to_tensor()

               __SCREAMING_SNAKE_CASE   :List[Any]						=  tf.cast(input_ids_dense > 0		,tf.intaa							)
               # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN])

               __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  self.model(input_ids=SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__							)['''logits''']

               return outputs






@require_tf
@require_keras_nlp
class 					_SCREAMING_SNAKE_CASE(   unittest.TestCase  ):




       def     _UpperCamelCase     (						self							)			->      List[str]:





             """simple docstring"""






             super().setUp()

             __SCREAMING_SNAKE_CASE   :Tuple						=  [GPTaTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__							) for checkpoint in (TOKENIZER_CHECKPOINTS)]
             __SCREAMING_SNAKE_CASE   :Tuple						=  [TFGPTaTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__							) for checkpoint in TOKENIZER_CHECKPOINTS]
             assert len(self.tokenizers							) == len(self.tf_tokenizers							)

             __SCREAMING_SNAKE_CASE   :Any						=  [
                 '''This is a straightforward English test sentence.''',
                 '''This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.''',
                 '''Now we\'re going to add some Chinese: 一 二 三 一二三''',
                 '''And some much more rare Chinese: 齉 堃 齉堃''',
                 '''Je vais aussi écrire en français pour tester les accents''',
                 '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''',
             ]
             __SCREAMING_SNAKE_CASE   :List[str]						=  list(zip(self.test_sentences		,self.test_sentences[::-1]							)							)




       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             for tokenizer, tf_tokenizer in zip(self.tokenizers		,self.tf_tokenizers							):
                   for test_inputs in self.test_sentences:
                         __SCREAMING_SNAKE_CASE   :Tuple						=  tokenizer([test_inputs]		,return_tensors='''tf'''							)
                         __SCREAMING_SNAKE_CASE   :Optional[int]						=  tf_tokenizer([test_inputs]							)

                         for key in python_outputs.keys():
                               # convert them to numpy to avoid messing with ragged tensors
                               __SCREAMING_SNAKE_CASE   :Any						=  python_outputs[key].numpy()
                               __SCREAMING_SNAKE_CASE   :List[Any]						=  tf_outputs[key].numpy()

                               self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape							)							)
                               self.assertTrue(tf.reduce_all(tf.cast(SCREAMING_SNAKE_CASE__		,tf.intaa							) == tf_outputs_values							)							)




       @slow
       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             for tf_tokenizer in self.tf_tokenizers:
                   __SCREAMING_SNAKE_CASE   :List[str]						=  tf.function(SCREAMING_SNAKE_CASE__							)
                   for test_inputs in self.test_sentences:
                         __SCREAMING_SNAKE_CASE   :int						=  tf.constant(SCREAMING_SNAKE_CASE__							)
                         __SCREAMING_SNAKE_CASE   :Tuple						=  compiled_tokenizer(SCREAMING_SNAKE_CASE__							)
                         __SCREAMING_SNAKE_CASE   :List[str]						=  tf_tokenizer(SCREAMING_SNAKE_CASE__							)

                         for key in eager_outputs.keys():
                               self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]							)							)




       @slow
       def     _UpperCamelCase     (						self							)			->      Optional[int]:





             """simple docstring"""






             for tf_tokenizer in self.tf_tokenizers:
                   __SCREAMING_SNAKE_CASE   :Optional[Any]						=  ModelToSave(tokenizer=SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE   :int						=  tf.convert_to_tensor([self.test_sentences[0]]							)
                   __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  model.serving(SCREAMING_SNAKE_CASE__							)  # Build model with some sample inputs
                   with TemporaryDirectory() as tempdir:
                         __SCREAMING_SNAKE_CASE   :Any						=  Path(SCREAMING_SNAKE_CASE__							) / '''saved.model'''
                         tf.saved_model.save(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,signatures={'''serving_default''': model.serving}							)
                         __SCREAMING_SNAKE_CASE   :Optional[int]						=  tf.saved_model.load(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE   :Any						=  loaded_model.signatures['''serving_default'''](SCREAMING_SNAKE_CASE__							)['''output_0''']
                   # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
                   self.assertTrue(tf.reduce_all(out == loaded_output							)							)




       @slow
       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             for tf_tokenizer in self.tf_tokenizers:
                   __SCREAMING_SNAKE_CASE   :Any						=  tf.convert_to_tensor([self.test_sentences[0]]							)
                   __SCREAMING_SNAKE_CASE   :List[str]						=  tf_tokenizer(SCREAMING_SNAKE_CASE__							)  # Build model with some sample inputs

                   __SCREAMING_SNAKE_CASE   :Tuple						=  tf_tokenizer.get_config()
                   __SCREAMING_SNAKE_CASE   :Dict						=  TFGPTaTokenizer.from_config(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE   :Any						=  model_from_config(SCREAMING_SNAKE_CASE__							)

                   for key in from_config_output.keys():
                         self.assertTrue(tf.reduce_all(from_config_output[key] == out[key]							)							)




       @slow
       def     _UpperCamelCase     (						self							)			->      Optional[Any]:





             """simple docstring"""






             for tf_tokenizer in self.tf_tokenizers:
                   # for the test to run
                   __SCREAMING_SNAKE_CASE   :Tuple						=  12_31_23

                   for max_length in [3, 5, 10_24]:
                         __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  tf.convert_to_tensor([self.test_sentences[0]]							)
                         __SCREAMING_SNAKE_CASE   :List[Any]						=  tf_tokenizer(SCREAMING_SNAKE_CASE__		,max_length=SCREAMING_SNAKE_CASE__							)

                         __SCREAMING_SNAKE_CASE   :int						=  out['''input_ids'''].numpy().shape[1]

                         assert out_length == max_length






"""simple docstring"""
import qiskit



def      __lowerCamelCase   (							a_     :		int ,   a_     :		int							)			->      qiskit.result.counts.Counts:
      __SCREAMING_SNAKE_CASE   :Tuple						=  qiskit.Aer.get_backend('''aer_simulator'''							)

      # Create a Quantum Circuit acting on the q register
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  qiskit.QuantumCircuit(a_ ,   a_							)

      # Apply X (NOT) Gate to Qubits 0 & 1
      circuit.x(0							)
      circuit.x(1							)

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

      # Execute the circuit on the qasm simulator
      __SCREAMING_SNAKE_CASE   :Tuple						=  qiskit.execute(a_ ,   a_ ,   shots=10_00							)

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


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