from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import numpy
    import tensorflow as tf

    from transformers import (
        TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        BertConfig,
        DPRConfig,
        TFDPRContextEncoder,
        TFDPRQuestionEncoder,
        TFDPRReader,
    )


class 			lowerCamelCase_  :






   '''simple docstring'''






   def __init__(			self				,				__lowercase				,				__lowercase=13				,				__lowercase=7				,				__lowercase=True				,				__lowercase=True				,				__lowercase=True				,				__lowercase=True				,				__lowercase=99				,				__lowercase=32				,				__lowercase=2				,				__lowercase=4				,				__lowercase=37				,				__lowercase="gelu"				,				__lowercase=0.1				,				__lowercase=0.1				,				__lowercase=512				,				__lowercase=16				,				__lowercase=2				,				__lowercase=0.02				,				__lowercase=3				,				__lowercase=4				,				__lowercase=None				,				__lowercase=0				,				) ->			List[Any]:
       __UpperCamelCase  :str    						=	parent
       __UpperCamelCase  :List[Any]    						=	batch_size
       __UpperCamelCase  :List[Any]    						=	seq_length
       __UpperCamelCase  :int    						=	is_training
       __UpperCamelCase  :List[Any]    						=	use_input_mask
       __UpperCamelCase  :int    						=	use_token_type_ids
       __UpperCamelCase  :Tuple    						=	use_labels
       __UpperCamelCase  :Union[str, Any]    						=	vocab_size
       __UpperCamelCase  :Tuple    						=	hidden_size
       __UpperCamelCase  :Optional[int]    						=	num_hidden_layers
       __UpperCamelCase  :Any    						=	num_attention_heads
       __UpperCamelCase  :List[str]    						=	intermediate_size
       __UpperCamelCase  :Tuple    						=	hidden_act
       __UpperCamelCase  :Any    						=	hidden_dropout_prob
       __UpperCamelCase  :Optional[int]    						=	attention_probs_dropout_prob
       __UpperCamelCase  :Union[str, Any]    						=	max_position_embeddings
       __UpperCamelCase  :Optional[Any]    						=	type_vocab_size
       __UpperCamelCase  :int    						=	type_sequence_label_size
       __UpperCamelCase  :Union[str, Any]    						=	initializer_range
       __UpperCamelCase  :List[str]    						=	num_labels
       __UpperCamelCase  :Optional[Any]    						=	num_choices
       __UpperCamelCase  :List[Any]    						=	scope
       __UpperCamelCase  :str    						=	projection_dim







   def     UpperCamelCase__     (			self) ->			str:
       __UpperCamelCase  :int    						=	ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size)

       __UpperCamelCase  :Tuple    						=	None
       if self.use_input_mask:
           # follow test_modeling_tf_ctrl.py
           __UpperCamelCase  :Optional[int]    						=	random_attention_mask([self.batch_size, self.seq_length])

       __UpperCamelCase  :Optional[int]    						=	None
       if self.use_token_type_ids:
           __UpperCamelCase  :List[Any]    						=	ids_tensor([self.batch_size, self.seq_length]				,				self.type_vocab_size)

       __UpperCamelCase  :Optional[int]    						=	None
       __UpperCamelCase  :List[str]    						=	None
       __UpperCamelCase  :Optional[Any]    						=	None
       if self.use_labels:
           __UpperCamelCase  :Tuple    						=	ids_tensor([self.batch_size]				,				self.type_sequence_label_size)
           __UpperCamelCase  :Optional[int]    						=	ids_tensor([self.batch_size, self.seq_length]				,				self.num_labels)
           __UpperCamelCase  :List[str]    						=	ids_tensor([self.batch_size]				,				self.num_choices)

       __UpperCamelCase  :Optional[Any]    						=	BertConfig(
           vocab_size=self.vocab_size				,				hidden_size=self.hidden_size				,				num_hidden_layers=self.num_hidden_layers				,				num_attention_heads=self.num_attention_heads				,				intermediate_size=self.intermediate_size				,				hidden_act=self.hidden_act				,				hidden_dropout_prob=self.hidden_dropout_prob				,				attention_probs_dropout_prob=self.attention_probs_dropout_prob				,				max_position_embeddings=self.max_position_embeddings				,				type_vocab_size=self.type_vocab_size				,				is_decoder=__lowercase				,				initializer_range=self.initializer_range				,				)
       __UpperCamelCase  :Dict    						=	DPRConfig(projection_dim=self.projection_dim				,				**config.to_dict())

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







   def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase) ->			int:
       __UpperCamelCase  :Tuple    						=	TFDPRContextEncoder(config=__lowercase)
       __UpperCamelCase  :Tuple    						=	model(__lowercase				,				attention_mask=__lowercase				,				token_type_ids=__lowercase)
       __UpperCamelCase  :Tuple    						=	model(__lowercase				,				token_type_ids=__lowercase)
       __UpperCamelCase  :str    						=	model(__lowercase)
       self.parent.assertEqual(result.pooler_output.shape				,				(self.batch_size, self.projection_dim or self.hidden_size))







   def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase) ->			List[str]:
       __UpperCamelCase  :int    						=	TFDPRQuestionEncoder(config=__lowercase)
       __UpperCamelCase  :int    						=	model(__lowercase				,				attention_mask=__lowercase				,				token_type_ids=__lowercase)
       __UpperCamelCase  :int    						=	model(__lowercase				,				token_type_ids=__lowercase)
       __UpperCamelCase  :Optional[Any]    						=	model(__lowercase)
       self.parent.assertEqual(result.pooler_output.shape				,				(self.batch_size, self.projection_dim or self.hidden_size))







   def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase) ->			List[Any]:
       __UpperCamelCase  :List[Any]    						=	TFDPRReader(config=__lowercase)
       __UpperCamelCase  :Dict    						=	model(__lowercase				,				attention_mask=__lowercase)

       self.parent.assertEqual(result.start_logits.shape				,				(self.batch_size, self.seq_length))
       self.parent.assertEqual(result.end_logits.shape				,				(self.batch_size, self.seq_length))
       self.parent.assertEqual(result.relevance_logits.shape				,				(self.batch_size,))







   def     UpperCamelCase__     (			self) ->			Optional[Any]:
       __UpperCamelCase  :Optional[int]    						=	self.prepare_config_and_inputs()
       (
           __UpperCamelCase    
       )  :List[Any]    						=	config_and_inputs
       __UpperCamelCase  :Optional[int]    						=	{'input_ids': input_ids}
       return config, inputs_dict


@require_tf
class 			lowerCamelCase_  (     __A		,			__A		,			unittest.TestCase				):






   '''simple docstring'''






   a__			:		List[str]      			=   (
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   a__			:		Optional[Any]      			=   {"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

   a__			:		str      			=   False
   a__			:		Optional[Any]      			=   False
   a__			:		List[str]      			=   False
   a__			:		Dict      			=   False
   a__			:		List[str]      			=   False







   def     UpperCamelCase__     (			self) ->			str:
       __UpperCamelCase  :Dict    						=	TFDPRModelTester(self)
       __UpperCamelCase  :Optional[Any]    						=	ConfigTester(self				,				config_class=__lowercase				,				hidden_size=37)







   def     UpperCamelCase__     (			self) ->			Dict:
       self.config_tester.run_common_tests()







   def     UpperCamelCase__     (			self) ->			str:
       __UpperCamelCase  :Union[str, Any]    						=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*__lowercase)







   def     UpperCamelCase__     (			self) ->			int:
       __UpperCamelCase  :Optional[int]    						=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*__lowercase)







   def     UpperCamelCase__     (			self) ->			List[Any]:
       __UpperCamelCase  :str    						=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*__lowercase)







   @slow
   def     UpperCamelCase__     (			self) ->			Dict:





       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __UpperCamelCase  :str    						=	TFDPRContextEncoder.from_pretrained(__lowercase)
           self.assertIsNotNone(__lowercase)

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __UpperCamelCase  :Optional[int]    						=	TFDPRContextEncoder.from_pretrained(__lowercase)
           self.assertIsNotNone(__lowercase)

       for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __UpperCamelCase  :List[Any]    						=	TFDPRQuestionEncoder.from_pretrained(__lowercase)
           self.assertIsNotNone(__lowercase)

       for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __UpperCamelCase  :Optional[Any]    						=	TFDPRReader.from_pretrained(__lowercase)
           self.assertIsNotNone(__lowercase)


@require_tf
class 			lowerCamelCase_  (     unittest.TestCase				):






   '''simple docstring'''






   @slow
   def     UpperCamelCase__     (			self) ->			Tuple:
       __UpperCamelCase  :Optional[Any]    						=	TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base''')

       __UpperCamelCase  :Tuple    						=	tf.constant(
           [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]])  # [CLS] hello, is my dog cute? [SEP]
       __UpperCamelCase  :List[str]    						=	model(__lowercase)[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       __UpperCamelCase  :List[str]    						=	tf.constant(
           [
               [
                   0.03_23_62_53,
                   0.12_75_33_35,
                   0.16_81_85_09,
                   0.00_27_97_86,
                   0.3_89_69_33,
                   0.24_26_49_45,
                   0.2_17_89_71,
                   -0.02_33_52_27,
                   -0.08_48_19_59,
                   -0.14_32_41_17,
               ]
           ])
       self.assertTrue(numpy.allclose(output[:, :10].numpy()				,				expected_slice.numpy()				,				atol=1E-4))






import qiskit



def lowerCamelCase						(     SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			):
    '''simple docstring'''







    __UpperCamelCase  :Dict    						=	qiskit.Aer.get_backend('''aer_simulator'''			)

    __UpperCamelCase  :Tuple    						=	qiskit.QuantumCircuit(4						,    2			)
    # encode inputs in qubits 0 and 1
    if bita == 1:
        qc_ha.x(0			)
    if bita == 1:
        qc_ha.x(1			)
    qc_ha.barrier()

    # use cnots to write XOR of the inputs on qubit2
    qc_ha.cx(0						,    2			)
    qc_ha.cx(1						,    2			)

    # use ccx / toffoli gate to write AND of the inputs on qubit3
    qc_ha.ccx(0						,    1						,    3			)
    qc_ha.barrier()

    # extract outputs
    qc_ha.measure(2						,    0			)  # extract XOR value
    qc_ha.measure(3						,    1			)  # extract AND value

    # Execute the circuit on the qasm simulator
    __UpperCamelCase  :Optional[Any]    						=	qiskit.execute(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    shots=1_000			)

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


if __name__ == "__main__":
    __lowercase											=      half_adder(1, 1)
    print(F'Half Adder Output Qubit Counts: {counts}')





