qram_simulator.baseline.metrics 源代码

"""
Distribution metrics shared by both sides of the baseline comparison.

The QRAM-Simulator side returns ``{"addr:bus": prob}`` dictionaries (from
``QRAMCircuitQubit.get_output_distribution``) and the circuit side returns the
same key convention (``CircuitQRAMQubit.marginal_distribution``), so all
metrics below are convention-identical on the two sides:

- ``classical_fidelity``: (sum sqrt(p q))^2 over the joint (addr, bus) keys;
- ``tvd``: total variation distance of the same;
- renormalization is applied per side by its own total (survival / trace)
  before comparison whenever either side is sub-normalized (Damping).
"""
from __future__ import annotations
import math as math
import typing
__all__: list = ['classical_fidelity', 'tvd', 'dist_total', 'normalize', 'mean_of_dists']
def _keys(p: Mapping[str, float], q: Mapping[str, float]):
    ...
[文档] def classical_fidelity(p: Mapping[str, float], q: Mapping[str, float]) -> float: """ Classical distribution fidelity (sum of sqrt(p*q))^2. """
[文档] def dist_total(p: Mapping[str, float]) -> float: """ Total mass (survival probability of a sub-normalized distribution). """
[文档] def mean_of_dists(dists: typing.Iterable[Mapping[str, float]]) -> dict[str, float]: """ Element-wise mean over a collection of distributions (missing keys = 0). """
[文档] def normalize(p: Mapping[str, float]) -> dict[str, float]: """ Return p divided by its total mass (identity when already normalized). """
[文档] def tvd(p: Mapping[str, float], q: Mapping[str, float]) -> float: """ Total variation distance 0.5 * sum |p - q|. """