azure-quantum-python icon indicating copy to clipboard operation
azure-quantum-python copied to clipboard

Cirq Job __repr__ method

Open guenp opened this issue 3 years ago • 4 comments

@vtomole pointed out that the Job.__repr__ method should be modified according to the following:

The class has a repr method that produces a python expression that evaluates to an object equal to the original value. The expression assumes that cirq, sympy, numpy as np, and pandas as pd have been imported.

If the repr is cumbersome, gates should specify a repr_pretty method. This method will be used preferentially by Jupyter notebooks, ipython, etc.

See https://github.com/quantumlib/Cirq/blob/51b56288fa9a84dff9697524da9ab0a4d57a56f5/docs/dev/gates.md#gate-and-operation-guidelines

@vtomole, I was wondering if the above is a correct recap of our conversation, could you please verify? Also, is it a correct observation that cirq_ionq.Job is similarly missing a __repr__ method? See: https://github.com/quantumlib/Cirq/blob/1f14edf6bae39b4146e3f25f04ca4f26effc6773/cirq-ionq/cirq_ionq/job.py#L247

guenp avatar Oct 13 '21 18:10 guenp

Also, feel free to use cirq.testing.assert_equivalent_repr to assist in testing this.

dstrain115 avatar Oct 13 '21 18:10 dstrain115

I was wondering if the above is a correct recap of our conversation, could you please verify?

Yes.

Also, is it a correct observation that cirq_ionq.Job is similarly missing a repr method?

Yes. CC: @dabacon

vtomole avatar Oct 13 '21 19:10 vtomole

Following up on the Cirq sync:

I find __repr__ extremely useful because I can get a string representation of the created object and recreate that object from the string. This follows from the quote

If a programmer cannot see what a program is doing, she can't understand it. - http://worrydream.com/#!/LearnableProgramming).

reprs will need to be built from the bottom up: all objects in the repr will need to define their own reprs. Since this repository contains no reprs, this is a huge task.

I'll leave it on your court as to if you want qdk-python to follow the repr design that Cirq implements or not. I'm probably not the only person who'll end up calling repr on Cirq's job objects because Cirq is proliferated with reprs. Most Cirq programmers lean on the repr functionality.

Here is a rough guide we can start with if you do decide to implement reprs:

Starting with the created Cirq job from the service

job = service.create_job(
    program=circuit,
    repetitions=100,
    target="ionq.simulator"
)

If I call repr(job) I should get

class Job:
    def __repr__(self) -> str:
        return f"azure.quantum.plugins.cirq.job.Job(azure_job={self._azure_job!r}, 
        program={self._program!r}, measurement_dict={self._measurement_dict!r}"

This repr calls repr(self._azure_job) along with repr(self._program) and repr(self._measurement_dict). Unlike the last two items, self._azure_job doesn't have a repr. We'll need to define it.

class Job(BaseJob, FilteredJob):
    def __repr__(self) -> str:
        return f"azure.quantum.Job(workspace={self.workspace!r}, job_details={self.details!r}"

We'll need to define a repr for self.workspace as well.

class Workspace:
    def __repr__(self) -> str:
        return f"azure.quantum.workspace.Workspace(credential={self.credentials!r}, name={self.name!r}, 
        resource_group={self.resource_group!r}, subscription_id={self.subscription_id!r}, 
        storage={sef.storage!r}, user_agent={self.user_agent!r}, location={self.location!r}"

JobDetails will also need a repr.

Please let me know your thoughts and concerns about this.

vtomole avatar Oct 20 '21 20:10 vtomole

Thanks @vtomole for the clarification and examples! I think this makes a lot of sense. Let me take it back to the team and see how it aligns with our internal guidelines for Azure Client packages; if it doesn't conflict then I think it's worth adding to the backlog, especially if it is something Cirq users are expecting.

guenp avatar Oct 22 '21 04:10 guenp