Add Retry Mechanism for sampler.run
shantanu-misra opened this issue · 2 comments
shantanu-misra commented
What should we add?
I would like to request a feature for the Qiskit library that provides an option to automatically retry the sampler.run(circuits, shots=SHOTS) command a specified number of times in the event of an error, such as a RequestAPIError, that is not caused by code issues.
Currently, to handle such errors, I use a custom function that wraps the sampler.run command and retries it a specified number of times. Here’s an example of how I currently implement this:
def run_with_retry(command, max_retries=10):
attempts = 0
while attempts < max_retries:
try:
result = command()
return result
except Exception as e:
attempts += 1
print(f"Error encountered: {e}. Attempt {attempts}/{max_retries}")
if attempts >= max_retries:
print("Max retries reached. Command failed.")
raise
job = run_with_retry(lambda: sampler.run(circuits, shots=SHOTS), max_retries=10)
t-imamichi commented
I suggest transferring this feature request to https://github.com/Qiskit/qiskit-ibm-runtime/issues, which actually takes care of IBM Quantum APIs.