Skip to content

metric_results

qubit_approximant.benchmarking.metrics.metric_results

metric_results(fn, fn_kwargs, circuit, params_list)

Returns 4 lists of error metrics, one for each layer. The metrics are: - L1 norm - L2 norm - Infinity norm - Infidelity

Parameters:

Name Type Description Default
fn Callable

Function we want to approximate.

required
fn_kwargs dict

Keyword arguments for 'fn'.

required
circuit Circuit

Circuit used to model 'fn'.

required
params_list list[NDArray]

List of parameters for the circuit with different number of layers.

required

Returns:

Type Description
Tuple[list[float], ...]

Returns lists of L1 norms, L2 norms, infinity norms and infidelities for every number of layers.

Source code in qubit_approximant/benchmarking/metrics/metric_results.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def metric_results(
    fn: Callable, fn_kwargs: dict, circuit: Circuit, params_list: list[NDArray]
) -> Tuple[list[float], ...]:
    """Returns 4 lists of error metrics, one for each layer. The metrics are:
        - L1 norm
        - L2 norm
        - Infinity norm
        - Infidelity

    Parameters
    ----------
    fn : Callable
        Function we want to approximate.
    fn_kwargs : dict
        Keyword arguments for 'fn'.
    circuit : Circuit
        Circuit used to model 'fn'.
    params_list : list[NDArray]
        List of parameters for the circuit with different number of layers.

    Returns
    -------
    Tuple[list[float], ...]
        Returns lists of L1 norms, L2 norms, infinity norms and infidelities
        for every number of layers.
    """
    l1_list = []
    l2_list = []
    inf_list = []
    infidelity_list = []

    save_model_x = circuit.x.copy()  # copy just in case, although not necessary
    x_limits = (circuit.x[0], circuit.x[-1])

    def fn_eval(x):
        return fn(x, **fn_kwargs)

    for params in params_list:

        def fn_approx_eval(x):
            circuit.x = np.array([x])
            return circuit.encoding(params)[0]  # one element array  # noqa: B023

        l1_list.append(l1_norm(fn_eval, fn_approx_eval, x_limits))
        l2_list.append(l2_norm(fn_eval, fn_approx_eval, x_limits))
        infidelity_list.append(infidelity(fn_eval, fn_approx_eval, x_limits))

        def fn_approx_inf_eval(x):
            circuit.x = np.array(x)
            return circuit.encoding(params)  # one element array  # noqa: B023

        inf_list.append(inf_norm(fn_eval, fn_approx_inf_eval, x_limits))

    circuit.x = save_model_x

    return l1_list, l2_list, inf_list, infidelity_list