diff --git a/scripts/ci_validate_tutorials.sh b/scripts/ci_validate_tutorials.sh index f1294f65b..261885b47 100755 --- a/scripts/ci_validate_tutorials.sh +++ b/scripts/ci_validate_tutorials.sh @@ -27,15 +27,21 @@ pip install \ "ipykernel>=6.29.0" # Gymnasium pip package needed for the quantum reinforcement learning tutorial -pip install gymnasium[classic-control]==1.2.3 +pip install "gymnasium[classic-control]==1.2.3" # seaborn has also numpy dependency, it requires version >= 0.12.0. pip install seaborn==0.12.0 # tf_docs pip package needed for noise tutorial. pip install -q git+https://github.com/tensorflow/docs -# Leave the quantum directory, otherwise errors may occur -cd .. -examples_output=$(python3 quantum/scripts/test_tutorials.py) +# Leave the repository directory, otherwise errors may occur +thisdir=$(CDPATH="" cd -- "$(dirname -- "${0}")" && pwd -P) +repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel 2>/dev/null) +parent_dir=$(dirname "${repo_dir}") +repo_name=$(basename "${repo_dir}") + +cd "${parent_dir}" + +examples_output=$(python3 "${repo_name}/scripts/test_tutorials.py") exit_code=$? if [ "$exit_code" == "0" ]; then diff --git a/scripts/run_example.sh b/scripts/run_example.sh index bb86edc22..d8d6cb9b2 100755 --- a/scripts/run_example.sh +++ b/scripts/run_example.sh @@ -1,18 +1,28 @@ #!/bin/bash # Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved. -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -cd .. -cp quantum/scripts/import_test.py import_test.py -python import_test.py \ No newline at end of file + +set -e + +thisdir=$(CDPATH="" cd -- "$(dirname -- "${0}")" && pwd -P) +repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel 2>/dev/null) +parent_dir=$(dirname "${repo_dir}") +repo_name=$(basename "${repo_dir}") + +# Leave the repository directory; otherwise, errors may occur. +cd "${parent_dir}" + +cp "${repo_name}/scripts/import_test.py" import_test.py +python import_test.py diff --git a/scripts/test_tutorials.py b/scripts/test_tutorials.py index 4d06010c6..2ea44ba19 100644 --- a/scripts/test_tutorials.py +++ b/scripts/test_tutorials.py @@ -17,6 +17,7 @@ import os import glob import re +import subprocess from absl.testing import parameterized import nbformat @@ -29,8 +30,11 @@ # Pylint doesn't like code before imports, but we need the env var set first. import tensorflow as tf # pylint: disable=wrong-import-position -# Must be run from the directory containing `quantum` repo. -NOTEBOOKS = glob.glob("quantum/docs/tutorials/*.ipynb") +# Must be run from the directory containing the repository. +this_dir = os.path.dirname(os.path.abspath(__file__)) +repo_name = os.path.basename(os.path.abspath(os.path.join(this_dir, ".."))) + +NOTEBOOKS = glob.glob(os.path.join(repo_name, "docs", "tutorials", "*.ipynb")) class ExamplesTest(tf.test.TestCase, parameterized.TestCase):