Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder u8 values for sparseobservable terms so that it is easier to determine diagonal vs off-diagonal element #13826

Open
nonhermitian opened this issue Feb 11, 2025 · 4 comments
Labels
type: feature request New feature or request
Milestone

Comments

@nonhermitian
Copy link
Contributor

What should we add?

It would be nice if the unsigned char values for the operators in a sparseobservable were renumbered so that lower indices corresponded to diagonal operators and the higher ones off-diagonal. Then checking if the operator is diagonal for a given qubit is a simple operator_u8 < num_diag_terms check.

@nonhermitian nonhermitian added the type: feature request New feature or request label Feb 11, 2025
@jakelishman
Copy link
Member

jakelishman commented Feb 11, 2025

The exact numbers are part of the stable API in order to enable interaction with Numpy and can't be changed without an API break. If you hadn't seen it, the logic behind the choices of numbers is described here: https://docs.quantum.ibm.com/api/qiskit/dev/qiskit.quantum_info.SparseObservable#bitterm.

The is_diagonal check is something like (term & 0b11).bit_count() % 2 == 0, which can be Numpy-vectorised, if that's useful for you? edit: sorry, that's not right - one sec

@jakelishman
Copy link
Member

jakelishman commented Feb 11, 2025

It's term & 0b11 == BitTerm.Z. Got there in the end.

(edit: or not (term & BitTerm.X), I suppose)

@jakelishman
Copy link
Member

If it's helpful, we can add a function BitTerm.is_diagonal as a feature that can be used either as an instance method or as a standalone function that will do it in a Numpy-vectorised way, so you can do BitTerm.is_diagonal(arr) or my_term.is_diagonal()?

@nonhermitian
Copy link
Contributor Author

If it is stuck the way it is then no worries. In practice knowing the number of, and location of off-diagonal terms is quite important. I have gone down the road of numbering diag lower than off-diagonal, but a helper function likely works just as well I think.

@jakelishman jakelishman added this to the 2.1.0 milestone Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants