Measurement

This section covers the API reference for obtaining the expectation value of a (molecular) Hamiltonian, either from a state vector simulation, or from sample measurements.

Expectation value of Hamiltonian

qibochem.measurement.result.expectation(circuit: Circuit, hamiltonian: SymbolicHamiltonian)

Expectation value using state vector simulations

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

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

Returns:

Expectation value of the Hamiltonian for the given circuit

Return type:

float

qibochem.measurement.result.expectation_from_samples(circuit: Circuit, hamiltonian: SymbolicHamiltonian, n_shots: int = 1000, group_pauli_terms=None, n_shots_per_pauli_term: bool = True, shot_allocation=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

  • group_pauli_terms – Whether or not to group Pauli X/Y terms in the Hamiltonian together to reduce the measurement cost. Available options: None: (Default) Hamiltonian terms containing X/Y are not grouped together, and "qwc": Terms that commute qubitwise are grouped together

  • n_shots_per_pauli_term (bool) – Whether or not n_shots is used for each Pauli term in the Hamiltonian, or for all the terms in the Hamiltonian. Default: True; n_shots are used to get the expectation value for each term in the Hamiltonian.

  • shot_allocation – Iterable containing the number of shots to be allocated to each term in the Hamiltonian respectively if n_shots_per_pauli_term is False. Default: None; shots are allocated based on the magnitudes of the coefficients of the Hamiltonian terms.

Returns:

Hamiltonian expectation value

Return type:

float

Measurement cost reduction

The following functions are used for reducing and optimising the measurement cost of obtaining the Hamiltonian expectation value when sample measurements are used.

qibochem.measurement.optimization.measurement_basis_rotations(hamiltonian, grouping=None)

Split up and sort the Hamiltonian terms to get the basis rotation gates to be applied to a quantum circuit for the respective (group of) terms in the Hamiltonian

Parameters:
  • hamiltonian (SymbolicHamiltonian) – Hamiltonian of interest

  • grouping – Whether or not to group the X/Y terms together, i.e. use the same set of measurements to get the expectation values of a group of terms simultaneously. Default value of None will not group any terms together, while "qwc" will group qubitwise commuting terms together, and return the measurement gates associated with each group of X/Y terms

Returns:

List of two-tuples, with each tuple given as ([list of measurement gates], [term1, term2, …]), where

term1, term2, … are SymbolicTerms. The first tuple always corresponds to all the Z terms present, which will be two empty lists - ([], []) - if there are no Z terms present.

Return type:

list

qibochem.measurement.optimization.allocate_shots(grouped_terms, n_shots, method=None, max_shots_per_term=None)

Allocate shots to each group of terms in the Hamiltonian for calculating the expectation value of the Hamiltonian.

Parameters:
  • grouped_terms (list) – Output of measurement_basis_rotations

  • n_shots (int) – Total number of shots to be allocated

  • method (str) – How to allocate the shots. The available options are: "c"/"coefficients": n_shots is distributed based on the relative magnitudes of the term coefficients, "u"/"uniform": n_shots is distributed evenly amongst each term. Default value: "c".

  • max_shots_per_term (int) – Upper limit for the number of shots allocated to an individual group of terms. If not given, will be defined as a fraction (largest coefficient over the sum of all coefficients in the Hamiltonian) of n_shots.

Returns:

A list containing the number of shots to be used for each group of Pauli terms respectively.

Return type:

list