


import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
   from PIL import Image

   from transformers.image_utils import load_image
else:
   class       UpperCAmelCase_   :

      '''simple docstring'''




      @staticmethod
      def _snake_case       (						*__A		,							**__A					):






        """simple docstring"""





        pass






   def 						lowercase_(    SCREAMING_SNAKE_CASE_      ):


     '''simple docstring'''

     return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
_snake_case			 =	(
    '''https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'''
)
@is_pipeline_test
@require_torch
@require_vision
class       UpperCAmelCase_   (   unittest.TestCase       ):

   '''simple docstring'''




   __A					: List[Any]							=						MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






   @require_pytesseract
   @require_vision
   def _snake_case       (						self		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Dict       =							pipeline(
         "document-question-answering"		,							model=lowerCamelCase_		,							tokenizer=lowerCamelCase_		,							image_processor=lowerCamelCase_					)

     lowerCamelCase					:   List[str]       =							INVOICE_URL
     lowerCamelCase					:   Any       =							list(zip(*apply_tesseract(load_image(lowerCamelCase_					)		,							lowerCamelCase_		,							""					)					)					)
     lowerCamelCase					:   Union[str, Any]       =							"What is the placebo?"
     lowerCamelCase					:   Dict       =							[
         {
             "image": load_image(lowerCamelCase_					),
             "question": question,
         },
         {
             "image": image,
             "question": question,
         },
         {
             "image": image,
             "question": question,
             "word_boxes": word_boxes,
         },
     ]
     return dqa_pipeline, examples






   def _snake_case       (						self		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   List[str]       =							dqa_pipeline(lowerCamelCase_		,							top_k=2					)
     self.assertEqual(
         lowerCamelCase_		,							[
             [
                 {"score": ANY(lowerCamelCase_					), "answer": ANY(lowerCamelCase_					), "start": ANY(lowerCamelCase_					), "end": ANY(lowerCamelCase_					)},
                 {"score": ANY(lowerCamelCase_					), "answer": ANY(lowerCamelCase_					), "start": ANY(lowerCamelCase_					), "end": ANY(lowerCamelCase_					)},
             ]
         ]
         * 3		,							)






   @require_torch
   @require_detectrona
   @require_pytesseract
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   List[str]       =							pipeline("document-question-answering"		,							model="hf-internal-testing/tiny-random-layoutlmv2"					)
     lowerCamelCase					:   List[Any]       =							INVOICE_URL
     lowerCamelCase					:   Union[str, Any]       =							"How many cats are there?"

     lowerCamelCase					:   List[str]       =							[
         {"score": 0.0001, "answer": "oy 2312/2019", "start": 38, "end": 39},
         {"score": 0.0001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40},
     ]
     lowerCamelCase					:   Optional[Any]       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(nested_simplify(lowerCamelCase_		,							decimals=4					)		,							lowerCamelCase_					)

     lowerCamelCase					:   Tuple       =							dqa_pipeline({"image": image, "question": question}		,							top_k=2					)
     self.assertEqual(nested_simplify(lowerCamelCase_		,							decimals=4					)		,							lowerCamelCase_					)

     # This image does not detect ANY text in it, meaning layoutlmv2 should fail.
     # Empty answer probably
     lowerCamelCase					:   Optional[int]       =							"./tests/fixtures/tests_samples/COCO/000000039769.png"
     lowerCamelCase					:   Union[str, Any]       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(lowerCamelCase_		,							[]					)

     # We can optionnally pass directly the words and bounding boxes
     lowerCamelCase					:   Union[str, Any]       =							"./tests/fixtures/tests_samples/COCO/000000039769.png"
     lowerCamelCase					:   List[Any]       =							[]
     lowerCamelCase					:   Dict       =							[]
     lowerCamelCase					:   Optional[int]       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							words=lowerCamelCase_		,							boxes=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(lowerCamelCase_		,							[]					)






   @slow
   @require_torch
   @require_detectrona
   @require_pytesseract
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							pipeline(
         "document-question-answering"		,							model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"		,							revision="9977165"		,							)
     lowerCamelCase					:   List[Any]       =							INVOICE_URL
     lowerCamelCase					:   str       =							"What is the invoice number?"

     lowerCamelCase					:   Any       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
             {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
         ]		,							)

     lowerCamelCase					:   Tuple       =							dqa_pipeline({"image": image, "question": question}		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
             {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
         ]		,							)

     lowerCamelCase					:   Any       =							dqa_pipeline(
         [{"image": image, "question": question}, {"image": image, "question": question}]		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             [
                 {"score": 0.9944, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.0009, "answer": "us-001", "start": 16, "end": 16},
             ],
         ]
         * 2		,							)






   @slow
   @require_torch
   @require_detectrona
   @require_pytesseract
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							pipeline(
         "document-question-answering"		,							model="tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"		,							revision="9977165"		,							max_seq_len=50		,							)
     lowerCamelCase					:   Tuple       =							INVOICE_URL
     lowerCamelCase					:   List[str]       =							"What is the invoice number?"

     lowerCamelCase					:   Any       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
             {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
         ]		,							)

     lowerCamelCase					:   int       =							dqa_pipeline({"image": image, "question": question}		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
             {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
         ]		,							)

     lowerCamelCase					:   Optional[int]       =							dqa_pipeline(
         [{"image": image, "question": question}, {"image": image, "question": question}]		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             [
                 {"score": 0.9974, "answer": "1110212019", "start": 23, "end": 23},
                 {"score": 0.9948, "answer": "us-001", "start": 16, "end": 16},
             ]
         ]
         * 2		,							)






   @slow
   @require_torch
   @require_pytesseract
   @require_vision
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Dict       =							AutoTokenizer.from_pretrained(
         "impira/layoutlm-document-qa"		,							revision="3dc6de3"		,							add_prefix_space=lowerCamelCase_					)
     lowerCamelCase					:   Dict       =							pipeline(
         "document-question-answering"		,							model="impira/layoutlm-document-qa"		,							tokenizer=lowerCamelCase_		,							revision="3dc6de3"		,							)
     lowerCamelCase					:   List[str]       =							INVOICE_URL
     lowerCamelCase					:   Union[str, Any]       =							"What is the invoice number?"

     lowerCamelCase					:   Any       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
             {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
         ]		,							)

     lowerCamelCase					:   Optional[Any]       =							dqa_pipeline({"image": image, "question": question}		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
             {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
         ]		,							)

     lowerCamelCase					:   List[str]       =							dqa_pipeline(
         [{"image": image, "question": question}, {"image": image, "question": question}]		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             [
                 {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
             ]
         ]
         * 2		,							)

     lowerCamelCase					:   List[str]       =							list(zip(*apply_tesseract(load_image(lowerCamelCase_					)		,							lowerCamelCase_		,							""					)					)					)

     # This model should also work if `image` is set to None
     lowerCamelCase					:   str       =							dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.4251, "answer": "us-001", "start": 16, "end": 16},
             {"score": 0.0819, "answer": "1110212019", "start": 23, "end": 23},
         ]		,							)






   @slow
   @require_torch
   @require_pytesseract
   @require_vision
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   int       =							AutoTokenizer.from_pretrained(
         "impira/layoutlm-document-qa"		,							revision="3dc6de3"		,							add_prefix_space=lowerCamelCase_					)
     lowerCamelCase					:   List[str]       =							pipeline(
         "document-question-answering"		,							model="impira/layoutlm-document-qa"		,							tokenizer=lowerCamelCase_		,							revision="3dc6de3"		,							max_seq_len=50		,							)
     lowerCamelCase					:   List[str]       =							INVOICE_URL
     lowerCamelCase					:   str       =							"What is the invoice number?"

     lowerCamelCase					:   List[str]       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
             {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
         ]		,							)

     lowerCamelCase					:   int       =							dqa_pipeline(
         [{"image": image, "question": question}, {"image": image, "question": question}]		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             [
                 {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
                 {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
             ]
         ]
         * 2		,							)

     lowerCamelCase					:   Optional[Any]       =							list(zip(*apply_tesseract(load_image(lowerCamelCase_					)		,							lowerCamelCase_		,							""					)					)					)

     # This model should also work if `image` is set to None
     lowerCamelCase					:   Optional[Any]       =							dqa_pipeline({"image": None, "word_boxes": word_boxes, "question": question}		,							top_k=2					)
     self.assertEqual(
         nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[
             {"score": 0.9999, "answer": "us-001", "start": 16, "end": 16},
             {"score": 0.9998, "answer": "us-001", "start": 16, "end": 16},
         ]		,							)






   @slow
   @require_torch
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Union[str, Any]       =							pipeline(
         "document-question-answering"		,							model="naver-clova-ix/donut-base-finetuned-docvqa"		,							tokenizer=AutoTokenizer.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa"					)		,							feature_extractor="naver-clova-ix/donut-base-finetuned-docvqa"		,							)

     lowerCamelCase					:   List[str]       =							INVOICE_URL
     lowerCamelCase					:   Dict       =							"What is the invoice number?"
     lowerCamelCase					:   int       =							dqa_pipeline(image=lowerCamelCase_		,							question=lowerCamelCase_		,							top_k=2					)
     self.assertEqual(nested_simplify(lowerCamelCase_		,							decimals=4					)		,							[{"answer": "us-001"}]					)






   @require_tf
   @unittest.skip("Document question answering not implemented in TF"					)
   def _snake_case       (						self					):






     """simple docstring"""





     pass






"""simple docstring"""


import qiskit



def   _A					(	_a	:						int     ,   _a	:						int				):



      """simple docstring"""






      A				  =		qiskit.Aer.get_backend("""aer_simulator"""				)

      # Create a Quantum Circuit acting on the q register
      A				  =		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
      A				  =		qiskit.execute(_a     ,   _a     ,   shots=1_0_0_0				)

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


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




