Skip to content

evaluate

Evaluation of compiled scalar graphs using exact arithmetic.

evaluate

evaluate(
    circuit: CompiledScalarGraphs, param_vals: Array
) -> Array

Evaluate compiled circuit with batched parameter values.

Each term family (NodePhases, HalfPiPhases, PiProducts, PhasePairs) computes its own contribution via .evaluate(param_vals). This function multiplies those together with the per-graph ScalarPrefactor and folds in power2 / any approximate floatfactor.

Parameters:

Name Type Description Default
circuit CompiledScalarGraphs

Compiled circuit representation.

required
param_vals Array

Binary parameter values (error bits + measurement/detector outcomes), shape (batch_size, n_params).

required

Returns:

Type Description
Array

Complex array of shape (batch_size,) — the per-sample amplitude.

Source code in src/tsim/compile/evaluate.py
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
@jax.jit
def evaluate(circuit: CompiledScalarGraphs, param_vals: Array) -> Array:
    """Evaluate compiled circuit with batched parameter values.

    Each term family (``NodePhases``, ``HalfPiPhases``, ``PiProducts``,
    ``PhasePairs``) computes its own contribution via ``.evaluate(param_vals)``.
    This function multiplies those together with the per-graph
    ``ScalarPrefactor`` and folds in ``power2`` / any approximate floatfactor.

    Args:
        circuit: Compiled circuit representation.
        param_vals: Binary parameter values (error bits + measurement/detector
            outcomes), shape ``(batch_size, n_params)``.

    Returns:
        Complex array of shape ``(batch_size,)`` — the per-sample amplitude.

    """
    prefactor = circuit.prefactor
    if prefactor.phase_indices.shape[0] == 0:
        return jnp.zeros(param_vals.shape[0], dtype=jnp.complex64)

    static_phases = ExactScalarArray(UNIT_PHASES[prefactor.phase_indices])
    float_factor = ExactScalarArray(prefactor.floatfactor)

    total = functools.reduce(
        operator.mul,
        [
            circuit.node_phases.evaluate(param_vals),
            circuit.halfpi_phases.evaluate(param_vals),
            circuit.pi_products.evaluate(param_vals),
            circuit.phase_pairs.evaluate(param_vals),
            static_phases,
            float_factor,
        ],
    )

    if not prefactor.has_approximate_floatfactors:
        total = ExactScalarArray(total.coeffs, total.power + prefactor.power2)
        return total.sum().to_complex()

    return jnp.sum(
        total.to_complex() * prefactor.approximate_floatfactors * 2.0**prefactor.power2,
        axis=-1,
    )