Source code for qolumbina.programs.benchmark_registry
# qolumbina/programs/benchmark_registry.py
import importlib
import pkgutil
from functools import wraps
from typing import Any
BENCHMARK_REGISTRY: dict[str, dict] = {}
[docs]
def register_benchmark(
name: str,
description: str | None = None,
family: str | None = None,
class_name: str | None = None,
source: dict[str, Any] | None = None,
testability_refactoring: list[str] | None = None,
functional_unit_tests: list[int] | None = None,
):
"""
Decorator to register a benchmark factory function.
The class_name will be automatically inferred from the return value
of the factory function on its first invocation, if not provided.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
obj = func(*args, **kwargs)
# ONLY auto-fill class_name if it was not explicitly provided
entry = BENCHMARK_REGISTRY[name]
if entry["class_name"] is None:
entry["class_name"] = obj.__class__.__name__
return obj
BENCHMARK_REGISTRY[name] = {
"func": wrapper, # wrapped factory function
"description": description,
"class_name": class_name, # None -> auto-filled on first call
"program_family": family,
"program_type": None, # to be filled later
"source": source, # recipe source information
"implementation_language": "Qiskit", # fixed as Qiskit
"testability_refactoring": testability_refactoring,
"functional_unit_tests": (
{"pass": functional_unit_tests[0], "fail": functional_unit_tests[1]}
if functional_unit_tests is not None
else None
),
}
return wrapper
return decorator
[docs]
def load_all_benchmarks():
"""
Dynamically import all sub-modules under qolumbina.programs
to trigger benchmark registration via decorators.
"""
import qolumbina.programs
for _, module_name, _ in pkgutil.iter_modules(qolumbina.programs.__path__):
importlib.import_module(f"qolumbina.programs.{module_name}")