

import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import BeitImageProcessor


class       __SCREAMING_SNAKE_CASE   (				unittest.TestCase):






						def __init__(		self       ,  _UpperCamelCase       ,  _UpperCamelCase=7       ,  _UpperCamelCase=3       ,  _UpperCamelCase=18       ,  _UpperCamelCase=30       ,  _UpperCamelCase=4_00       ,  _UpperCamelCase=True       ,  _UpperCamelCase=None       ,  _UpperCamelCase=True       ,  _UpperCamelCase=None       ,  _UpperCamelCase=True       ,  _UpperCamelCase=[0.5, 0.5, 0.5]       ,  _UpperCamelCase=[0.5, 0.5, 0.5]       ,  _UpperCamelCase=False       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  size if size is not None else {'height': 20, 'width': 20}
												lowerCAmelCase__        =  crop_size if crop_size is not None else {'height': 18, 'width': 18}
												lowerCAmelCase__        =  parent
												lowerCAmelCase__        =  batch_size
												lowerCAmelCase__        =  num_channels
												lowerCAmelCase__        =  image_size
												lowerCAmelCase__        =  min_resolution
												lowerCAmelCase__        =  max_resolution
												lowerCAmelCase__        =  do_resize
												lowerCAmelCase__        =  size
												lowerCAmelCase__        =  do_center_crop
												lowerCAmelCase__        =  crop_size
												lowerCAmelCase__        =  do_normalize
												lowerCAmelCase__        =  image_mean
												lowerCAmelCase__        =  image_std
												lowerCAmelCase__        =  do_reduce_labels







						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												return {
												    "do_resize": self.do_resize,
												    "size": self.size,
												    "do_center_crop": self.do_center_crop,
												    "crop_size": self.crop_size,
												    "do_normalize": self.do_normalize,
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_reduce_labels": self.do_reduce_labels,
												}







def 		_UpperCamelCase				(      )					->				Optional[Any]:

						"""simple docstring"""



						lowerCAmelCase__        =  load_dataset('hf-internal-testing/fixtures_ade20k'      ,    split='test'						)

						lowerCAmelCase__        =  Image.open(dataset[0]['file']						)
						lowerCAmelCase__        =  Image.open(dataset[1]['file']						)

						return image, map







def 		_UpperCamelCase				(      )					->				Dict:

						"""simple docstring"""



						lowerCAmelCase__        =  load_dataset('hf-internal-testing/fixtures_ade20k'      ,    split='test'						)

						lowerCAmelCase__        =  Image.open(ds[0]['file']						)
						lowerCAmelCase__        =  Image.open(ds[1]['file']						)
						lowerCAmelCase__        =  Image.open(ds[2]['file']						)
						lowerCAmelCase__        =  Image.open(ds[3]['file']						)

						return [imagea, imagea], [mapa, mapa]


@require_torch
@require_vision
class       __SCREAMING_SNAKE_CASE   (				__lowercase						,   unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:    Tuple						=   BeitImageProcessor if is_vision_available() else None






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  BeitImageProcessingTester(self				)






						@property
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												return self.image_processor_tester.prepare_image_processor_dict()






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class(**self.image_processor_dict				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'do_resize'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'size'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'do_center_crop'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'center_crop'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'do_normalize'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'image_mean'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'image_std'				)				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class.from_dict(self.image_processor_dict				)
												self.assertEqual(image_processor.size       ,  {'height': 20, 'width': 20}				)
												self.assertEqual(image_processor.crop_size       ,  {'height': 18, 'width': 18}				)
												self.assertEqual(image_processor.do_reduce_labels       ,  _UpperCamelCase				)

												lowerCAmelCase__        =  self.image_processing_class.from_dict(
												    self.image_processor_dict       ,  size=42       ,  crop_size=84       ,  reduce_labels=_UpperCamelCase				)
												self.assertEqual(image_processor.size       ,  {'height': 42, 'width': 42}				)
												self.assertEqual(image_processor.crop_size       ,  {'height': 84, 'width': 84}				)
												self.assertEqual(image_processor.do_reduce_labels       ,  _UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												pass






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class(**self.image_processor_dict				)
												# create random PIL images
												lowerCAmelCase__        =  prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=_UpperCamelCase				)
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase       ,  Image.Image				)

												# Test not batched input
												lowerCAmelCase__        =  image_processing(image_inputs[0]       ,  return_tensors='pt'				).pixel_values
												self.assertEqual(
												    encoded_images.shape       ,  (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )

												# Test batched
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  return_tensors='pt'				).pixel_values
												self.assertEqual(
												    encoded_images.shape       ,  (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class(**self.image_processor_dict				)
												# create random numpy tensors
												lowerCAmelCase__        =  prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=_UpperCamelCase       ,  numpify=_UpperCamelCase				)
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase       ,  np.ndarray				)

												# Test not batched input
												lowerCAmelCase__        =  image_processing(image_inputs[0]       ,  return_tensors='pt'				).pixel_values
												self.assertEqual(
												    encoded_images.shape       ,  (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )

												# Test batched
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  return_tensors='pt'				).pixel_values
												self.assertEqual(
												    encoded_images.shape       ,  (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class(**self.image_processor_dict				)
												# create random PyTorch tensors
												lowerCAmelCase__        =  prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=_UpperCamelCase       ,  torchify=_UpperCamelCase				)
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase       ,  torch.Tensor				)

												# Test not batched input
												lowerCAmelCase__        =  image_processing(image_inputs[0]       ,  return_tensors='pt'				).pixel_values
												self.assertEqual(
												    encoded_images.shape       ,  (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )

												# Test batched
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  return_tensors='pt'				).pixel_values
												self.assertEqual(
												    encoded_images.shape       ,  (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class(**self.image_processor_dict				)
												# create random PyTorch tensors
												lowerCAmelCase__        =  prepare_image_inputs(self.image_processor_tester       ,  equal_resolution=_UpperCamelCase       ,  torchify=_UpperCamelCase				)
												lowerCAmelCase__        =  []
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase       ,  torch.Tensor				)
																		maps.append(torch.zeros(image.shape[-2:]				).long()				)

												# Test not batched input
												lowerCAmelCase__        =  image_processing(image_inputs[0]       ,  maps[0]       ,  return_tensors='pt'				)
												self.assertEqual(
												    encoding['pixel_values'].shape       ,  (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(
												    encoding['labels'].shape       ,  (
												        1,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(encoding['labels'].dtype       ,  torch.long				)
												self.assertTrue(encoding['labels'].min().item() >= 0				)
												self.assertTrue(encoding['labels'].max().item() <= 2_55				)

												# Test batched
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  _UpperCamelCase       ,  return_tensors='pt'				)
												self.assertEqual(
												    encoding['pixel_values'].shape       ,  (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(
												    encoding['labels'].shape       ,  (
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(encoding['labels'].dtype       ,  torch.long				)
												self.assertTrue(encoding['labels'].min().item() >= 0				)
												self.assertTrue(encoding['labels'].max().item() <= 2_55				)

												# Test not batched input (PIL images)
												lowerCAmelCase__  ,    lowerCAmelCase__        =  prepare_semantic_single_inputs()

												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  _UpperCamelCase       ,  return_tensors='pt'				)
												self.assertEqual(
												    encoding['pixel_values'].shape       ,  (
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(
												    encoding['labels'].shape       ,  (
												        1,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(encoding['labels'].dtype       ,  torch.long				)
												self.assertTrue(encoding['labels'].min().item() >= 0				)
												self.assertTrue(encoding['labels'].max().item() <= 2_55				)

												# Test batched input (PIL images)
												lowerCAmelCase__  ,    lowerCAmelCase__        =  prepare_semantic_batch_inputs()

												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  _UpperCamelCase       ,  return_tensors='pt'				)
												self.assertEqual(
												    encoding['pixel_values'].shape       ,  (
												        2,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(
												    encoding['labels'].shape       ,  (
												        2,
												        self.image_processor_tester.crop_size['height'],
												        self.image_processor_tester.crop_size['width'],
												    )       ,  )
												self.assertEqual(encoding['labels'].dtype       ,  torch.long				)
												self.assertTrue(encoding['labels'].min().item() >= 0				)
												self.assertTrue(encoding['labels'].max().item() <= 2_55				)







						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class(**self.image_processor_dict				)

												# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
												lowerCAmelCase__  ,    lowerCAmelCase__        =  prepare_semantic_single_inputs()
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  _UpperCamelCase       ,  return_tensors='pt'				)
												self.assertTrue(encoding['labels'].min().item() >= 0				)
												self.assertTrue(encoding['labels'].max().item() <= 1_50				)

												lowerCAmelCase__        =  True
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  _UpperCamelCase       ,  return_tensors='pt'				)
												self.assertTrue(encoding['labels'].min().item() >= 0				)
												self.assertTrue(encoding['labels'].max().item() <= 2_55				)













import qiskit







def 		_UpperCamelCase				(      UpperCamelCase_    :   int      ,    UpperCamelCase_    :   int						)					->				qiskit.result.counts.Counts:

						"""simple docstring"""



						lowerCAmelCase__        =  qiskit.Aer.get_backend('aer_simulator'						)

						# Create a Quantum Circuit acting on the q register
						lowerCAmelCase__        =  qiskit.QuantumCircuit(UpperCamelCase_      ,    UpperCamelCase_						)

						# Map the quantum measurement to the classical bits
						circuit.measure([0]      ,    [0]						)

						# Execute the circuit on the simulator
						lowerCAmelCase__        =  qiskit.execute(UpperCamelCase_      ,    UpperCamelCase_      ,    shots=1000						)

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


if __name__ == "__main__":
					print(f'Total count for various states are: {single_qubit_measure(1, 1)}')





