Skip to content

VQEResult

Source code in q3as/algo/vqe.py
@dataclass
class VQEResult:
    params: np.ndarray
    "The parameters of the best iteration"
    iter: int = 0
    "The number of iterations"
    reason: HaltReason = HaltReason.INTERRUPT
    "The reason why the optimization stopped"
    cost: Optional[float] = None
    "The value of the cost function of the best iteration"
    estimated: Optional[PrimitiveResult[PubResult]] = None
    "The result of the estimation of the best iteration"
    sampled: Optional[PrimitiveResult[SamplerPubResult]] = None
    "The result of the sampling"
    meas_counts: Optional[Dict[str, int]] = None
    "The bit string counts after sampling"
    interpreted: Optional[List[Tuple[Any, int]]] = None
    "The interpreted results of the sampling as defined by the application if given"

    def most_sampled(self) -> Optional[Any]:
        """
        Get the the most sampled value from the results.
        This is the interpreted value as defined by the application if given or otherwise the bitstring
        """
        samples = None
        if self.interpreted is not None:
            samples = [(count, value) for value, count in self.interpreted]
        elif self.meas_counts is not None:
            samples = [
                (count, bitstring) for bitstring, count in self.meas_counts.items()
            ]
        max_sample = max(samples, key=lambda x: x[0]) if samples is not None else None
        if max_sample is not None:
            return max_sample[1]
        return None

cost: Optional[float] = None class-attribute instance-attribute

The value of the cost function of the best iteration

estimated: Optional[PrimitiveResult[PubResult]] = None class-attribute instance-attribute

The result of the estimation of the best iteration

interpreted: Optional[List[Tuple[Any, int]]] = None class-attribute instance-attribute

The interpreted results of the sampling as defined by the application if given

iter: int = 0 class-attribute instance-attribute

The number of iterations

meas_counts: Optional[Dict[str, int]] = None class-attribute instance-attribute

The bit string counts after sampling

params: np.ndarray instance-attribute

The parameters of the best iteration

reason: HaltReason = HaltReason.INTERRUPT class-attribute instance-attribute

The reason why the optimization stopped

sampled: Optional[PrimitiveResult[SamplerPubResult]] = None class-attribute instance-attribute

The result of the sampling

most_sampled()

Get the the most sampled value from the results. This is the interpreted value as defined by the application if given or otherwise the bitstring

Source code in q3as/algo/vqe.py
def most_sampled(self) -> Optional[Any]:
    """
    Get the the most sampled value from the results.
    This is the interpreted value as defined by the application if given or otherwise the bitstring
    """
    samples = None
    if self.interpreted is not None:
        samples = [(count, value) for value, count in self.interpreted]
    elif self.meas_counts is not None:
        samples = [
            (count, bitstring) for bitstring, count in self.meas_counts.items()
        ]
    max_sample = max(samples, key=lambda x: x[0]) if samples is not None else None
    if max_sample is not None:
        return max_sample[1]
    return None