From 4ab2b4370d8633dc88265f530accd48a79128919 Mon Sep 17 00:00:00 2001 From: Shiro-Raven Date: Wed, 24 Jul 2024 13:46:08 -0400 Subject: [PATCH 1/3] localize RNG in TF test --- ..._tensorflow_autograph_qnode_shot_vector.py | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/interfaces/test_tensorflow_autograph_qnode_shot_vector.py b/tests/interfaces/test_tensorflow_autograph_qnode_shot_vector.py index ab3147dfc95..f5b6e37a85b 100644 --- a/tests/interfaces/test_tensorflow_autograph_qnode_shot_vector.py +++ b/tests/interfaces/test_tensorflow_autograph_qnode_shot_vector.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Integration tests for using the TF interface with shot vectors and with a QNode""" -# pylint: disable=too-many-arguments,too-few-public-methods +# pylint: disable=too-many-arguments,too-few-public-methods,redefined-outer-name import pytest import pennylane as qml @@ -26,11 +26,17 @@ shots_and_num_copies = [((1, (5, 2), 10), 4)] shots_and_num_copies_hess = [((10, (5, 1)), 2)] -spsa_kwargs = {"h": 10e-2, "num_directions": 30, "sampler_rng": np.random.default_rng(42)} + +kwargs = { + "finite-diff": {"h": 10e-2}, + "parameter-shift": {}, + "spsa": {"h": 10e-2, "num_directions": 30}, +} + qubit_device_and_diff_method = [ - ["default.qubit.legacy", "finite-diff", {"h": 10e-2}], - ["default.qubit.legacy", "parameter-shift", {}], - ["default.qubit.legacy", "spsa", spsa_kwargs], + ["default.qubit.legacy", "finite-diff"], + ["default.qubit.legacy", "parameter-shift"], + ["default.qubit.legacy", "spsa"], ] TOLS = { @@ -40,8 +46,16 @@ } +@pytest.fixture +def gradient_kwargs(request): + diff_method = request.node.funcargs["diff_method"] + return kwargs[diff_method] | ( + {"sampler_rng": np.random.default_rng(42)} if diff_method == "spsa" else {} + ) + + @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) -@pytest.mark.parametrize("dev_name,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev_name,diff_method", qubit_device_and_diff_method) @pytest.mark.parametrize( "decorator,interface", [(tf.function, "tf"), (lambda x: x, "tf-autograph")], @@ -337,7 +351,7 @@ def circuit(a): @pytest.mark.slow @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies_hess) -@pytest.mark.parametrize("dev_name,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev_name,diff_method", qubit_device_and_diff_method) @pytest.mark.parametrize( "decorator,interface", [(tf.function, "tf"), (lambda x: x, "tf-autograph")], @@ -384,11 +398,11 @@ def circuit(x, y): assert h.shape == (2, num_copies) -shots_and_num_copies = [((1000000, 900000, 800000), 3), ((1000000, (900000, 2)), 3)] +shots_and_num_copies = [((20000, 18000, 16000), 3), ((20000, (18000, 2)), 3)] @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) -@pytest.mark.parametrize("dev_name,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev_name,diff_method", qubit_device_and_diff_method) @pytest.mark.parametrize( "decorator,interface", [(tf.function, "tf"), (lambda x: x, "tf-autograph")], From e2fc642712d58c096421873d18677a7ce053d312 Mon Sep 17 00:00:00 2001 From: Shiro-Raven Date: Wed, 24 Jul 2024 14:28:50 -0400 Subject: [PATCH 2/3] runtime improvements and more stable RNGs --- ...graph_qnode_shot_vector_default_qubit_2.py | 34 ++++++++++++------- ...rflow_qnode_shot_vector_default_qubit_2.py | 2 +- .../test_autograd_qnode_shot_vector.py | 33 ++++++++++++------ .../test_tensorflow_qnode_shot_vector.py | 2 +- 4 files changed, 46 insertions(+), 25 deletions(-) diff --git a/tests/interfaces/default_qubit_2_integration/test_tensorflow_autograph_qnode_shot_vector_default_qubit_2.py b/tests/interfaces/default_qubit_2_integration/test_tensorflow_autograph_qnode_shot_vector_default_qubit_2.py index c831fce26cc..f0978ab1e86 100644 --- a/tests/interfaces/default_qubit_2_integration/test_tensorflow_autograph_qnode_shot_vector_default_qubit_2.py +++ b/tests/interfaces/default_qubit_2_integration/test_tensorflow_autograph_qnode_shot_vector_default_qubit_2.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Integration tests for using the TF interface with shot vectors and with a QNode""" -# pylint: disable=too-many-arguments,too-few-public-methods,unexpected-keyword-arg +# pylint: disable=too-many-arguments,too-few-public-methods,unexpected-keyword-arg,redefined-outer-name import pytest import pennylane as qml @@ -27,14 +27,16 @@ shots_and_num_copies = [((1, (5, 2), 10), 4)] shots_and_num_copies_hess = [((10, (5, 1)), 2)] +kwargs = { + "finite-diff": {"h": 10e-2}, + "parameter-shift": {}, + "spsa": {"h": 10e-2, "num_directions": 20}, +} + qubit_device_and_diff_method = [ - [DefaultQubit(), "finite-diff", {"h": 10e-2}], - [DefaultQubit(), "parameter-shift", {}], - [ - DefaultQubit(), - "spsa", - {"h": 10e-2, "num_directions": 20, "sampler_rng": np.random.default_rng(42)}, - ], + [DefaultQubit(), "finite-diff"], + [DefaultQubit(), "parameter-shift"], + [DefaultQubit(), "spsa"], ] TOLS = { @@ -44,8 +46,16 @@ } +@pytest.fixture +def gradient_kwargs(request): + diff_method = request.node.funcargs["diff_method"] + return kwargs[diff_method] | ( + {"sampler_rng": np.random.default_rng(42)} if diff_method == "spsa" else {} + ) + + @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) -@pytest.mark.parametrize("dev,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev,diff_method", qubit_device_and_diff_method) @pytest.mark.parametrize( "decorator,interface", [(tf.function, "tf"), (lambda x: x, "tf-autograph")], @@ -329,7 +339,7 @@ def circuit(a, **_): @pytest.mark.slow @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies_hess) -@pytest.mark.parametrize("dev,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev,diff_method", qubit_device_and_diff_method) @pytest.mark.parametrize( "decorator,interface", [(tf.function, "tf"), (lambda x: x, "tf-autograph")], @@ -374,11 +384,11 @@ def circuit(x, y, **_): assert h.shape == (2, num_copies) -shots_and_num_copies = [((1000000, 900000, 800000), 3), ((1000000, (900000, 2)), 3)] +shots_and_num_copies = [((30000, 28000, 26000), 3), ((20000, (18000, 2)), 3)] @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) -@pytest.mark.parametrize("dev,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev,diff_method", qubit_device_and_diff_method) @pytest.mark.parametrize( "decorator,interface", [(tf.function, "tf"), (lambda x: x, "tf-autograph")], diff --git a/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py b/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py index a3a5606fc78..f35ba1127c2 100644 --- a/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py +++ b/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py @@ -438,7 +438,7 @@ def circuit(x): assert hess.shape == (num_copies, 3, 2, 2) -shots_and_num_copies = [((1000000, 900000, 800000), 3), ((1000000, (900000, 2)), 3)] +shots_and_num_copies = [((20000, 18000, 16000), 3), ((20000, (18000, 2)), 3)] @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) diff --git a/tests/interfaces/test_autograd_qnode_shot_vector.py b/tests/interfaces/test_autograd_qnode_shot_vector.py index 6aee767d01e..24d1d824ced 100644 --- a/tests/interfaces/test_autograd_qnode_shot_vector.py +++ b/tests/interfaces/test_autograd_qnode_shot_vector.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Integration tests for using the Autograd interface with shot vectors and with a QNode""" -# pylint: disable=too-many-arguments +# pylint: disable=too-many-arguments,redefined-outer-name import pytest @@ -25,13 +25,17 @@ shots_and_num_copies = [(((5, 2), 1, 10), 4), ((1, 10, (5, 2)), 4)] shots_and_num_copies_hess = [(((5, 1), 10), 2), ((10, (5, 1)), 2)] -SEED_FOR_SPSA = 42 -spsa_kwargs = {"h": 0.05, "num_directions": 20, "sampler_rng": np.random.default_rng(SEED_FOR_SPSA)} + +kwargs = { + "finite-diff": {"h": 0.05}, + "parameter-shift": {}, + "spsa": {"h": 0.05, "num_directions": 20}, +} qubit_device_and_diff_method = [ - ["default.qubit.legacy", "finite-diff", {"h": 0.05}], - ["default.qubit.legacy", "parameter-shift", {}], - ["default.qubit.legacy", "spsa", spsa_kwargs], + ["default.qubit.legacy", "finite-diff"], + ["default.qubit.legacy", "parameter-shift"], + ["default.qubit.legacy", "spsa"], ] TOLS = { @@ -41,8 +45,16 @@ } +@pytest.fixture +def gradient_kwargs(request): + diff_method = request.node.funcargs["diff_method"] + return kwargs[diff_method] | ( + {"sampler_rng": np.random.default_rng(42)} if diff_method == "spsa" else {} + ) + + @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) -@pytest.mark.parametrize("dev_name,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev_name,diff_method", qubit_device_and_diff_method) class TestReturnWithShotVectors: """Class to test the shape of the Jacobian/Hessian with different return types and shot vectors.""" @@ -369,7 +381,7 @@ def cost(a): @pytest.mark.slow @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies_hess) -@pytest.mark.parametrize("dev_name,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev_name,diff_method", qubit_device_and_diff_method) class TestReturnShotVectorHessian: """Class to test the shape of the Hessian with different return types and shot vectors.""" @@ -553,12 +565,11 @@ def cost2(x): assert hess.shape == (num_copies, 3, 2, 2) -shots_and_num_copies = [((1000000, 900000, 800000), 3), ((1000000, (900000, 2)), 3)] +shots_and_num_copies = [((30000, 28000, 26000), 3), ((30000, (28000, 2)), 3)] -@pytest.mark.skip("failing in CI for inscrutable reasons, passes locally") @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) -@pytest.mark.parametrize("dev_name,diff_method,gradient_kwargs", qubit_device_and_diff_method) +@pytest.mark.parametrize("dev_name,diff_method", qubit_device_and_diff_method) class TestReturnShotVectorIntegration: """Tests for the integration of shots with the autograd interface.""" diff --git a/tests/interfaces/test_tensorflow_qnode_shot_vector.py b/tests/interfaces/test_tensorflow_qnode_shot_vector.py index 614c71ee7d4..bea3056b7c5 100644 --- a/tests/interfaces/test_tensorflow_qnode_shot_vector.py +++ b/tests/interfaces/test_tensorflow_qnode_shot_vector.py @@ -453,7 +453,7 @@ def circuit(x): assert hess.shape == (num_copies, 3, 2, 2) -shots_and_num_copies = [((1000000, 900000, 800000), 3), ((1000000, (900000, 2)), 3)] +shots_and_num_copies = [((20000, 18000, 16000), 3), ((20000, (18000, 2)), 3)] @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies) From 3b0ad7645e5cbbd996ea48137534add43c603bf2 Mon Sep 17 00:00:00 2001 From: Shiro-Raven Date: Wed, 24 Jul 2024 14:49:16 -0400 Subject: [PATCH 3/3] more shots --- .../test_tensorflow_qnode_shot_vector_default_qubit_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py b/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py index f35ba1127c2..c0340aaab9a 100644 --- a/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py +++ b/tests/interfaces/default_qubit_2_integration/test_tensorflow_qnode_shot_vector_default_qubit_2.py @@ -438,7 +438,7 @@ def circuit(x): assert hess.shape == (num_copies, 3, 2, 2) -shots_and_num_copies = [((20000, 18000, 16000), 3), ((20000, (18000, 2)), 3)] +shots_and_num_copies = [((30000, 28000, 26000), 3), ((30000, (28000, 2)), 3)] @pytest.mark.parametrize("shots,num_copies", shots_and_num_copies)