qiskit: Remove save_state instructions when transpiling to backend that does not support them
What is the expected enhancement?
Summary: When transpiling a circuit with save_state instructions (such as those inserted into a circuit with the `save_statevector() function) to a backend that does not support it, remove/ignore them.
Users can save the statevector at the midpoint of a circuit by calling the save_statevector() function, which is useful for probing the circuit during simulations. However, then transpiling the same circuit to run on a backend that does not support the save_state instruction, such as the real quantum computer backends, the transpiler will fail with the message like this:
Unable to map source basis {('measure', 1), ('save_state', 1), ('h', 1)} to target basis {'reset', 'rz', 'measure', 'barrier', 'delay', 'x', 'snapshot', 'id', 'sx', 'cx'}
This is may be confusing the the user. Assuming that they interpret this error message correctly, their recourse is to regenerate the source circuit without the save_state instructions (e.g. by removing calls to save_statevector()) and transpiling again. This is cumbersome and perhaps error prone.
An improved approach would be for the transpiler to simply remove/skip/ignore save_state instructions when the target backend does not support them. The benefit of this is that it would allow the user to switch easily between statevector simulators and real quantum computer backends.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 17 (16 by maintainers)
I don’t think
circuit_to_dagshould be worrying about removing backend specific instructions from the circuit for a couple of reasons. First, that is more the job of the transpiler. Second, converting to dag and back to circuit should get back to the original circuit (or close to it). To handle this inBasisTranslatorone could record the directive nodes when going through the list (which aren’t in target basis) and delete them from the dag afterwards.From some discussions with the team, the best fix here is to update the
BasisTranslatorto check ifInstruction._directive(added in #5701 , andTrueforBarrier,snapshot, and all thesave_*instructions) isTrue, and not attempt to translate the instruction if so.Automatically removing instructions like these from a circuit, which are both not applicable and known not to affect either the result or runtime execution of a given backend, is something we should consider in the future, but will depend on some of the ongoing work in https://github.com/Qiskit/qiskit-terra/labels/ISA .