diff --git a/qiskit/visualization/bloch.py b/qiskit/visualization/bloch.py index 2855c6ba9651..ae7083a9fd61 100644 --- a/qiskit/visualization/bloch.py +++ b/qiskit/visualization/bloch.py @@ -50,6 +50,7 @@ import math import os +import re import numpy as np import matplotlib import matplotlib.pyplot as plt @@ -60,6 +61,47 @@ from .utils import matplotlib_close_if_inline +# This version pattern is taken from the pypa packaging project: +# https://github.com/pypa/packaging/blob/21.3/packaging/version.py#L223-L254 +# which is dual licensed Apache 2.0 and BSD see the source for the original +# authors and other details +VERSION_PATTERN = ( + "^" + + r""" + v? + (?: + (?:(?P[0-9]+)!)? # epoch + (?P[0-9]+(?:\.[0-9]+)*) # release segment + (?P
                                          # pre-release
+            [-_\.]?
+            (?P(a|b|c|rc|alpha|beta|pre|preview))
+            [-_\.]?
+            (?P[0-9]+)?
+        )?
+        (?P                                         # post release
+            (?:-(?P[0-9]+))
+            |
+            (?:
+                [-_\.]?
+                (?Ppost|rev|r)
+                [-_\.]?
+                (?P[0-9]+)?
+            )
+        )?
+        (?P                                          # dev release
+            [-_\.]?
+            (?Pdev)
+            [-_\.]?
+            (?P[0-9]+)?
+        )?
+    )
+    (?:\+(?P[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?       # local version
+"""
+    + "$"
+)
+VERSION_PATTERN_REGEX = re.compile(VERSION_PATTERN, re.VERBOSE | re.IGNORECASE)
+
+
 class Arrow3D(Patch3D, FancyArrowPatch):
     """Makes a fancy arrow"""
 
@@ -419,7 +461,8 @@ def render(self, title=""):
             self.fig = plt.figure(figsize=self.figsize)
 
         if not self._ext_axes:
-            if tuple(int(x) for x in matplotlib.__version__.split(".")) >= (3, 4, 0):
+            version_match = VERSION_PATTERN_REGEX.search(matplotlib.__version__)
+            if tuple(int(x) for x in version_match.group("release").split(".")) >= (3, 4, 0):
                 self.axes = Axes3D(
                     self.fig, azim=self.view[0], elev=self.view[1], auto_add_to_figure=False
                 )