Fix for save_expval truncation #2265
Open
+36
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 isI
, 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 thesave_expval
is extended to all the qubits in the backend, meaning we might have a 127-qubitsave_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 excludesI
qubits. Since thesave_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, thesave_expval
command would always fail this test, defaulting to thequbitset_.insert(op.qubits.begin(), op.qubits.end());
line which adds all its qubits.This PR removes the
!qubitset_.empty()
check and fixes two problems arising from this: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 toI
. This does not enlarge the qubit set enough to pose a real problem, although smarter logics can be implemented.op.qubits.resize(qubitmap_.size());
command was missing in the remapping code (although present in the non-remapping truncation code inset_params
); it was added. Without resizing, thesave_expval
command fails due to generation of invalid pauli mask data in theexpval_pauli
method.