Skip to content

Commit

Permalink
Move QPY version regex construction to import time (#8235)
Browse files Browse the repository at this point in the history
* Move QPY version regex construction to import time

As pointed out by @nkanazawa1989 in #8232 the regex construction added
to the QPY interface functions in #8200 can be a significant portion of
the overall function time especially for smaller inputs. Especially as
we're building it on every call interface function call. To ameliorate
that cost this commit compiles the version regex a single time at the
module level. This adds the overhead to import but it will only be done
once which should be a net improvement in performance.

* Fix regex flags usage

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit ebf800a)
  • Loading branch information
mtreinish authored and mergify[bot] committed Jun 24, 2022
1 parent f51dd9c commit 51e1cfd
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions qiskit/qpy/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"""
+ "$"
)
VERSION_PATTERN_REGEX = re.compile(VERSION_PATTERN, re.VERBOSE | re.IGNORECASE)


@deprecate_arguments({"circuits": "programs"})
Expand Down Expand Up @@ -149,7 +150,7 @@ def dump(
else:
raise TypeError(f"'{program_type}' is not supported data type.")

version_match = re.search(VERSION_PATTERN, __version__, re.VERBOSE | re.IGNORECASE)
version_match = VERSION_PATTERN_REGEX.search(__version__)
version_parts = [int(x) for x in version_match.group("release").split(".")]
header = struct.pack(
formats.FILE_HEADER_PACK,
Expand Down Expand Up @@ -226,7 +227,7 @@ def load(
)
if data.preface.decode(common.ENCODE) != "QISKIT":
raise QiskitError("Input file is not a valid QPY file")
version_match = re.search(VERSION_PATTERN, __version__, re.VERBOSE | re.IGNORECASE)
version_match = VERSION_PATTERN_REGEX.search(__version__)
version_parts = [int(x) for x in version_match.group("release").split(".")]

header_version_parts = [data.major_version, data.minor_version, data.patch_version]
Expand Down

0 comments on commit 51e1cfd

Please sign in to comment.