





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    __snake_case	:





             '''simple docstring'''







             @staticmethod
             def 							UpperCAmelCase__			(  *A			:   str							,    **A			:   List[Any]		):
               pass







      def       A__							(   SCREAMING_SNAKE_CASE__)       ->   Optional[Any]:
        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.
__UpperCAmelCase	:       Tuple    	=							(
    "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"
)







@is_pipeline_test
@require_torch
@require_vision
class    __snake_case	(							unittest.TestCase      ):





       '''simple docstring'''







       lowerCAmelCase__						=					MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING




       @require_pytesseract
       @require_vision
       def 							UpperCAmelCase__			(  self			:   Optional[int]							,    A			:   Union[str, Any]							,    A			:   Tuple							,    A			:   str		):
         __snake_case:						str			  =						pipeline(
             """document-question-answering"""							,    model=A							,    tokenizer=A							,    image_processor=A		)

         __snake_case:						Dict			  =						INVOICE_URL
         __snake_case:						Optional[Any]			  =						list(zip(*apply_tesseract(load_image(A		)							,    A							,    """"""		)		)		)
         __snake_case:						Optional[int]			  =						"""What is the placebo?"""
         __snake_case:						Optional[Any]			  =						[
             {
                 """image""": load_image(A		),
                 """question""": question,
             },
             {
                 """image""": image,
                 """question""": question,
             },
             {
                 """image""": image,
                 """question""": question,
                 """word_boxes""": word_boxes,
             },
         ]
         return dqa_pipeline, examples




       def 							UpperCAmelCase__			(  self			:   int							,    A			:   Optional[Any]							,    A			:   Tuple		):
         __snake_case:						Tuple			  =						dqa_pipeline(A							,    top_k=2		)
         self.assertEqual(
             A							,    [
                 [
                     {"""score""": ANY(A		), """answer""": ANY(A		), """start""": ANY(A		), """end""": ANY(A		)},
                     {"""score""": ANY(A		), """answer""": ANY(A		), """start""": ANY(A		), """end""": ANY(A		)},
                 ]
             ]
             * 3							,    )




       @require_torch
       @require_detectrona
       @require_pytesseract
       def 							UpperCAmelCase__			(  self			:   Union[str, Any]		):
         __snake_case:						Optional[Any]			  =						pipeline("""document-question-answering"""							,    model="""hf-internal-testing/tiny-random-layoutlmv2"""		)
         __snake_case:						Optional[int]			  =						INVOICE_URL
         __snake_case:						List[str]			  =						"""How many cats are there?"""

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

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

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

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




       @slow
       @require_torch
       @require_detectrona
       @require_pytesseract
       def 							UpperCAmelCase__			(  self			:   List[Any]		):
         __snake_case:						int			  =						pipeline(
             """document-question-answering"""							,    model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""							,    revision="""9977165"""							,    )
         __snake_case:						Tuple			  =						INVOICE_URL
         __snake_case:						List[str]			  =						"""What is the invoice number?"""

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

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

         __snake_case:						Any			  =						dqa_pipeline(
             [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]							,    top_k=2		)
         self.assertEqual(
             nested_simplify(A							,    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 							UpperCAmelCase__			(  self			:   str		):
         __snake_case:						Dict			  =						pipeline(
             """document-question-answering"""							,    model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""							,    revision="""9977165"""							,    max_seq_len=50							,    )
         __snake_case:						int			  =						INVOICE_URL
         __snake_case:						int			  =						"""What is the invoice number?"""

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

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

         __snake_case:						Any			  =						dqa_pipeline(
             [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]							,    top_k=2		)
         self.assertEqual(
             nested_simplify(A							,    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 							UpperCAmelCase__			(  self			:   Any		):
         __snake_case:						Tuple			  =						AutoTokenizer.from_pretrained(
             """impira/layoutlm-document-qa"""							,    revision="""3dc6de3"""							,    add_prefix_space=A		)
         __snake_case:						Optional[Any]			  =						pipeline(
             """document-question-answering"""							,    model="""impira/layoutlm-document-qa"""							,    tokenizer=A							,    revision="""3dc6de3"""							,    )
         __snake_case:						List[str]			  =						INVOICE_URL
         __snake_case:						Dict			  =						"""What is the invoice number?"""

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

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

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

         __snake_case:						int			  =						list(zip(*apply_tesseract(load_image(A		)							,    A							,    """"""		)		)		)

         # This model should also work if `image` is set to None
         __snake_case:						Optional[Any]			  =						dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}							,    top_k=2		)
         self.assertEqual(
             nested_simplify(A							,    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 							UpperCAmelCase__			(  self			:   List[str]		):
         __snake_case:						Union[str, Any]			  =						AutoTokenizer.from_pretrained(
             """impira/layoutlm-document-qa"""							,    revision="""3dc6de3"""							,    add_prefix_space=A		)
         __snake_case:						Dict			  =						pipeline(
             """document-question-answering"""							,    model="""impira/layoutlm-document-qa"""							,    tokenizer=A							,    revision="""3dc6de3"""							,    max_seq_len=50							,    )
         __snake_case:						Tuple			  =						INVOICE_URL
         __snake_case:						Optional[Any]			  =						"""What is the invoice number?"""

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

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

         __snake_case:						Optional[Any]			  =						list(zip(*apply_tesseract(load_image(A		)							,    A							,    """"""		)		)		)

         # This model should also work if `image` is set to None
         __snake_case:						Optional[int]			  =						dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}							,    top_k=2		)
         self.assertEqual(
             nested_simplify(A							,    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 							UpperCAmelCase__			(  self			:   Tuple		):
         __snake_case:						Tuple			  =						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"""							,    )

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






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













import math

import numpy as np
import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute







def       A__							(   SCREAMING_SNAKE_CASE__ = 3)       ->   qiskit.result.counts.Counts:
  if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__):
    raise TypeError("""number of qubits must be a integer.""")
  if number_of_qubits <= 0:
    raise ValueError("""number of qubits must be > 0.""")
  if math.floor(SCREAMING_SNAKE_CASE__) != number_of_qubits:
    raise ValueError("""number of qubits must be exact integer.""")
  if number_of_qubits > 10:
    raise ValueError("""number of qubits too large to simulate(>10).""")

  __snake_case:						int			  =						QuantumRegister(SCREAMING_SNAKE_CASE__ , """qr""")
  __snake_case:						List[str]			  =						ClassicalRegister(SCREAMING_SNAKE_CASE__ , """cr""")

  __snake_case:						Optional[Any]			  =						QuantumCircuit(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__)

  __snake_case:						Tuple			  =						number_of_qubits

  for i in range(SCREAMING_SNAKE_CASE__):
    quantum_circuit.h(number_of_qubits - i - 1)
    counter -= 1
    for j in range(SCREAMING_SNAKE_CASE__):
      quantum_circuit.cp(np.pi / 2 ** (counter - j) , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__)

  for k in range(number_of_qubits // 2):
    quantum_circuit.swap(SCREAMING_SNAKE_CASE__ , number_of_qubits - k - 1)

  # measure all the qubits
  quantum_circuit.measure(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__)
  # simulate with 10000 shots
  __snake_case:						Union[str, Any]			  =						Aer.get_backend("""qasm_simulator""")
  __snake_case:						Optional[Any]			  =						execute(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , shots=1_0000)

  return job.result().get_counts(SCREAMING_SNAKE_CASE__)


if __name__ == "__main__":
      print(
          f'Total count for quantum fourier transform state is: \
    {quantum_fourier_transform(3)}'
      )






