Skip to content

Post processing

generate_post_processing

generate_post_processing(
    mt: Method[Params, ReturnType],
) -> (
    None
    | typing.Callable[
        [np.ndarray], typing.Iterator[ReturnType]
    ]
)

Generate a post-processing function to extract user-level values from the raw measurement results.

Parameters:

Name Type Description Default
mt Method[Params, ReturnType]

The entry point of the program

required

Returns:

Type Description
Callable[[ndarray], ReturnType] | None

A function that takes in a 2D numpy array

None | Callable[[ndarray], Iterator[ReturnType]]

of raw measurement results and yields user-level results. The input array shape is

None | Callable[[ndarray], Iterator[ReturnType]]

(n_shots, n_measurements), where each row corresponds to a measurement result and each

None | Callable[[ndarray], Iterator[ReturnType]]

column corresponds to a shot. The output is an iterator over user-level results for

None | Callable[[ndarray], Iterator[ReturnType]]

each shot. If the user-level results cannot be determined, returns None.

Source code in .venv/lib/python3.12/site-packages/bloqade/gemini/post_processing.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def generate_post_processing(
    mt: ir.Method[Params, ReturnType],
) -> None | typing.Callable[[np.ndarray], typing.Iterator[ReturnType]]:
    """Generate a post-processing function to extract user-level values from the raw measurement results.


    Args:
        mt (ir.Method[Params, ReturnType]): The entry point of the program

    Returns:
        (typing.Callable[[ndarray], ReturnType] | None): A function that takes in a 2D numpy array
        of raw measurement results and yields user-level results. The input array shape is
        (n_shots, n_measurements), where each row corresponds to a measurement result and each
        column corresponds to a shot. The output is an iterator over user-level results for
        each shot. If the user-level results cannot be determined, returns None.

    """

    _, user_output = MeasurementIDAnalysis(mt.dialects).run(mt)
    func = typing.cast(
        typing.Callable[[np.ndarray], ReturnType],
        _post_processing_function(user_output),
    )
    if func is None:
        return None

    def _generate_user_results(measurements: np.ndarray):
        """A generator that yields user-level results from raw measurement results.

        Args:
            measurements (np.ndarray): A 2D numpy array of raw measurement results with shape
            (n_shots, n_measurements).

        Yields:
            User-level results for each shot.

        """
        yield from map(func, measurements[:])

    return _generate_user_results