
'''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.
import numpy as np
import torch

from ..models.clipseg import CLIPSegForImageSegmentation
from ..utils import is_vision_available, requires_backends
from .base import PipelineTool


if is_vision_available():
						from PIL import Image




class   UpperCAmelCase_					(		_SCREAMING_SNAKE_CASE			):






						'''simple docstring'''






						_lowercase		:				Optional[Any]									=       (
						    '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.'''
						    '''It takes two arguments named `image` which should be the original image, and `label` which should be a text '''
						    '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.'''
						)
						_lowercase		:				Optional[Any]									=       '''CIDAS/clipseg-rd64-refined'''
						_lowercase		:				Dict									=       '''image_segmenter'''
						_lowercase		:				Union[str, Any]									=       CLIPSegForImageSegmentation

						_lowercase		:				List[str]									=       ['''image''', '''text''']
						_lowercase		:				int									=       ['''image''']




						def __init__( self   ,       *_lowercase   ,       **_lowercase				):

												"""simple docstring"""



												requires_backends(self   ,       ["""vision"""]				)
												super().__init__(*_lowercase   ,       **_lowercase				)




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												return self.pre_processor(text=[label]   ,       images=[image]   ,       padding=_lowercase   ,       return_tensors="""pt"""				)




						def  _lowercase     ( self   ,       _lowercase				):

												"""simple docstring"""



												with torch.no_grad():
																		_lowerCAmelCase  						=			self.model(**_lowercase				).logits
												return logits




						def  _lowercase     ( self   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			outputs.cpu().detach().numpy()
												_lowerCAmelCase  						=			0
												_lowerCAmelCase  						=			1
												return Image.fromarray((array * 255).astype(np.uinta				)				)


'''simple docstring'''



import numpy as np
import qiskit


def A				(__lowerCamelCase				:int = 8			,     __lowerCamelCase				:int | None = None		):
						_lowerCAmelCase  						=			np.random.default_rng(seed=__lowerCamelCase		)

						# 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=__lowerCamelCase		)
						# The set of states Alice will prepare.
						_lowerCAmelCase  						=			rng.integers(2			,     size=__lowerCamelCase		)
						# Measurement basis for Bob's qubits.
						_lowerCAmelCase  						=			rng.integers(2			,     size=__lowerCamelCase		)

						# Quantum Circuit to simulate BB84
						_lowerCAmelCase  						=			qiskit.QuantumCircuit(__lowerCamelCase			,     name="""BB84"""		)

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

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

						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(__lowerCamelCase			,     __lowerCamelCase			,     shots=1			,     seed_simulator=__lowerCamelCase		)
						# Returns the result of measurement.
						_lowerCAmelCase  						=			job.result().get_counts(__lowerCamelCase		).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(
						            __lowerCamelCase			,     __lowerCamelCase			,     __lowerCamelCase		)
						        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(__lowerCamelCase		) >= key_len else gen_key.ljust(__lowerCamelCase			,     """0"""		)
						return key


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

						testmod()
