Skip to content

qubosolver.analysis

qubosolver.analysis

Free functions for analysing QUBO solutions.

Converts one or more Solution objects into a unified pandas.DataFrame, for filtering, comparing, and summarizing solver outputs.

Example
df = to_dataframe([sol_a, sol_b], labels=["classical", "quantum"])

Functions:

to_dataframe

to_dataframe(solutions: Sequence[Solution], *, labels: Sequence[str] | Literal['auto'] = 'auto') -> pd.DataFrame

Convert one or more Solution into a single, unified pandas.DataFrame.

The resulting pandas.DataFrame can be used for filtering, sorting, and analysis.

Parameters:

Returns:

Raises:

  • ValueError –

    If the number of labels does not match the number of solutions.

Source code in qubosolver/utils/analysis.py
def to_dataframe(
    solutions: Sequence[Solution],
    *,
    labels: Sequence[str] | Literal["auto"] = "auto",
) -> pd.DataFrame:
    """Convert one or more [`Solution`][] into a single, unified [`pandas.DataFrame`][].

    The resulting [`pandas.DataFrame`][] can be used for filtering, sorting, and analysis.

    Args:
        solutions: A sequence of [`Solution`][].
        labels: One label per solution used to identify each group in the
            [`pandas.DataFrame`][]. Defaults to ``"0"``, ``"1"``, … when ``"auto"``.

    Returns:
        The concatenated [`pandas.DataFrame`][] containing all solutions.

    Raises:
        ValueError: If the number of labels does not match the number of solutions.
    """
    if labels == "auto":
        labels = [str(i) for i in range(len(solutions))]
    elif len(labels) != len(solutions):
        raise ValueError("The number of labels must equal the number of QUBOSolutions provided.")

    df_list = []
    df_list.append(_solution_to_dataframe(Solution(), solution_label=""))
    for label, sol in zip(labels, solutions, strict=True):
        df_list.append(_solution_to_dataframe(sol, solution_label=label))
    return pd.concat(df_list, ignore_index=True)