


import inspect
import os
import unittest

import torch

import accelerate
from accelerate import Accelerator
from accelerate.test_utils import execute_subprocess_async, require_multi_gpu
from accelerate.utils import patch_environment






class   __lowercase				(  unittest.TestCase  ):





      def        _lowercase				(		self						:					int       )    ->   Union[str, Any]:






         """simple docstring"""







         UpperCAmelCase							=     inspect.getfile(accelerate.test_utils       )
         UpperCAmelCase							=     os.path.sep.join(mod_file.split(os.path.sep       )[:-1] + ["""scripts""", """test_script.py"""]       )
         UpperCAmelCase							=     os.path.sep.join(
             mod_file.split(os.path.sep       )[:-1] + ["""scripts""", """test_distributed_data_loop.py"""]       )
         UpperCAmelCase							=     os.path.sep.join(mod_file.split(os.path.sep       )[:-1] + ["""scripts""", """test_ops.py"""]       )





      @require_multi_gpu
      def        _lowercase				(		self						:					str       )    ->   Optional[int]:






         """simple docstring"""







         print(F"""Found {torch.cuda.device_count()} devices."""       )
         UpperCAmelCase							=     ['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path]
         with patch_environment(omp_num_threads=1       ):
            execute_subprocess_async(__lowerCamelCase					,       env=os.environ.copy()       )





      @require_multi_gpu
      def        _lowercase				(		self						:					Dict       )    ->   Union[str, Any]:






         """simple docstring"""







         print(F"""Found {torch.cuda.device_count()} devices."""       )
         UpperCAmelCase							=     ['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", self.operation_file_path]
         print(F"""Command: {cmd}"""       )
         with patch_environment(omp_num_threads=1       ):
            execute_subprocess_async(__lowerCamelCase					,       env=os.environ.copy()       )





      @require_multi_gpu
      def        _lowercase				(		self						:					Dict       )    ->   str:






         """simple docstring"""







         UpperCAmelCase							=     ['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__       )]
         with patch_environment(omp_num_threads=1       ):
            execute_subprocess_async(__lowerCamelCase					,       env=os.environ.copy()       )





      @require_multi_gpu
      def        _lowercase				(		self						:					str       )    ->   List[Any]:






         """simple docstring"""







         print(F"""Found {torch.cuda.device_count()} devices, using 2 devices only"""       )
         UpperCAmelCase							=     ['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", self.data_loop_file_path]
         with patch_environment(omp_num_threads=1					,       cuda_visible_devices="""0,1"""       ):
            execute_subprocess_async(__lowerCamelCase					,       env=os.environ.copy()       )


if __name__ == "__main__":
   __a     =			Accelerator()
   __a     =			(accelerator.state.process_index + 2, 10)
   __a     =			torch.randint(0, 10, shape).to(accelerator.device)

   __a     =			""""""

   __a     =			accelerator.pad_across_processes(tensor)
   if tensora.shape[0] != accelerator.state.num_processes + 1:
      error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
   if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor):
      error_msg += "Tensors have different values."
   if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0):
      error_msg += "Padding was not done with the right value (0)."

   __a     =			accelerator.pad_across_processes(tensor, pad_first=True)
   if tensora.shape[0] != accelerator.state.num_processes + 1:
      error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
   __a     =			accelerator.state.num_processes - accelerator.state.process_index - 1
   if not torch.equal(tensora[index:], tensor):
      error_msg += "Tensors have different values."
   if not torch.all(tensora[:index] == 0):
      error_msg += "Padding was not done with the right value (0)."

   # Raise error at the end to make sure we don't stop at the first failure.
   if len(error_msg) > 0:
      raise ValueError(error_msg)







import math

import qiskit
def   _UpperCamelCase				(						lowerCAmelCase_ = 1  ,						lowerCAmelCase_ = 1  ,						lowerCAmelCase_ = 1   )						->qiskit.result.counts.Counts:
   if (
       isinstance(lowerCAmelCase_  ,						lowerCAmelCase_   )
       or isinstance(lowerCAmelCase_  ,						lowerCAmelCase_   )
       or isinstance(lowerCAmelCase_  ,						lowerCAmelCase_   )
   ):
      raise TypeError("""inputs must be integers."""   )

   if (input_a < 0) or (input_a < 0) or (carry_in < 0):
      raise ValueError("""inputs must be positive."""   )

   if (
       (math.floor(lowerCAmelCase_   ) != input_a)
       or (math.floor(lowerCAmelCase_   ) != input_a)
       or (math.floor(lowerCAmelCase_   ) != carry_in)
   ):
      raise ValueError("""inputs must be exact integers."""   )

   if (input_a > 2) or (input_a > 2) or (carry_in > 2):
      raise ValueError("""inputs must be less or equal to 2."""   )

   # build registers
   UpperCAmelCase							=     qiskit.QuantumRegister(4  ,						"""qr"""   )
   UpperCAmelCase							=     qiskit.ClassicalRegister(2  ,						"""cr"""   )
   # list the entries
   UpperCAmelCase							=     [input_a, input_a, carry_in]

   UpperCAmelCase							=     qiskit.QuantumCircuit(lowerCAmelCase_  ,						lowerCAmelCase_   )

   for i in range(0  ,						3   ):
      if entry[i] == 2:
         quantum_circuit.h(lowerCAmelCase_   )  # for hadamard entries
      elif entry[i] == 1:
         quantum_circuit.x(lowerCAmelCase_   )  # for 1 entries
      elif entry[i] == 0:
         quantum_circuit.i(lowerCAmelCase_   )  # for 0 entries

    # build the circuit
   quantum_circuit.ccx(0  ,						1  ,						3   )  # ccx = toffoli gate
   quantum_circuit.cx(0  ,						1   )
   quantum_circuit.ccx(1  ,						2  ,						3   )
   quantum_circuit.cx(1  ,						2   )
   quantum_circuit.cx(0  ,						1   )

   quantum_circuit.measure([2, 3]  ,						lowerCAmelCase_   )  # measure the last two qbits

   UpperCAmelCase							=     qiskit.Aer.get_backend("""aer_simulator"""   )
   UpperCAmelCase							=     qiskit.execute(lowerCAmelCase_  ,						lowerCAmelCase_  ,						shots=1_0_0_0   )

   return job.result().get_counts(lowerCAmelCase_   )


if __name__ == "__main__":
 print(F"""Total sum count for state is: {quantum_full_adder(1, 1, 1)}""")



