Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom timeout and retry #104

Closed
wants to merge 7 commits into from
Closed

Custom timeout and retry #104

wants to merge 7 commits into from

Conversation

splch
Copy link
Contributor

@splch splch commented Apr 29, 2024

adds to #27 and addresses #103 by allowing retry counts and custom timeouts

Copy link

codecov bot commented Apr 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.91%. Comparing base (a7d2d30) to head (0be8601).

❗ Current head 0be8601 differs from pull request most recent head f775924. Consider uploading reports for the commit f775924 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #104      +/-   ##
==========================================
- Coverage   96.85%   95.91%   -0.95%     
==========================================
  Files           5        5              
  Lines         318      318              
==========================================
- Hits          308      305       -3     
- Misses         10       13       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@splch
Copy link
Contributor Author

splch commented Apr 29, 2024

temp fix:

import os
import time
import pennylane as qml
import pennylane_ionq


# Define a custom _submit_job method
def _submit_job_timeout_retry(self, retry_attempts=3, timeout=10):
    job = pennylane_ionq.api_client.Job(api_key=self.api_key)
    for attempt in range(retry_attempts):
        try:
            job.manager.create(**self.job, timeout=timeout)
            break
        except Exception as e:
            if attempt == retry_attempts - 1:
                raise e
            time.sleep(2**attempt)  # Exponential backoff

    for attempt in range(retry_attempts):
        start = time.time()
        while time.time() - start < timeout and not job.is_complete:
            try:
                job.reload()
                if job.is_complete:
                    break
                if job.is_failed:
                    raise pennylane_ionq.api_client.JobExecutionError("Job failed")
            except Exception:
                continue  # Silently handle minor reload failures

    for attempt in range(retry_attempts):
        start = time.time()
        while time.time() - start < timeout:
            try:
                params = {"sharpen": self.sharpen} if self.sharpen else {}
                job.manager.get(resource_id=job.id.value, params=params)
                self.histogram = job.data.value
                break
            except Exception:
                continue


# Specifically use an IonQ device
dev = pennylane_ionq.SimulatorDevice(wires=2, api_key=os.getenv("IONQ_API_KEY"))
# dev = pennylane_ionq.QPUDevice(backend="aria-1", wires=2)

# Replace the default _submit_job method with the custom one
dev._submit_job = _submit_job_timeout_retry.__get__(dev, pennylane_ionq.SimulatorDevice)


# Run the circuit as usual and the `_submit_job_timeout_retry` method will be used
@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0, 1])
    return qml.counts()


results = circuit()
print({k: v.item() for k, v in results.items()})

@trbromley
Copy link
Contributor

Thanks @splch! We'll take a look at this when it's ready.

Also, if you have a chance would you be able to respond to this comment and add some unit tests? We'd love to merge in #101 by the end of this week as the PL release is next week.

@splch splch closed this by deleting the head repository May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants