qp.labs.tcdq.build_qudit_expval_func

build_qudit_expval_func(config)[source]

Build an estimator for expectation values of a qudit IQP circuit.

Returns a pure function that estimates the complex expectation value \(\langle O(\mathbf{l}, \mathbf{m}) \rangle\) for each observable by averaging over randomly sampled dit-strings.

The returned function captures precomputed data from config (generator matrices, default samples, preprocessed observables) so that repeated evaluations with different parameters are fast.

Parameters:

config (QuditCircuitConfig) – Full circuit description including gate structure, observables, and sampling parameters. See QuditCircuitConfig for details on how to construct one.

Returns:

A function with signature:

expval_fn(
    gates_params,
    phase_fn_params=None,
    key=None,
    n_samples=None,
    observables=None,
    init_state_elems=None,
    init_state_amps=None,
) -> (expvals, cov)

where expvals is a complex array of shape (n_obs,) containing the estimated moments, and cov has shape (n_obs, 2, 2) providing the real/imaginary covariance matrix of the mean estimator for each observable.

When config.phase_fn is set, the returned callable requires phase_fn_params to be passed as the second argument (the trainable parameters of the phase layer).

Return type:

Callable

Raises:

ValueError – If no observables are provided either in config or at call time.

Example

>>> import jax
>>> import jax.numpy as jnp
>>> from pennylane.labs.tcdq import QuditCircuitConfig, build_qudit_expval_func
>>> config = QuditCircuitConfig(
...     dims=3,
...     n_qudits=2,
...     gates={0: [{0: 1}], 1: [{1: 1}]},
...     n_samples=512,
...     key=jax.random.PRNGKey(0),
...     observables=(
...         jnp.array([[1, 0], [0, 1]], dtype=jnp.int32),
...         jnp.zeros((2, 2), dtype=jnp.int32),
...     ),
... )
>>> expval_fn = build_qudit_expval_func(config)
>>> params = jnp.array([0.2, -0.1])
>>> expvals, cov = expval_fn(params)
>>> expvals.shape, cov.shape
((2,), (2, 2, 2))