




'''simple docstring'''





import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
  import torch_xla.core.xla_model as xm


def   UpperCamelCase_(			snake_case							: List[str]							,       snake_case							: List[str] = False  ):

       '''simple docstring'''






       if device_specific:
              seed += AcceleratorState().process_index
       random.seed(_UpperCAmelCase  )
       np.random.seed(_UpperCAmelCase  )
       torch.manual_seed(_UpperCAmelCase  )
       if is_xpu_available():
              torch.xpu.manual_seed_all(_UpperCAmelCase  )
       elif is_npu_available():
              torch.npu.manual_seed_all(_UpperCAmelCase  )
       else:
              torch.cuda.manual_seed_all(_UpperCAmelCase  )
       # ^^ safe to call this function even if cuda is not available
       if is_tpu_available():
              xm.set_rng_state(_UpperCAmelCase  )


def   UpperCamelCase_(			snake_case							: List[str] = None							,       snake_case							: Tuple = None  ):

       '''simple docstring'''






       if rng_type == RNGType.TORCH:
              snake_case_			      =   torch.get_rng_state()
       elif rng_type == RNGType.CUDA:
              snake_case_			      =   torch.cuda.get_rng_state()
       elif rng_type == RNGType.XLA:
              assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
              snake_case_			      =   torch.tensor(xm.get_rng_state()  )
       elif rng_type == RNGType.NPU:
              assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
              snake_case_			      =   torch.npu.get_rng_state()
       elif rng_type == RNGType.XPU:
              assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
              snake_case_			      =   torch.xpu.get_rng_state()
       elif rng_type == RNGType.GENERATOR:
              assert generator is not None, "Need a generator to synchronize its seed."
              snake_case_			      =   generator.get_state()

       # Broadcast the rng state from device 0 to other devices
       snake_case_			      =   AcceleratorState()
       if state.distributed_type == DistributedType.TPU:
              snake_case_			      =   rng_state.to(xm.xla_device()  )
              xm.collective_broadcast([rng_state]  )
              xm.mark_step()
              snake_case_			      =   rng_state.cpu()
       elif (
           state.distributed_type in CUDA_DISTRIBUTED_TYPES
           or state.distributed_type == DistributedType.MULTI_NPU
           or state.distributed_type == DistributedType.MULTI_XPU
       ):
              snake_case_			      =   rng_state.to(state.device  )
              torch.distributed.broadcast(_UpperCAmelCase							,       0  )
              snake_case_			      =   rng_state.cpu()
       elif state.distributed_type == DistributedType.MULTI_CPU:
              torch.distributed.broadcast(_UpperCAmelCase							,       0  )

       # Set the broadcast rng state
       if rng_type == RNGType.TORCH:
              torch.set_rng_state(_UpperCAmelCase  )
       elif rng_type == RNGType.CUDA:
              torch.cuda.set_rng_state(_UpperCAmelCase  )
       elif rng_type == RNGType.NPU:
              torch.npu.set_rng_state(_UpperCAmelCase  )
       elif rng_type == RNGType.XPU:
              torch.xpu.set_rng_state(_UpperCAmelCase  )
       elif rng_type == RNGType.XLA:
              xm.set_rng_state(rng_state.item()  )
       elif rng_type == RNGType.GENERATOR:
              generator.set_state(_UpperCAmelCase  )




def   UpperCamelCase_(			snake_case							: Optional[Any]							,       snake_case							: List[Any] = None  ):

       '''simple docstring'''






       for rng_type in rng_types:
              synchronize_rng_state(RNGType(_UpperCAmelCase  )							,       generator=_UpperCAmelCase  )










'''simple docstring'''





import qiskit
from qiskit.providers import Backend


def   UpperCamelCase_(			snake_case							: int							,       snake_case							: int  ):

							'''simple docstring'''






							snake_case_      ,   snake_case_			      =   bin(snake_case  )[2:], bin(snake_case  )[2:]  # Remove leading '0b'

							# Ensure that both strings are of the same length
							if len(snake_case  ) > len(snake_case  ):
														snake_case_			      =   y.zfill(len(snake_case  )  )
							else:
														snake_case_			      =   x.zfill(len(snake_case  )  )

							# We need (3 * number of bits in the larger number)+1 qBits
							# The second parameter is the number of classical registers, to measure the result
							snake_case_			      =   qiskit.QuantumCircuit((len(snake_case  ) * 3) + 1							,       len(snake_case  ) + 1  )

							# We are essentially "not-ing" the bits that are 1
							# Reversed because it's easier to perform ops on more significant bits
							for i in range(len(snake_case  )  ):
														if x[::-1][i] == "1":
																					circuit.x(snake_case  )
							for j in range(len(snake_case  )  ):
														if y[::-1][j] == "1":
																					circuit.x(len(snake_case  ) + j  )

							return circuit, x, y


def   UpperCamelCase_(			snake_case							: qiskit.QuantumCircuit							,       snake_case							: int							,       snake_case							: int							,       snake_case							: int							,       snake_case							: int							,       ):

							'''simple docstring'''






							circuit.ccx(snake_case							,       snake_case							,       snake_case  )
							circuit.cx(snake_case							,       snake_case  )
							circuit.ccx(snake_case							,       snake_case							,       snake_case  )
							circuit.cx(snake_case							,       snake_case  )
							circuit.cx(snake_case							,       snake_case  )


def   UpperCamelCase_(			snake_case							: int							,       snake_case							: int							,       snake_case							: Backend = qiskit.Aer.get_backend("aer_simulator"  )							,       ):

							'''simple docstring'''






							if vala < 0 or vala < 0:
														raise ValueError("Both Integers must be positive!"  )

							# Store the Integers
							snake_case_      ,   snake_case_      ,   snake_case_			      =   store_two_classics(snake_case							,       snake_case  )
							for i in range(len(snake_case  )  ):
														full_adder(snake_case							,       snake_case							,       len(snake_case  ) + i							,       len(snake_case  ) + len(snake_case  ) + i							,       len(snake_case  ) + len(snake_case  ) + i + 1  )
														circuit.barrier()  # Optional, just for aesthetics

							# Measure the resultant qBits
							for i in range(len(snake_case  ) + 1  ):
														circuit.measure([(len(snake_case  ) * 2) + i]							,       [i]  )

							snake_case_			      =   qiskit.execute(snake_case							,       snake_case							,       shots=1  ).result()

							# The result is in binary. Convert it back to int
							return int(list(res.get_counts()  )[0]							,       2  )


if __name__ == "__main__":
		import doctest

		doctest.testmod()




