Metrics Base & Types

Base Metric Calculator Class

The MetricCalculator class is the abstract base class for all metric calculators in QWARD. It defines the unified interface that all metric implementations follow.

Key Features: - Unified Interface: All metric calculators implement get_metrics() returning schema objects - Type Safety: Schema-based validation with automatic error checking - Extensible: Easy to create custom metric calculators

Usage Pattern:

from qward.metrics.base_metric import MetricCalculator
from qward.metrics.types import MetricsType, MetricsId
from pydantic import BaseModel

class MyCustomMetricsSchema(BaseModel):
    custom_value: float
    circuit_signature: str

class MyCustomMetric(MetricCalculator):
    def _get_metric_type(self) -> MetricsType:
        return MetricsType.PRE_RUNTIME

    def _get_metric_id(self) -> MetricsId:
        return MetricsId.QISKIT  # Or define new ID

    def is_ready(self) -> bool:
        return self.circuit is not None

    def get_metrics(self) -> MyCustomMetricsSchema:
        # Custom calculation logic
        return MyCustomMetricsSchema(
            custom_value=self.circuit.depth() * self.circuit.num_qubits,
            circuit_signature=f"{self.circuit.num_qubits}q_{self.circuit.depth()}d"
        )

API Reference

Base metric calculator class for QWARD.

class MetricCalculator(circuit)[source]

Bases: 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.

Initialize a MetricCalculator object.

Parameters:

circuit (QuantumCircuit) – The quantum circuit to analyze

property circuit: QuantumCircuit

Get the quantum circuit.

Returns:

The quantum circuit

Return type:

QuantumCircuit

abstract get_metrics()[source]

Get the metrics for this circuit.

Returns:

Dictionary of metric names and values, or a schema object

Return type:

Union[Dict[str, Any], Any]

property id: MetricsId

Get the ID of the metric.

Returns:

The ID of this metric

Return type:

MetricsId

abstract is_ready()[source]

Check if the metric is ready to be calculated.

Returns:

True if the metric is ready to be calculated, False otherwise

Return type:

bool

property metric_type: MetricsType

Get the type of this metric.

Returns:

The type of this metric

Return type:

MetricsType

property name: str

Get the name of the metric.

Returns:

The name of the metric class.

Return type:

str

class MetricCalculator(circuit)[source]

Bases: 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.

Initialize a MetricCalculator object.

Parameters:

circuit (QuantumCircuit) – The quantum circuit to analyze

property circuit: QuantumCircuit

Get the quantum circuit.

Returns:

The quantum circuit

Return type:

QuantumCircuit

abstract get_metrics()[source]

Get the metrics for this circuit.

Returns:

Dictionary of metric names and values, or a schema object

Return type:

Union[Dict[str, Any], Any]

property id: MetricsId

Get the ID of the metric.

Returns:

The ID of this metric

Return type:

MetricsId

abstract is_ready()[source]

Check if the metric is ready to be calculated.

Returns:

True if the metric is ready to be calculated, False otherwise

Return type:

bool

property metric_type: MetricsType

Get the type of this metric.

Returns:

The type of this metric

Return type:

MetricsType

property name: str

Get the name of the metric.

Returns:

The name of the metric class.

Return type:

str

Metric Enums

Types for QWARD metrics.

class MetricsId(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Enum for the different types of metrics available.

BEHAVIORAL = 'BEHAVIORAL'
CIRCUIT_PERFORMANCE = 'CIRCUIT_PERFORMANCE'
COMPLEXITY = 'COMPLEXITY'
ELEMENT = ('ELEMENT',)
QISKIT = 'QISKIT'
QUANTUM_SPECIFIC = 'QUANTUM_SPECIFIC'
STRUCTURAL = 'STRUCTURAL'
class MetricsType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Enum for the different types of metrics available.

POST_RUNTIME = 'POST_RUNTIME'
PRE_RUNTIME = 'PRE_RUNTIME'
class MetricsId(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Enum for the different types of metrics available.

BEHAVIORAL = 'BEHAVIORAL'
CIRCUIT_PERFORMANCE = 'CIRCUIT_PERFORMANCE'
COMPLEXITY = 'COMPLEXITY'
ELEMENT = ('ELEMENT',)
QISKIT = 'QISKIT'
QUANTUM_SPECIFIC = 'QUANTUM_SPECIFIC'
STRUCTURAL = 'STRUCTURAL'
class MetricsType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Enum for the different types of metrics available.

POST_RUNTIME = 'POST_RUNTIME'
PRE_RUNTIME = 'PRE_RUNTIME'