From da775d4209b0d25fc81a058d444d8a25214ab524 Mon Sep 17 00:00:00 2001 From: Luciano Date: Wed, 25 Sep 2019 15:04:42 -0400 Subject: [PATCH] Deprecate allowing extra keys in circuit drawer style dict (#3105) * 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 #3100, since it was not my intention to be on it * style * remove plotbarrier * unused import * replace error with warning * release note --- qiskit/visualization/circuit_visualization.py | 9 +- qiskit/visualization/qcstyle.py | 100 ++++++++++-------- ...ecate_unknown_styles-93f84aedd1887c44.yaml | 6 ++ .../test_circuit_matplotlib_drawer.py | 2 +- .../visualization/test_circuit_text_drawer.py | 2 +- 5 files changed, 66 insertions(+), 53 deletions(-) create mode 100644 releasenotes/notes/deprecate_unknown_styles-93f84aedd1887c44.yaml diff --git a/qiskit/visualization/circuit_visualization.py b/qiskit/visualization/circuit_visualization.py index 8b304d9fc9b6..d0e320622446 100644 --- a/qiskit/visualization/circuit_visualization.py +++ b/qiskit/visualization/circuit_visualization.py @@ -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, @@ -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], @@ -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. @@ -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 @@ -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 diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index 64c6239342c8..f5beafdde21b 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -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' @@ -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: @@ -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) diff --git a/releasenotes/notes/deprecate_unknown_styles-93f84aedd1887c44.yaml b/releasenotes/notes/deprecate_unknown_styles-93f84aedd1887c44.yaml new file mode 100644 index 000000000000..7ada0d9f524c --- /dev/null +++ b/releasenotes/notes/deprecate_unknown_styles-93f84aedd1887c44.yaml @@ -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. diff --git a/test/python/visualization/test_circuit_matplotlib_drawer.py b/test/python/visualization/test_circuit_matplotlib_drawer.py index f38b92a6eb82..279b45564c04 100644 --- a/test/python/visualization/test_circuit_matplotlib_drawer.py +++ b/test/python/visualization/test_circuit_matplotlib_drawer.py @@ -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) diff --git a/test/python/visualization/test_circuit_text_drawer.py b/test/python/visualization/test_circuit_text_drawer.py index fc1ef42c09fb..cb7c0726a2eb 100644 --- a/test/python/visualization/test_circuit_text_drawer.py +++ b/test/python/visualization/test_circuit_text_drawer.py @@ -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. """