

"""simple docstring"""




import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import CvtForImageClassification, CvtModel
				from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor
class 			_UpperCAmelCase					(				__UpperCAmelCase    ):



	def     a   (   self			:    List[str]		):
		__UpperCAmelCase							=      self.config_class(**self.inputs_dict		)
		self.parent.assertTrue(hasattr(UpperCamelCase__       ,  '''embed_dim'''		)		)
		self.parent.assertTrue(hasattr(UpperCamelCase__       ,  '''num_heads'''		)		)






class 			_UpperCAmelCase					:



	def __init__(   self			:    Optional[int]       ,  _lowercase			:    int       ,  _lowercase			:    List[str]=13       ,  _lowercase			:    Tuple=64       ,  _lowercase			:    str=3       ,  _lowercase			:    int=[16, 48, 96]       ,  _lowercase			:    List[Any]=[1, 3, 6]       ,  _lowercase			:    Tuple=[1, 2, 10]       ,  _lowercase			:    str=[7, 3, 3]       ,  _lowercase			:    List[Any]=[4, 2, 2]       ,  _lowercase			:    Any=[2, 1, 1]       ,  _lowercase			:    Tuple=[2, 2, 2]       ,  _lowercase			:    int=[False, False, True]       ,  _lowercase			:    Union[str, Any]=[0.0, 0.0, 0.0]       ,  _lowercase			:    Union[str, Any]=0.02       ,  _lowercase			:    Union[str, Any]=1E-12       ,  _lowercase			:    Optional[int]=True       ,  _lowercase			:    Union[str, Any]=True       ,  _lowercase			:    Tuple=2       ,  ):
		__UpperCAmelCase							=      parent
		__UpperCAmelCase							=      batch_size
		__UpperCAmelCase							=      image_size
		__UpperCAmelCase							=      patch_sizes
		__UpperCAmelCase							=      patch_stride
		__UpperCAmelCase							=      patch_padding
		__UpperCAmelCase							=      is_training
		__UpperCAmelCase							=      use_labels
		__UpperCAmelCase							=      num_labels
		__UpperCAmelCase							=      num_channels
		__UpperCAmelCase							=      embed_dim
		__UpperCAmelCase							=      num_heads
		__UpperCAmelCase							=      stride_kv
		__UpperCAmelCase							=      depth
		__UpperCAmelCase							=      cls_token
		__UpperCAmelCase							=      attention_drop_rate
		__UpperCAmelCase							=      initializer_range
		__UpperCAmelCase							=      layer_norm_eps



	def     a   (   self			:    Optional[int]		):
		__UpperCAmelCase							=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

		__UpperCAmelCase							=      None
		if self.use_labels:
			__UpperCAmelCase							=      ids_tensor([self.batch_size]       ,  self.num_labels		)

		__UpperCAmelCase							=      self.get_config()
		return config, pixel_values, labels



	def     a   (   self			:    Union[str, Any]		):
		return CvtConfig(
		    image_size=self.image_size       ,  num_labels=self.num_labels       ,  num_channels=self.num_channels       ,  embed_dim=self.embed_dim       ,  num_heads=self.num_heads       ,  patch_sizes=self.patch_sizes       ,  patch_padding=self.patch_padding       ,  patch_stride=self.patch_stride       ,  stride_kv=self.stride_kv       ,  depth=self.depth       ,  cls_token=self.cls_token       ,  attention_drop_rate=self.attention_drop_rate       ,  initializer_range=self.initializer_range       ,  )



	def     a   (   self			:    Tuple       ,  _lowercase			:    Optional[Any]       ,  _lowercase			:    Optional[int]       ,  _lowercase			:    str		):
		__UpperCAmelCase							=      CvtModel(config=UpperCamelCase__		)
		model.to(UpperCamelCase__		)
		model.eval()
		__UpperCAmelCase							=      model(UpperCamelCase__		)
		__UpperCAmelCase							=      (self.image_size, self.image_size)
		__UpperCAmelCase						,				__UpperCAmelCase							=      image_size[0], image_size[1]
		for i in range(len(self.depth		)		):
			__UpperCAmelCase							=      floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
			__UpperCAmelCase							=      floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
		self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.embed_dim[-1], height, width)		)



	def     a   (   self			:    Union[str, Any]       ,  _lowercase			:    Dict       ,  _lowercase			:    Dict       ,  _lowercase			:    Optional[int]		):
		__UpperCAmelCase							=      self.num_labels
		__UpperCAmelCase							=      CvtForImageClassification(UpperCamelCase__		)
		model.to(UpperCamelCase__		)
		model.eval()
		__UpperCAmelCase							=      model(UpperCamelCase__       ,  labels=UpperCamelCase__		)
		self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)		)



	def     a   (   self			:    Dict		):
		__UpperCAmelCase							=      self.prepare_config_and_inputs()
		__UpperCAmelCase						,				__UpperCAmelCase						,				__UpperCAmelCase							=      config_and_inputs
		__UpperCAmelCase							=      {'''pixel_values''': pixel_values}
		return config, inputs_dict






@require_torch
class 			_UpperCAmelCase					(				__UpperCAmelCase						,  __UpperCAmelCase						,  unittest.TestCase    ):
	a__						:      List[str]						       = (CvtModel, CvtForImageClassification) if is_torch_available() else ()
	a__						:      Optional[Any]						       = (
	    {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
	    if is_torch_available()
	    else {}
	)

	a__						:      Union[str, Any]						       = False
	a__						:      Tuple						       = False
	a__						:      Optional[int]						       = False
	a__						:      List[str]						       = False
	a__						:      Dict						       = False



	def     a   (   self			:    Any		):
		__UpperCAmelCase							=      CvtModelTester(self		)
		__UpperCAmelCase							=      ConfigTester(self       ,  config_class=UpperCamelCase__       ,  has_text_modality=UpperCamelCase__       ,  hidden_size=37		)



	def     a   (   self			:    Any		):
		self.create_and_test_config_common_properties()
		self.config_tester.create_and_test_config_to_json_string()
		self.config_tester.create_and_test_config_to_json_file()
		self.config_tester.create_and_test_config_from_and_save_pretrained()
		self.config_tester.create_and_test_config_with_num_labels()
		self.config_tester.check_config_can_be_init_without_params()
		self.config_tester.check_config_arguments_init()



	def     a   (   self			:    Optional[int]		):
		return



	@unittest.skip(reason='''Cvt does not output attentions'''		)
	def     a   (   self			:    Union[str, Any]		):
		pass



	@unittest.skip(reason='''Cvt does not use inputs_embeds'''		)
	def     a   (   self			:    Optional[int]		):
		pass



	@unittest.skip(reason='''Cvt does not support input and output embeddings'''		)
	def     a   (   self			:    str		):
		pass



	def     a   (   self			:    List[str]		):
		__UpperCAmelCase						,				__UpperCAmelCase							=      self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			__UpperCAmelCase							=      model_class(UpperCamelCase__		)
			__UpperCAmelCase							=      inspect.signature(model.forward		)
			# signature.parameters is an OrderedDict => so arg_names order is deterministic
			__UpperCAmelCase							=      [*signature.parameters.keys()]

			__UpperCAmelCase							=      ['''pixel_values''']
			self.assertListEqual(arg_names[:1]       ,  UpperCamelCase__		)



	def     a   (   self			:    Optional[Any]		):
		__UpperCAmelCase							=      self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*UpperCamelCase__		)



	def     a   (   self			:    int		):



		def check_hidden_states_output(_lowercase			:    Dict       ,  _lowercase			:    Union[str, Any]       ,  _lowercase			:    Optional[int]		):
			__UpperCAmelCase							=      model_class(UpperCamelCase__		)
			model.to(UpperCamelCase__		)
			model.eval()

			with torch.no_grad():
				__UpperCAmelCase							=      model(**self._prepare_for_class(UpperCamelCase__       ,  UpperCamelCase__		)		)

			__UpperCAmelCase							=      outputs.hidden_states

			__UpperCAmelCase							=      len(self.model_tester.depth		)
			self.assertEqual(len(UpperCamelCase__		)       ,  UpperCamelCase__		)

			# verify the first hidden states (first block)
			self.assertListEqual(
			    list(hidden_states[0].shape[-3:]		)       ,  [
			        self.model_tester.embed_dim[0],
			        self.model_tester.image_size // 4,
			        self.model_tester.image_size // 4,
			    ]       ,  )

		__UpperCAmelCase						,				__UpperCAmelCase							=      self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			__UpperCAmelCase							=      True
			check_hidden_states_output(UpperCamelCase__       ,  UpperCamelCase__       ,  UpperCamelCase__		)

			# check that output_hidden_states also work using config
			del inputs_dict["output_hidden_states"]
			__UpperCAmelCase							=      True

			check_hidden_states_output(UpperCamelCase__       ,  UpperCamelCase__       ,  UpperCamelCase__		)



	def     a   (   self			:    List[str]		):
		__UpperCAmelCase							=      self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)



	@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''		)
	def     a   (   self			:    Any		):
		pass



	@slow
	def     a   (   self			:    str		):



		for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__UpperCAmelCase							=      CvtModel.from_pretrained(UpperCamelCase__		)
			self.assertIsNotNone(UpperCamelCase__		)






def 	lowercase__		(    ):
	__UpperCAmelCase							=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
	return image




@require_torch
@require_vision
class 			_UpperCAmelCase					(				unittest.TestCase    ):



	@cached_property
	def     a   (   self			:    str		):
		return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)



	@slow
	def     a   (   self			:    Optional[Any]		):
		__UpperCAmelCase							=      CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(UpperCamelCase__		)

		__UpperCAmelCase							=      self.default_image_processor
		__UpperCAmelCase							=      prepare_img()
		__UpperCAmelCase							=      image_processor(images=UpperCamelCase__       ,  return_tensors='''pt'''		).to(UpperCamelCase__		)

		# forward pass
		with torch.no_grad():
			__UpperCAmelCase							=      model(**UpperCamelCase__		)

		# verify the logits
		__UpperCAmelCase							=      torch.Size((1, 10_00)		)
		self.assertEqual(outputs.logits.shape       ,  UpperCamelCase__		)

		__UpperCAmelCase							=      torch.tensor([0.9_285, 0.9_015, -0.3_150]		).to(UpperCamelCase__		)

		self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,  UpperCamelCase__       ,  atol=1E-4		)		)






import math

import numpy as np
import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute





def __UpperCamelCase			(       _A = 3  ):
						if isinstance(_A ,  _A  ):
												raise TypeError('''number of qubits must be a integer.'''  )
						if number_of_qubits <= 0:
												raise ValueError('''number of qubits must be > 0.'''  )
						if math.floor(_A  ) != number_of_qubits:
												raise ValueError('''number of qubits must be exact integer.'''  )
						if number_of_qubits > 10:
												raise ValueError('''number of qubits too large to simulate(>10).'''  )

						lowerCAmelCase_      			=  QuantumRegister(_A ,  '''qr'''  )
						lowerCAmelCase_      			=  ClassicalRegister(_A ,  '''cr'''  )

						lowerCAmelCase_      			=  QuantumCircuit(_A ,  _A  )

						lowerCAmelCase_      			=  number_of_qubits

						for i in range(_A  ):
												quantum_circuit.h(number_of_qubits - i - 1  )
												counter -= 1
												for j in range(_A  ):
																		quantum_circuit.cp(np.pi / 2 ** (counter - j) ,  _A ,  _A  )

						for k in range(number_of_qubits // 2  ):
												quantum_circuit.swap(_A ,  number_of_qubits - k - 1  )

						# measure all the qubits
						quantum_circuit.measure(_A ,  _A  )
						# simulate with 10000 shots
						lowerCAmelCase_      			=  Aer.get_backend('''qasm_simulator'''  )
						lowerCAmelCase_      			=  execute(_A ,  _A ,  shots=10000  )

						return job.result().get_counts(_A  )


if __name__ == "__main__":
						print(
						    f"Total count for quantum fourier transform state is: \
    {quantum_fourier_transform(3)}"
						)




