Skip to content

Commit

Permalink
Deprecate allowing extra keys in circuit drawer style dict (Qiskit#3105)
Browse files Browse the repository at this point in the history
* test

* cast in insinstance

* style

* layouts are already layouts :)

* style['plotbarrier'] is being deprecated

* internal param for text drawer renamed to plot_barriers

* error when style contains unknown options

* better error message

* remove Qiskit#3100, since it was not my intention to be on it

* style

* remove plotbarrier

* unused import

* replace error with warning

* release note
  • Loading branch information
Luciano authored Sep 25, 2019
1 parent 7d2fa6b commit da775d4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 53 deletions.
9 changes: 4 additions & 5 deletions qiskit/visualization/circuit_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def circuit_drawer(circuit,
return _text_circuit_drawer(circuit, filename=filename,
line_length=line_length,
reverse_bits=reverse_bits,
plotbarriers=plot_barriers,
plot_barriers=plot_barriers,
justify=justify,
vertical_compression=vertical_compression,
idle_wires=idle_wires,
Expand Down Expand Up @@ -327,7 +327,6 @@ def qx_color_scheme():
"latexdrawerstyle": True,
"usepiformat": False,
"cregbundle": False,
"plotbarrier": False,
"showindex": False,
"compress": True,
"margin": [2.0, 0.0, 0.0, 0.3],
Expand All @@ -342,7 +341,7 @@ def qx_color_scheme():


def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits=False,
plotbarriers=True, justify=None, vertical_compression='high',
plot_barriers=True, justify=None, vertical_compression='high',
idle_wires=True, with_layout=True):
"""
Draws a circuit using ascii art.
Expand All @@ -355,7 +354,7 @@ def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits=
shutil.get_terminal_size(). If you don't want pagination
at all, set line_length=-1.
reverse_bits (bool): Rearrange the bits in reverse order.
plotbarriers (bool): Draws the barriers when they are there.
plot_barriers (bool): Draws the barriers when they are there.
justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how
the circuit should be justified.
vertical_compression (string): `high`, `medium`, or `low`. It merges the
Expand All @@ -375,7 +374,7 @@ def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits=
else:
layout = None
text_drawing = _text.TextDrawing(qregs, cregs, ops, layout=layout)
text_drawing.plotbarriers = plotbarriers
text_drawing.plotbarriers = plot_barriers
text_drawing.line_length = line_length
text_drawing.vertical_compression = vertical_compression

Expand Down
100 changes: 54 additions & 46 deletions qiskit/visualization/qcstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@

# pylint: disable=invalid-name,missing-docstring

from copy import copy
from warnings import warn


class DefaultStyle:
"""IBM Design Style colors
"""

def __init__(self):
# Set colors
basis_color = '#FA74A6'
Expand Down Expand Up @@ -88,37 +92,39 @@ def __init__(self):
self.latexmode = False
self.fold = 25
self.bundle = True
self.barrier = True
self.index = False
self.figwidth = -1
self.dpi = 150
self.margin = [2.0, 0.1, 0.1, 0.3]
self.cline = 'doublet'

def set_style(self, dic):
self.tc = dic.get('textcolor', self.tc)
self.sc = dic.get('subtextcolor', self.sc)
self.lc = dic.get('linecolor', self.lc)
self.cc = dic.get('creglinecolor', self.cc)
self.gt = dic.get('gatetextcolor', self.tc)
self.gc = dic.get('gatefacecolor', self.gc)
self.bc = dic.get('barrierfacecolor', self.bc)
self.bg = dic.get('backgroundcolor', self.bg)
self.fs = dic.get('fontsize', self.fs)
self.sfs = dic.get('subfontsize', self.sfs)
self.disptex = dic.get('displaytext', self.disptex)
self.dispcol = dic.get('displaycolor', self.dispcol)
self.latexmode = dic.get('latexdrawerstyle', self.latexmode)
self.fold = dic.get('fold', self.fold)
def set_style(self, style_dic):
dic = copy(style_dic)
self.tc = dic.pop('textcolor', self.tc)
self.sc = dic.pop('subtextcolor', self.sc)
self.lc = dic.pop('linecolor', self.lc)
self.cc = dic.pop('creglinecolor', self.cc)
self.gt = dic.pop('gatetextcolor', self.tc)
self.gc = dic.pop('gatefacecolor', self.gc)
self.bc = dic.pop('barrierfacecolor', self.bc)
self.bg = dic.pop('backgroundcolor', self.bg)
self.fs = dic.pop('fontsize', self.fs)
self.sfs = dic.pop('subfontsize', self.sfs)
self.disptex = dic.pop('displaytext', self.disptex)
self.dispcol = dic.pop('displaycolor', self.dispcol)
self.latexmode = dic.pop('latexdrawerstyle', self.latexmode)
self.fold = dic.pop('fold', self.fold)
if self.fold < 2:
self.fold = -1
self.bundle = dic.get('cregbundle', self.bundle)
self.barrier = dic.get('plotbarrier', self.barrier)
self.index = dic.get('showindex', self.index)
self.figwidth = dic.get('figwidth', self.figwidth)
self.dpi = dic.get('dpi', self.dpi)
self.margin = dic.get('margin', self.margin)
self.cline = dic.get('creglinestyle', self.cline)
self.bundle = dic.pop('cregbundle', self.bundle)
self.index = dic.pop('showindex', self.index)
self.figwidth = dic.pop('figwidth', self.figwidth)
self.dpi = dic.pop('dpi', self.dpi)
self.margin = dic.pop('margin', self.margin)
self.cline = dic.pop('creglinestyle', self.cline)
if dic:
warn('style option/s ({}) is/are not '
'supported'.format(', '.join(dic.keys())), DeprecationWarning, 2)


class BWStyle:
Expand Down Expand Up @@ -184,36 +190,38 @@ def __init__(self):
self.latexmode = False
self.fold = 25
self.bundle = True
self.barrier = True
self.index = False
self.figwidth = -1
self.dpi = 150
self.margin = [2.0, 0.0, 0.0, 0.3]
self.cline = 'doublet'

def set_style(self, dic):
self.tc = dic.get('textcolor', self.tc)
self.sc = dic.get('subtextcolor', self.sc)
self.lc = dic.get('linecolor', self.lc)
self.cc = dic.get('creglinecolor', self.cc)
self.gt = dic.get('gatetextcolor', self.tc)
self.gc = dic.get('gatefacecolor', self.gc)
self.bc = dic.get('barrierfacecolor', self.bc)
self.bg = dic.get('backgroundcolor', self.bg)
self.fs = dic.get('fontsize', self.fs)
self.sfs = dic.get('subfontsize', self.sfs)
self.disptex = dic.get('displaytext', self.disptex)
def set_style(self, style_dic):
dic = copy(style_dic)
self.tc = dic.pop('textcolor', self.tc)
self.sc = dic.pop('subtextcolor', self.sc)
self.lc = dic.pop('linecolor', self.lc)
self.cc = dic.pop('creglinecolor', self.cc)
self.gt = dic.pop('gatetextcolor', self.tc)
self.gc = dic.pop('gatefacecolor', self.gc)
self.bc = dic.pop('barrierfacecolor', self.bc)
self.bg = dic.pop('backgroundcolor', self.bg)
self.fs = dic.pop('fontsize', self.fs)
self.sfs = dic.pop('subfontsize', self.sfs)
self.disptex = dic.pop('displaytext', self.disptex)
for key in self.dispcol.keys():
self.dispcol[key] = self.gc
self.dispcol = dic.get('displaycolor', self.dispcol)
self.latexmode = dic.get('latexdrawerstyle', self.latexmode)
self.fold = dic.get('fold', self.fold)
self.dispcol = dic.pop('displaycolor', self.dispcol)
self.latexmode = dic.pop('latexdrawerstyle', self.latexmode)
self.fold = dic.pop('fold', self.fold)
if self.fold < 2:
self.fold = -1
self.bundle = dic.get('cregbundle', self.bundle)
self.barrier = dic.get('plotbarrier', self.barrier)
self.index = dic.get('showindex', self.index)
self.figwidth = dic.get('figwidth', self.figwidth)
self.dpi = dic.get('dpi', self.dpi)
self.margin = dic.get('margin', self.margin)
self.cline = dic.get('creglinestyle', self.cline)
self.bundle = dic.pop('cregbundle', self.bundle)
self.index = dic.pop('showindex', self.index)
self.figwidth = dic.pop('figwidth', self.figwidth)
self.dpi = dic.pop('dpi', self.dpi)
self.margin = dic.pop('margin', self.margin)
self.cline = dic.pop('creglinestyle', self.cline)
if dic:
warn('style option/s ({}) is/are not '
'supported'.format(', '.join(dic.keys())), DeprecationWarning, 2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
deprecations:
- |
The matplotlib drawer raises a warning when the `style` argument dictonary includes
a key that is not supported by the drawer. In the future, unsupported keys will raise
an exception.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_long_name(self):

filename = self._get_resource_path('current_%s_long_name_matplotlib.png' % os.name)
visualization.circuit_drawer(circuit, output='mpl', filename=filename)
# self.addCleanup(os.remove, filename)
self.addCleanup(os.remove, filename)

ref_filename = self._get_resource_path(
'visualization/references/%s_long_name_matplotlib.png' % os.name)
Expand Down
2 changes: 1 addition & 1 deletion test/python/visualization/test_circuit_text_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def test_text_no_barriers(self):
circuit.barrier(qr1)
circuit.barrier(qr2[1])
circuit.h(qr2)
self.assertEqual(str(_text_circuit_drawer(circuit, plotbarriers=False)), expected)
self.assertEqual(str(_text_circuit_drawer(circuit, plot_barriers=False)), expected)

def test_text_measure_html(self):
""" The measure operator. HTML representation. """
Expand Down

0 comments on commit da775d4

Please sign in to comment.