code: def pauli_commuting_sets(element: Pauli) -> Tuple[Pauli, ...]:
      
      if len(element) < 2:
          return (element,)
  
      groups: List[Pauli] = []  # typing: List[Pauli]
  
      for term in element:
          pterm = Pauli((term,))
  
          assigned = False
          for i, grp in enumerate(groups):
              if paulis_commute(grp, pterm):
                  groups[i] = grp + pterm
                  assigned = True
                  break
          if not assigned:
              groups.append(pterm)
  
      return tuple(groups)
docs: Gather the terms of a Pauli polynomial into commuting sets.
  
      Uses the algorithm defined in (Raeisi, Wiebe, Sanders,
      arXiv:1108.4318, 2011) to find commuting sets. Except uses commutation
      check from arXiv:1405.5749v2