



import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference

class 		UpperCAmelCase__			(			__UpperCamelCase						,unittest.TestCase   ):




			'''simple docstring'''






			UpperCamelCase									=       ShapEPipeline
			UpperCamelCase									=       ["""prompt"""]
			UpperCamelCase									=       ["""prompt"""]
			UpperCamelCase									=       [
			    """num_images_per_prompt""",
			    """num_inference_steps""",
			    """generator""",
			    """latents""",
			    """guidance_scale""",
			    """frame_size""",
			    """output_type""",
			    """return_dict""",
			]
			UpperCamelCase									=       False




			@property
			def  snake_case__     (   self							: List[Any] ):
					'''simple docstring'''





					return 32




			@property
			def  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					return 32




			@property
			def  snake_case__     (   self							: Optional[Any] ):
					'''simple docstring'''





					return self.time_input_dim * 4




			@property
			def  snake_case__     (   self							: str ):
					'''simple docstring'''





					return 8




			@property
			def  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     List[Any]													=	CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )
					return tokenizer




			@property
			def  snake_case__     (   self							: List[Any] ):
					'''simple docstring'''





					torch.manual_seed(0 )
					__UpperCAmelCase    :     int													=	CLIPTextConfig(
					    bos_token_id=0       ,       eos_token_id=2       ,       hidden_size=self.text_embedder_hidden_size       ,       projection_dim=self.text_embedder_hidden_size       ,       intermediate_size=37       ,       layer_norm_eps=1e-05       ,       num_attention_heads=4       ,       num_hidden_layers=5       ,       pad_token_id=1       ,       vocab_size=10_00       ,       )
					return CLIPTextModelWithProjection(a_ )




			@property
			def  snake_case__     (   self							: List[Any] ):
					'''simple docstring'''





					torch.manual_seed(0 )

					__UpperCAmelCase    :     Any													=	{
					    '''num_attention_heads''': 2,
					    '''attention_head_dim''': 16,
					    '''embedding_dim''': self.time_input_dim,
					    '''num_embeddings''': 32,
					    '''embedding_proj_dim''': self.text_embedder_hidden_size,
					    '''time_embed_dim''': self.time_embed_dim,
					    '''num_layers''': 1,
					    '''clip_embed_dim''': self.time_input_dim * 2,
					    '''additional_embeddings''': 0,
					    '''time_embed_act_fn''': '''gelu''',
					    '''norm_in_type''': '''layer''',
					    '''encoder_hid_proj_type''': None,
					    '''added_emb_type''': None,
					}

					__UpperCAmelCase    :     Tuple													=	PriorTransformer(**a_ )
					return model




			@property
			def  snake_case__     (   self							: Any ):
					'''simple docstring'''





					torch.manual_seed(0 )

					__UpperCAmelCase    :     int													=	{
					    '''param_shapes''': (
					        (self.renderer_dim, 93),
					        (self.renderer_dim, 8),
					        (self.renderer_dim, 8),
					        (self.renderer_dim, 8),
					    ),
					    '''d_latent''': self.time_input_dim,
					    '''d_hidden''': self.renderer_dim,
					    '''n_output''': 12,
					    '''background''': (
					        0.1,
					        0.1,
					        0.1,
					    ),
					}
					__UpperCAmelCase    :     List[Any]													=	ShapERenderer(**a_ )
					return model




			def  snake_case__     (   self							: List[str] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	self.dummy_prior
					__UpperCAmelCase    :     Tuple													=	self.dummy_text_encoder
					__UpperCAmelCase    :     Dict													=	self.dummy_tokenizer
					__UpperCAmelCase    :     List[Any]													=	self.dummy_renderer

					__UpperCAmelCase    :     Any													=	HeunDiscreteScheduler(
					    beta_schedule='''exp'''       ,       num_train_timesteps=10_24       ,       prediction_type='''sample'''       ,       use_karras_sigmas=a_       ,       clip_sample=a_       ,       clip_sample_range=1.0       ,       )
					__UpperCAmelCase    :     List[str]													=	{
					    '''prior''': prior,
					    '''text_encoder''': text_encoder,
					    '''tokenizer''': tokenizer,
					    '''renderer''': renderer,
					    '''scheduler''': scheduler,
					}

					return components




			def  snake_case__     (   self							: int       ,       a_							: List[Any]       ,       a_							: int=0 ):
					'''simple docstring'''





					if str(a_ ).startswith('''mps''' ):
							__UpperCAmelCase    :     List[Any]													=	torch.manual_seed(a_ )
					else:
							__UpperCAmelCase    :     Any													=	torch.Generator(device=a_ ).manual_seed(a_ )
					__UpperCAmelCase    :     Union[str, Any]													=	{
					    '''prompt''': '''horse''',
					    '''generator''': generator,
					    '''num_inference_steps''': 1,
					    '''frame_size''': 32,
					    '''output_type''': '''np''',
					}
					return inputs




			def  snake_case__     (   self							: Optional[Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     List[str]													=	'''cpu'''

					__UpperCAmelCase    :     Union[str, Any]													=	self.get_dummy_components()

					__UpperCAmelCase    :     str													=	self.pipeline_class(**a_ )
					__UpperCAmelCase    :     List[str]													=	pipe.to(a_ )

					pipe.set_progress_bar_config(disable=a_ )

					__UpperCAmelCase    :     List[Any]													=	pipe(**self.get_dummy_inputs(a_ ) )
					__UpperCAmelCase    :     int													=	output.images[0]
					__UpperCAmelCase    :     List[str]													=	image[0, -3:, -3:, -1]

					assert image.shape == (20, 32, 32, 3)

					__UpperCAmelCase    :     Any													=	np.array(
					    [
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					    ] )

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




			def  snake_case__     (   self							: Dict ):
					'''simple docstring'''





					self._test_inference_batch_consistent(batch_sizes=[1, 2] )




			def  snake_case__     (   self							: str ):
					'''simple docstring'''





					__UpperCAmelCase    :     int													=	torch_device == '''cpu'''
					__UpperCAmelCase    :     int													=	True

					self._test_inference_batch_single_identical(
					    batch_size=2       ,       test_max_difference=a_       ,       relax_max_difference=a_       ,       )




			def  snake_case__     (   self							: int ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[int]													=	self.get_dummy_components()
					__UpperCAmelCase    :     str													=	self.pipeline_class(**a_ )
					__UpperCAmelCase    :     Optional[Any]													=	pipe.to(a_ )
					pipe.set_progress_bar_config(disable=a_ )

					__UpperCAmelCase    :     List[str]													=	1
					__UpperCAmelCase    :     Dict													=	2

					__UpperCAmelCase    :     List[Any]													=	self.get_dummy_inputs(a_ )

					for key in inputs.keys():
							if key in self.batch_params:
									__UpperCAmelCase    :     Optional[int]													=	batch_size * [inputs[key]]

					__UpperCAmelCase    :     Tuple													=	pipe(**a_       ,       num_images_per_prompt=a_ )[0]

					assert images.shape[0] == batch_size * num_images_per_prompt

@slow
@require_torch_gpu
class 		UpperCAmelCase__			(			unittest.TestCase   ):




			'''simple docstring'''






			def  snake_case__     (   self							: str ):
					'''simple docstring'''





					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()




			def  snake_case__     (   self							: Any ):
					'''simple docstring'''





					__UpperCAmelCase    :     List[str]													=	load_numpy(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
					    '''/shap_e/test_shap_e_np_out.npy''' )
					__UpperCAmelCase    :     List[str]													=	ShapEPipeline.from_pretrained('''openai/shap-e''' )
					__UpperCAmelCase    :     Tuple													=	pipe.to(a_ )
					pipe.set_progress_bar_config(disable=a_ )

					__UpperCAmelCase    :     List[str]													=	torch.Generator(device=a_ ).manual_seed(0 )

					__UpperCAmelCase    :     Optional[Any]													=	pipe(
					    '''a shark'''       ,       generator=a_       ,       guidance_scale=1_5.0       ,       num_inference_steps=64       ,       frame_size=64       ,       output_type='''np'''       ,       ).images[0]

					assert images.shape == (20, 64, 64, 3)

					assert_mean_pixel_difference(a_       ,       a_ )










import qiskit

def        a						(      _UpperCAmelCase							:					int ,   _UpperCAmelCase							:					int						):







		'''simple docstring'''





		__UpperCAmelCase    :     List[str]													=	qiskit.Aer.get_backend('''aer_simulator'''						)

		__UpperCAmelCase    :     Optional[Any]													=	qiskit.QuantumCircuit(4 ,   2						)
		# encode inputs in qubits 0 and 1
		if bita == 1:
				qc_ha.x(0						)
		if bita == 1:
				qc_ha.x(1						)
		qc_ha.barrier()

		# use cnots to write XOR of the inputs on qubit2
		qc_ha.cx(0 ,   2						)
		qc_ha.cx(1 ,   2						)

		# use ccx / toffoli gate to write AND of the inputs on qubit3
		qc_ha.ccx(0 ,   1 ,   3						)
		qc_ha.barrier()

		# extract outputs
		qc_ha.measure(2 ,   0						)  # extract XOR value
		qc_ha.measure(3 ,   1						)  # extract AND value

		# Execute the circuit on the qasm simulator
		__UpperCAmelCase    :     str													=	qiskit.execute(_UpperCAmelCase ,   _UpperCAmelCase ,   shots=10_00						)

		# Return the histogram data of the results of the experiment
		return job.result().get_counts(_UpperCAmelCase						)


if __name__ == "__main__":
				__A   			=half_adder(1, 1)
				print(f'''Half Adder Output Qubit Counts: {counts}''')





