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

Cirq Job __repr__ method #158

Open
guenp opened this issue Oct 13, 2021 · 4 comments
Open

Cirq Job __repr__ method #158

guenp opened this issue Oct 13, 2021 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@guenp
Copy link
Contributor

guenp commented Oct 13, 2021

@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

@dstrain115
Copy link

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

@vtomole
Copy link

vtomole commented Oct 13, 2021

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
Copy link

vtomole commented Oct 20, 2021

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.

@guenp
Copy link
Contributor Author

guenp commented Oct 22, 2021

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 guenp added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants