diff --git a/test/qpy_compat/compare_versions.py b/test/qpy_compat/compare_versions.py new file mode 100755 index 000000000000..5deec1b9ca5b --- /dev/null +++ b/test/qpy_compat/compare_versions.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# This code is part of Qiskit. +# +# (C) Copyright IBM 2023. +# +# This code is licensed under the Apache License, Version 2.0. You may +# obtain a copy of this license in the LICENSE.txt file in the root directory +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +"""Compare Qiskit versions to determine if we should run qpy compat tests.""" + +import argparse +import sys + +from qiskit.qpy.interface import VERSION_PATTERN_REGEX + + +def main(): + """Main function.""" + parser = argparse.ArgumentParser(prog="compare_version", description="Compare version strings") + parser.add_argument( + "source_version", help="Source version of Qiskit that is generating the payload" + ) + parser.add_argument("test_version", help="Version under test that will load the QPY payload") + args = parser.parse_args() + source_match = VERSION_PATTERN_REGEX.search(args.source_version) + target_match = VERSION_PATTERN_REGEX.search(args.test_version) + source_version = tuple(int(x) for x in source_match.group("release").split(".")) + target_version = tuple(int(x) for x in target_match.group("release").split(".")) + if source_version > target_version: + sys.exit(1) + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/test/qpy_compat/process_version.sh b/test/qpy_compat/process_version.sh index 59c5610eb95c..9112be67adb1 100755 --- a/test/qpy_compat/process_version.sh +++ b/test/qpy_compat/process_version.sh @@ -14,9 +14,25 @@ set -e set -x +# version is the source version, this is the release with which to generate +# qpy files with to load with the version under test version=$1 parts=( ${version//./ } ) -if [[ ${parts[1]} -lt 18 ]] ; then +# qiskit_version is the version under test, We're testing that we can correctly +# read the qpy files generated with source version with this version. +qiskit_version=`./qiskit_venv/bin/python -c "import qiskit;print(qiskit.__version__)"` +qiskit_parts=( ${qiskit_version//./ } ) + + +# If source version is less than 0.18 QPY didn't exist yet so exit fast +if [[ ${parts[0]} -eq 0 && ${parts[1]} -lt 18 ]] ; then + exit 0 +fi + +# If the source version is newer than the version under test exit fast because +# there is no QPY compatibility for loading a qpy file generated from a newer +# release with an older release of Qiskit. +if ! ./qiskit_venv/bin/python ./compare_versions.py "$version" "$qiskit_version" ; then exit 0 fi