





import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
    import jax
    import jax.numpy as jnp
    from flax.jax_utils import replicate
    from flax.training.common_utils import shard



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





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       super().tearDown()
       gc.collect()





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      ,       UpperCamelCase      :   str			      =  FlaxStableDiffusionPipeline.from_pretrained(
           "stabilityai/stable-diffusion-2"			,				revision="bf16"			,				dtype=jnp.bfloataa			,				)

       UpperCamelCase      :   Optional[int]			      =  "A painting of a squirrel eating a burger"
       UpperCamelCase      :   Tuple			      =  jax.device_count()
       UpperCamelCase      :   Optional[Any]			      =  num_samples * [prompt]
       UpperCamelCase      :   Union[str, Any]			      =  sd_pipe.prepare_inputs(A_  )

       UpperCamelCase      :   int			      =  replicate(A_  )
       UpperCamelCase      :   Any			      =  shard(A_  )

       UpperCamelCase      :   Dict			      =  jax.random.PRNGKey(0  )
       UpperCamelCase      :   Tuple			      =  jax.random.split(A_			,				jax.device_count()  )

       UpperCamelCase      :   Tuple			      =  sd_pipe(A_			,				A_			,				A_			,				num_inference_steps=25			,				jit=A_  )[0]
       assert images.shape == (jax.device_count(), 1, 768, 768, 3)

       UpperCamelCase      :   List[str]			      =  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]  )
       UpperCamelCase      :   Any			      =  images[0, 253:256, 253:256, -1]

       UpperCamelCase      :   Dict			      =  jnp.asarray(jax.device_get(image_slice.flatten()  )  )
       UpperCamelCase      :   Dict			      =  jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12]  )
       print(F"""output_slice: {output_slice}"""  )
       assert jnp.abs(output_slice - expected_slice  ).max() < 1e-2





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Any			      =  "stabilityai/stable-diffusion-2"
       UpperCamelCase      ,       UpperCamelCase      :   str			      =  FlaxDPMSolverMultistepScheduler.from_pretrained(A_			,				subfolder="scheduler"  )
       UpperCamelCase      ,       UpperCamelCase      :   int			      =  FlaxStableDiffusionPipeline.from_pretrained(
           A_			,				scheduler=A_			,				revision="bf16"			,				dtype=jnp.bfloataa			,				)
       UpperCamelCase      :   Any			      =  scheduler_params

       UpperCamelCase      :   Dict			      =  "A painting of a squirrel eating a burger"
       UpperCamelCase      :   List[Any]			      =  jax.device_count()
       UpperCamelCase      :   Any			      =  num_samples * [prompt]
       UpperCamelCase      :   List[str]			      =  sd_pipe.prepare_inputs(A_  )

       UpperCamelCase      :   Any			      =  replicate(A_  )
       UpperCamelCase      :   int			      =  shard(A_  )

       UpperCamelCase      :   Dict			      =  jax.random.PRNGKey(0  )
       UpperCamelCase      :   Any			      =  jax.random.split(A_			,				jax.device_count()  )

       UpperCamelCase      :   int			      =  sd_pipe(A_			,				A_			,				A_			,				num_inference_steps=25			,				jit=A_  )[0]
       assert images.shape == (jax.device_count(), 1, 768, 768, 3)

       UpperCamelCase      :   Optional[int]			      =  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]  )
       UpperCamelCase      :   int			      =  images[0, 253:256, 253:256, -1]

       UpperCamelCase      :   Union[str, Any]			      =  jnp.asarray(jax.device_get(image_slice.flatten()  )  )
       UpperCamelCase      :   Optional[int]			      =  jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97]  )
       print(F"""output_slice: {output_slice}"""  )
       assert jnp.abs(output_slice - expected_slice  ).max() < 1e-2







import numpy as np
import qiskit



def 						A_							(	_lowerCAmelCase = 8				,  _lowerCAmelCase = None	)		->					str:
 UpperCamelCase      :   Tuple			      =  np.random.default_rng(seed=_lowerCAmelCase	)

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

 # Quantum Circuit to simulate BB84
 UpperCamelCase      :   List[Any]			      =  qiskit.QuantumCircuit(_lowerCAmelCase				,  name="BB84"	)

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

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

 bbaa_circ.barrier()
 bbaa_circ.measure_all()

 # Simulate the quantum circuit.
 UpperCamelCase      :   Union[str, 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.
 UpperCamelCase      :   Tuple			      =  qiskit.execute(_lowerCAmelCase				,  _lowerCAmelCase				,  shots=1				,  seed_simulator=_lowerCAmelCase	)
 # Returns the result of measurement.
 UpperCamelCase      :   Optional[Any]			      =  job.result().get_counts(_lowerCAmelCase	).most_frequent()

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

 # Get final key. Pad with 0 if too short, otherwise truncate.
 UpperCamelCase      :   Tuple			      =  gen_key[:key_len] if len(_lowerCAmelCase	) >= key_len else gen_key.ljust(_lowerCAmelCase				,  "0"	)
 return key


if __name__ == "__main__":
    print(f"""The generated key is : {bbaa(8, seed=0)}""")
    from doctest import testmod

    testmod()
