





'''simple docstring'''

import numpy as np
import qiskit







def    a__							(							lowercase   :	int = 8,   lowercase   :	int | None = None )						->						str:





     """simple docstring"""

     _UpperCamelCase							       =			np.random.default_rng(seed=lowercase )

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

     # Quantum Circuit to simulate BB84
     _UpperCamelCase							       =			qiskit.QuantumCircuit(lowercase,   name='''BB84''' )

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

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

     bbaa_circ.barrier()
     bbaa_circ.measure_all()

     # Simulate the quantum circuit.
     _UpperCamelCase							       =			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.
     _UpperCamelCase							       =			qiskit.execute(lowercase,   lowercase,   shots=1,   seed_simulator=lowercase )
     # Returns the result of measurement.
     _UpperCamelCase							       =			job.result().get_counts(lowercase ).most_frequent()

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

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


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

   testmod()










'''simple docstring'''

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
   from PIL import Image
class 							__lowerCAmelCase	(       __magic_name__					):






       """simple docstring"""
       _snake_case      :	str									=       'Salesforce/blip-image-captioning-base'
       _snake_case      :	Union[str, Any]									=       (
           'This is a tool that generates a description of an image. It takes an input named `image` which should be the '
           'image to caption, and returns a text that contains the description in English.'
       )
       _snake_case      :	List[Any]									=       'image_captioner'
       _snake_case      :	Union[str, Any]									=       AutoModelForVisionaSeq

       _snake_case      :	Dict									=       ['image']
       _snake_case      :	Optional[int]									=       ['text']





       def __init__(				self  :						Union[str, Any]			,					*lowerCAmelCase__  :						Optional[Any]			,					**lowerCAmelCase__  :						Optional[int]	)       ->	Tuple:

            '''simple docstring'''




            requires_backends(self			,					['''vision''']	)
            super().__init__(*lowerCAmelCase__			,					**lowerCAmelCase__	)





       def snake_case__		(				self  :						Tuple			,					lowerCAmelCase__  :						"Image"	)       ->	Optional[int]:

            '''simple docstring'''




            return self.pre_processor(images=lowerCAmelCase__			,					return_tensors='''pt'''	)





       def snake_case__		(				self  :						Any			,					lowerCAmelCase__  :						Union[str, Any]	)       ->	Union[str, Any]:

            '''simple docstring'''




            return self.model.generate(**lowerCAmelCase__	)







       def snake_case__		(				self  :						int			,					lowerCAmelCase__  :						List[str]	)       ->	List[str]:

            '''simple docstring'''




            return self.pre_processor.batch_decode(lowerCAmelCase__			,					skip_special_tokens=lowerCAmelCase__	)[0].strip()



