Skip to content

Commit

Permalink
Move QPY version regex construction to import time
Browse files Browse the repository at this point in the history
As pointed out by @nkanazawa1989 in Qiskit#8232 the regex construction added
to the QPY interface functions in Qiskit#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.
  • Loading branch information
mtreinish committed Jun 24, 2022
1 parent 4aa18e8 commit 53e6add
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)


@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__, re.VERBOSE | re.IGNORECASE)
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__, re.VERBOSE | re.IGNORECASE)
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 53e6add

Please sign in to comment.