This section covers the API reference for obtaining the expectation value of a (molecular) Hamiltonian

qibochem.measurement.optimization.measurement_basis_rotations(hamiltonian, n_qubits, 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

  • hamiltonian (SymbolicHamiltonian) – Hamiltonian (that only contains X/Y terms?)

  • n_qubits – Number of qubits in the quantum circuit.

  • 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, which is the only option currently implemented.


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:


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.

  • grouped_terms (list) – Output of measurement_basis_rotations(hamiltonian, n_qubits, grouping=None

  • 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.


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

Return type:


qibochem.measurement.expectation(circuit: Circuit, hamiltonian: SymbolicHamiltonian, from_samples: bool = False, 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 either the state vector or sample measurements from running a quantum circuit

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

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

  • from_samples (bool) – Whether the expectation value calculation uses samples or the simulated state vector. Default: False; Results are from a state vector simulation

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

  • group_pauli_terms – Whether or not to group Pauli X/Y terms in the Hamiltonian together to reduce the measurement cost. Default: None; each of the Hamiltonian terms containing X/Y are in their own individual groups.

  • 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.


Hamiltonian expectation value

Return type: