
import numpy as np
import qiskit



def      __lowerCAmelCase						(							UpperCAmelCase__				:      int = 8			,				UpperCAmelCase__				:      int | None = None       )  ->    str:
       lowerCamelCase_							  =					np.random.default_rng(seed=UpperCAmelCase__       )

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

       # Quantum Circuit to simulate BB84
       lowerCamelCase_							  =					qiskit.QuantumCircuit(UpperCAmelCase__			,				name="""BB84"""       )

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

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

       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(UpperCAmelCase__			,				UpperCAmelCase__			,				shots=1			,				seed_simulator=UpperCAmelCase__       )
       # Returns the result of measurement.
       lowerCamelCase_							  =					job.result().get_counts(UpperCAmelCase__       ).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(
                   UpperCAmelCase__			,				UpperCAmelCase__			,				UpperCAmelCase__       )
               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(UpperCAmelCase__       ) >= key_len else gen_key.ljust(UpperCAmelCase__			,				"""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_tf_available, is_torch_available


lowercase        =  {
    '''configuration_rag''': ['''RagConfig'''],
    '''retrieval_rag''': ['''RagRetriever'''],
    '''tokenization_rag''': ['''RagTokenizer'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase        =  [
		    '''RagModel''',
		    '''RagPreTrainedModel''',
		    '''RagSequenceForGeneration''',
		    '''RagTokenForGeneration''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase        =  [
		    '''TFRagModel''',
		    '''TFRagPreTrainedModel''',
		    '''TFRagSequenceForGeneration''',
		    '''TFRagTokenForGeneration''',
		]


if TYPE_CHECKING:
		from .configuration_rag import RagConfig
		from .retrieval_rag import RagRetriever
		from .tokenization_rag import RagTokenizer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_rag import (
				    TFRagModel,
				    TFRagPreTrainedModel,
				    TFRagSequenceForGeneration,
				    TFRagTokenForGeneration,
				)

else:
		import sys

		lowercase        =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





