



'''simple docstring'''





import numpy as np
import qiskit




def 							lowerCamelCase	(					_snake_case	:    int = 8  ,_snake_case	:    int | None = None							):





    '''simple docstring'''


    lowercase__									=    np.random.default_rng(seed=_snake_case							)

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

    # Quantum Circuit to simulate BB84
    lowercase__									=    qiskit.QuantumCircuit(_snake_case  ,name="BB84"							)

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

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

    bbaa_circ.barrier()
    bbaa_circ.measure_all()

    # Simulate the quantum circuit.
    lowercase__									=    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.
    lowercase__									=    qiskit.execute(_snake_case  ,_snake_case  ,shots=1  ,seed_simulator=_snake_case							)
    # Returns the result of measurement.
    lowercase__									=    job.result().get_counts(_snake_case							).most_frequent()

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

    # Get final key. Pad with 0 if too short, otherwise truncate.
    lowercase__									=    gen_key[:key_len] if len(_snake_case							) >= key_len else gen_key.ljust(_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'''





from typing import Optional, Union

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models.modeling_utils import ModelMixin


class 					snake_case			(UpperCamelCase		,   UpperCamelCase ):



       @register_to_config
       def __init__(					self  ,UpperCAmelCase_ = 768  ,)	->				List[Any]:
           super().__init__()

           lowercase__									=    nn.Parameter(torch.zeros(1  ,UpperCAmelCase_   )   )
           lowercase__									=    nn.Parameter(torch.ones(1  ,UpperCAmelCase_   )   )



       def 				_a   (					self  ,UpperCAmelCase_ = None  ,UpperCAmelCase_ = None  ,)	->				Any:
           lowercase__									=    nn.Parameter(self.mean.to(UpperCAmelCase_   ).to(UpperCAmelCase_   )   )
           lowercase__									=    nn.Parameter(self.std.to(UpperCAmelCase_   ).to(UpperCAmelCase_   )   )
           return self



       def 				_a   (					self  ,UpperCAmelCase_   )	->				Tuple:
           lowercase__									=    (embeds - self.mean) * 1.0 / self.std
           return embeds







       def 				_a   (					self  ,UpperCAmelCase_   )	->				List[str]:
           lowercase__									=    (embeds * self.std) + self.mean
           return embeds




