"""Telemetry event logging for QDK Chemistry module."""
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from __future__ import annotations
import time
from functools import wraps
from typing import Any
from .telemetry import log_telemetry
[docs]
def get_basis_functions_bucket(basis_functions: str | int) -> str:
"""Categorize the number of basis functions into buckets for telemetry aggregation.
This function groups number of basis functions into discrete buckets to enable
meaningful aggregation and analysis in telemetry data. Rather than tracking
exact counts (which would result in too many unique values), this bucketing
approach provides useful ranges for performance and usage analysis.
Args:
basis_functions: The number of basis functions.
Returns:
The bucket that the basis function count falls into.
Examples:
>>> get_basis_functions_bucket(7)
"10"
>>> get_basis_functions_bucket(23)
"30"
>>> get_basis_functions_bucket(150)
"150"
>>> get_basis_functions_bucket(750)
"800"
>>> get_basis_functions_bucket(1500)
"1500+"
>>> get_basis_functions_bucket("unknown")
"unknown"
"""
if basis_functions == "unknown":
return "unknown"
basis_functions = int(basis_functions)
if basis_functions < 50:
# Buckets of 10 for basis functions 1-49
return str(((basis_functions - 1) // 10 + 1) * 10)
if basis_functions <= 500:
# Buckets of 50 for basis functions 50-500
return str(((basis_functions - 1) // 50 + 1) * 50)
if basis_functions < 1500:
# Buckets of 100 for basis functions 501-1499
return str(((basis_functions - 1) // 100 + 1) * 100)
# Returns "1500+" for basis functions >= 1500
return "1500+"
[docs]
def on_qdk_chemistry_import() -> None:
"""Logs a telemetry event indicating that the QDK Chemistry module has been imported."""
log_telemetry("qdk_chemistry.import", 1)
[docs]
def on_algorithm(algorithm_type: str, algorithm_name: str) -> None:
"""Logs a telemetry event for the execution of a quantum chemistry algorithm.
Args:
algorithm_type: The type or category of the algorithm being executed.
algorithm_name: The specific name of the algorithm.
"""
log_telemetry(
"qdk_chemistry.algorithm",
1,
properties={"algorithm_type": algorithm_type, "algorithm_name": algorithm_name},
)
[docs]
def on_algorithm_end(
algorithm_type: str,
duration_sec: float,
status: str,
algorithm_name: str,
error_type: str | None = None,
**properties,
) -> None:
"""Logs the execution duration and outcome of a chemistry algorithm.
Logs relevant metadata about algorithm execution including timing,
success/failure status, and additional contextual information.
Args:
algorithm_type: The category of algorithm executed (e.g.,
'scf_solver', 'active_space_selector').
duration_sec: The time taken to execute the algorithm,
in seconds.
status: The result of the execution, typically 'success'
or 'failed'.
algorithm_name: The specific implementation or backend
used (e.g., 'qdk', 'pyscf').
error_type: The type of error encountered, if
any. Defaults to None.
properties: Additional contextual information about the
execution (e.g., 'num_basis_functions').
"""
telemetry_properties = {
"algorithm_type": algorithm_type,
"algorithm_name": algorithm_name,
"status": status,
"error_type": error_type,
**properties,
}
log_telemetry(
"qdk_chemistry.algorithm.durationSec",
duration_sec,
properties=telemetry_properties,
type="histogram",
)
[docs]
def telemetry_tracker():
"""Decorator to track telemetry for algorithm run execution."""
def decorator(run_method):
@wraps(run_method)
def wrapper(self, *args, **kwargs):
start_time = time.perf_counter()
try:
result = run_method(self, *args, **kwargs)
duration = time.perf_counter() - start_time
try:
n_basis = extract_data(result)
on_algorithm_end(
algorithm_type=self.type_name(),
algorithm_name=self.name(),
duration_sec=duration,
status="success",
num_basis_functions=n_basis,
)
except (AttributeError, TypeError, IndexError):
on_algorithm_end(
algorithm_type=self.type_name(),
algorithm_name=self.name(),
duration_sec=duration,
status="success",
)
return result
except Exception as e:
duration = time.perf_counter() - start_time
on_algorithm_end(
algorithm_type=self.type_name(),
algorithm_name=self.name(),
duration_sec=duration,
status="failed",
error_type=type(e).__name__,
)
raise
return wrapper
return decorator