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

Cosmetic/pretty printing of compiler output #599

Draft
wants to merge 37 commits into
base: main
Choose a base branch
from
Draft

Conversation

dowusu-antwi
Copy link
Contributor

@dowusu-antwi dowusu-antwi commented Jul 4, 2023

Fixes #144. (edit: Now with circuit drawings.) E.g.,

import qiskit_superstaq as qss
import qiskit
qc = qiskit.QuantumCircuit(1)
qc.h(0)
qc.draw()

provider = qss.SuperstaqProvider()
compile_result = provider.ibmq_compile(qc, target="ibmq_lagos_qpu")
print(compile_result.__repr_pretty__())
# CompilerOutput(    
#          ┌─────────┐┌─────────┐┌─────────┐
#     q_0: ┤ Rz(π/2) ├┤ Rx(π/2) ├┤ Rz(π/2) ├
#          └─────────┘└─────────┘└─────────┘,
#     {0: 0},
#     Schedule(
#         (0, ShiftPhase(-1.5707963268, DriveChannel(0))),
#         (0, ShiftPhase(-1.5707963268, ControlChannel(1))),
#         (
#             0,
#             Play(
#                 Drag(
#                     duration=160,
#                     sigma=40,
#                     beta=-1.053810766518306,
#                     amp=0.1987049754148708,
#                     angle=0.022620855474399307,
#                     name="X90p_d0",
#                 ),
#                 DriveChannel(0),
#                 name="X90p_d0",
#             ),
#         ),
#         (160, ShiftPhase(-1.5707963268, DriveChannel(0))),
#         (160, ShiftPhase(-1.5707963268, ControlChannel(1))),
#         name="sched25",
#     ),
#     None,
#     None,
#     None,
# )

Copy link
Contributor

@bharat-thotakura bharat-thotakura left a comment

Choose a reason for hiding this comment

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

LGTM

def _replace_placeholders(pretty_output: str) -> str:
"""Replace placeholders for object `repr`s, since black can't parse them."""
if not self.has_multiple_circuits():
pretty_output = pretty_output.replace("quantum_circuit", repr(self.circuit))
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be possible/make sense to use the circuit diagrams instead of their reprs, assuming they're small enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

would it be possible/make sense to use the circuit diagrams instead of their reprs, assuming they're small enough?

Yes, by the way.
image
Working on handling an edge case: empty circuits give empty pulse schedules which black complains about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. (Also moved pretty printing to separate files per chat with Rich.)

@dowusu-antwi
Copy link
Contributor Author

dowusu-antwi commented Jul 21, 2023

I bet preprocess_compiler_output and postprocess_compiler_output can be replaced by clever regex -- I would be glad to nuke as much of it as possible, but wanted to get something in since I've been dragging my feet.

edit: pretty_printing.py does feel like overkill -- I really would be glad to nuke it for a cleaner regex implementation, so I added profuse comments to make what I'm doing explicitly clear (to facilitate translation into regex, now or in the future)

Copy link
Contributor

@epelaaez epelaaez left a comment

Choose a reason for hiding this comment

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

lgtm (with micro nit)

general-superstaq/general_superstaq/pretty_printing.py Outdated Show resolved Hide resolved
dowusu-antwi and others added 2 commits August 22, 2023 16:45
Co-authored-by: Emilio Peláez <63567458+epelaaez@users.noreply.github.com>
@richrines1
Copy link
Contributor

black needs to be moved from dev-requirements to requirements

" {cirq.LineQubit(0): cirq.LineQubit(0)},\n"
" ],\n"
" [\n"
" Schedule(\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be nice to abbreviate things that are this long - i think one of the goals of adding a pretty repr is to make it so e.g. typing compiler_output in a console/notebook just prints something manageable and (somewhat) succinct

(i think textwrap.shorten does something like this?)

@@ -146,6 +146,23 @@ def __repr__(self) -> str:
f"{self.pulse_lists!r})"
)

def __repr_pretty__(self) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

to be picked up by ipython/jupyter/etc i think this needs to be

Suggested change
def __repr_pretty__(self) -> str:
def _repr_pretty_(self, p, cycle=False) -> None:
...
p.text(<string>)

(see https://ipython.readthedocs.io/en/stable/config/integrating.html#pretty-printing)

"CompilerOutput(\n"
" [\n"
f" {circuits[0]!r},\n"
" \n"
Copy link
Contributor

@richrines1 richrines1 Aug 22, 2023

Choose a reason for hiding this comment

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

is it possible/easy to avoid these spaces on black *blank* lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean replace them with \t tab characters?

Copy link
Contributor

Choose a reason for hiding this comment

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

oops sorry that was supposed to say "blank lines" - my thinking was just that there's no reason to include spaces in otherwise empty lines, e.g. this line could just be

Suggested change
" \n"
"\n"

@dowusu-antwi
Copy link
Contributor Author

black needs to be moved from dev-requirements to requirements

Done.

@stephanielee9 stephanielee9 marked this pull request as draft October 12, 2023 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AQTCompilerOutput should have a _repr_pretty_ method
5 participants