



'''simple docstring'''






import numpy as np
import qiskit
def       lowerCamelCase__       (						_A = 8 ,			_A = None				):
  a						:					List[Any]        =     np.random.default_rng(seed=UpperCamelCase__				)

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

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




"""simple docstring"""
import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
_lowerCAmelCase    :str     =					object()

# For specifying empty leaf dict `{}`
_lowerCAmelCase    :str     =					object()





def  lowerCamelCase_						(UpperCamelCase__		:		List[str]     ,       UpperCamelCase__		:		int							):
    _UpperCAmelCase							:  Dict											=    tuple((re.compile(x + '''$'''							) for x in qs)							)
    for i in range(len(UpperCamelCase__							) - len(UpperCamelCase__							) + 1							):
        _UpperCAmelCase							:  str											=    [x.match(UpperCamelCase__							) for x, y in zip(UpperCamelCase__     ,       ks[i:]							)]
        if matches and all(UpperCamelCase__							):
            return True
    return False





def  lowerCamelCase_						(UpperCamelCase__		:		List[str]							):
    def replace(UpperCamelCase__		:		List[str]     ,       UpperCamelCase__		:		Tuple							):
        for rule, replacement in rules:
            if _match(UpperCamelCase__     ,       UpperCamelCase__							):
                return replacement
        return val

    return replace





def  lowerCamelCase_						():
    return [
        # embeddings
        (("transformer", "wpe", "embedding"), P('''mp'''     ,       UpperCamelCase__							)),
        (("transformer", "wte", "embedding"), P('''mp'''     ,       UpperCamelCase__							)),
        # atention
        (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(UpperCamelCase__     ,       '''mp'''							)),
        (("attention", "out_proj", "kernel"), P('''mp'''     ,       UpperCamelCase__							)),
        (("attention", "out_proj", "bias"), None),
        # mlp
        (("mlp", "c_fc", "kernel"), P(UpperCamelCase__     ,       '''mp'''							)),
        (("mlp", "c_fc", "bias"), P('''mp'''							)),
        (("mlp", "c_proj", "kernel"), P('''mp'''     ,       UpperCamelCase__							)),
        (("mlp", "c_proj", "bias"), None),
        # layer norms
        ((r"ln_\d+", "bias"), None),
        ((r"\d+", r"ln_\d+", "scale"), None),
        (("ln_f", "bias"), None),
        (("ln_f", "scale"), None),
    ]





def  lowerCamelCase_						(UpperCamelCase__		:		str							):
    _UpperCAmelCase							:  List[str]											=    _get_partition_rules()
    _UpperCAmelCase							:  List[str]											=    _replacement_rules(UpperCamelCase__							)
    _UpperCAmelCase							:  List[Any]											=    {k: _unmatched for k in flatten_dict(UpperCamelCase__							)}
    _UpperCAmelCase							:  int											=    {k: replace(UpperCamelCase__     ,       UpperCamelCase__							) for k, v in initd.items()}
    assert _unmatched not in result.values(), "Incomplete partition spec."
    return freeze(unflatten_dict(UpperCamelCase__							)							)


