Skip to content

enrich executor_expectation_values input options#170

Open
dekelmeirom wants to merge 2 commits intomainfrom
obs_and_bases_input
Open

enrich executor_expectation_values input options#170
dekelmeirom wants to merge 2 commits intomainfrom
obs_and_bases_input

Conversation

@dekelmeirom
Copy link
Copy Markdown
Collaborator

@dekelmeirom dekelmeirom commented Apr 20, 2026

add option to give (obs, bases) as input for executor_expectation_values

in addition, add tests for measurement_bases.py

addresses issue #163

add option to give (obs, bases) as input for executor_expectation_values
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 20, 2026

Coverage Report for CI Build 24664371484

Coverage increased (+3.2%) to 93.426%

Details

  • Coverage increased (+3.2%) from the base build.
  • Patch coverage: 2 uncovered changes across 1 file (55 of 57 lines covered, 96.49%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
qiskit_addon_utils/exp_vals/expectation_values.py 21 19 90.48%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 867
Covered Lines: 810
Line Coverage: 93.43%
Coverage Strength: 0.93 hits per line

💛 - Coveralls

ValueError if the number of entries in `basis_dict` does not equal the length of `bool_array` along `meas_basis_axis`.
ValueError if `meas_basis_axis` is `None` but `len(basis_mapping) != 1`.
ValueError if the number of entries in `basis_mapping` does not equal the length of `bool_array` along `meas_basis_axis`.
ValueError if the given measurement_basis can not cover all of the terms in al lof the observables.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ValueError if the given measurement_basis can not cover all of the terms in al lof the observables.
ValueError if the given measurement_basis can not cover all of the terms in all of the observables.

basis_dict: dict[Pauli, list[SparsePauliOp | None]],
bool_array: np.ndarray[tuple[int, ...], np.dtype[np.bool_]],
basis_mapping: dict[Pauli, list[SparsePauliOp | None]]
| tuple[Sequence[SparsePauliOp], Sequence[str | PauliList]],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the type hint for the tuple input be: tuple[Sequence[SparsePauliOp], Sequence[str] | PauliList], rather than what you have here?



def find_measure_basis_to_observable_mapping(
observables: Sequence[SparsePauliOp], measure_bases: Sequence[str | int | PauliList]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused about the measure_bases type hint here too. Should it be Sequence[str | Sequence[int]] | PauliList?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not sure how generally useful this function is. Maybe it could be a private method in the expectation_values module where it's used? We can just compress the docstring down to one line if we make it private. What do you think?

False in observable_elements_list
for observable_elements_list in observables_elements_basis_found
):
raise ValueError("Some observable elements do not commute with any measurement basis.")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could print which observable doesn't commute with which basis here


def _apply_pec_signs(
bool_array: np.ndarray[tuple[int, ...], np.dtype[np.bool]],
bool_array: np.ndarray[tuple[int, ...], np.dtype[np.bool_]],
Copy link
Copy Markdown
Collaborator

@caleb-johnson caleb-johnson Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the linter throwing an error about this? Wondering why these changes are necessary here and above

raise ValueError(
f"{len(basis_dict) = } does not match {bool_array.shape[meas_basis_axis] = }."
)
if isinstance(basis_mapping, dict):
Copy link
Copy Markdown
Collaborator

@caleb-johnson caleb-johnson Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code (123-147) where we check basis mapping and get everything in order can be moved to a private method at the bottom of the module. That will clear up some space for the core code in the module. I think it'll help the readability.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants