


'''simple docstring'''



import numpy as np
import qiskit






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   int = 8						,      _SCREAMING_SNAKE_CASE      :   int | None = None   ):
					__a			:       int					    =  np.random.default_rng(seed=_SCREAMING_SNAKE_CASE   )

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

					# Quantum Circuit to simulate BB84
					__a			:       Union[str, Any]					    =  qiskit.QuantumCircuit(_SCREAMING_SNAKE_CASE						,      name='BB84'   )

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

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

					bbaa_circ.barrier()
					bbaa_circ.measure_all()

					# Simulate the quantum circuit.
					__a			:       str					    =  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.
					__a			:       List[Any]					    =  qiskit.execute(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE						,      shots=1						,      seed_simulator=_SCREAMING_SNAKE_CASE   )
					# Returns the result of measurement.
					__a			:       Tuple					    =  job.result().get_counts(_SCREAMING_SNAKE_CASE   ).most_frequent()

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

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


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

	testmod()







'''simple docstring'''



import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment





@require_huggingface_suite
class 							__UpperCamelCase				(		unittest.TestCase					):




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[Any]					    =  inspect.getfile(accelerate.test_utils							)
						__a			:       List[str]					    =  os.path.sep.join(
						    mod_file.split(os.path.sep							)[:-1] + ['scripts', 'external_deps', 'test_metrics.py']							)

						from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

						__a			:       Union[str, Any]					    =  test_metrics




	@require_cpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						debug_launcher(self.test_metrics.main						,				num_processes=1							)




	@require_cpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						debug_launcher(self.test_metrics.main							)




	@require_single_gpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						self.test_metrics.main()





	@require_multi_gpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						print(f"""Found {torch.cuda.device_count()} devices."""							)
						__a			:       List[Any]					    =  ['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path]
						with patch_environment(omp_num_threads=1							):
											execute_subprocess_async(__a						,				env=os.environ.copy()							)



