






"""simple docstring"""



import qiskit
from qiskit.providers import Backend


def   SCREAMING_SNAKE_CASE__					(     snake_case					:   int ,				snake_case					:   int							)->		tuple[qiskit.QuantumCircuit, str, str]:






     '''simple docstring'''



     UpperCAmelCase__	:						Optional[int]       =       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							):
          UpperCAmelCase__	:						Optional[int]       =       y.zfill(len(snake_case							)							)
     else:
          UpperCAmelCase__	:						Dict       =       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
     UpperCAmelCase__	:						List[str]       =       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   SCREAMING_SNAKE_CASE__					(     snake_case					:   qiskit.QuantumCircuit ,				snake_case					:   int ,				snake_case					:   int ,				snake_case					:   int ,				snake_case					:   int ,				)->		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   SCREAMING_SNAKE_CASE__					(     snake_case					:   int ,				snake_case					:   int ,				snake_case					:   Backend = qiskit.Aer.get_backend("aer_simulator"							) ,				)->		int:






     '''simple docstring'''



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

     # Store the Integers
     UpperCAmelCase__	:						Any       =       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]							)

     UpperCAmelCase__	:						List[Any]       =       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()











"""simple docstring"""



#
# Sort large text files in a minimum amount of memory
#
import argparse
import os




class  lowerCAmelCase__ :
   SCREAMING_SNAKE_CASE_         ='''block_{0}.dat'''
   def __init__(		self			:  Tuple       , snake_case__			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[Any]       =       filename
        UpperCAmelCase__	:						List[Any]       =       []
   def 			__a       (		self			:  Optional[Any]       , snake_case__			:  Union[str, Any]       , snake_case__			:  Union[str, Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Dict       =       self.BLOCK_FILENAME_FORMAT.format(snake_case__       )
        with open(snake_case__       , "w"       ) as file:
             file.write(snake_case__       )
        self.block_filenames.append(snake_case__       )
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        return self.block_filenames
   def 			__a       (		self			:  Optional[Any]       , snake_case__			:  List[str]       , snake_case__			:  str=None       ):


        '''simple docstring'''
        UpperCAmelCase__	:						int       =       0
        with open(self.filename       ) as file:
             while True:
                  UpperCAmelCase__	:						Union[str, Any]       =       file.readlines(snake_case__       )

                  if lines == []:
                       break

                  if sort_key is None:
                       lines.sort()
                  else:
                       lines.sort(key=snake_case__       )

                  self.write_block("".join(snake_case__       )       , snake_case__       )
                  i += 1




   def 			__a       (		self			:  Optional[Any]       ):


        '''simple docstring'''
        map(os.remove       , self.block_filenames       )




class  lowerCAmelCase__ :
   def 			__a       (		self			:  Union[str, Any]       , snake_case__			:  Optional[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Any       =       -1
        UpperCAmelCase__	:						Tuple       =       None

        for i in range(len(snake_case__       )       ):
             if min_str is None or choices[i] < min_str:
                  UpperCAmelCase__	:						List[str]       =       i

        return min_index




class  lowerCAmelCase__ :
   def __init__(		self			:  Dict       , snake_case__			:  Optional[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       files
        UpperCAmelCase__	:						str       =       set()
        UpperCAmelCase__	:						Any       =       len(snake_case__       )
        UpperCAmelCase__	:						Tuple       =       {i: None for i in range(self.num_buffers       )}
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        return {
            i: self.buffers[i] for i in range(self.num_buffers       ) if i not in self.empty
        }
   def 			__a       (		self			:  str       ):


        '''simple docstring'''
        for i in range(self.num_buffers       ):
             if self.buffers[i] is None and i not in self.empty:
                  UpperCAmelCase__	:						Tuple       =       self.files[i].readline()

                  if self.buffers[i] == "":
                       self.empty.add(snake_case__       )
                       self.files[i].close()

        if len(self.empty       ) == self.num_buffers:
             return False

        return True




   def 			__a       (		self			:  List[str]       , snake_case__			:  Tuple       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Any       =       self.buffers[index]
        UpperCAmelCase__	:						Union[str, Any]       =       None

        return value




class  lowerCAmelCase__ :
   def __init__(		self			:  Optional[Any]       , snake_case__			:  int       ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       merge_strategy
   def 			__a       (		self			:  Dict       , snake_case__			:  Dict       , snake_case__			:  Optional[int]       , snake_case__			:  Optional[int]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Dict       =       FilesArray(self.get_file_handles(snake_case__       , snake_case__       )       )
        with open(snake_case__       , "w"       , snake_case__       ) as outfile:
             while buffers.refresh():
                  UpperCAmelCase__	:						Any       =       self.merge_strategy.select(buffers.get_dict()       )
                  outfile.write(buffers.unshift(snake_case__       )       )




   def 			__a       (		self			:  Tuple       , snake_case__			:  Dict       , snake_case__			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       {}

        for i in range(len(snake_case__       )       ):
             UpperCAmelCase__	:						List[Any]       =       open(filenames[i]       , "r"       , snake_case__       )  # noqa: UP015

        return files




class  lowerCAmelCase__ :
   def __init__(		self			:  str       , snake_case__			:  Optional[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[str]       =       block_size
   def 			__a       (		self			:  Optional[Any]       , snake_case__			:  Optional[int]       , snake_case__			:  Any=None       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       self.get_number_blocks(snake_case__       , self.block_size       )
        UpperCAmelCase__	:						Union[str, Any]       =       FileSplitter(snake_case__       )
        splitter.split(self.block_size       , snake_case__       )

        UpperCAmelCase__	:						int       =       FileMerger(NWayMerge()       )
        UpperCAmelCase__	:						int       =       self.block_size / (num_blocks + 1)
        merger.merge(splitter.get_block_filenames()       , filename + ".out"       , snake_case__       )

        splitter.cleanup()




   def 			__a       (		self			:  Union[str, Any]       , snake_case__			:  Optional[int]       , snake_case__			:  Optional[Any]       ):


        '''simple docstring'''
        return (os.stat(snake_case__       ).st_size / block_size) + 1




def   SCREAMING_SNAKE_CASE__					(     snake_case					:   Tuple							)->		Union[str, Any]:






     '''simple docstring'''



     if string[-1].lower() == "k":
          return int(string[:-1]							) * 1024
     elif string[-1].lower() == "m":
          return int(string[:-1]							) * 1024 * 1024
     elif string[-1].lower() == "g":
          return int(string[:-1]							) * 1024 * 1024 * 1024
     else:
          return int(snake_case							)


def   SCREAMING_SNAKE_CASE__					(     )->		Any:






     '''simple docstring'''



     UpperCAmelCase__	:						Any       =       argparse.ArgumentParser()
     parser.add_argument(
         "-m" ,				"--mem" ,				help="amount of memory to use for sorting" ,				default="100M"							)
     parser.add_argument(
         "filename" ,				metavar="<filename>" ,				nargs=1 ,				help="name of file to sort"							)
     UpperCAmelCase__	:						int       =       parser.parse_args()

     UpperCAmelCase__	:						Optional[int]       =       ExternalSort(parse_memory(args.mem							)							)
     sorter.sort(args.filename[0]							)


if __name__ == "__main__":
 main()


