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 ansatzhamiltonian (
qibo.hamiltonians.SymbolicHamiltonian
) – Molecular Hamiltonian
- Returns:
Expectation value of the Hamiltonian for the given circuit
- Return type:
- 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 ansatzhamiltonian (
qibo.hamiltonians.SymbolicHamiltonian
) – Molecular Hamiltoniann_shots (int) – Number of times the circuit is run. Default:
1000
group_pauli_terms (str) – Whether or not to group Hamiltonian terms together to reduce the measurement cost. Available options:
None
: (Default) No grouping of Hamiltonian terms, and"qwc"
: Terms that commute qubitwise are grouped togethern_shots_per_pauli_term (bool) – Whether or not
n_shots
is used for each Pauli term (or group of terms) in the Hamiltonian, or for all the (group of) terms in the Hamiltonian. Default:True
;n_shots
are used to get the expectation value for each term (group of terms) in the Hamiltonian.shot_allocation – Iterable containing the number of shots to be allocated to each term (or group of terms) in the Hamiltonian respectively if the n_shots_per_pauli_term argument is
False
. Default:None
; shots are allocated based on the magnitudes of the coefficients of the Hamiltonian terms.
- Returns:
Hamiltonian expectation value for the given circuit using sample measurements
- Return type:
Measurement cost reduction¶
The following functions are used for reducing and optimising the measurement cost of obtaining the Hamiltonian expectation value using sample measurements instead of a state vector simulation.
- 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 (
qibo.hamiltonians.SymbolicHamiltonian
) – Hamiltonian of interestgrouping (str) – Whether or not to group Hamiltonian 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 terms
- Returns:
- List of two-tuples; the first item is a list of measurement gates (
qibo.gates.M
), and the second item is the corresponding list of Hamiltonian terms (
qibo.hamiltonian.terms.SymbolicTerm
).
- List of two-tuples; the first item is a list of measurement gates (
- 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.
- 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: