From 9310ec86e17bf32cf2bd7b573a86e6b44b932ed9 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 30 Sep 2021 11:08:55 +0900 Subject: [PATCH 1/3] Execute import qiskit_metal without PySide2 --- qiskit_metal/__init__.py | 9 +++++---- qiskit_metal/draw/__init__.py | 4 ++-- qiskit_metal/toolbox_metal/about.py | 4 ++-- qiskit_metal/toolbox_python/display.py | 3 ++- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/qiskit_metal/__init__.py b/qiskit_metal/__init__.py index 42dd86f28..e7e893ef7 100644 --- a/qiskit_metal/__init__.py +++ b/qiskit_metal/__init__.py @@ -45,6 +45,7 @@ ########################################################################### ### Basic Setups ## Setup Qt +''' def __setup_Qt_backend(): # pylint: disable=invalid-name """Setup matplotlib to use Qt5's visualization. @@ -112,7 +113,7 @@ def set_attribute(name: str, value=True): __setup_Qt_backend() del __setup_Qt_backend - +''' ## Setup logging from . import config from .toolbox_python._logging import setup_logger @@ -139,16 +140,16 @@ def set_attribute(name: str, value=True): from . import draw from . import renderers from . import qgeometries -from . import analyses +#from . import analyses from . import toolbox_python from . import toolbox_metal # Metal GUI -from ._gui.main_window import MetalGUI +#from ._gui.main_window import MetalGUI # Utility modules # For plotting in matplotlib; May be superseded by a renderer? -from .renderers.renderer_mpl import mpl_toolbox as plt +#from .renderers.renderer_mpl import mpl_toolbox as plt # Utility functions from .toolbox_python.display import Headings diff --git a/qiskit_metal/draw/__init__.py b/qiskit_metal/draw/__init__.py index 490a047f1..eecce048a 100644 --- a/qiskit_metal/draw/__init__.py +++ b/qiskit_metal/draw/__init__.py @@ -23,10 +23,10 @@ from . import basic from . import utility -from . import mpl +#from . import mpl # Useful functions from .utility import get_poly_pts, Vector from .basic import rectangle, is_rectangle, flip_merge, rotate, translate, scale, buffer,\ rotate_position, _iter_func_geom_, union, subtract -from .mpl import render, figure_spawn +#from .mpl import render, figure_spawn diff --git a/qiskit_metal/toolbox_metal/about.py b/qiskit_metal/toolbox_metal/about.py index de5ce0e89..58cb86046 100644 --- a/qiskit_metal/toolbox_metal/about.py +++ b/qiskit_metal/toolbox_metal/about.py @@ -49,8 +49,8 @@ def about(): str: About message """ import qiskit_metal - from PySide2.QtCore import __version__ as QT_VERSION_STR - from PySide2 import __version__ as PYSIDE_VERSION_STR + #from PySide2.QtCore import __version__ as QT_VERSION_STR + #from PySide2 import __version__ as PYSIDE_VERSION_STR try: import matplotlib diff --git a/qiskit_metal/toolbox_python/display.py b/qiskit_metal/toolbox_python/display.py index 8f4aa7431..736f96dec 100644 --- a/qiskit_metal/toolbox_python/display.py +++ b/qiskit_metal/toolbox_python/display.py @@ -97,6 +97,7 @@ def h1(cls, text): # TODO: Move to module for GUI programming +''' def get_screenshot(self: 'QMainWindow', name='shot.png', type_='png', @@ -127,7 +128,7 @@ def get_screenshot(self: 'QMainWindow', _disp_ops = dict(width=500) _disp_ops.update(disp_ops or {}) display(Image(filename=str(path), **_disp_ops)) - +''' ########################################################################## # Shell print From 4b5b29e62309d48438d1c4c71aa4041bdd3603e0 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 10 Oct 2021 21:18:33 +0900 Subject: [PATCH 2/3] Add if statements to check the os is linux --- qiskit_metal/__init__.py | 146 +++++++++++++------------ qiskit_metal/draw/__init__.py | 9 +- qiskit_metal/toolbox_metal/about.py | 6 +- qiskit_metal/toolbox_python/display.py | 64 +++++------ 4 files changed, 121 insertions(+), 104 deletions(-) diff --git a/qiskit_metal/__init__.py b/qiskit_metal/__init__.py index e7e893ef7..22f99a83f 100644 --- a/qiskit_metal/__init__.py +++ b/qiskit_metal/__init__.py @@ -45,75 +45,79 @@ ########################################################################### ### Basic Setups ## Setup Qt -''' -def __setup_Qt_backend(): # pylint: disable=invalid-name - """Setup matplotlib to use Qt5's visualization. - - This function needs to remain in the __init__ of the library's root - to prevent Qt windows from hanging. - """ - # pylint: disable=import-outside-toplevel - os.environ["QT_API"] = "pyside2" - - from PySide2 import QtCore #, QtWidgets - from PySide2.QtCore import Qt - - def set_attribute(name: str, value=True): - """Describes attributes that change the behavior of application-wide - features.""" - if hasattr(Qt, name): - # Does Qt have this attribute - attr = getattr(Qt, name) - if not QtCore.QCoreApplication.testAttribute(attr) == value: - # Only set if not already set - QtCore.QCoreApplication.setAttribute(attr, value) - - if 1: - - if QtCore.QCoreApplication.instance() is None: - # No application launched yet - - # zkm: The following seems to fix warning. - # For example if user ran %gui qt already. - # Qt WebEngine seems to be initialized from a plugin. - # Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute - # before constructing QGuiApplication. - # https://stackoverflow.com/questions/56159475/qt-webengine-seems-to-be-initialized - # Enables resource sharing between the OpenGL contexts used by classes - # like QOpenGLWidget and QQuickWidget. - # Has to do with render mode 'gles'. There is also desktop and software. - # QCoreApplication.setAttribute(QtCore.Qt.AA_UseOpenGLES) - # QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts) - # QCoreApplication.setAttribute(QtCore.Qt.AA_DisableShaderDiskCache) - set_attribute('AA_ShareOpenGLContexts') - - # Enables high-DPI scaling in Qt on supported platforms (see also High DPI Displays). - # Supported platforms are X11, Windows and Android. - # Enabling makes Qt scale the main (device independent) coordinate - # system according to display scale factors provided by the - # operating system. - set_attribute('AA_EnableHighDpiScaling') - - # Make QIcon::pixmap() generate high-dpi pixmaps that can be larger than - # the requested size. - set_attribute('AA_UseHighDpiPixmaps') - - # Other options of interest: - # AA_DontUseNativeMenuBar - # AA_MacDontSwapCtrlAndMeta - - if not os.getenv('QISKIT_METAL_HEADLESS', None): + +# Check that the os is not linux +if os.name != 'posix': + + def __setup_Qt_backend(): # pylint: disable=invalid-name + """Setup matplotlib to use Qt5's visualization. + + This function needs to remain in the __init__ of the library's root + to prevent Qt windows from hanging. + """ # pylint: disable=import-outside-toplevel - import matplotlib as mpl - mpl.use("Qt5Agg") - # pylint: disable=redefined-outer-name - import matplotlib.pyplot as plt - plt.ion() # interactive + os.environ["QT_API"] = "pyside2" + + from PySide2 import QtCore #, QtWidgets + from PySide2.QtCore import Qt + + def set_attribute(name: str, value=True): + """Describes attributes that change the behavior of application-wide + features.""" + if hasattr(Qt, name): + # Does Qt have this attribute + attr = getattr(Qt, name) + if not QtCore.QCoreApplication.testAttribute(attr) == value: + # Only set if not already set + QtCore.QCoreApplication.setAttribute(attr, value) + + if 1: + + if QtCore.QCoreApplication.instance() is None: + # No application launched yet + + # zkm: The following seems to fix warning. + # For example if user ran %gui qt already. + # Qt WebEngine seems to be initialized from a plugin. + # Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute + # before constructing QGuiApplication. + # https://stackoverflow.com/questions/56159475/qt-webengine-seems-to-be-initialized + # Enables resource sharing between the OpenGL contexts used by classes + # like QOpenGLWidget and QQuickWidget. + # Has to do with render mode 'gles'. There is also desktop and software. + # QCoreApplication.setAttribute(QtCore.Qt.AA_UseOpenGLES) + # QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts) + # QCoreApplication.setAttribute(QtCore.Qt.AA_DisableShaderDiskCache) + set_attribute('AA_ShareOpenGLContexts') + + # Enables high-DPI scaling in Qt on supported platforms (see also High DPI Displays). + # Supported platforms are X11, Windows and Android. + # Enabling makes Qt scale the main (device independent) coordinate + # system according to display scale factors provided by the + # operating system. + set_attribute('AA_EnableHighDpiScaling') + + # Make QIcon::pixmap() generate high-dpi pixmaps that can be larger than + # the requested size. + set_attribute('AA_UseHighDpiPixmaps') + + # Other options of interest: + # AA_DontUseNativeMenuBar + # AA_MacDontSwapCtrlAndMeta + + if not os.getenv('QISKIT_METAL_HEADLESS', None): + # pylint: disable=import-outside-toplevel + import matplotlib as mpl + mpl.use("Qt5Agg") + # pylint: disable=redefined-outer-name + import matplotlib.pyplot as plt + plt.ion() # interactive + + + __setup_Qt_backend() + del __setup_Qt_backend -__setup_Qt_backend() -del __setup_Qt_backend -''' ## Setup logging from . import config from .toolbox_python._logging import setup_logger @@ -126,6 +130,7 @@ def set_attribute(name: str, value=True): ########################################################################### ### User-accessible scope +# For executing well on cloud; Insert three if statements # Metal Dict from .toolbox_python.attr_dict import Dict @@ -140,16 +145,19 @@ def set_attribute(name: str, value=True): from . import draw from . import renderers from . import qgeometries -#from . import analyses +if os.name != 'posix': + from . import analyses from . import toolbox_python from . import toolbox_metal # Metal GUI -#from ._gui.main_window import MetalGUI +if os.name != 'posix': + from ._gui.main_window import MetalGUI # Utility modules # For plotting in matplotlib; May be superseded by a renderer? -#from .renderers.renderer_mpl import mpl_toolbox as plt +if os.name != 'posix': + from .renderers.renderer_mpl import mpl_toolbox as plt # Utility functions from .toolbox_python.display import Headings diff --git a/qiskit_metal/draw/__init__.py b/qiskit_metal/draw/__init__.py index eecce048a..a77e2e140 100644 --- a/qiskit_metal/draw/__init__.py +++ b/qiskit_metal/draw/__init__.py @@ -23,10 +23,15 @@ from . import basic from . import utility -#from . import mpl + +# For executing well on cloud +import os +if os.name != 'posix': + from . import mpl # Useful functions from .utility import get_poly_pts, Vector from .basic import rectangle, is_rectangle, flip_merge, rotate, translate, scale, buffer,\ rotate_position, _iter_func_geom_, union, subtract -#from .mpl import render, figure_spawn +if os.name != 'posix': + from .mpl import render, figure_spawn diff --git a/qiskit_metal/toolbox_metal/about.py b/qiskit_metal/toolbox_metal/about.py index 58cb86046..9deda066d 100644 --- a/qiskit_metal/toolbox_metal/about.py +++ b/qiskit_metal/toolbox_metal/about.py @@ -49,8 +49,10 @@ def about(): str: About message """ import qiskit_metal - #from PySide2.QtCore import __version__ as QT_VERSION_STR - #from PySide2 import __version__ as PYSIDE_VERSION_STR + # For executing well on cloud; + if os.name != 'posix': + from PySide2.QtCore import __version__ as QT_VERSION_STR + from PySide2 import __version__ as PYSIDE_VERSION_STR try: import matplotlib diff --git a/qiskit_metal/toolbox_python/display.py b/qiskit_metal/toolbox_python/display.py index 736f96dec..91c4cfbaa 100644 --- a/qiskit_metal/toolbox_python/display.py +++ b/qiskit_metal/toolbox_python/display.py @@ -97,38 +97,40 @@ def h1(cls, text): # TODO: Move to module for GUI programming -''' -def get_screenshot(self: 'QMainWindow', - name='shot.png', - type_='png', - do_display=True, - disp_ops=None): - """Grad a screenshot of the main window, save to file, and then copy to - clipboard. - Args: - self (QMainWindow): Window to take the screenshot of. - name (str): File to save the screenshot to. Defaults to 'shot.png'. - type (str): Type of file to save. Defaults to 'png'. - do_display (bool): True to display the file. Defaults to True. - disp_ops (dict): Disctionary of options. Defaults to None. - """ - from PySide2.QtWidgets import QApplication, QMainWindow - - path = Path(name).resolve() - - # just grab the main window - screenshot = self.grab() # type: QtGui.QPixelMap - screenshot.save(str(path), type_) # Save - - QApplication.clipboard().setPixmap(screenshot) # To clipboard - #print(f'Screenshot copied to clipboard and saved to:\n {path}') - - if do_display: - _disp_ops = dict(width=500) - _disp_ops.update(disp_ops or {}) - display(Image(filename=str(path), **_disp_ops)) -''' +# For executing well on cloud; +import os +if os.name != 'posix': + def get_screenshot(self: 'QMainWindow', + name='shot.png', + type_='png', + do_display=True, + disp_ops=None): + """Grad a screenshot of the main window, save to file, and then copy to + clipboard. + + Args: + self (QMainWindow): Window to take the screenshot of. + name (str): File to save the screenshot to. Defaults to 'shot.png'. + type (str): Type of file to save. Defaults to 'png'. + do_display (bool): True to display the file. Defaults to True. + disp_ops (dict): Disctionary of options. Defaults to None. + """ + from PySide2.QtWidgets import QApplication, QMainWindow + + path = Path(name).resolve() + + # just grab the main window + screenshot = self.grab() # type: QtGui.QPixelMap + screenshot.save(str(path), type_) # Save + + QApplication.clipboard().setPixmap(screenshot) # To clipboard + #print(f'Screenshot copied to clipboard and saved to:\n {path}') + + if do_display: + _disp_ops = dict(width=500) + _disp_ops.update(disp_ops or {}) + display(Image(filename=str(path), **_disp_ops)) ########################################################################## # Shell print From 215f27b47df514016f7e552c49fba2e219aa1fe9 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 24 Oct 2021 20:02:24 +0900 Subject: [PATCH 3/3] Add is_cloud function to consider additional os options --- qiskit_metal/__init__.py | 14 +++++++++----- qiskit_metal/draw/__init__.py | 6 +++--- qiskit_metal/toolbox_metal/about.py | 4 ++-- qiskit_metal/toolbox_python/display.py | 6 +++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/qiskit_metal/__init__.py b/qiskit_metal/__init__.py index 22f99a83f..a27b89410 100644 --- a/qiskit_metal/__init__.py +++ b/qiskit_metal/__init__.py @@ -41,13 +41,17 @@ ) raise +def is_cloud(): + # Check that the os is linux + if os.name == 'posix': + return True + return False ########################################################################### ### Basic Setups ## Setup Qt -# Check that the os is not linux -if os.name != 'posix': +if not is_cloud(): def __setup_Qt_backend(): # pylint: disable=invalid-name """Setup matplotlib to use Qt5's visualization. @@ -145,18 +149,18 @@ def set_attribute(name: str, value=True): from . import draw from . import renderers from . import qgeometries -if os.name != 'posix': +if not is_cloud(): from . import analyses from . import toolbox_python from . import toolbox_metal # Metal GUI -if os.name != 'posix': +if not is_cloud(): from ._gui.main_window import MetalGUI # Utility modules # For plotting in matplotlib; May be superseded by a renderer? -if os.name != 'posix': +if not is_cloud(): from .renderers.renderer_mpl import mpl_toolbox as plt # Utility functions diff --git a/qiskit_metal/draw/__init__.py b/qiskit_metal/draw/__init__.py index a77e2e140..2645cbcac 100644 --- a/qiskit_metal/draw/__init__.py +++ b/qiskit_metal/draw/__init__.py @@ -25,13 +25,13 @@ from . import utility # For executing well on cloud -import os -if os.name != 'posix': +from qiskit_metal import is_cloud +if not is_cloud(): from . import mpl # Useful functions from .utility import get_poly_pts, Vector from .basic import rectangle, is_rectangle, flip_merge, rotate, translate, scale, buffer,\ rotate_position, _iter_func_geom_, union, subtract -if os.name != 'posix': +if not is_cloud(): from .mpl import render, figure_spawn diff --git a/qiskit_metal/toolbox_metal/about.py b/qiskit_metal/toolbox_metal/about.py index 9deda066d..e0c0a2450 100644 --- a/qiskit_metal/toolbox_metal/about.py +++ b/qiskit_metal/toolbox_metal/about.py @@ -49,8 +49,8 @@ def about(): str: About message """ import qiskit_metal - # For executing well on cloud; - if os.name != 'posix': + # For executing well on cloud + if not qiskit_metal.is_cloud(): from PySide2.QtCore import __version__ as QT_VERSION_STR from PySide2 import __version__ as PYSIDE_VERSION_STR diff --git a/qiskit_metal/toolbox_python/display.py b/qiskit_metal/toolbox_python/display.py index 91c4cfbaa..5bdd2e8b8 100644 --- a/qiskit_metal/toolbox_python/display.py +++ b/qiskit_metal/toolbox_python/display.py @@ -98,9 +98,9 @@ def h1(cls, text): # TODO: Move to module for GUI programming -# For executing well on cloud; -import os -if os.name != 'posix': +# For executing well on cloud +from qiskit_metal import is_cloud +if not is_cloud(): def get_screenshot(self: 'QMainWindow', name='shot.png', type_='png',