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

Transform result to dict before returning from Job.result #944

Merged
merged 10 commits into from
Oct 2, 2023

Conversation

caleb-johnson
Copy link
Collaborator

Fixes #942

@@ -375,7 +375,11 @@ def result(self, wait=True, cadence=5, verbose=False):
time.sleep(cadence)
if verbose:
logging.info(".")
return self._job_client.result(self.job_id)
results = self._job_client.result(self.job_id)
if isinstance(results, str):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be overthinking things here, but would it be better to handle this within the job client (i.e. here)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the return is already loaded into a dictionary in that line, but when I use save_result inside my remote program, job.result() returns me a string representation of a dictionary

Copy link
Collaborator Author

@caleb-johnson caleb-johnson Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IceKhan13 why would the return object in the deleted line ever be a string here? _job_client.result() returns a decoded object here

Is there another client which may be returning encoded strings to job.result besides GatewayJobClient?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good question. It should be decoded object (dict). I do not know why it's returning string. Worth digging into it

@IceKhan13
Copy link
Member

@caleb-johnson can you update code to pass lints and we can merge this :)

@caleb-johnson
Copy link
Collaborator Author

caleb-johnson commented Sep 27, 2023

@caleb-johnson can you update code to pass lints and we can merge this :)

I can do that, but I am not really satisfied that this is the right fix. I do not understand why we need to decode the string here, when it is already being done in the GatewayJobClient. Doing what I have in this PR seems redundant. Is there another client being used I am not aware of?

Do you understand what is causing these JSON strings to make it through the result call?

@psschwei
Copy link
Collaborator

Something looks a bit off with the commits...

@caleb-johnson
Copy link
Collaborator Author

caleb-johnson commented Sep 27, 2023

Something looks a bit off with the commits...

Thanks, I cleaned it up. I made a mess by merging main into this branch :P

Going to take a closer look at this now that Aki solved the other problem with results

@IceKhan13
Copy link
Member

@caleb-johnson thing is we have DB schema in following format

Model: Job
Fields:
- id: str
- status: str
- logs: str
- results: str

so when it is returned from DB json looks like this

{
  "id": "42",
  "status": "SUCCEEDED",
  "results": "{'bla-bla': 'bla-bla'}"
}

therefore we need to do another decoding on results field.

It can be fixed by changing https://github.com/Qiskit-Extensions/quantum-serverless/blob/main/gateway/api/models.py#L94 to JsonField, probably. Or even better, by creating custom serializer which will decode results field before returning back to user https://github.com/Qiskit-Extensions/quantum-serverless/blob/main/gateway/api/serializers.py#L23

try:
results = json.loads(results, cls=QiskitObjectsDecoder)
except json.JSONDecodeError:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we log something here?

logger.warning("Error during results decoding. Details: %s", exception)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we are good to go :)

@caleb-johnson
Copy link
Collaborator Author

@IceKhan13 , as discussed, we can just merge this patch and follow up with a better fix in a following PR.

Added a warning when the string isn't interpretable by json, as requested.

Ready for final review/approval

@IceKhan13 IceKhan13 merged commit 32eaec5 into Qiskit:main Oct 2, 2023
6 checks passed
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.

job.result() returns a string representation of dict rather than the dict itself
3 participants