8000 Circuit image plotter by ajavadia · Pull Request #295 · Qiskit/qiskit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Circuit image plotter #295

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

Merged
merged 25 commits into from
Feb 19, 2018
Merged

Circuit image plotter #295

merged 25 commits into from
Feb 19, 2018

Conversation

ajavadia
Copy link
Member

This addresses Issue #235: drawing quantum circuits locally.

It adds 2 functions to visualization.py:

  • circuit_drawer returns a PIL image format of the circuit.
  • plot_circuit is a thin wrapper around the above, which just displays the circuit on screen.
    The reason for this split is that PIL images cannot be easily inlined in jupyter notebooks, contrary to e.g. matplotlib (and matplotlib did not render these images well). So for Jupyter inlining one would call the 1st function, otherwise the 2nd function.

This PR adds several dependencies:

  • pdflatex installation for compiling latex code to pdf.
  • poppler installation for converting pdf to image.
  • The Pillow package for working with Python images (this added to requirements.txt)

Motivation and Context

This will be a nice feature to have, especially for writing tutorials.

How Has This Been Tested?

  • Test added to test/python/test_visualization.py.
  • Example added to examples/python/initializer.py.

Screenshots (if appropriate):

image

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • [x ] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [x ] My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • [x ] I have read the CONTRIBUTING document.
  • [x ] I have added tests to cover my changes.
  • [x ] All new and existing tests passed.

@diego-plan9 diego-plan9 self-requested a review February 15, 2018 15:51
@ajavadia
Copy link
Member Author

@diego-plan9 this now checks to see if poppler and pdflatex are installed. if not, it emits a warning and returns no image.

Copy link
Member
@diego-plan9 diego-plan9 left a comment

Choose a reason for hiding this comment

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

Thanks, @ajavadia ! It's looking good, with some tiny inline comments! Eventually it would be great to move to a lighter solution that does not require the extra steps for converting to latex->pdf->png, but in the meantime the solution would work wonderfully!

@@ -82,6 +77,7 @@
QASM_source = Q_program.get_qasm("initializer_circ")

print(QASM_source)
plot_circuit(circuit)
Copy link
Member

Choose a reason for hiding this comment

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

Mainly for double-checking: are the changes on this example intentional?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I just wanted to enrich one of the existing examples with the new circuit drawing capabilities.

from collections import Counter, OrderedDict
from functools import reduce
from io import StringIO

from PIL import Image, ImageChops
Copy link
Member

Choose a reason for hiding this comment

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

Following up on the idea of making the feature "optional": can we also make pillow optional, as it will not be used unless the non-python dependencies are not installed? This would involve:

  • making this import "conditional-ish", either using something similar to how projectq is handled, or importing it inside the function that uses (they are currently two, but with a bit of moving things around it seems it might be simple to reduce it to one)?
  • remove it from requirements.txt (ideally, marking is as an "extra" dependency in setup.py.in - although we are not really using the extras_require officially, it might be a good time to also add projectq to that dict if you feel inclined to do so)
  • update the docstrings and the try block to be aware of pillow

Copy link
Member Author

Choose a reason for hiding this comment

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

I implemented your 1st and 3rd bullets.
Regarding the 2nd - I agree with you that pillow is only required for circuit drawing, similar to the other dependencies introduced here. But adding it as extras_require would mean that it does not get installed automatically. I think this is not a heavy dependency (unlike pdflatex and poppler); so wouldn't it be better to remove the burden of manual installation on the user? I also imagine pillow will be used in any eventual, light-weight solution for circuit drawing (as indicated in Ismail's original issue #235).
For projectq, I agree -- it is tangential to the SDK. Added to extras_require

Note: Requires poppler installed (to convert pdf to image)
"""
filename = 'circuit'
tmpdir = 'tmp/'
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 use tempfile.TemporaryDirectory or similar for creating the temporary directory? It's quite handy for these cases, as it will make sure that the folder is created in a safe place and also takes care of deleting it if using it as a context manager.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes that's much better. Done!

@ajavadia
Copy link
Member Author

Thanks for your nice detailed comments @diego-plan9. I addressed your suggestions. See further comments inline.

@diego-plan9
Copy link
Member

Thanks, @ajavadia! I added a small fix to setup.py and should be ready to merge.

In regards to the optional dependencies vs regular dependencies: while making pillow a required dependency arguably makes installing all the dependencies needed for the feature easier for the users, I think moving forward we should prime keeping the dependencies lean and respecting the usual convention (ie. consider the feature as a whole "optional" or "required", and be consequent with its dependencies), helping the user via other means (for example, documentation).

This would help us with mantainability and avoiding to have a number of dependencies installed that are not really in use, being some sort of "good citizen" in that regard. Still, I see the point of view, and we are still far away from a point where we have an unmanageable number of dependencies - so we can revise later if needed!

@diego-plan9 diego-plan9 merged commit c30110e into Qiskit:master Feb 19, 2018
@ajavadia ajavadia deleted the circuit_plotter branch February 24, 2018 17:10
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this pull request Jul 30, 2019
* one more initializer test case

* updating version requirement for IBMQuantumExperience

* fixing per diego's comments

* circuit_drawer() visualization.py, generating PIL images

* adding circuit drawing function and embedded example in initialize

* added test

* linting

* more linting

* better exception handling in case dependencies not installed.

* fixing circuit drawing tests

* more linting-_-

* fixing per diego's comments

* lint

* improved exception handling for dependencies pillow, poppler, pdflatex, qcircuit

* adding id to drawing basis default

* Fix typo in setup.py.in
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.

2 participants
0