EstimatorFromSampler#

Module: iqm.qaoa.backends

class iqm.qaoa.backends.EstimatorFromSampler(sampler, shots, cvar=None)[source]#

Bases: EstimatorBackend

The estimator class for calculating the expectation value using counts obtained from a sampler.

Takes an instance of a subclass of SamplerBackend and uses it to generate samples from the QAOA. These energy of these samples is then calculated classically and averaged-out to produce an estimate of the expectation value of the Hamiltonian. If cvar is provided, the estimator returns not the average of the energies, but its CVaR at the cvar threshold.

Parameters:
  • sampler (SamplerBackend) – The sampler to produce the samples.

  • shots (int) – The number of shots that should be produced with the sampler.

  • cvar (float | None) – The threshold used to calculate CVaR (if provided).

Raises:

ValueError – If cvar is provided, but it’s not between 0 and 1.

Methods

estimate

Calculates the expectation value of the Hamiltonian by sampling from the QAOA circuit.

estimate_correlations_z

The method for estimating the exp.

estimate(qaoa_object, **kwargs)[source]#

Calculates the expectation value of the Hamiltonian by sampling from the QAOA circuit.

Uses the sampler provided at initialization to sample from the QAOA circuit and then calculates the expectation value from the counts.

Parameters:
  • qaoa_object (QUBOQAOA) – The instance of QUBOQAOA whose expectation value is to be calculated.

  • **kwargs (Any) – Keyword arguments passed through to the sample(). In practice, this is often just seed_transpiler for the samplers which allow input seed to derandomize the circuit transpilation.

Returns:

The average energy of the sampled docstrings (to serve as estimation of the expectation value).

Return type:

float

estimate_correlations_z(qaoa_object, target_qubits, **kwargs)[source]#

The method for estimating the exp. value of products of Z operators on target_qubits.

The correlations are picked out from the counts. Each bitstring contributes to the exp. value as follows: 1. The positions in the bitstrings corresponding to target_qubits are located. 2. The values at the picked positions are transformed as “0” -> 1 and “1” -> -1. 3. These values are multiplied together. 4. The results for all bitstrings are averaged-out (weighted by their corresponding counts).

Examples

Bitstring

target_qubits

Contribution of this bitstring

"011100001"

\(\{3, 6, 8\}\)

\((-1)\cdot(1)\cdot(-1) = 1\)

"011100001"

\(\{0, 1\}\)

\((1)\cdot(-1) = -1\)

Parameters:
  • qaoa_object (QUBOQAOA) – The QAOA object whose correlations are to be estimated.

  • target_qubits (set[LogQubit] | list[set[LogQubit]]) – The set of qubits on which the operators act, or a list thereof.

  • **kwargs (Any) – Keyword arguments passed through to the sample(). In practice, this is often just seed_transpiler for the samplers which allow input seed to derandomize the circuit transpilation.

Returns:

The estimated expected value of product of Z operators on given target_qubits. Or a list of those, if target_qubits was given as a list.

Return type:

float | list[float]

Inheritance

Inheritance diagram of iqm.qaoa.backends.EstimatorFromSampler