def vectorize(density_matrix, method='<STR_LIT>'):
density_matrix = np.array(density_matrix)<EOL>if method == '<STR_LIT>':<EOL><INDENT>return density_matrix.flatten(order='<STR_LIT:F>')<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>return density_matrix.flatten(order='<STR_LIT:C>')<EOL><DEDENT>elif method in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>num = int(np.log2(len(density_matrix)))  <EOL>if len(density_matrix) != <NUM_LIT:2>**num:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if method == '<STR_LIT>':<EOL><INDENT>pgroup = pauli_group(num, case='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>pgroup = pauli_group(num, case='<STR_LIT>')<EOL><DEDENT>vals = [np.trace(np.dot(p.to_matrix(), density_matrix))<EOL>for p in pgroup]<EOL>return np.array(vals)<EOL><DEDENT>return None<EOL>
Flatten an operator to a vector in a specified basis.

    Args:
        density_matrix (ndarray): a density matrix.
        method (str): the method of vectorization. Allowed values are
            - 'col' (default) flattens to column-major vector.
            - 'row' flattens to row-major vector.
            - 'pauli'flattens in the n-qubit Pauli basis.
            - 'pauli-weights': flattens in the n-qubit Pauli basis ordered by
               weight.

    Returns:
        ndarray: the resulting vector.
    Raises:
        Exception: if input state is not a n-qubit state