



import numpy as np
import qiskit






def     __a					(		__UpperCAmelCase = 8							,  __UpperCAmelCase = None       ):
 a__												= np.random.default_rng(seed=__UpperCAmelCase       )

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

 # Quantum Circuit to simulate BB84
 a__												= 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.
 a__												= 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__												= qiskit.execute(__UpperCAmelCase							,  __UpperCAmelCase							,  shots=1							,  seed_simulator=__UpperCAmelCase       )
 # Returns the result of measurement.
 a__												= 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.
 a__												= ''''''.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.
 a__												= 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()










a_      :      str    =     6_55_21






def     __a					(		__UpperCAmelCase       ):
 a__												= 1
 a__												= 0
 for plain_chr in plain_text:
  a__												= (a + ord(__UpperCAmelCase       )) % MOD_ADLER
  a__												= (b + a) % MOD_ADLER
 return (b << 16) | a





