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

Fix for save_expval truncation #2265

Merged
merged 7 commits into from
Dec 10, 2024
Merged

Conversation

gadial
Copy link
Collaborator

@gadial gadial commented Nov 24, 2024

Summary

Fixes small problems preventing the usage of save_expval truncation.

Fixes #2249

Details and comments

PR #2216 added a mechanism for truncating save_expval, removing qubits for which the pauli measurement operator is I, since those qubits are not needed for the simulation of the measurement. This is crucial for running the simulator on small circuits compiled on large backends, since the save_expval is extended to all the qubits in the backend, meaning we might have a 127-qubit save_expval for a circuit with only 12 active qubits (e.g. in the added test case).

However, the truncation code was not activated in practice due to the !qubitset_.empty() check performed prior to the execution of the code which excludes I qubits. Since the save_expval command is the last one in the circuit when, e.g., running via an estimator (which adds the command to the circuit just before its run) and gates are traversed in reverse order at this stage, the save_expval command would always fail this test, defaulting to the qubitset_.insert(op.qubits.begin(), op.qubits.end()); line which adds all its qubits.

This PR removes the !qubitset_.empty() check and fixes some problems arising from this:

  1. In edge cases, e.g. a circuit consisting only of a measurement of the pauli op I the truncation stage results in a circuit without qubits, and so it is skipped and no expval result it obtained. Hence, we always add the a qubit to the qubit set if it is empty, even if it corresponds to I. This does not enlarge the qubit set enough to pose a real problem, although smarter logics can be implemented.
  2. A op.qubits.resize(qubitmap_.size()); command was missing in the remapping code (although present in the non-remapping truncation code in set_params); it was added. Without resizing, the save_expval command fails due to generation of invalid pauli mask data in the expval_pauli method.
  3. When truncating the pauli string, the index of each pauli in the string is computed from the qubit index. However, in cases where the operator is only on a subset of the qubits, the qubit index might be too large. For example, if the pauli ZZ is used on qubits 1,2 and qubit 0 is truncated away, while creating the new pauli the code will attempt to access the pauli in index 2 (more precisely, in location pauli.size()-1-2 since paulis are accessed in reverse order). This is fixed by using the index of the qubits inside op.qubits instead of their absolute index.

hhorii
hhorii previously approved these changes Nov 24, 2024
Copy link
Collaborator

@hhorii hhorii left a comment

Choose a reason for hiding this comment

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

LGTM

with:
path: ./wheelhouse/*.whl
name: artifact-${{ github.job }}
wheel-gpu:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you remove GPU build wheel ?
I think we still need them for testing build

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's a temporary measure since those builds are failing (probably due to CUDA update) and this prevents us from merging this fix.

@gadial gadial force-pushed the fix_save_expval_truncation branch from cfa3697 to 1ca7b96 Compare December 10, 2024 07:26
@doichanj doichanj mentioned this pull request Dec 10, 2024
@gadial gadial merged commit e26970a into Qiskit:main Dec 10, 2024
34 checks passed
@gadial gadial deleted the fix_save_expval_truncation branch December 10, 2024 08:40
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.

AerEstimatorV2 fails to execute small circuits transpiled against larger backends
3 participants