-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
"fold" option unified across drawers #3108
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
52c314f
test
53bbbcd
cast in insinstance
2113b49
style
8b4a124
layouts are already layouts :)
af05d52
Merge branch 'master' of github.com:Qiskit/qiskit-terra into 2797_plo…
23bba70
style['plotbarrier'] is being deprecated
37894e5
internal param for text drawer renamed to plot_barriers
21fa04e
error when style contains unknown options
acfec7f
better error message
e622a62
deprecated style option
f2f5cf0
mp karg fold
8c65c50
text fold
58b230d
lint
b9316f2
docstring
bba055f
cregbundle text option
cc4082d
Merge branch '2797_cregbundle' into 2797_line_length
90a839b
Revert "cregbundle text option"
d7f7e6f
revert 3100
b7e5193
merge
77ee3d5
new pickles
75b4244
cleaning up the diff
5880e21
Merge branch 'master' of github.com:Qiskit/qiskit-terra into 2797_lin…
73da317
Merge branch 'master' of github.com:Qiskit/qiskit-terra into 2797_lin…
bbe08ac
docstring
ec7543a
Merge branch 'master' into 2797_line_length
8c90661
Merge branch 'master' into 2797_line_length
ajavadia 4a2cf22
pickles
1c77744
Merge branch 'master' of github.com:Qiskit/qiskit-terra into 2797_lin…
7fe8815
Merge branch 'master' into 2797_line_length
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -560,8 +560,8 @@ def qasm(self): | |
|
||
def draw(self, scale=0.7, filename=None, style=None, output=None, | ||
interactive=False, line_length=None, plot_barriers=True, | ||
reverse_bits=False, justify=None, vertical_compression='medium', idle_wires=True, | ||
with_layout=True): | ||
reverse_bits=False, justify=None, idle_wires=True, vertical_compression='medium', | ||
with_layout=True, fold=None): | ||
"""Draw the quantum circuit | ||
|
||
Using the output parameter you can specify the format. The choices are: | ||
|
@@ -604,6 +604,12 @@ def draw(self, scale=0.7, filename=None, style=None, output=None, | |
idle_wires (bool): Include idle wires. Default is True. | ||
with_layout (bool): Include layout information, with labels on the physical | ||
layout. Default is True. | ||
fold (int): Sets pagination. It can be disabled using -1. | ||
In `text`, sets the length of the lines. This useful when the | ||
drawing does not fit in the console. If None (default), it will try to | ||
guess the console width using `shutil.get_terminal_size()`. However, if | ||
running in jupyter, the default line length is set to 80 characters. | ||
In `mpl` is the amount of operations before folding. Default is 25. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth saying explicitly that for |
||
Returns: | ||
PIL.Image or matplotlib.figure or str or TextDrawing: | ||
* PIL.Image: (output `latex`) an in-memory representation of the | ||
|
@@ -629,7 +635,7 @@ def draw(self, scale=0.7, filename=None, style=None, output=None, | |
justify=justify, | ||
vertical_compression=vertical_compression, | ||
idle_wires=idle_wires, | ||
with_layout=with_layout) | ||
with_layout=with_layout, fold=fold) | ||
|
||
def size(self): | ||
"""Returns total number of gate operations in circuit. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,6 @@ | |
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
# TODO: Remove after 0.7 and the deprecated methods are removed | ||
|
||
|
||
""" | ||
Two quantum circuit drawers based on: | ||
|
@@ -27,6 +25,7 @@ | |
import os | ||
import subprocess | ||
import tempfile | ||
from warnings import warn | ||
|
||
try: | ||
from PIL import Image | ||
|
@@ -56,7 +55,8 @@ def circuit_drawer(circuit, | |
justify=None, | ||
vertical_compression='medium', | ||
idle_wires=True, | ||
with_layout=True): | ||
with_layout=True, | ||
fold=None): | ||
"""Draw a quantum circuit to different formats (set by output parameter): | ||
0. text: ASCII art TextDrawing that can be printed in the console. | ||
1. latex: high-quality images, but heavy external software dependencies | ||
|
@@ -81,12 +81,7 @@ def circuit_drawer(circuit, | |
supporting this). Note when used with either the `text` or the | ||
`latex_source` output type this has no effect and will be silently | ||
ignored. | ||
line_length (int): Sets the length of the lines generated by `text` | ||
output type. This useful when the drawing does not fit in the | ||
console. If None (default), it will try to guess the console width | ||
using shutil.get_terminal_size(). However, if you're running in | ||
jupyter the default line length is set to 80 characters. If you | ||
don't want pagination at all, set `line_length=-1`. | ||
line_length (int): Deprecated. See `fold`. | ||
reverse_bits (bool): When set to True reverse the bit order inside | ||
registers for the output visualization. | ||
plot_barriers (bool): Enable/disable drawing barriers in the output | ||
|
@@ -102,6 +97,12 @@ def circuit_drawer(circuit, | |
idle_wires (bool): Include idle wires. Default is True. | ||
with_layout (bool): Include layout information, with labels on the physical | ||
layout. | ||
fold (int): Sets pagination. It can be disabled using -1. | ||
In `text`, sets the length of the lines. This useful when the | ||
drawing does not fit in the console. If None (default), it will try to | ||
guess the console width using `shutil.get_terminal_size()`. However, if | ||
running in jupyter, the default line length is set to 80 characters. | ||
In `mpl` is the amount of operations before folding. Default is 25. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above |
||
Returns: | ||
PIL.Image: (output `latex`) an in-memory representation of the image | ||
of the circuit diagram. | ||
|
@@ -229,7 +230,8 @@ def circuit_drawer(circuit, | |
justify=justify, | ||
vertical_compression=vertical_compression, | ||
idle_wires=idle_wires, | ||
with_layout=with_layout) | ||
with_layout=with_layout, | ||
fold=fold) | ||
elif output == 'latex': | ||
image = _latex_circuit_drawer(circuit, scale=scale, | ||
filename=filename, style=style, | ||
|
@@ -254,7 +256,8 @@ def circuit_drawer(circuit, | |
reverse_bits=reverse_bits, | ||
justify=justify, | ||
idle_wires=idle_wires, | ||
with_layout=with_layout) | ||
with_layout=with_layout, | ||
fold=fold) | ||
else: | ||
raise exceptions.VisualizationError( | ||
'Invalid output type %s selected. The only valid choices ' | ||
|
@@ -342,17 +345,13 @@ def qx_color_scheme(): | |
|
||
def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits=False, | ||
plot_barriers=True, justify=None, vertical_compression='high', | ||
idle_wires=True, with_layout=True): | ||
idle_wires=True, with_layout=True, fold=None,): | ||
""" | ||
Draws a circuit using ascii art. | ||
Args: | ||
circuit (QuantumCircuit): Input circuit | ||
filename (str): optional filename to write the result | ||
line_length (int): Optional. Breaks the circuit drawing to this length. This | ||
useful when the drawing does not fit in the console. If | ||
None (default), it will try to guess the console width using | ||
shutil.get_terminal_size(). If you don't want pagination | ||
at all, set line_length=-1. | ||
line_length (int): Deprecated. See `fold`. | ||
reverse_bits (bool): Rearrange the bits in reverse order. | ||
plot_barriers (bool): Draws the barriers when they are there. | ||
justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how | ||
|
@@ -362,6 +361,11 @@ def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits= | |
idle_wires (bool): Include idle wires. Default is True. | ||
with_layout (bool): Include layout information, with labels on the physical | ||
layout. Default: True | ||
fold (int): Optional. Breaks the circuit drawing to this length. This | ||
useful when the drawing does not fit in the console. If | ||
None (default), it will try to guess the console width using | ||
`shutil.get_terminal_size()`. If you don't want pagination | ||
at all, set `fold=-1`. | ||
Returns: | ||
TextDrawing: An instances that, when printed, draws the circuit in ascii art. | ||
""" | ||
|
@@ -373,9 +377,12 @@ def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits= | |
layout = circuit._layout | ||
else: | ||
layout = None | ||
if line_length: | ||
warn('The parameter "line_length" is being replaced by "fold"', DeprecationWarning, 3) | ||
fold = line_length | ||
text_drawing = _text.TextDrawing(qregs, cregs, ops, layout=layout) | ||
text_drawing.plotbarriers = plot_barriers | ||
text_drawing.line_length = line_length | ||
text_drawing.line_length = fold | ||
text_drawing.vertical_compression = vertical_compression | ||
|
||
if filename: | ||
|
@@ -530,7 +537,8 @@ def _matplotlib_circuit_drawer(circuit, | |
reverse_bits=False, | ||
justify=None, | ||
idle_wires=True, | ||
with_layout=True): | ||
with_layout=True, | ||
fold=None): | ||
"""Draw a quantum circuit based on matplotlib. | ||
If `%matplotlib inline` is invoked in a Jupyter notebook, it visualizes a circuit inline. | ||
We recommend `%config InlineBackend.figure_format = 'svg'` for the inline visualization. | ||
|
@@ -544,11 +552,12 @@ def _matplotlib_circuit_drawer(circuit, | |
registers for the output visualization. | ||
plot_barriers (bool): Enable/disable drawing barriers in the output | ||
circuit. Defaults to True. | ||
justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how | ||
justify (str): `left`, `right` or `none`. Defaults to `left`. Says how | ||
the circuit should be justified. | ||
idle_wires (bool): Include idle wires. Default is True. | ||
with_layout (bool): Include layout information, with labels on the physical | ||
layout. Default: True. | ||
fold (int): amount ops allowed before folding. Default is 25. | ||
Returns: | ||
matplotlib.figure: a matplotlib figure object for the circuit diagram | ||
""" | ||
|
@@ -562,7 +571,10 @@ def _matplotlib_circuit_drawer(circuit, | |
else: | ||
layout = None | ||
|
||
if fold is None: | ||
fold = 25 | ||
|
||
qcd = _matplotlib.MatplotlibDrawer(qregs, cregs, ops, scale=scale, style=style, | ||
plot_barriers=plot_barriers, | ||
reverse_bits=reverse_bits, layout=layout) | ||
reverse_bits=reverse_bits, layout=layout, fold=fold) | ||
return qcd.draw(filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.