Qiskit / qiskit-ibm-runtime

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

Add Retry Mechanism for sampler.run #1871

Closed shantanu-misra closed 2 weeks ago

shantanu-misra commented 1 month ago

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 1 month ago

I suggest transferring this feature request to https://github.com/Qiskit/qiskit-ibm-runtime/issues, which actually takes care of IBM Quantum APIs.

kt474 commented 3 weeks ago

Duplicate of https://github.com/Qiskit/qiskit-ibm-runtime/issues/682