qxmt.evaluation.evaluation module

qxmt.evaluation.evaluation module#

class qxmt.evaluation.evaluation.ClassificationEvaluation(params, default_metrics_name=None, custom_metrics=None)

Bases: Evaluation

Evaluation class specifically for classification tasks.

Inherits from Evaluation and uses classification-specific metrics.

Parameters:
  • params (dict[str, Any])

  • default_metrics_name (list[str] | None)

  • custom_metrics (list[dict[str, Any]] | None)

DEFAULT_METRICS_NAME: list[str] = ['accuracy', 'precision', 'recall', 'f1_score']
NAME2METRIC: dict[str, Type[BaseMetric]] = {'accuracy': <class 'qxmt.evaluation.metrics.defaults_classification.Accuracy'>, 'f1_score': <class 'qxmt.evaluation.metrics.defaults_classification.F1Score'>, 'precision': <class 'qxmt.evaluation.metrics.defaults_classification.Precision'>, 'recall': <class 'qxmt.evaluation.metrics.defaults_classification.Recall'>}
class qxmt.evaluation.evaluation.Evaluation(params, default_metrics_name=None, custom_metrics=None)

Bases: object

A class for evaluating model performance using various metrics.

This class provides functionality to evaluate model predictions against actual values using both default and custom metrics. Results can be accessed as dictionaries or pandas DataFrames.

Parameters:
  • params (dict[str, Any])

  • default_metrics_name (list[str] | None)

  • custom_metrics (list[dict[str, Any]] | None)

DEFAULT_METRICS_NAME

List of default metric names

Type:

list[str]

NAME2METRIC

Mapping of metric names to metric classes

Type:

dict[str, Type[BaseMetric]]

Examples

>>> from qxmt.evaluation.evaluation import Evaluation
>>> params = {"actual": np.array([1, 0, 1]), "predicted": np.array([1, 1, 1])}
>>> evaluation = Evaluation(params)
>>> evaluation.evaluate()
>>> evaluation.to_dict()
{'accuracy': 0.6666666666666666, 'precision': 0.6666666666666666, 'recall': 1.0, 'f1_score': 0.8}
>>> evaluation.to_dataframe()
   accuracy  precision  recall  f1_score
0  0.666667   0.666667     1.0       0.8
DEFAULT_METRICS_NAME: list[str] = []
NAME2METRIC: dict[str, Type[BaseMetric]] = {}
__init__(params, default_metrics_name=None, custom_metrics=None)

Initialize the evaluation class with parameters and metrics.

Parameters:
  • params (dict[str, Any]) – Dictionary containing evaluation parameters

  • default_metrics_name (Optional[list[str]], optional) – List of default metric names to use. If None, uses class default metrics.

  • custom_metrics (Optional[list[dict[str, Any]]], optional) – List of custom metric configurations. Each metric must be a subclass of BaseMetric.

Return type:

None

evaluate()

Evaluate all default and custom metrics.

Return type:

None

init_custom_metrics(custom_metrics)

Initialize and validate custom metrics.

Parameters:

custom_metrics (Optional[list[dict[str, Any]]]) – List of custom metric configurations

Raises:

ValueError – If any custom metric is not a subclass of BaseMetric

Return type:

None

init_default_metrics(default_metrics_name)

Initialize and validate default metrics.

Parameters:

default_metrics_name (Optional[list[str]]) – List of default metric names to use

Raises:

ValueError – If any specified metric is not implemented

Return type:

None

set_evaluation_result(metrics)

Calculate scores for a list of metrics using the evaluation parameters.

This method evaluates each metric using only the parameters it requires. If a metric requires parameters that are not available in self.params, a KeyError will be raised.

Parameters:

metrics (list[BaseMetric]) – List of metrics to evaluate

Raises:

KeyError – If a required parameter is missing from self.params

Return type:

None

to_dataframe(id=None, id_columns_name=None)

Convert evaluation results to a pandas DataFrame.

Parameters:
  • id (Optional[str], optional) – Identifier for the evaluation (e.g., run_id)

  • id_columns_name (Optional[str], optional) – Name of the ID column in the DataFrame

Returns:

DataFrame containing evaluation metrics

Return type:

pd.DataFrame

to_dict()

Convert evaluation results to a dictionary.

Returns:

Dictionary containing metric names as keys and their scores as values

Return type:

dict

Raises:

ValueError – If metrics have not been evaluated yet

class qxmt.evaluation.evaluation.RegressionEvaluation(params, default_metrics_name=None, custom_metrics=None)

Bases: Evaluation

Evaluation class specifically for regression tasks.

Inherits from Evaluation and uses regression-specific metrics.

Parameters:
  • params (dict[str, Any])

  • default_metrics_name (list[str])

  • custom_metrics (list[BaseMetric])

DEFAULT_METRICS_NAME: list[str] = ['mean_absolute_error', 'root_mean_squared_error', 'r2_score']
NAME2METRIC: dict[str, Type[BaseMetric]] = {'mean_absolute_error': <class 'qxmt.evaluation.metrics.defaults_regression.MeanAbsoluteError'>, 'r2_score': <class 'qxmt.evaluation.metrics.defaults_regression.R2Score'>, 'root_mean_squared_error': <class 'qxmt.evaluation.metrics.defaults_regression.RootMeanSquaredError'>}
class qxmt.evaluation.evaluation.VQEEvaluation(params, default_metrics_name=None, custom_metrics=None)

Bases: Evaluation

Evaluation class specifically for Variational Quantum Eigensolver (VQE) tasks.

Inherits from Evaluation and uses VQE-specific metrics.

Parameters:
  • params (dict[str, Any])

  • default_metrics_name (list[str])

  • custom_metrics (list[BaseMetric])

DEFAULT_METRICS_NAME: list[str] = ['final_cost', 'hf_energy', 'fci_energy']
NAME2METRIC: dict[str, Type[BaseMetric]] = {'fci_energy': <class 'qxmt.evaluation.metrics.defaults_vqe.FCIEnergy'>, 'final_cost': <class 'qxmt.evaluation.metrics.defaults_vqe.FinalCost'>, 'hf_energy': <class 'qxmt.evaluation.metrics.defaults_vqe.HFEnergy'>}