Skip to content

_cost_metrics

qubit_approximant.core.cost._cost_metrics

Metrics and their gradients to use in the cost function and optimization process.

grad_kl_divergence(fn, fn_approx, grad_fn_approx)

Returns the gradient of the KL divergence.

Source code in qubit_approximant/core/cost/_cost_metrics.py
51
52
53
def grad_kl_divergence(fn: NDArray, fn_approx: NDArray, grad_fn_approx: NDArray) -> NDArray:
    """Returns the gradient of the KL divergence."""
    return np.real(np.einsum("g, gi -> i", fn / fn_approx, grad_fn_approx)) / fn.size

grad_log_cosh(fn, fn_approx, grad_fn_approx)

Returns the gradiend of the logarithm of the hyperbolic cosine.

Source code in qubit_approximant/core/cost/_cost_metrics.py
63
64
65
66
def grad_log_cosh(fn: NDArray, fn_approx: NDArray, grad_fn_approx: NDArray) -> NDArray:
    """Returns the gradiend of the logarithm of the hyperbolic cosine."""
    fn_diff = fn_approx - fn
    return np.real(np.einsum("g, gi -> i", np.tanh(fn_diff), grad_fn_approx)) / fn.size

grad_mse(fn, fn_approx, grad_fn_approx)

Returns the gradient of the minimum square error.

Source code in qubit_approximant/core/cost/_cost_metrics.py
13
14
15
16
def grad_mse(fn: NDArray, fn_approx: NDArray, grad_fn_approx: NDArray) -> NDArray:
    """Returns the gradient of the minimum square error."""
    fn_diff = fn_approx - fn
    return 2 * np.real(np.einsum("g, gi -> i", fn_diff.conj(), grad_fn_approx)) / fn.size

grad_mse_weighted(fn, fn_approx, grad_fn_approx)

Returns the gradient of the weighted minimum square error.

Source code in qubit_approximant/core/cost/_cost_metrics.py
25
26
27
28
29
30
def grad_mse_weighted(fn: NDArray, fn_approx: NDArray, grad_fn_approx: NDArray) -> NDArray:
    """Returns the gradient of the weighted minimum square error."""
    fn_diff = fn_approx - fn
    return (
        2 * np.real(np.einsum("g, g, gi -> i", fn, fn_diff.conj(), grad_fn_approx)) / fn.size
    )  # fn is real!!

grad_rmse(fn, fn_approx, grad_fn_approx)

Returns the gradient of the root minimum square error.

Source code in qubit_approximant/core/cost/_cost_metrics.py
38
39
40
41
42
def grad_rmse(fn: NDArray, fn_approx: NDArray, grad_fn_approx: NDArray) -> NDArray:
    """Returns the gradient of the root minimum square error."""
    fn_diff = fn_approx - fn
    coef = 1 / (np.sqrt(fn.size) * np.sqrt(np.sum(np.abs(fn_diff) ** 2) + 1e-9))
    return coef * np.real(np.einsum("g, gi -> i", fn_diff.conj(), grad_fn_approx))

kl_divergence(fn, fn_approx)

Returns the KL divergence. This metric should be used with strictly real positive functions

Source code in qubit_approximant/core/cost/_cost_metrics.py
45
46
47
48
def kl_divergence(fn: NDArray, fn_approx: NDArray) -> float:
    """Returns the KL divergence. This metric should be used
    with strictly real positive functions"""
    return np.mean(fn * np.log(fn_approx / fn))

log_cosh(fn, fn_approx)

Returns the logarithm of the hyperbolic cosine. This metric should be used with strictly real positive functions

Source code in qubit_approximant/core/cost/_cost_metrics.py
56
57
58
59
60
def log_cosh(fn: NDArray, fn_approx: NDArray) -> float:
    """Returns the logarithm of the hyperbolic cosine. This metric
    should be used with strictly real positive functions"""
    fn_diff = fn_approx - fn
    return np.mean(np.log(np.cosh(fn_diff)))

mse(fn, fn_approx)

Returns the minimum square error.

Source code in qubit_approximant/core/cost/_cost_metrics.py
 7
 8
 9
10
def mse(fn: NDArray, fn_approx: NDArray) -> float:
    """Returns the minimum square error."""
    fn_diff = fn_approx - fn
    return np.mean(np.absolute(fn_diff) ** 2)

mse_weighted(fn, fn_approx)

Returns a weighted minimum square error.

Source code in qubit_approximant/core/cost/_cost_metrics.py
19
20
21
22
def mse_weighted(fn: NDArray, fn_approx: NDArray) -> float:
    """Returns a weighted minimum square error."""
    fn_diff = fn_approx - fn
    return np.mean(fn * np.absolute(fn_diff) ** 2)

rmse(fn, fn_approx)

Returns the root minimum square error.

Source code in qubit_approximant/core/cost/_cost_metrics.py
33
34
35
def rmse(fn: NDArray, fn_approx: NDArray) -> float:
    """Returns the root minimum square error."""
    return np.sqrt(mse(fn, fn_approx))