Qiskit / qiskit-ibm-runtime

IBM Client for Qiskit Runtime
https://docs.quantum.ibm.com/api/qiskit-ibm-runtime
Apache License 2.0
154 stars 154 forks source link

Is a warning really necessary when using a session with local mode? #1626

Closed garrison closed 6 months ago

garrison commented 6 months ago

What is the expected feature or enhancement?

Currently if one uses a session with a fake backend in local mode, qiskit-ibm-runtime triggers a warning:

https://github.com/Qiskit/qiskit-ibm-runtime/blob/4c00d409fcfdab00fa0128f44d9c66b38a381aa1/qiskit_ibm_runtime/session.py#L157-L159

I've wondered: is this warning really necessary? Or, what constructive action would we expect a user to take upon seeing this warning?

For instance, the following code taken directly from https://docs.quantum.ibm.com/api/migration-guides/local-simulators will trigger it:

from qiskit_aer import AerSimulator
from qiskit.circuit.library import RealAmplitudes
from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import Session, SamplerV2 as Sampler

# Bell Circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

# Run the sampler job locally using AerSimulator.
# Session syntax is supported but ignored because local mode doesn't support sessions.
aer_sim = AerSimulator()
pm = generate_preset_pass_manager(backend=aer_sim, optimization_level=1)
isa_qc = pm.run(qc)
with Session(backend=aer_sim) as session:
    sampler = Sampler(session=session)
    result = sampler.run([isa_qc]).result()

In the circuit-knitting-toolbox, I am rephrasing our tutorials to use Qiskit Runtime with fake backends in local mode and will ideally demonstrate the use of Batch so that one can simply swap the backend for a hardware backend and have everything work optimally. However, then there will always be this warning in the notebook when it is run without change.

For this reason I ask: Is it truly necessary?

I'll note in passing that because this warning doesn't have the stacklevel set, it is displayed as a UserWarning from the above line of qiskit-ibm-runtime rather than from the user's code. One could perhaps set stacklevel=2, but this still wouldn't point to the correct line when the user creates a Batch rather than a Session; in that case stacklevel=3 would be needed to get to user code (Batch.__init__ is an extra function on the stack).

Acceptance criteria

I suggest clarifying why the warning is believed to be beneficial and possibly changing it to a message at INFO log level.

kt474 commented 6 months ago

I think we can remove the warning since that example already has a comment explaining that local mode doesn't support sessions and it's also mentioned in the execution modes faq https://docs.quantum.ibm.com/run/execution-modes-faq