'''simple docstring'''

import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
 import jax
 import jax.numpy as jnp







@slow
@require_flax
class 				_lowercase     (				unittest.TestCase			):


 '''simple docstring'''






 def a					(      self   :	str      ,       SCREAMING_SNAKE_CASE__   :	str      ,       SCREAMING_SNAKE_CASE__   :	Dict							)  ->    List[Any]:
  return f"""gaussian_noise_s={seed}_shape={"_".join([str(SCREAMING_SNAKE_CASE__							) for s in shape]							)}.npy"""
 def a					(      self   :	Union[str, Any]							)  ->    Optional[int]:
  # clean up the VRAM after each test
  super().tearDown()
  gc.collect()
 def a					(      self   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Any=0      ,       SCREAMING_SNAKE_CASE__   :	int=(4, 4, 64, 64)      ,       SCREAMING_SNAKE_CASE__   :	str=False							)  ->    Any:
  __lowerCAmelCase											=						jnp.bfloataa if fpaa else jnp.floataa
  __lowerCAmelCase											=						jnp.array(load_hf_numpy(self.get_file_format(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							)							)      ,       dtype=SCREAMING_SNAKE_CASE__							)
  return image
 def a					(      self   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Tuple=False      ,       SCREAMING_SNAKE_CASE__   :	Dict="CompVis/stable-diffusion-v1-4"							)  ->    Dict:
  __lowerCAmelCase											=						jnp.bfloataa if fpaa else jnp.floataa
  __lowerCAmelCase											=						"""bf16""" if fpaa else None

  __lowerCAmelCase							,       __lowerCAmelCase											=						FlaxUNetaDConditionModel.from_pretrained(
      SCREAMING_SNAKE_CASE__      ,       subfolder="""unet"""      ,       dtype=SCREAMING_SNAKE_CASE__      ,       revision=SCREAMING_SNAKE_CASE__							)
  return model, params
 def a					(      self   :	str      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]=0      ,       SCREAMING_SNAKE_CASE__   :	Any=(4, 77, 7_68)      ,       SCREAMING_SNAKE_CASE__   :	Any=False							)  ->    List[Any]:
  __lowerCAmelCase											=						jnp.bfloataa if fpaa else jnp.floataa
  __lowerCAmelCase											=						jnp.array(load_hf_numpy(self.get_file_format(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							)							)      ,       dtype=SCREAMING_SNAKE_CASE__							)
  return hidden_states
 @parameterized.expand(
     [
         # fmt: off
         [83, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]],
         [17, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]],
         [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]],
         [3, 10_00, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]],
         # fmt: on
     ]							)
 def a					(      self   :	List[str]      ,       SCREAMING_SNAKE_CASE__   :	List[str]      ,       SCREAMING_SNAKE_CASE__   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	int							)  ->    Optional[Any]:
  __lowerCAmelCase							,       __lowerCAmelCase											=						self.get_unet_model(model_id="""CompVis/stable-diffusion-v1-4"""      ,       fpaa=SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						self.get_latents(SCREAMING_SNAKE_CASE__      ,       fpaa=SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						self.get_encoder_hidden_states(SCREAMING_SNAKE_CASE__      ,       fpaa=SCREAMING_SNAKE_CASE__							)

  __lowerCAmelCase											=						model.apply(
      {"""params""": params}      ,       SCREAMING_SNAKE_CASE__      ,       jnp.array(SCREAMING_SNAKE_CASE__      ,       dtype=jnp.intaa							)      ,       encoder_hidden_states=SCREAMING_SNAKE_CASE__      ,       ).sample

  assert sample.shape == latents.shape

  __lowerCAmelCase											=						jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())							)      ,       dtype=jnp.floataa							)
  __lowerCAmelCase											=						jnp.array(SCREAMING_SNAKE_CASE__      ,       dtype=jnp.floataa							)

  # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
  assert jnp.allclose(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__      ,       atol=1e-2							)





 @parameterized.expand(
     [
         # fmt: off
         [83, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]],
         [17, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]],
         [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]],
         [3, 10_00, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]],
         # fmt: on
     ]							)
 def a					(      self   :	int      ,       SCREAMING_SNAKE_CASE__   :	Tuple      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Any							)  ->    Optional[int]:
  __lowerCAmelCase							,       __lowerCAmelCase											=						self.get_unet_model(model_id="""stabilityai/stable-diffusion-2"""      ,       fpaa=SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						self.get_latents(SCREAMING_SNAKE_CASE__      ,       shape=(4, 4, 96, 96)      ,       fpaa=SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						self.get_encoder_hidden_states(SCREAMING_SNAKE_CASE__      ,       shape=(4, 77, 10_24)      ,       fpaa=SCREAMING_SNAKE_CASE__							)

  __lowerCAmelCase											=						model.apply(
      {"""params""": params}      ,       SCREAMING_SNAKE_CASE__      ,       jnp.array(SCREAMING_SNAKE_CASE__      ,       dtype=jnp.intaa							)      ,       encoder_hidden_states=SCREAMING_SNAKE_CASE__      ,       ).sample

  assert sample.shape == latents.shape

  __lowerCAmelCase											=						jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())							)      ,       dtype=jnp.floataa							)
  __lowerCAmelCase											=						jnp.array(SCREAMING_SNAKE_CASE__      ,       dtype=jnp.floataa							)

  # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
  assert jnp.allclose(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__      ,       atol=1e-2							)







'''simple docstring'''

import numpy as np
import qiskit







def      UpperCamelCase_						(					snake_case_       :       int = 8						,		snake_case_       :       int | None = None      )					->     str:






 '''simple docstring'''

 __lowerCAmelCase											=						np.random.default_rng(seed=snake_case_      )

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

 # Quantum Circuit to simulate BB84
 __lowerCAmelCase											=						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.
 __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(snake_case_						,		snake_case_						,		shots=1						,		seed_simulator=snake_case_      )
 # Returns the result of measurement.
 __lowerCAmelCase											=						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.
 __lowerCAmelCase											=						"""""".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.
 __lowerCAmelCase											=						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()






