






import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()




class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



       @property
       def 					__lowercase						(							self    ) ->      Dict:






            '''simple docstring'''





            torch.manual_seed(0    )
            __snake_case			:List[Any]          =						UNetaDModel(
                block_out_channels=(32, 64)			,						layers_per_block=2			,						sample_size=32			,						in_channels=3			,						out_channels=3			,						down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")			,						up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")			,						)
            return model



       def 					__lowercase						(							self    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:List[Any]          =						self.dummy_uncond_unet
            __snake_case			:Optional[Any]          =						ScoreSdeVeScheduler()

            __snake_case			:Dict          =						ScoreSdeVePipeline(unet=a__			,						scheduler=a__    )
            sde_ve.to(a__    )
            sde_ve.set_progress_bar_config(disable=a__    )

            __snake_case			:Any          =						torch.manual_seed(0    )
            __snake_case			:int          =						sde_ve(num_inference_steps=2			,						output_type="""numpy"""			,						generator=a__    ).images

            __snake_case			:str          =						torch.manual_seed(0    )
            __snake_case			:Any          =						sde_ve(num_inference_steps=2			,						output_type="""numpy"""			,						generator=a__			,						return_dict=a__    )[
                0
            ]

            __snake_case			:Union[str, Any]          =						image[0, -3:, -3:, -1]
            __snake_case			:Dict          =						image_from_tuple[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            __snake_case			:Tuple          =						np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]    )

            assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2
            assert np.abs(image_from_tuple_slice.flatten() - expected_slice    ).max() < 1e-2




@slow
@require_torch
class 			snake_case__   (							unittest.TestCase):

       '''simple docstring'''



       def 					__lowercase						(							self    ) ->      List[Any]:






            '''simple docstring'''





            __snake_case			:List[str]          =						"""google/ncsnpp-church-256"""
            __snake_case			:Optional[Any]          =						UNetaDModel.from_pretrained(a__    )

            __snake_case			:Any          =						ScoreSdeVeScheduler.from_pretrained(a__    )

            __snake_case			:int          =						ScoreSdeVePipeline(unet=a__			,						scheduler=a__    )
            sde_ve.to(a__    )
            sde_ve.set_progress_bar_config(disable=a__    )

            __snake_case			:List[str]          =						torch.manual_seed(0    )
            __snake_case			:Optional[Any]          =						sde_ve(num_inference_steps=10			,						output_type="""numpy"""			,						generator=a__    ).images

            __snake_case			:Any          =						image[0, -3:, -3:, -1]

            assert image.shape == (1, 2_56, 2_56, 3)

            __snake_case			:List[str]          =						np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]    )

            assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2










import numpy as np
import qiskit






def   UpperCamelCase  (       snake_case__     :   int = 8	,snake_case__     :   int | None = None  ):




     '''simple docstring'''






     __snake_case			:Tuple          =						np.random.default_rng(seed=snake_case__  )

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

     # Quantum Circuit to simulate BB84
     __snake_case			:Optional[int]          =						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.
     __snake_case			:Optional[int]          =						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.
     __snake_case			:List[Any]          =						qiskit.execute(snake_case__	,snake_case__	,shots=1	,seed_simulator=snake_case__  )
     # Returns the result of measurement.
     __snake_case			:Tuple          =						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.
     __snake_case			:Any          =						"""""".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.
     __snake_case			:Dict          =						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()


