




"""simple docstring"""






import numpy as np
import qiskit







def      _snake_case			(				lowercase__ = 8							,    lowercase__ = None		):
				_lowerCamelCase     :			str    =    np.random.default_rng(seed=lowercase__		)

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

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


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCAmelCase_       	=	{'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']}

try:
					if not is_sentencepiece_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	['''XGLMTokenizer''']

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	['''XGLMTokenizerFast''']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	[
					    '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''XGLMForCausalLM''',
					    '''XGLMModel''',
					    '''XGLMPreTrainedModel''',
					]


try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	[
					    '''FlaxXGLMForCausalLM''',
					    '''FlaxXGLMModel''',
					    '''FlaxXGLMPreTrainedModel''',
					]


try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCAmelCase_       	=	[
					    '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TFXGLMForCausalLM''',
					    '''TFXGLMModel''',
					    '''TFXGLMPreTrainedModel''',
					]


if TYPE_CHECKING:
					from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

					try:
										if not is_sentencepiece_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_xglm import XGLMTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_xglm_fast import XGLMTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_xglm import (
										    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFXGLMForCausalLM,
										    TFXGLMModel,
										    TFXGLMPreTrainedModel,
										)


else:
					import sys

					lowerCAmelCase_       	=	_LazyModule(__name__, globals()['''__file__'''], _import_structure)