


'''simple docstring'''






import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class 						__UpperCamelCase	(						unittest.TestCase							):





							@property
							def 		a__   (  self		:int			):
									torch.manual_seed(0			)
									snake_case_		:							List[Any]			       =						UNetaDModel(
									    block_out_channels=(3_2, 6_4)  ,layers_per_block=2  ,sample_size=3_2  ,in_channels=3  ,out_channels=3  ,down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")  ,up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")  ,)
									return model





							def 		a__   (  self		:Dict			):
									snake_case_		:							str			       =						self.dummy_uncond_unet
									snake_case_		:							Dict			       =						PNDMScheduler()

									snake_case_		:							Optional[Any]			       =						PNDMPipeline(unet=_UpperCamelCase  ,scheduler=_UpperCamelCase			)
									pndm.to(_UpperCamelCase			)
									pndm.set_progress_bar_config(disable=_UpperCamelCase			)

									snake_case_		:							Dict			       =						torch.manual_seed(0			)
									snake_case_		:							Optional[Any]			       =						pndm(generator=_UpperCamelCase  ,num_inference_steps=2_0  ,output_type="""numpy"""			).images

									snake_case_		:							List[Any]			       =						torch.manual_seed(0			)
									snake_case_		:							List[str]			       =						pndm(generator=_UpperCamelCase  ,num_inference_steps=2_0  ,output_type="""numpy"""  ,return_dict=_UpperCamelCase			)[0]

									snake_case_		:							Union[str, Any]			       =						image[0, -3:, -3:, -1]
									snake_case_		:							Tuple			       =						image_from_tuple[0, -3:, -3:, -1]

									assert image.shape == (1, 3_2, 3_2, 3)
									snake_case_		:							str			       =						np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]			)

									assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2
									assert np.abs(image_from_tuple_slice.flatten() - expected_slice			).max() < 1E-2





@slow
@require_torch
class 						__UpperCamelCase	(						unittest.TestCase							):





							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							List[Any]			       =						"""google/ddpm-cifar10-32"""

									snake_case_		:							Union[str, Any]			       =						UNetaDModel.from_pretrained(_UpperCamelCase			)
									snake_case_		:							Optional[int]			       =						PNDMScheduler()

									snake_case_		:							Optional[int]			       =						PNDMPipeline(unet=_UpperCamelCase  ,scheduler=_UpperCamelCase			)
									pndm.to(_UpperCamelCase			)
									pndm.set_progress_bar_config(disable=_UpperCamelCase			)
									snake_case_		:							Union[str, Any]			       =						torch.manual_seed(0			)
									snake_case_		:							Any			       =						pndm(generator=_UpperCamelCase  ,output_type="""numpy"""			).images

									snake_case_		:							Dict			       =						image[0, -3:, -3:, -1]

									assert image.shape == (1, 3_2, 3_2, 3)
									snake_case_		:							Dict			       =						np.array([0.15_64, 0.1_46_45, 0.14_06, 0.1_47_15, 0.1_24_25, 0.1_40_45, 0.1_31_15, 0.1_21_75, 0.1_25]			)

									assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2



'''simple docstring'''






import numpy as np
import qiskit





def      UpperCAmelCase      (		lowerCamelCase_  :int = 8				,      lowerCamelCase_  :int | None = None ):







		'''simple docstring'''
		snake_case_		:							Tuple			       =						np.random.default_rng(seed=lowerCamelCase_ )

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

		# Quantum Circuit to simulate BB84
		snake_case_		:							Optional[int]			       =						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.
		snake_case_		:							Union[str, Any]			       =						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.
		snake_case_		:							Tuple			       =						qiskit.execute(lowerCamelCase_				,      lowerCamelCase_				,      shots=1				,      seed_simulator=lowerCamelCase_ )
		# Returns the result of measurement.
		snake_case_		:							Tuple			       =						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.
		snake_case_		:							Optional[Any]			       =						"""""".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.
		snake_case_		:							str			       =						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()