From 52c314fd05d19069a7666d01562f7b6514e2cae9 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Sat, 14 Sep 2019 15:07:48 +0200 Subject: [PATCH 01/14] test --- test/python/transpiler/test_layout.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/python/transpiler/test_layout.py b/test/python/transpiler/test_layout.py index 95d90f98acd3..0b7149f8c12c 100644 --- a/test/python/transpiler/test_layout.py +++ b/test/python/transpiler/test_layout.py @@ -17,6 +17,7 @@ import copy import unittest import warnings +import numpy from qiskit.circuit import QuantumRegister, Qubit from qiskit.transpiler.layout import Layout @@ -325,6 +326,20 @@ def test_layout_from_intlist(self): self.assertDictEqual(layout._p2v, expected._p2v) self.assertDictEqual(layout._v2p, expected._v2p) + def test_layout_from_intlist_numpy(self): + """Create a layout from a list of numpy integers. See #3097 + """ + qr1 = QuantumRegister(1, 'qr1') + qr2 = QuantumRegister(2, 'qr2') + qr3 = QuantumRegister(3, 'qr3') + intlist_layout = list(numpy.array([0, 1, 2, 3, 4, 5])) + layout = Layout.from_intlist(intlist_layout, qr1, qr2, qr3) + + expected = Layout.generate_trivial_layout(qr1, qr2, qr3) + self.maxDiff = None + self.assertDictEqual(layout._p2v, expected._p2v) + self.assertDictEqual(layout._v2p, expected._v2p) + def test_layout_from_intlist_short(self): """If the intlist is longer that your quantum register, map them to None. virtual physical From 53bbbcd36b50ed79b585d432e796dd13d81ae73a Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Sat, 14 Sep 2019 16:18:48 +0200 Subject: [PATCH 02/14] cast in insinstance --- qiskit/compiler/transpile.py | 10 +++++++--- qiskit/converters/__init__.py | 21 +++++++++++++++++++++ qiskit/transpiler/layout.py | 11 ++++++----- test/python/transpiler/test_layout.py | 3 +-- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/qiskit/compiler/transpile.py b/qiskit/compiler/transpile.py index aa14d3ffeaa0..4aa8792d9c5d 100644 --- a/qiskit/compiler/transpile.py +++ b/qiskit/compiler/transpile.py @@ -24,7 +24,7 @@ from qiskit.circuit.quantumregister import Qubit from qiskit import user_config from qiskit.transpiler.exceptions import TranspilerError - +from qiskit.converters import isinstanceint, isinstancelist def transpile(circuits, backend=None, @@ -330,13 +330,17 @@ def _parse_initial_layout(initial_layout, circuits): # initial_layout could be None, or a list of ints, e.g. [0, 5, 14] # or a list of tuples/None e.g. [qr[0], None, qr[1]] or a dict e.g. {qr[0]: 0} def _layout_from_raw(initial_layout, circuit): - if isinstance(initial_layout, list): - if all(isinstance(elem, int) for elem in initial_layout): + if initial_layout is None: + return initial_layout + elif isinstancelist(initial_layout): + if all(isinstanceint(elem) for elem in initial_layout): initial_layout = Layout.from_intlist(initial_layout, *circuit.qregs) elif all(elem is None or isinstance(elem, Qubit) for elem in initial_layout): initial_layout = Layout.from_qubit_list(initial_layout) elif isinstance(initial_layout, dict): initial_layout = Layout(initial_layout) + else: + raise TranspilerError("The initial_layout parameter could not be parsed") return initial_layout # multiple layouts? diff --git a/qiskit/converters/__init__.py b/qiskit/converters/__init__.py index 2c3fa89968f5..f24c77e73ac4 100644 --- a/qiskit/converters/__init__.py +++ b/qiskit/converters/__init__.py @@ -21,3 +21,24 @@ from .dag_to_circuit import dag_to_circuit from .ast_to_dag import ast_to_dag from .circuit_to_instruction import circuit_to_instruction + + +def isinstanceint(obj): + """ Like isinstance(obj,int), but with casting. Except for strings.""" + if isinstance(obj, str): + return False + try: + int(obj) + return True + except TypeError: + return False + +def isinstancelist(obj): + """ Like isinstance(obj, list), but with casting. Except for strings and dicts.""" + if isinstance(obj, (str, dict)): + return False + try: + list(obj) + return True + except TypeError: + return False diff --git a/qiskit/transpiler/layout.py b/qiskit/transpiler/layout.py index 4b10e72774a8..8a1143a1a5ff 100644 --- a/qiskit/transpiler/layout.py +++ b/qiskit/transpiler/layout.py @@ -23,6 +23,7 @@ from qiskit.circuit.quantumregister import Qubit from qiskit.transpiler.exceptions import LayoutError +from qiskit.converters import isinstanceint class Layout(): @@ -87,11 +88,11 @@ def from_dict(self, input_dict): @staticmethod def order_based_on_type(value1, value2): """decides which one is physical/virtual based on the type. Returns (virtual, physical)""" - if isinstance(value1, int) and isinstance(value2, (Qubit, type(None))): - physical = value1 + if isinstanceint(value1) and isinstance(value2, (Qubit, type(None))): + physical = int(value1) virtual = value2 - elif isinstance(value2, int) and isinstance(value1, (Qubit, type(None))): - physical = value2 + elif isinstanceint(value2) and isinstance(value1, (Qubit, type(None))): + physical = int(value2) virtual = value1 else: raise LayoutError('The map (%s -> %s) has to be a (Bit -> integer)' @@ -272,7 +273,7 @@ def from_intlist(int_list, *qregs): Raises: LayoutError: Invalid input layout. """ - if not all(isinstance(i, int) for i in int_list): + if not all(isinstanceint(i) for i in int_list): raise LayoutError('Expected a list of ints') if len(int_list) != len(set(int_list)): raise LayoutError('Duplicate values not permitted; Layout is bijective.') diff --git a/test/python/transpiler/test_layout.py b/test/python/transpiler/test_layout.py index 0b7149f8c12c..cd8b99bc88d5 100644 --- a/test/python/transpiler/test_layout.py +++ b/test/python/transpiler/test_layout.py @@ -332,11 +332,10 @@ def test_layout_from_intlist_numpy(self): qr1 = QuantumRegister(1, 'qr1') qr2 = QuantumRegister(2, 'qr2') qr3 = QuantumRegister(3, 'qr3') - intlist_layout = list(numpy.array([0, 1, 2, 3, 4, 5])) + intlist_layout = numpy.array([0, 1, 2, 3, 4, 5]) layout = Layout.from_intlist(intlist_layout, qr1, qr2, qr3) expected = Layout.generate_trivial_layout(qr1, qr2, qr3) - self.maxDiff = None self.assertDictEqual(layout._p2v, expected._p2v) self.assertDictEqual(layout._v2p, expected._v2p) From 2113b49b3d2a55de72e3420d499fdca5819cdc84 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Sat, 14 Sep 2019 16:32:39 +0200 Subject: [PATCH 03/14] style --- qiskit/compiler/transpile.py | 1 + qiskit/converters/__init__.py | 1 + 2 files changed, 2 insertions(+) diff --git a/qiskit/compiler/transpile.py b/qiskit/compiler/transpile.py index 4aa8792d9c5d..9b178089853a 100644 --- a/qiskit/compiler/transpile.py +++ b/qiskit/compiler/transpile.py @@ -26,6 +26,7 @@ from qiskit.transpiler.exceptions import TranspilerError from qiskit.converters import isinstanceint, isinstancelist + def transpile(circuits, backend=None, basis_gates=None, coupling_map=None, backend_properties=None, diff --git a/qiskit/converters/__init__.py b/qiskit/converters/__init__.py index f24c77e73ac4..ca668d3a2d0a 100644 --- a/qiskit/converters/__init__.py +++ b/qiskit/converters/__init__.py @@ -33,6 +33,7 @@ def isinstanceint(obj): except TypeError: return False + def isinstancelist(obj): """ Like isinstance(obj, list), but with casting. Except for strings and dicts.""" if isinstance(obj, (str, dict)): From 8b4a124b0b4004bd9c413bdd00280b517f7bec0a Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Sun, 15 Sep 2019 22:55:26 +0200 Subject: [PATCH 04/14] layouts are already layouts :) --- qiskit/compiler/transpile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit/compiler/transpile.py b/qiskit/compiler/transpile.py index 9b178089853a..2fcacc993d54 100644 --- a/qiskit/compiler/transpile.py +++ b/qiskit/compiler/transpile.py @@ -331,7 +331,7 @@ def _parse_initial_layout(initial_layout, circuits): # initial_layout could be None, or a list of ints, e.g. [0, 5, 14] # or a list of tuples/None e.g. [qr[0], None, qr[1]] or a dict e.g. {qr[0]: 0} def _layout_from_raw(initial_layout, circuit): - if initial_layout is None: + if initial_layout is None or isinstance(initial_layout, Layout): return initial_layout elif isinstancelist(initial_layout): if all(isinstanceint(elem) for elem in initial_layout): From 23bba70a5d14494b31a4281610998d26853aec9f Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Mon, 16 Sep 2019 17:18:43 +0200 Subject: [PATCH 05/14] style['plotbarrier'] is being deprecated --- qiskit/visualization/circuit_visualization.py | 1 - qiskit/visualization/qcstyle.py | 11 +++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/qiskit/visualization/circuit_visualization.py b/qiskit/visualization/circuit_visualization.py index 8b304d9fc9b6..9a84a325efdd 100644 --- a/qiskit/visualization/circuit_visualization.py +++ b/qiskit/visualization/circuit_visualization.py @@ -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], diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index 64c6239342c8..b32b3ced49c3 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -14,6 +14,7 @@ # pylint: disable=invalid-name,missing-docstring +import warnings class DefaultStyle: """IBM Design Style colors @@ -88,7 +89,6 @@ def __init__(self): self.latexmode = False self.fold = 25 self.bundle = True - self.barrier = True self.index = False self.figwidth = -1 self.dpi = 150 @@ -113,12 +113,14 @@ def set_style(self, dic): 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) + if 'plotbarrier' in dic: + warnings.warn('The key "plotbarrier" in the argument "style" is being replaced by the' + ' argument "plot_barriers"', DeprecationWarning, 5) class BWStyle: @@ -184,7 +186,6 @@ def __init__(self): self.latexmode = False self.fold = 25 self.bundle = True - self.barrier = True self.index = False self.figwidth = -1 self.dpi = 150 @@ -211,9 +212,11 @@ def set_style(self, dic): 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) + if 'plotbarrier' in dic: + warnings.warn('The key "plotbarrier" in the argument "style" is being replaced by the' + ' argument "plot_barriers"', DeprecationWarning, 5) From 37894e526e41057834ae77ba9035e3171e7f655a Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Mon, 16 Sep 2019 17:20:33 +0200 Subject: [PATCH 06/14] internal param for text drawer renamed to plot_barriers --- qiskit/visualization/circuit_visualization.py | 8 ++++---- test/python/visualization/test_circuit_text_drawer.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/qiskit/visualization/circuit_visualization.py b/qiskit/visualization/circuit_visualization.py index 9a84a325efdd..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, @@ -341,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. @@ -354,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 @@ -374,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/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. """ From 21fa04ee1f67afe9c80224175f9898a20848d402 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Mon, 16 Sep 2019 20:50:54 +0200 Subject: [PATCH 07/14] error when style contains unknown options --- qiskit/visualization/qcstyle.py | 96 +++++++++++-------- .../test_circuit_matplotlib_drawer.py | 2 +- 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index b32b3ced49c3..ac0f6381b1e8 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -15,10 +15,15 @@ # pylint: disable=invalid-name,missing-docstring import warnings +from copy import copy + +from qiskit.visualization.exceptions import VisualizationError + class DefaultStyle: """IBM Design Style colors """ + def __init__(self): # Set colors basis_color = '#FA74A6' @@ -95,32 +100,36 @@ def __init__(self): 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.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 'plotbarrier' in dic: warnings.warn('The key "plotbarrier" in the argument "style" is being replaced by the' ' argument "plot_barriers"', DeprecationWarning, 5) + dic.pop('plotbarrier') + if dic: + raise VisualizationError('style option/s {} are/is not supported'.format(dic)) class BWStyle: @@ -192,31 +201,34 @@ def __init__(self): 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.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 'plotbarrier' in dic: warnings.warn('The key "plotbarrier" in the argument "style" is being replaced by the' ' argument "plot_barriers"', DeprecationWarning, 5) + if dic: + raise VisualizationError('style option/s {} are/is not supported'.format(dic)) 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) From acfec7f474435d2d386992c79a36cfb657f4f4af Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Mon, 16 Sep 2019 21:12:18 +0200 Subject: [PATCH 08/14] better error message --- qiskit/visualization/qcstyle.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index ac0f6381b1e8..a6b810d8a254 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -129,7 +129,8 @@ def set_style(self, style_dic): ' argument "plot_barriers"', DeprecationWarning, 5) dic.pop('plotbarrier') if dic: - raise VisualizationError('style option/s {} are/is not supported'.format(dic)) + raise VisualizationError('style option/s ({}) is/are not ' + 'supported'.format(', '.join(dic.keys()))) class BWStyle: @@ -231,4 +232,6 @@ def set_style(self, style_dic): warnings.warn('The key "plotbarrier" in the argument "style" is being replaced by the' ' argument "plot_barriers"', DeprecationWarning, 5) if dic: - raise VisualizationError('style option/s {} are/is not supported'.format(dic)) + raise VisualizationError('style option/s ({}) is/are not ' + 'supported'.format(', '.join(dic.keys()))) + From 4b38a4128a4aac7a6bdb2f2c3eec648b2a60b401 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Tue, 17 Sep 2019 06:19:54 -0400 Subject: [PATCH 09/14] remove #3100, since it was not my intention to be on it --- qiskit/compiler/transpile.py | 9 ++------- qiskit/converters/__init__.py | 22 ---------------------- qiskit/transpiler/layout.py | 11 +++++------ test/python/transpiler/test_layout.py | 14 -------------- 4 files changed, 7 insertions(+), 49 deletions(-) diff --git a/qiskit/compiler/transpile.py b/qiskit/compiler/transpile.py index 2fcacc993d54..aa14d3ffeaa0 100644 --- a/qiskit/compiler/transpile.py +++ b/qiskit/compiler/transpile.py @@ -24,7 +24,6 @@ from qiskit.circuit.quantumregister import Qubit from qiskit import user_config from qiskit.transpiler.exceptions import TranspilerError -from qiskit.converters import isinstanceint, isinstancelist def transpile(circuits, @@ -331,17 +330,13 @@ def _parse_initial_layout(initial_layout, circuits): # initial_layout could be None, or a list of ints, e.g. [0, 5, 14] # or a list of tuples/None e.g. [qr[0], None, qr[1]] or a dict e.g. {qr[0]: 0} def _layout_from_raw(initial_layout, circuit): - if initial_layout is None or isinstance(initial_layout, Layout): - return initial_layout - elif isinstancelist(initial_layout): - if all(isinstanceint(elem) for elem in initial_layout): + if isinstance(initial_layout, list): + if all(isinstance(elem, int) for elem in initial_layout): initial_layout = Layout.from_intlist(initial_layout, *circuit.qregs) elif all(elem is None or isinstance(elem, Qubit) for elem in initial_layout): initial_layout = Layout.from_qubit_list(initial_layout) elif isinstance(initial_layout, dict): initial_layout = Layout(initial_layout) - else: - raise TranspilerError("The initial_layout parameter could not be parsed") return initial_layout # multiple layouts? diff --git a/qiskit/converters/__init__.py b/qiskit/converters/__init__.py index ca668d3a2d0a..2c3fa89968f5 100644 --- a/qiskit/converters/__init__.py +++ b/qiskit/converters/__init__.py @@ -21,25 +21,3 @@ from .dag_to_circuit import dag_to_circuit from .ast_to_dag import ast_to_dag from .circuit_to_instruction import circuit_to_instruction - - -def isinstanceint(obj): - """ Like isinstance(obj,int), but with casting. Except for strings.""" - if isinstance(obj, str): - return False - try: - int(obj) - return True - except TypeError: - return False - - -def isinstancelist(obj): - """ Like isinstance(obj, list), but with casting. Except for strings and dicts.""" - if isinstance(obj, (str, dict)): - return False - try: - list(obj) - return True - except TypeError: - return False diff --git a/qiskit/transpiler/layout.py b/qiskit/transpiler/layout.py index 8a1143a1a5ff..4b10e72774a8 100644 --- a/qiskit/transpiler/layout.py +++ b/qiskit/transpiler/layout.py @@ -23,7 +23,6 @@ from qiskit.circuit.quantumregister import Qubit from qiskit.transpiler.exceptions import LayoutError -from qiskit.converters import isinstanceint class Layout(): @@ -88,11 +87,11 @@ def from_dict(self, input_dict): @staticmethod def order_based_on_type(value1, value2): """decides which one is physical/virtual based on the type. Returns (virtual, physical)""" - if isinstanceint(value1) and isinstance(value2, (Qubit, type(None))): - physical = int(value1) + if isinstance(value1, int) and isinstance(value2, (Qubit, type(None))): + physical = value1 virtual = value2 - elif isinstanceint(value2) and isinstance(value1, (Qubit, type(None))): - physical = int(value2) + elif isinstance(value2, int) and isinstance(value1, (Qubit, type(None))): + physical = value2 virtual = value1 else: raise LayoutError('The map (%s -> %s) has to be a (Bit -> integer)' @@ -273,7 +272,7 @@ def from_intlist(int_list, *qregs): Raises: LayoutError: Invalid input layout. """ - if not all(isinstanceint(i) for i in int_list): + if not all(isinstance(i, int) for i in int_list): raise LayoutError('Expected a list of ints') if len(int_list) != len(set(int_list)): raise LayoutError('Duplicate values not permitted; Layout is bijective.') diff --git a/test/python/transpiler/test_layout.py b/test/python/transpiler/test_layout.py index cd8b99bc88d5..95d90f98acd3 100644 --- a/test/python/transpiler/test_layout.py +++ b/test/python/transpiler/test_layout.py @@ -17,7 +17,6 @@ import copy import unittest import warnings -import numpy from qiskit.circuit import QuantumRegister, Qubit from qiskit.transpiler.layout import Layout @@ -326,19 +325,6 @@ def test_layout_from_intlist(self): self.assertDictEqual(layout._p2v, expected._p2v) self.assertDictEqual(layout._v2p, expected._v2p) - def test_layout_from_intlist_numpy(self): - """Create a layout from a list of numpy integers. See #3097 - """ - qr1 = QuantumRegister(1, 'qr1') - qr2 = QuantumRegister(2, 'qr2') - qr3 = QuantumRegister(3, 'qr3') - intlist_layout = numpy.array([0, 1, 2, 3, 4, 5]) - layout = Layout.from_intlist(intlist_layout, qr1, qr2, qr3) - - expected = Layout.generate_trivial_layout(qr1, qr2, qr3) - self.assertDictEqual(layout._p2v, expected._p2v) - self.assertDictEqual(layout._v2p, expected._v2p) - def test_layout_from_intlist_short(self): """If the intlist is longer that your quantum register, map them to None. virtual physical From 5ecf565b0e7d35801bc40960bb961e189fe11b1a Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Tue, 17 Sep 2019 09:09:46 -0400 Subject: [PATCH 10/14] style --- qiskit/visualization/qcstyle.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index a6b810d8a254..0dba50d874b0 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -234,4 +234,3 @@ def set_style(self, style_dic): if dic: raise VisualizationError('style option/s ({}) is/are not ' 'supported'.format(', '.join(dic.keys()))) - From f4273375b36363b6c87afe5d01f6a8c76c2386fd Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Tue, 17 Sep 2019 09:32:12 -0400 Subject: [PATCH 11/14] remove plotbarrier --- qiskit/visualization/qcstyle.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index 0dba50d874b0..9f69f1f6ee4e 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -124,10 +124,6 @@ def set_style(self, style_dic): self.dpi = dic.pop('dpi', self.dpi) self.margin = dic.pop('margin', self.margin) self.cline = dic.pop('creglinestyle', self.cline) - if 'plotbarrier' in dic: - warnings.warn('The key "plotbarrier" in the argument "style" is being replaced by the' - ' argument "plot_barriers"', DeprecationWarning, 5) - dic.pop('plotbarrier') if dic: raise VisualizationError('style option/s ({}) is/are not ' 'supported'.format(', '.join(dic.keys()))) @@ -228,9 +224,6 @@ def set_style(self, style_dic): self.dpi = dic.pop('dpi', self.dpi) self.margin = dic.pop('margin', self.margin) self.cline = dic.pop('creglinestyle', self.cline) - if 'plotbarrier' in dic: - warnings.warn('The key "plotbarrier" in the argument "style" is being replaced by the' - ' argument "plot_barriers"', DeprecationWarning, 5) if dic: raise VisualizationError('style option/s ({}) is/are not ' 'supported'.format(', '.join(dic.keys()))) From 9da3208273645e93a6318a3a124209a8ce5155aa Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Tue, 17 Sep 2019 09:39:54 -0400 Subject: [PATCH 12/14] unused import --- qiskit/visualization/qcstyle.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index 9f69f1f6ee4e..d2193e39ed0b 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -14,7 +14,6 @@ # pylint: disable=invalid-name,missing-docstring -import warnings from copy import copy from qiskit.visualization.exceptions import VisualizationError From da802e5af548562599abac0c6df1958f7249f4c1 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Fri, 20 Sep 2019 18:07:07 -0400 Subject: [PATCH 13/14] replace error with warning --- qiskit/visualization/qcstyle.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/qiskit/visualization/qcstyle.py b/qiskit/visualization/qcstyle.py index d2193e39ed0b..f5beafdde21b 100644 --- a/qiskit/visualization/qcstyle.py +++ b/qiskit/visualization/qcstyle.py @@ -15,8 +15,7 @@ # pylint: disable=invalid-name,missing-docstring from copy import copy - -from qiskit.visualization.exceptions import VisualizationError +from warnings import warn class DefaultStyle: @@ -124,8 +123,8 @@ def set_style(self, style_dic): self.margin = dic.pop('margin', self.margin) self.cline = dic.pop('creglinestyle', self.cline) if dic: - raise VisualizationError('style option/s ({}) is/are not ' - 'supported'.format(', '.join(dic.keys()))) + warn('style option/s ({}) is/are not ' + 'supported'.format(', '.join(dic.keys())), DeprecationWarning, 2) class BWStyle: @@ -224,5 +223,5 @@ def set_style(self, style_dic): self.margin = dic.pop('margin', self.margin) self.cline = dic.pop('creglinestyle', self.cline) if dic: - raise VisualizationError('style option/s ({}) is/are not ' - 'supported'.format(', '.join(dic.keys()))) + warn('style option/s ({}) is/are not ' + 'supported'.format(', '.join(dic.keys())), DeprecationWarning, 2) From 5f397c8840329297be70b88d2ebe2715d6eb9858 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Wed, 25 Sep 2019 10:56:07 -0400 Subject: [PATCH 14/14] release note --- .../notes/deprecate_unknown_styles-93f84aedd1887c44.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 releasenotes/notes/deprecate_unknown_styles-93f84aedd1887c44.yaml 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.