






import numpy as np
import qiskit



def  a__ (__lowercase							:int = 8					,  __lowercase							:int | None = None   )		-> str:
				_A	:				str   =      np.random.default_rng(seed=__lowercase   )

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

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











import warnings


warnings.warn(
    'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: '
    '`from accelerate import find_executable_batch_size` to avoid this warning.',
    FutureWarning,
)



