




'''simple docstring'''







import numpy as np
import qiskit






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			qiskit.QuantumCircuit(num_qubits + 1    )

		# First, let's deal with the case in which oracle is balanced
		if case == "balanced":
				# First generate a random number that tells us which CNOTs to
				# wrap in X-gates:
				_snake_case      			=			np.random.randint(1		,				2**num_qubits    )
				# Next, format 'b' as a binary string of length 'n', padded with zeros:
				_snake_case      			=			format(_UpperCamelCase		,				f'''0{num_qubits}b'''    )
				# Next, we place the first X-gates. Each digit in our binary string
				# corresponds to a qubit, if the digit is 0, we do nothing, if it's 1
				# we apply an X-gate to that qubit:
				for index, bit in enumerate(_UpperCamelCase    ):
						if bit == "1":
								oracle_qc.x(_UpperCamelCase    )
        # Do the controlled-NOT gates for each qubit, using the output qubit
        # as the target:
				for index in range(_UpperCamelCase    ):
						oracle_qc.cx(_UpperCamelCase		,				_UpperCamelCase    )
				# Next, place the final X-gates
				for index, bit in enumerate(_UpperCamelCase    ):
						if bit == "1":
								oracle_qc.x(_UpperCamelCase    )

    # Case in which oracle is constant
		if case == "constant":
				# First decide what the fixed output of the oracle will be
				# (either always 0 or always 1)
				_snake_case      			=			np.random.randint(2    )
				if output == 1:
						oracle_qc.x(_UpperCamelCase    )

		_snake_case      			=			oracle_qc.to_gate()
		_snake_case      			=			"Oracle"  # To show when we display the circuit
		return oracle_gate






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			qiskit.QuantumCircuit(num_qubits + 1		,				_UpperCamelCase    )
		# Set up the output qubit:
		dj_circuit.x(_UpperCamelCase    )
		dj_circuit.h(_UpperCamelCase    )
		# And set up the input register:
		for qubit in range(_UpperCamelCase    ):
				dj_circuit.h(_UpperCamelCase    )
		# Let's append the oracle gate to our circuit:
		dj_circuit.append(_UpperCamelCase		,				range(num_qubits + 1    )    )
		# Finally, perform the H-gates again and measure:
		for qubit in range(_UpperCamelCase    ):
				dj_circuit.h(_UpperCamelCase    )

		for i in range(_UpperCamelCase    ):
				dj_circuit.measure(_UpperCamelCase		,				_UpperCamelCase    )

		return dj_circuit






def 						_lowercase						(      lowercase_		,				lowercase_    ):



		'''simple docstring'''







		_snake_case      			=			qiskit.Aer.get_backend("aer_simulator"    )

		_snake_case      			=			dj_oracle(_UpperCamelCase		,				_UpperCamelCase    )
		_snake_case      			=			dj_algorithm(_UpperCamelCase		,				_UpperCamelCase    )

		# Execute the circuit on the simulator
		_snake_case      			=			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'''Deutsch Jozsa - Constant Oracle: {deutsch_jozsa('constant', 3)}''')
						print(F'''Deutsch Jozsa - Balanced Oracle: {deutsch_jozsa('balanced', 3)}''')






"""simple docstring"""
import unittest

import numpy as np

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


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import SwinaSRImageProcessor
					from transformers.image_transforms import get_image_size



class 	lowerCAmelCase_     (      unittest.TestCase       ):



				"""simple docstring"""

				def __init__(					self		,     lowerCAmelCase		,     lowerCAmelCase=7		,     lowerCAmelCase=3		,     lowerCAmelCase=18		,     lowerCAmelCase=30		,     lowerCAmelCase=4_00		,     lowerCAmelCase=True		,     lowerCAmelCase=1 / 2_55		,     lowerCAmelCase=True		,     lowerCAmelCase=8		,     ):






										"""simple docstring"""

										snake_case             =      parent
										snake_case             =      batch_size
										snake_case             =      num_channels
										snake_case             =      image_size
										snake_case             =      min_resolution
										snake_case             =      max_resolution
										snake_case             =      do_rescale
										snake_case             =      rescale_factor
										snake_case             =      do_pad
										snake_case             =      pad_size

				def 		snake_case			(					self     ):






										"""simple docstring"""

										return {
										    "do_rescale": self.do_rescale,
										    "rescale_factor": self.rescale_factor,
										    "do_pad": self.do_pad,
										    "pad_size": self.pad_size,
										}

				def 		snake_case			(					self		,     lowerCAmelCase=False		,     lowerCAmelCase=False		,     lowerCAmelCase=False     ):






										"""simple docstring"""

										assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

										if equal_resolution:
																snake_case             =      []
																for i in range(self.batch_size     ):
																						image_inputs.append(
																						    np.random.randint(
																						        2_55		,     size=(self.num_channels, self.max_resolution, self.max_resolution)		,     dtype=np.uinta     )     )
										else:
																snake_case             =      []
																for i in range(self.batch_size     ):
																						snake_case       ,snake_case             =      np.random.choice(np.arange(self.min_resolution		,     self.max_resolution     )		,     2     )
																						image_inputs.append(np.random.randint(2_55		,     size=(self.num_channels, width, height)		,     dtype=np.uinta     )     )

										if not numpify and not torchify:
																# PIL expects the channel dimension as last dimension
																snake_case             =      [Image.fromarray(np.moveaxis(lowerCAmelCase		,     0		,     -1     )     ) for x in image_inputs]

										if torchify:
																snake_case             =      [torch.from_numpy(lowerCAmelCase     ) for x in image_inputs]

										return image_inputs





@require_torch
@require_vision
class 	lowerCAmelCase_     (      lowerCAmelCase  ,     unittest.TestCase       ):



				"""simple docstring"""
				_lowerCAmelCase  :     Tuple      	=    SwinaSRImageProcessor if is_vision_available() else None

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      SwinaSRImageProcessingTester(self     )

				@property
				def 		snake_case			(					self     ):






										"""simple docstring"""

										return self.image_processor_tester.prepare_image_processor_dict()

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.image_processing_class(**self.image_processor_dict     )
										self.assertTrue(hasattr(lowerCAmelCase		,     'do_rescale'     )     )
										self.assertTrue(hasattr(lowerCAmelCase		,     'rescale_factor'     )     )
										self.assertTrue(hasattr(lowerCAmelCase		,     'do_pad'     )     )
										self.assertTrue(hasattr(lowerCAmelCase		,     'pad_size'     )     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										pass

				def 		snake_case			(					self		,     lowerCAmelCase     ):






										"""simple docstring"""

										snake_case       ,snake_case             =      get_image_size(lowerCAmelCase     )
										snake_case             =      self.image_processor_tester.pad_size

										snake_case             =      (old_height // size + 1) * size - old_height
										snake_case             =      (old_width // size + 1) * size - old_width
										return old_height + pad_height, old_width + pad_width

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.image_processing_class(**self.image_processor_dict     )
										# create random PIL images
										snake_case             =      self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase     )
										for image in image_inputs:
																self.assertIsInstance(lowerCAmelCase		,     Image.Image     )

										# Test not batched input
										snake_case             =      image_processor(image_inputs[0]		,     return_tensors='pt'     ).pixel_values
										snake_case       ,snake_case             =      self.calculate_expected_size(np.array(image_inputs[0]     )     )
										self.assertEqual(
										    encoded_images.shape		,     (
										        1,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )		,     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.image_processing_class(**self.image_processor_dict     )
										# create random numpy tensors
										snake_case             =      self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase		,     numpify=lowerCAmelCase     )
										for image in image_inputs:
																self.assertIsInstance(lowerCAmelCase		,     np.ndarray     )

										# Test not batched input
										snake_case             =      image_processor(image_inputs[0]		,     return_tensors='pt'     ).pixel_values
										snake_case       ,snake_case             =      self.calculate_expected_size(image_inputs[0]     )
										self.assertEqual(
										    encoded_images.shape		,     (
										        1,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )		,     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.image_processing_class(**self.image_processor_dict     )
										# create random PyTorch tensors
										snake_case             =      self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase		,     torchify=lowerCAmelCase     )
										for image in image_inputs:
																self.assertIsInstance(lowerCAmelCase		,     torch.Tensor     )

										# Test not batched input
										snake_case             =      image_processor(image_inputs[0]		,     return_tensors='pt'     ).pixel_values
										snake_case       ,snake_case             =      self.calculate_expected_size(image_inputs[0]     )
										self.assertEqual(
										    encoded_images.shape		,     (
										        1,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )		,     )


