code: def permutation_distance(
      A,
      B,
      matrix=None,
      magnitude_targets=None,
      debug=False,
      verbosity=0,
  ):
      global num_diffs
      swaps = 0
      debug = False
  
      swap_limit = int(math.pow(2, int(len(A) / 4) - 1))
      num_diffs = swap_limit
      common = []
      target_results = None
      if magnitude_targets is not None:
          assert matrix is not None
          cur_mag = sum_after_2_to_4(matrix[:, A])
          target_results = [
              (cur_mag, A) for i in range(len(magnitude_targets))
          ]
  
      if verbosity > 0 and matrix is not None:
          print(
              f"swap {'0':>4} {sum_after_2_to_4(matrix[:, B]):>15.3f}"
          )
          if verbosity > 5:
              print(f"swap {0:>4}, {make_grouped(A)} {make_grouped(B)}")
  
      while not np.all(
          np.array(A) - np.array(B) == np.zeros(np.array(A).shape)
      ):
          cGroups = common_groups(A, B)
          for g in cGroups:
              common.append(g)
          A, B = remove_common_groups(A, B)
          if len(A) == 0:
              break
  
          B = move_permutation_towards(
              np.array(B), np.array(A), debug=debug
          )
          swaps += 1
  
          if matrix is not None:
              total_cur_permute = [c for c in B]
  
              for c in [item for sublist in common for item in sublist]:
                  total_cur_permute.append(c)
  
              if verbosity > 0 or magnitude_targets is not None:
                  cur_mag = sum_after_2_to_4(
                      matrix[:, total_cur_permute]
                  )
                  for i in range(len(target_results)):
                      result = target_results[i]
                      if abs(magnitude_targets[i] - result[0]) > abs(
                          magnitude_targets[i] - cur_mag
                      ):
                          target_results[i] = (
                              cur_mag,
                              total_cur_permute,
                          )
                  if verbosity > 0:
                      print(f"swap {swaps:>4} {cur_mag:>15.3f}")
  
          if verbosity > 5 or swaps > swap_limit:
              print(
                  f"swap {swaps:>4}, {A} {B}, {num_diffs} diffs remain"
              )
  
          # safety net
          if swaps > swap_limit + 3:
              return swaps, target_results
  
      return swaps, target_results
  
imports: import numpy
  import time
  import subprocess
  import math
label_desc: Use image node_id 18 for calling a global function with example usage: permutation_distance(A, B, matrix, magnitude_targets, debug, verbosity) and returns: swaps,  target_results, swaps,  target_results