Skip to content

Commit

Permalink
Updating the quantum extension to add simulator support (Azure#3575)
Browse files Browse the repository at this point in the history
This PR edits the output function to allow for jobs that run on the simulator to have output logged during execution that is not in json format. For simulator jobs, we expect the job result to be printed as the last item on its own line, and string output to be enclosed with `"`.
  • Loading branch information
bettinaheim authored Jul 6, 2021
1 parent e44bb62 commit b4e0709
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/quantum/azext_quantum/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def one(key, value):
return OrderedDict([
('Result', key),
('Frequency', f"{value:10.8f}"),
('', f"\u2590{barra:<22} |"),
('', f"\u007C{barra:^20}\u007C")
])

if 'Histogram' in results:
Expand Down
24 changes: 19 additions & 5 deletions src/quantum/azext_quantum/operations/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,15 @@ def output(cmd, job_id, resource_group_name=None, workspace_name=None, location=
from azure.cli.command_modules.storage._client_factory import blob_data_service_factory

path = os.path.join(tempfile.gettempdir(), job_id)
info = WorkspaceInfo(cmd, resource_group_name, workspace_name, location)
client = cf_jobs(cmd.cli_ctx, info.subscription, info.resource_group, info.name, info.location)
job = client.get(job_id)

if os.path.exists(path):
logger.debug("Using existing blob from %s", path)
else:
logger.debug("Downloading job results blob into %s", path)

info = WorkspaceInfo(cmd, resource_group_name, workspace_name, location)
client = cf_jobs(cmd.cli_ctx, info.subscription, info.resource_group, info.name, info.location)
job = client.get(job_id)

if job.status != "Succeeded":
return f"Job status: {job.status}. Output only available if Succeeded."

Expand All @@ -204,7 +203,22 @@ def output(cmd, job_id, resource_group_name=None, workspace_name=None, location=
blob_service.get_blob_to_path(args['container'], args['blob'], path)

with open(path) as json_file:
data = json.load(json_file)
if job.target.startswith("microsoft.simulator"):

lines = [line.strip() for line in json_file.readlines()]
result_start_line = len(lines) - 1
if lines[-1].endswith('"'):
while not lines[result_start_line].startswith('"'):
result_start_line -= 1

print('\n'.join(lines[:result_start_line]))
result = ' '.join(lines[result_start_line:])[1:-1] # seems the cleanest version to display
print("_" * len(result) + "\n")

json_string = "{ \"histogram\" : { \"" + result + "\" : 1 } }"
data = json.loads(json_string)
else:
data = json.load(json_file)
return data


Expand Down

0 comments on commit b4e0709

Please sign in to comment.