POVMPostProcessor¶
- class POVMPostProcessor(povm_sample: POVMPubResult, dual: BaseDual | None = None)[source]¶
Bases:
object
The canonical POVM result post-processor.
This post-processor implementation provides a straight-forward interface for computing the expectation values (and standard deviations) of any Pauli-based observable. It is initialized with a
POVMPubResult
as shown below:>>> from povm_toolbox.library import ClassicalShadows >>> from povm_toolbox.sampler import POVMSampler >>> from povm_toolbox.post_processor import POVMPostProcessor >>> from qiskit.circuit import QuantumCircuit >>> from qiskit.primitives import StatevectorSampler >>> from qiskit.quantum_info import SparsePauliOp >>> circ = QuantumCircuit(2) >>> _ = circ.h(0) >>> _ = circ.cx(0, 1) >>> povm = ClassicalShadows(2, seed=42) >>> sampler = StatevectorSampler(seed=42) >>> povm_sampler = POVMSampler(sampler) >>> job = povm_sampler.run([circ], povm=povm, shots=16) >>> result = job.result() >>> post_processor = POVMPostProcessor(result[0]) >>> post_processor.get_expectation_value(SparsePauliOp("ZI")) (-0.75, 0.33541019662496846)
Additionally, this post-processor also supports the customization of the Dual frame in which the decomposition weights of the provided observable are obtained. Check out this how-to guide for more details on how to do this.
Initialize the POVM post-processor.
- Parameters:
povm_sample (POVMPubResult) – a result from a POVM sampler run.
dual (BaseDual | None) – the Dual frame that will be used to obtain the decomposition weights of an observable when computing its expectation value. For more details, refer to
get_decomposition_weights()
. When this isNone
, the standard “state-average” Dual frame will be constructed from the POVM stored in thepovm_sample
’sPOVMPubResult.metadata
.
- Raises:
ValueError – If the provided
dual
is not a dual frame to the POVM stored in thepovm_samples
’sPOVMPubResult.metadata
.
Attributes
- counts¶
Return the histogram of the POVM outcomes via
POVMPubResult.get_counts()
.
- dual¶
Return the Dual frame that is used.
Warning
If the dual frame is not already built, accessing this property could be computationally demanding.
- povm¶
Return the POVM definition that was used to sample outcomes.
Methods
- get_decomposition_weights(observable: SparsePauliOp, outcome_set: set[Any]) dict[Any, float] [source]¶
Get the decomposition weights of
observable
into the elements ofpovm
.Given an observable \(O\) which is in the span of a POVM (here,
povm
), one can write \(O\) as the weighted sum of the POVM effects, \(O = \sum_k w_k M_k\) for real weights \(w_k\) and where \(k\) labels the outcomes.See also
BaseDual.get_omegas()
.- Parameters:
observable (SparsePauliOp) – the observable to be decomposed into the POVM effects.
outcome_set (set[Any]) – set of outcome labels indicating which decomposition weights are queried. An outcome of a
ProductPOVM
is labeled by a tuple of integers for instance. For aMultiQubitPOVM
, an outcome is simply labeled by an integer.
- Returns:
A dictionary mapping outcome labels to decomposition weights.
- Return type:
- get_expectation_value(observable: SparsePauliOp, *, loc: int | tuple[int, ...] | None = None) tuple[ndarray, ndarray] | tuple[float, float] [source]¶
Return the expectation value and standard deviation of the given
observable
.- Parameters:
observable (SparsePauliOp) – the observable whose expectation value is queried.
loc (int | tuple[int, ...] | None) – this argument is relevant if multiple sets of parameter values were supplied to the sampler in the same
POVMSamplerPub
. The indexloc
then corresponds to the set of parameter values that was supplied to the sampler through the Pub. IfNone
, the expectation value (and standard deviation) for each set of circuit parameters is returned.
- Returns:
A tuple of (estimated) expectation value(s) and standard deviation(s). If a single value was queried (via
loc
), both of these will be afloat
. Otherwise, they will be instances ofnumpy.ndarray
.- Return type: