"""Base metric calculator class for QWARD."""fromabcimportABC,abstractmethodfromtypingimportAny,Dict,Union,TYPE_CHECKINGfromqiskitimportQuantumCircuitifTYPE_CHECKING:fromqward.metrics.typesimportMetricsType,MetricsId
[docs]classMetricCalculator(ABC):""" Base class for all metric calculators in QWARD. This class defines the interface that all metric calculators must implement, providing methods for metric calculation and type identification. """def__init__(self,circuit:QuantumCircuit):""" Initialize a MetricCalculator object. Args: circuit: The quantum circuit to analyze """self._circuit=circuitself._metric_type=self._get_metric_type()self._id=self._get_metric_id()@propertydefmetric_type(self)->"MetricsType":""" Get the type of this metric. Returns: MetricsType: The type of this metric """returnself._metric_type@propertydefid(self)->"MetricsId":""" Get the ID of the metric. Returns: MetricsId: The ID of this metric """returnself._id@propertydefname(self)->str:""" Get the name of the metric. Returns: str: The name of the metric class. """returnself.__class__.__name__@propertydefcircuit(self)->QuantumCircuit:""" Get the quantum circuit. Returns: QuantumCircuit: The quantum circuit """returnself._circuit@abstractmethoddef_get_metric_type(self)->"MetricsType":""" Get the type of this metric. Returns: MetricsType: The type of this metric """pass@abstractmethoddef_get_metric_id(self)->"MetricsId":""" Get the ID of this metric. Returns: MetricsId: The ID of this metric """pass
[docs]@abstractmethoddefis_ready(self)->bool:""" Check if the metric is ready to be calculated. Returns: bool: True if the metric is ready to be calculated, False otherwise """pass
[docs]@abstractmethoddefget_metrics(self)->Union[Dict[str,Any],Any]:""" Get the metrics for this circuit. Returns: Union[Dict[str, Any], Any]: Dictionary of metric names and values, or a schema object """pass