Measurement

This section covers the functionality for obtaining the expectation value of a (molecular) Hamiltonian using circuit measurements

Expectation value of Hamiltonian

qibochem.measurement.result.expectation_from_samples(circuit: Circuit, hamiltonian: SymbolicHamiltonian, n_shots: int = 1000, grouping: str | None = None, n_shots_per_pauli_term: bool = True, shot_allocation: list[int] | None = None) float

Calculate expectation value of some Hamiltonian using sample measurements from running a Qibo quantum circuit

Parameters:
  • circuit (qibo.models.Circuit) – Quantum circuit ansatz

  • hamiltonian (qibo.hamiltonians.SymbolicHamiltonian) – Molecular Hamiltonian

  • n_shots (int) – Number of times the circuit is run. Default: 1000

  • grouping (str | None) –

    Whether to group Hamiltonian terms together to reduce the measurement cost

    Options:
    • None: No grouping of Hamiltonian terms (Default)

    • "qwc": Qubit-wise commuting terms are grouped together

    • "gc": Generally commuting terms are measured simultaneously by adding additional gates following the formulation by Gokhale et al.,

    • "gc2": Same as "gc", but uses the circuit formulation by Yen et al. instead

  • n_shots_per_pauli_term (bool) – If True (Default), uses n_shots per Pauli term (or group of terms) to calculate the expectation value

  • shot_allocation (list[int]) – Shot allocation per Pauli term (or group of terms) when n_shots_per_pauli_term is False.

Returns:

Hamiltonian expectation value for the given circuit using sample measurements

Return type:

float

qibochem.measurement.result.v_expectation(circuit: Circuit, hamiltonian: SymbolicHamiltonian, n_shots: int, n_trial_shots: int, grouping: str | None = None, method: str = 'vmsa') float

An alternative loss function for finding the expectation value of a Hamiltonian using shots. Shots are allocated according to the Variance-Minimized Shot Assignment (VMSA) or Variance-Preserved Shot Reduction (VPSR) approaches suggested in the reference paper (given below).

Essentially, a uniform number of trial shots are first used to find the sample variance for each term (group) in the Hamiltonian. For the VMSA method, the remaining shots are all allocated to minimise the total variance (calculated as the sum of the variances), while for the VPSR method, a sufficient number of shots are allocated to each term (group) to keep their variance - and by extension, the total variance - below a certain threshold. Unlike in the VMSA method, the VPSR method does not allocate all of the remaining shots.

Parameters:
  • circuit (qibo.models.Circuit) – Circuit ansatz for running VQE

  • hamiltonian (qibo.hamiltonians.SymbolicHamiltonian) – Hamiltonian of interest

  • n_shots (int) – Total number of shots for finding the Hamiltonian expectation value

  • n_trial_shots (int) – Number of shots to use for finding the sample variance for each Hamiltonian term

  • grouping (str | None) – Whether to group Hamiltonian terms together. The available options are: None (Default), "qwc", "gc", and "gc2" (see expectation_from_samples for details)

  • method (str) – Variance-based method to use; must be either “vmsa” (default) or “vpsr”.

Returns:

Hamiltonian expectation value obtained using a variance-based shot allocation scheme

Return type:

float

Reference:

1. L. Zhu, S. Liang, C. Yang, X. Li, Optimizing Shot Assignment in Variational Quantum Eigensolver Measurement, Journal of Chemical Theory and Computation, 2024, 20, 2390-2403 (link)

Utility functions

qibochem.measurement.result.sample_statistics(circuit: Circuit, grouped_terms: list[tuple[Expr, list[Gate]]], n_shots: int = 1000) tuple[list[float], list[float]]

An alternative to the expectation_from_samples function when both the expectation values and sample variances are of interest. Unlike expectation_from_samples, this function does not have the flexibility of allocating shots specifically to each term (group) in the Hamiltonian; a fixed number of shots will be allocated to each term (group) instead.

Parameters:
  • circuit (qibo.models.Circuit) – Quantum circuit ansatz

  • grouped_terms (list[tuple[Expr, list[Gate]]]) – Groups of Pauli terms and their corresponding rotation gates.

  • n_shots (int) – Number of times the circuit is run for each Hamiltonian term (group). Default: 1000

Returns:

Sample means (expectation values) and variances for each Hamiltonian term (group)

Return type:

tuple[list[float], list[float]]