Skip to content

Sampler failing when setting skip_transpilation to True #598

@luciacuervovalor

Description

@luciacuervovalor

Describe the bug

When setting skip_transpilation to True in transpilation_settings in the run method of the Sampler, jobs fail randomly. For example, evaluating a 6 qubit Bell state circuit with no parameters failed twice in 1000 trials (job ids of the jobs which failed were cdd3msvhc7fs715q3t10 and cdd3s52auedvlnhhqc0g). When a job fails, an AlgorithmError is raised which prevents the program from continuing.

Steps to reproduce

from qiskit import IBMQ
IBMQ.load_account()
from qiskit_ibm_runtime import Session, Sampler
from qiskit.circuit import QuantumCircuit
            
qc = QuantumCircuit(6)
for i in range(6):
    qc.h(i)
for i in range(0,6,2):
    qc.cx(i,i+1)
qc.measure_all()

with Session(backend='ibmq_qasm_simulator') as session:
    sampler = Sampler(session=session)
    for iteration in range(1000):
        if iteration%25==0:
            print(f"Iteration {iteration} completed")
        job = sampler.run(
                            [qc], [], 
                            run_options={"shots": 1024}, 
                            transpilation_settings={"skip_transpilation": True}
                          )
        result = job.result()

Expected behavior

Evaluates all 1000 circuits without failure

Suggested solutions

To prevent the program from breaking, I added a patch which resends a job when it randomly fails, and also prints the job id, in case I want to view the error log. This doesn't solve the core problem but at least allows the program to continue. The patch was:

class RetrySampler(Sampler):
    
    def __init__(self, *args, max_retries: int = 5, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.max_retries = max_retries
    
    def run(self, circuits, parameter_values, **kwargs):
        result = None
        for i in range(self.max_retries):
            try:
                job = super().run(circuits, parameter_values, **kwargs)
                result = job.result()
                if result is not None:
                    return job
            except:
                if i != self.max_retries-1:
                    print(f"Sampler job with ID {job.job_id} failed... Starting trial number {i+2} for this iteration")
                pass
        if result is None:
            raise RuntimeError(f"Runtime job failed! Maximum number of retries {self.max_retries} exceeded")

where then we replace Sampler with RetrySampler in the above code.

Additional Information

  • qiskit-ibm-runtime version: 0.7.0rc3
  • Python version: 3.10.4
  • Operating system: macOS Monterey 12.6 M1 2020

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpriority: mediumMedium Priority issue (must have for current release)server actionNeed actions from the server side

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions