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

Add _repr_pretty_ to all results and devices classes #682

Closed
Strilanc opened this issue Jul 16, 2018 · 4 comments
Closed

Add _repr_pretty_ to all results and devices classes #682

Strilanc opened this issue Jul 16, 2018 · 4 comments
Assignees
Labels
area/visualization complexity/medium introduces/modifies 3-5 concepts, takes max up to a month for an advanced contributor kind/feature-request Describes new functionality triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@Strilanc
Copy link
Contributor

Strilanc commented Jul 16, 2018

This method is used preferentially over repr in Jupyter notebooks and in ipython:

In [1]: class C:
   ...:     def __str__(self):
   ...:         return 'str'
   ...:     def __repr__(self):
   ...:         return 'repr'
   ...:     def _repr_pretty_(self, p, cycle):
   ...:         p.text('pretty')
   ...:     

In [2]: C()
Out[2]: pretty

repr is supposed to be unambiguous, so we often can't be too creative with it. In order to make the ipython experience smoother we should show coarser easier to read information.

Circuit works the way it should (pretty printing a circuit), but e.g. TrialResult does not. Support is generally very easy, because our __str__ methods do the hard lifting:

    def _repr_pretty_(self, p: Any, cycle: bool) -> None:
        """Shown in ipython and Jupyter notebooks."""
        if cycle:
            p.text('TrialResult(...)')
        else:
            p.text(self.__str__())
@dabacon
Copy link
Collaborator

dabacon commented Apr 30, 2020

We don't have many implementations of this. It is probably most urgent for "heavy" objects since otherwise it uses repr.

@dabacon
Copy link
Collaborator

dabacon commented Apr 30, 2020

Here is a pass on object we should consider making sure we have this for

cirq.Device
cirq.CliffordTrialResult
cirq.DensityMatrixStepResult
cirq.SimulationTrialResult
cirq.WaveFunctionStepResult
cirq.WaveFunctionTrialResult
cirq.IonDevice
cirq.google.Sycamore
cirq.google.Sycamore23

Some of these may already be done or covered by superclasses.

@dabacon dabacon changed the title Add _repr_pretty_ to more classes Add _repr_pretty_ to all results and devices classes Apr 30, 2020
@dabacon
Copy link
Collaborator

dabacon commented Apr 30, 2020

I guess we also have guidance to use repr_pretty for Gates. So we should do a complete audit.

@maffoo maffoo added the kind/feature-request Describes new functionality label Sep 22, 2020
@balopat balopat added area/visualization complexity/medium introduces/modifies 3-5 concepts, takes max up to a month for an advanced contributor triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on labels Sep 25, 2020
@dabacon dabacon self-assigned this Nov 9, 2021
CirqBot pushed a commit that referenced this issue Nov 11, 2021
Implements `_repr_pretty_` which pretty prints in iPython (jupyter notebooks)

Addresses #682
@dabacon
Copy link
Collaborator

dabacon commented Nov 11, 2021

There are now on results and on devices.

@dabacon dabacon closed this as completed Nov 11, 2021
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
Implements `_repr_pretty_` which pretty prints in iPython (jupyter notebooks)

Addresses quantumlib#682
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
Implements `_repr_pretty_` which pretty prints in iPython (jupyter notebooks)

Addresses quantumlib#682
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/visualization complexity/medium introduces/modifies 3-5 concepts, takes max up to a month for an advanced contributor kind/feature-request Describes new functionality triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

No branches or pull requests

4 participants