Skip to content

Commit

Permalink
Fix PEP8 code style and misspelling issues (#53)
Browse files Browse the repository at this point in the history
* Fix PEP8 code style and mispelling issues

* Fix credential scan false positive

* Spelling fix

* Xfield/code style fixes (#56)

* auto correction with yapf and black

* styling for job.py

* pep8 for workspace.py

* pep8 storage.py

* pep8: streaming_problem

* pep8: solvers.py

* pep8 problem.py

* pep8: termp.py

* pep8: toshiba/solvers.py

* pep8: onequibit/solvers

* clean up

* pep8 init

* setup.py

* nit

Co-authored-by: Sanjana Gupta <[email protected]>
  • Loading branch information
vxfield and sanjgupt authored May 4, 2021
1 parent 9a7bcb6 commit 335696d
Show file tree
Hide file tree
Showing 26 changed files with 1,980 additions and 1,277 deletions.
10 changes: 3 additions & 7 deletions azure-quantum/azure/quantum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
# Licensed under the MIT License.
##
import logging
from .version import __version__

try:
from .version import __version__
except:
__version__ = "<unknown>"
from .job import *
from .workspace import *

logger = logging.getLogger(__name__)
logger.info(f"version: {__version__}")

from .job import *
from .workspace import *
74 changes: 41 additions & 33 deletions azure-quantum/azure/quantum/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,96 @@
##
import logging
import time
import io
import json
import uuid

from typing import List, TYPE_CHECKING
from typing import TYPE_CHECKING
from urllib.parse import urlparse

from msrest.authentication import Authentication
from azure.quantum._client.models import JobDetails
from azure.quantum.storage import download_blob
from azure.storage.blob import BlobServiceClient, ContainerClient, BlobClient, BlobSasPermissions, generate_blob_sas, generate_container_sas
from azure.storage.blob import BlobClient

__all__ = ['Job']
__all__ = ["Job"]

logger = logging.getLogger(__name__)

if TYPE_CHECKING:
from azure.quantum.workspace import Workspace


class Job:
"""Azure Quantum Job that is submitted to a given Workspace.
:param workspace: Workspace instance to submit job to
:type workspace: Workspace
:param job_details: Job details model, contains Job ID, name and other details
:param job_details: Job details model,
contains Job ID, name and other details
:type job_details: JobDetails
"""

def __init__(self, workspace: "Workspace", job_details: JobDetails):
self.workspace = workspace
self.details = job_details
self.id = job_details.id
self.results = None
self.workspace = workspace
self.details = job_details
self.id = job_details.id
self.results = None

def refresh(self):
"""Refreshes the Job's details by querying the workspace.
"""
"""Refreshes the Job's details by querying the workspace."""
self.details = self.workspace.get_job(self.id).details

def has_completed(self):
return (
self.details.status == "Succeeded" or
self.details.status == "Failed" or
self.details.status == "Cancelled"
)
return (self.details.status == "Succeeded"
or self.details.status == "Failed"
or self.details.status == "Cancelled")

def wait_until_completed(self, max_poll_wait_secs=30):
"""Keeps refreshing the Job's details until it reaches a finished status.
"""
"""Keeps refreshing the Job's details
until it reaches a finished status."""
self.refresh()
poll_wait = 0.2
while not self.has_completed():
logger.debug(f"Waiting for job {self.id}, it is in status '{self.details.status}'")
print('.', end='', flush=True)
logger.debug(
f"Waiting for job {self.id}," +
f"it is in status '{self.details.status}'"
)
print(".", end="", flush=True)
time.sleep(poll_wait)
self.refresh()
poll_wait = max_poll_wait_secs if poll_wait >= max_poll_wait_secs else poll_wait * 1.5
poll_wait = (max_poll_wait_secs if poll_wait >= max_poll_wait_secs
else poll_wait * 1.5)

def get_results(self):
if not self.results is None:
if self.results is not None:
return self.results

if not self.has_completed():
self.wait_until_completed()

if not self.details.status == "Succeeded":
raise RuntimeError(f"Cannot retrieve results as job execution failed (status: {self.details.status}. error: {self.details.error_data})")
raise RuntimeError(
f'{"Cannot retrieve results as job execution failed"}' +
f"(status: {self.details.status}." +
f"error: {self.details.error_data})"
)

url = urlparse(self.details.output_data_uri)
if url.query.find('se=') == -1:
# output_data_uri does not contains SAS token, get sas url from service
blob_client = BlobClient.from_blob_url(self.details.output_data_uri)
blob_uri = self.workspace._get_linked_storage_sas_uri(blob_client.container_name, blob_client.blob_name)
url = urlparse(self.details.output_data_uri)
if url.query.find("se=") == -1:
# output_data_uri does not contains SAS token,
# get sas url from service
blob_client = BlobClient.from_blob_url(
self.details.output_data_uri)
blob_uri = self.workspace._get_linked_storage_sas_uri(
blob_client.container_name, blob_client.blob_name)
payload = download_blob(blob_uri)
else:
# output_data_uri contains SAS token, use it
payload = download_blob(self.details.output_data_uri)

result = json.loads(payload.decode('utf8'))
result = json.loads(payload.decode("utf8"))
return result

@staticmethod
def create_job_id() -> str:
"""Create a unique id for a new job.
"""
"""Create a unique id for a new job."""
return str(uuid.uuid1())
3 changes: 1 addition & 2 deletions azure-quantum/azure/quantum/optimization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# Licensed under the MIT License.
##

from .term import *
from .term import *
from .problem import *
from .solvers import *
from .streaming_problem import *

2 changes: 0 additions & 2 deletions azure-quantum/azure/quantum/optimization/oneqbit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
##

from .solvers import *


Loading

0 comments on commit 335696d

Please sign in to comment.