

import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
       from PIL import Image
else:







       class 						__magic_name__			:

          '''simple docstring'''




          @staticmethod
          def  _UpperCAmelCase (				*lowercase_,    **lowercase_       )							->    Optional[Any]:






            """simple docstring"""






            pass







@is_pipeline_test
@require_vision
class 						__magic_name__			(							unittest.TestCase							):

   '''simple docstring'''




   @require_torch
   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






     a__  =pipeline(
         model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''',    )
     a__  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
     a__  =image_classifier(SCREAMING_SNAKE_CASE__,    candidate_labels=['''a''', '''b''', '''c''']       )

     # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
     # python and torch versions.
     self.assertIn(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [
             [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}],
             [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}],
         ],    )

     a__  =image_classifier([image] * 5,    candidate_labels=['''A''', '''B''', '''C'''],    batch_size=2       )
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
         ],    )



   @require_tf
   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






     a__  =pipeline(
         model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''',    framework='''tf'''       )
     a__  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
     a__  =image_classifier(SCREAMING_SNAKE_CASE__,    candidate_labels=['''a''', '''b''', '''c''']       )

     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}],    )

     a__  =image_classifier([image] * 5,    candidate_labels=['''A''', '''B''', '''C'''],    batch_size=2       )
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
             [
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
                 {'''score''': 0.333, '''label''': ANY(SCREAMING_SNAKE_CASE__       )},
             ],
         ],    )



   @slow
   @require_torch
   def  _UpperCAmelCase (				self       )							->    Union[str, Any]:






     """simple docstring"""






     a__  =pipeline(
         task='''zero-shot-image-classification''',    model='''openai/clip-vit-base-patch32''',    )
     # This is an image of 2 cats with remotes and no planes
     a__  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
     a__  =image_classifier(SCREAMING_SNAKE_CASE__,    candidate_labels=['''cat''', '''plane''', '''remote''']       )

     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [
             {'''score''': 0.511, '''label''': '''remote'''},
             {'''score''': 0.485, '''label''': '''cat'''},
             {'''score''': 0.004, '''label''': '''plane'''},
         ],    )

     a__  =image_classifier([image] * 5,    candidate_labels=['''cat''', '''plane''', '''remote'''],    batch_size=2       )
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [
             [
                 {'''score''': 0.511, '''label''': '''remote'''},
                 {'''score''': 0.485, '''label''': '''cat'''},
                 {'''score''': 0.004, '''label''': '''plane'''},
             ],
         ]
         * 5,    )



   @slow
   @require_tf
   def  _UpperCAmelCase (				self       )							->    List[str]:






     """simple docstring"""






     a__  =pipeline(
         task='''zero-shot-image-classification''',    model='''openai/clip-vit-base-patch32''',    framework='''tf'''       )
     # This is an image of 2 cats with remotes and no planes
     a__  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
     a__  =image_classifier(SCREAMING_SNAKE_CASE__,    candidate_labels=['''cat''', '''plane''', '''remote''']       )
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [
             {'''score''': 0.511, '''label''': '''remote'''},
             {'''score''': 0.485, '''label''': '''cat'''},
             {'''score''': 0.004, '''label''': '''plane'''},
         ],    )

     a__  =image_classifier([image] * 5,    candidate_labels=['''cat''', '''plane''', '''remote'''],    batch_size=2       )
     self.assertEqual(
         nested_simplify(SCREAMING_SNAKE_CASE__       ),    [
             [
                 {'''score''': 0.511, '''label''': '''remote'''},
                 {'''score''': 0.485, '''label''': '''cat'''},
                 {'''score''': 0.004, '''label''': '''plane'''},
             ],
         ]
         * 5,    )

'''simple docstring'''

import numpy as np
import qiskit







def      UpperCamelCase_						(					snake_case_       :       int = 8						,		snake_case_       :       int | None = None      )					->     str:






 '''simple docstring'''

 __lowerCAmelCase											=						np.random.default_rng(seed=snake_case_      )

 # Roughly 25% of the qubits will contribute to the key.
 # So we take more than we need.
 __lowerCAmelCase											=						6 * key_len
 # Measurement basis for Alice's qubits.
 __lowerCAmelCase											=						rng.integers(2						,		size=snake_case_      )
 # The set of states Alice will prepare.
 __lowerCAmelCase											=						rng.integers(2						,		size=snake_case_      )
 # Measurement basis for Bob's qubits.
 __lowerCAmelCase											=						rng.integers(2						,		size=snake_case_      )

 # Quantum Circuit to simulate BB84
 __lowerCAmelCase											=						qiskit.QuantumCircuit(snake_case_						,		name="""BB84"""      )

 # Alice prepares her qubits according to rules above.
 for index, _ in enumerate(snake_case_      ):
  if alice_state[index] == 1:
   bbaa_circ.x(snake_case_      )
  if alice_basis[index] == 1:
   bbaa_circ.h(snake_case_      )
 bbaa_circ.barrier()

 # Bob measures the received qubits according to rules above.
 for index, _ in enumerate(snake_case_      ):
  if bob_basis[index] == 1:
   bbaa_circ.h(snake_case_      )

 bbaa_circ.barrier()
 bbaa_circ.measure_all()

 # Simulate the quantum circuit.
 __lowerCAmelCase											=						qiskit.Aer.get_backend("""aer_simulator"""      )
 # We only need to run one shot because the key is unique.
 # Multiple shots will produce the same key.
 __lowerCAmelCase											=						qiskit.execute(snake_case_						,		snake_case_						,		shots=1						,		seed_simulator=snake_case_      )
 # Returns the result of measurement.
 __lowerCAmelCase											=						job.result().get_counts(snake_case_      ).most_frequent()

 # Extracting the generated key from the simulation results.
 # Only keep measurement results where Alice and Bob chose the same basis.
 __lowerCAmelCase											=						"""""".join(
     [
         result_bit
         for alice_basis_bit, bob_basis_bit, result_bit in zip(
             snake_case_						,		snake_case_						,		snake_case_      )
         if alice_basis_bit == bob_basis_bit
     ]      )

 # Get final key. Pad with 0 if too short, otherwise truncate.
 __lowerCAmelCase											=						gen_key[:key_len] if len(snake_case_      ) >= key_len else gen_key.ljust(snake_case_						,		"""0"""      )
 return key


if __name__ == "__main__":
 print(f'The generated key is : {bbaa(8, seed=0)}')
 from doctest import testmod

 testmod()






