Skip to content

New module with stand-alone functions for computation of expected values#1716

Draft
andre-juan wants to merge 19 commits intomasterfrom
pauli_lin_comb_expval
Draft

New module with stand-alone functions for computation of expected values#1716
andre-juan wants to merge 19 commits intomasterfrom
pauli_lin_comb_expval

Conversation

@andre-juan
Copy link
Contributor

@andre-juan andre-juan commented Sep 16, 2025

Resolves to #1715


Implementation of a new module with functions for the calculation of the expected value of a Hamiltonian expressed as a linear combination of Pauli words, either in infinite precision from the state (by building a sparse matrix for each Pauli word separately); or from samples (by performing the appropriate measurements, including the grouping of commuting terms).

Here's a simple example executing the new functions for the Heisenberg XXZ model Hamiltonian on n=4 qubits with delta=0.5:

from qibo.models.encodings import hamming_weight_encoder
from qibo.quantum_info import random_statevector
from qibo.hamiltonians.expectation_value import (
    get_expval_from_linear_comb_of_paulis_from_samples,
    get_expval_from_linear_comb_of_paulis_from_statevector,
)
from scipy.special import comb

nqubits = 4
weight = 2
dim = int(comb(nqubits, weight))

seed = 100
np.random.seed(seed)
circuit = hamming_weight_encoder(random_statevector(dim, seed=seed), nqubits, weight)

# XXZ hamiltonian, n=4, delta=0.5
lin_comb_pauli = [
    (1.0, "XXII"),
    (1.0, "IXXI"),
    (1.0, "IIXX"),
    (1.0, "XIIX"),
    (1.0, "YYII"),
    (1.0, "IYYI"),
    (1.0, "IIYY"),
    (1.0, "YIIY"),
    (0.5, "ZZII"),
    (0.5, "IZZI"),
    (0.5, "IIZZ"),
    (0.5, "ZIIZ"),
]

expval_inf_prec = get_expval_from_linear_comb_of_paulis_from_statevector(
    circuit, lin_comb_pauli
)

nshots = int(1e8)
expval_samples, expval_SE_samples, expval_95_CI_samples = (
    get_expval_from_linear_comb_of_paulis_from_samples(circuit, lin_comb_pauli, nshots)
)

print(f"Expval at infinite precision: <H> = {expval_inf_prec:.5f}")
print(
    f"Expval with {nshots:.2e} shots: <H> = {expval_samples:.5f}, 95% CI: {[float(round(x, 5)) for x in expval_95_CI_samples]}"
)

>>> Expval at infinite precision: <H> = 0.51472
>>> Expval with 1.00e+08 shots: <H> = 0.51400, 95% CI: [0.51341, 0.51459]

Checklist:

  • Reviewers confirm new code works as expected.
  • Tests are passing.
  • Coverage does not decrease.
  • Documentation is updated.

@andre-juan andre-juan added the enhancement New feature or request label Sep 16, 2025
@andre-juan andre-juan linked an issue Sep 16, 2025 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Sep 16, 2025

Codecov Report

❌ Patch coverage is 0% with 132 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.14%. Comparing base (79fbb2d) to head (c3afc23).
⚠️ Report is 65 commits behind head on master.

Files with missing lines Patch % Lines
src/qibo/hamiltonians/expectation_value.py 0.00% 132 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1716      +/-   ##
==========================================
- Coverage   99.16%   98.14%   -1.02%     
==========================================
  Files          80       81       +1     
  Lines       12751    12883     +132     
==========================================
  Hits        12644    12644              
- Misses        107      239     +132     
Flag Coverage Δ
unittests 98.14% <0.00%> (-1.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stand-alone function to compute expected values

1 participant