From 992569f20e56a606826171ac264c79d83c967634 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:31:04 -0400 Subject: [PATCH 01/10] deprecation --- doc/development/deprecations.rst | 6 ++++++ doc/releases/changelog-dev.md | 5 +++++ pennylane/ops/qubit/state_preparation.py | 13 +++++++++++++ tests/drawer/test_drawable_layers.py | 2 +- tests/ops/functions/conftest.py | 1 - tests/ops/qubit/test_state_prep.py | 5 +++++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/doc/development/deprecations.rst b/doc/development/deprecations.rst index 2f05830d4b8..8e9724a9b6c 100644 --- a/doc/development/deprecations.rst +++ b/doc/development/deprecations.rst @@ -85,6 +85,12 @@ Pending deprecations - Deprecated in v0.37 - Will be removed in v0.39 +* The ``QubitStateVector`` template is deprecated. + Instead, use ``StatePrep``. + + - Deprecated in v0.39 + - Will be removed in v0.40 + New operator arithmetic deprecations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 6f9420e76b6..e222e6f65f6 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -10,6 +10,10 @@

Deprecations 👋

+* The ``QubitStateVector`` template is deprecated. + Instead, use ``StatePrep``. + [(#___)](______) +

Documentation 📝

Bug fixes 🐛

@@ -21,4 +25,5 @@ This release contains contributions from (in alphabetical order): +Guillermo Alonso Jack Brown diff --git a/pennylane/ops/qubit/state_preparation.py b/pennylane/ops/qubit/state_preparation.py index e7fe6c77ece..0da996a7c1c 100644 --- a/pennylane/ops/qubit/state_preparation.py +++ b/pennylane/ops/qubit/state_preparation.py @@ -18,6 +18,8 @@ # pylint:disable=too-many-branches,abstract-method,arguments-differ,protected-access,no-member from typing import Optional +import warnings + import numpy as np import pennylane as qml @@ -388,6 +390,17 @@ def _preprocess(state, wires, pad_with, normalize, validate_norm): # pylint: disable=missing-class-docstring class QubitStateVector(StatePrep): + r""" + ``QubitStateVector`` is deprecated and will be removed in version 0.40. Instead, please use ``StatePrep``. + """ + + def __init__(self, state, wires, pad_with=None, normalize=False, validate_norm=True): + warnings.warn( + "QubitStateVector is deprecated and will be removed in version 0.40. " + "Instead, please use StatePrep.", + qml.PennyLaneDeprecationWarning, + ) + super().__init__(state, wires, pad_with, normalize, validate_norm) pass # QSV is still available diff --git a/tests/drawer/test_drawable_layers.py b/tests/drawer/test_drawable_layers.py index 7e1bdb1d0bd..729e0f74609 100644 --- a/tests/drawer/test_drawable_layers.py +++ b/tests/drawer/test_drawable_layers.py @@ -196,7 +196,7 @@ def test_mid_measure_custom_wires(self): m1 = qml.measurements.MeasurementValue([mp1], lambda v: v) def teleport(state): - qml.QubitStateVector(state, wires=["A"]) + qml.StatePrep(state, wires=["A"]) qml.Hadamard(wires="a") qml.CNOT(wires=["a", "B"]) qml.CNOT(wires=["A", "a"]) diff --git a/tests/ops/functions/conftest.py b/tests/ops/functions/conftest.py index 92863eb7ab1..c32c9d228a2 100644 --- a/tests/ops/functions/conftest.py +++ b/tests/ops/functions/conftest.py @@ -38,7 +38,6 @@ qml.sum(qml.X(0), qml.X(0), qml.Z(0), qml.Z(0)), qml.BasisState([1], wires=[0]), qml.ControlledQubitUnitary(np.eye(2), control_wires=1, wires=0), - qml.QubitStateVector([0, 1], wires=0), qml.QubitChannel([np.array([[1, 0], [0, 0.8]]), np.array([[0, 0.6], [0, 0]])], wires=0), qml.MultiControlledX(wires=[0, 1]), qml.Projector([1], 0), # the state-vector version is already tested diff --git a/tests/ops/qubit/test_state_prep.py b/tests/ops/qubit/test_state_prep.py index 342aaff5df0..56662a62027 100644 --- a/tests/ops/qubit/test_state_prep.py +++ b/tests/ops/qubit/test_state_prep.py @@ -36,6 +36,11 @@ def test_adjoint_error_exception(op): op.adjoint() +def test_QubitStateVector_is_deprecated(): + """Test that QubitStateVector is deprecated.""" + with pytest.warns(qml.PennyLaneDeprecationWarning, match="QubitStateVector is deprecated"): + _ = qml.QubitStateVector([1, 0, 0, 0], wires=[0, 1]) + @pytest.mark.parametrize( "op, mat, base", [ From 63cc54f280c7e31ae9ba72ce5b4361aee60a2a4c Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:08:19 -0400 Subject: [PATCH 02/10] formating and remove test --- pennylane/ops/qubit/state_preparation.py | 3 +-- tests/ops/qubit/test_attributes.py | 1 - tests/ops/qubit/test_state_prep.py | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pennylane/ops/qubit/state_preparation.py b/pennylane/ops/qubit/state_preparation.py index 0da996a7c1c..f3b916ac3e8 100644 --- a/pennylane/ops/qubit/state_preparation.py +++ b/pennylane/ops/qubit/state_preparation.py @@ -388,12 +388,12 @@ def _preprocess(state, wires, pad_with, normalize, validate_norm): return state -# pylint: disable=missing-class-docstring class QubitStateVector(StatePrep): r""" ``QubitStateVector`` is deprecated and will be removed in version 0.40. Instead, please use ``StatePrep``. """ + # pylint: disable=too-many-arguments def __init__(self, state, wires, pad_with=None, normalize=False, validate_norm=True): warnings.warn( "QubitStateVector is deprecated and will be removed in version 0.40. " @@ -401,7 +401,6 @@ def __init__(self, state, wires, pad_with=None, normalize=False, validate_norm=T qml.PennyLaneDeprecationWarning, ) super().__init__(state, wires, pad_with, normalize, validate_norm) - pass # QSV is still available class QubitDensityMatrix(Operation): diff --git a/tests/ops/qubit/test_attributes.py b/tests/ops/qubit/test_attributes.py index fd20efacc13..9808c07d71d 100644 --- a/tests/ops/qubit/test_attributes.py +++ b/tests/ops/qubit/test_attributes.py @@ -137,7 +137,6 @@ def test_tensor_check(self): "SpecialUnitary", "PauliRot", "MultiRZ", - "QubitStateVector", "StatePrep", "AmplitudeEmbedding", "AngleEmbedding", diff --git a/tests/ops/qubit/test_state_prep.py b/tests/ops/qubit/test_state_prep.py index 56662a62027..e6da832a8eb 100644 --- a/tests/ops/qubit/test_state_prep.py +++ b/tests/ops/qubit/test_state_prep.py @@ -41,6 +41,7 @@ def test_QubitStateVector_is_deprecated(): with pytest.warns(qml.PennyLaneDeprecationWarning, match="QubitStateVector is deprecated"): _ = qml.QubitStateVector([1, 0, 0, 0], wires=[0, 1]) + @pytest.mark.parametrize( "op, mat, base", [ From 41cf49bb4dc436a22a55378e3c31abdc1e9089e3 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:33:33 -0400 Subject: [PATCH 03/10] test captures --- doc/releases/changelog-dev.md | 2 +- tests/ops/functions/test_assert_valid.py | 18 +++++++++++++++++- tests/ops/qubit/test_attributes.py | 10 ++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index e222e6f65f6..213432f7a3f 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -12,7 +12,7 @@ * The ``QubitStateVector`` template is deprecated. Instead, use ``StatePrep``. - [(#___)](______) + [(#6172)](https://github.com/PennyLaneAI/pennylane/pull/6172)

Documentation 📝

diff --git a/tests/ops/functions/test_assert_valid.py b/tests/ops/functions/test_assert_valid.py index 9fff7758338..4e770db56fd 100644 --- a/tests/ops/functions/test_assert_valid.py +++ b/tests/ops/functions/test_assert_valid.py @@ -342,7 +342,11 @@ def create_op_instance(c, str_wires=False): if dim == 0: params = [1] * len(ndim_params) elif dim == 1: - params = [[1] * 2**n_wires] * len(ndim_params) + + if c == qml.QubitStateVector: + params = [[1] + [0] * (2**n_wires - 1)] * len(ndim_params) + else: + params = [[1] * 2**n_wires] * len(ndim_params) elif dim == 2: params = [np.eye(2)] * len(ndim_params) else: @@ -352,8 +356,19 @@ def create_op_instance(c, str_wires=False): @pytest.mark.jax +@pytest.fixture(scope="function", autouse=True) +def capture_warnings(recwarn): + """Capture warnings.""" + yield + if len(recwarn) > 0: + for w in recwarn: + assert isinstance(w.message, qml.PennyLaneDeprecationWarning) + assert "QubitStateVector is deprecated" in str(w.message) + + @pytest.mark.parametrize("str_wires", (True, False)) def test_generated_list_of_ops(class_to_validate, str_wires): + """Test every auto-generated operator instance.""" """Test every auto-generated operator instance.""" if class_to_validate.__module__[14:20] == "qutrit": pytest.xfail(reason="qutrit ops fail matrix validation") @@ -390,4 +405,5 @@ def test_explicit_list_of_failing_ops(invalid_instance_and_error): """Test instances of ops that fail validation.""" op, exc_type = invalid_instance_and_error with pytest.raises(exc_type): + print(op) assert_valid(op) diff --git a/tests/ops/qubit/test_attributes.py b/tests/ops/qubit/test_attributes.py index 9808c07d71d..fadd08c7b3b 100644 --- a/tests/ops/qubit/test_attributes.py +++ b/tests/ops/qubit/test_attributes.py @@ -137,6 +137,7 @@ def test_tensor_check(self): "SpecialUnitary", "PauliRot", "MultiRZ", + "QubitStateVector", "StatePrep", "AmplitudeEmbedding", "AngleEmbedding", @@ -150,6 +151,15 @@ class TestSupportsBroadcasting: """Test that all operations in the ``supports_broadcasting`` attribute actually support broadcasting.""" + @pytest.fixture(scope="function", autouse=False) + def capture_warnings(self, recwarn): + """Capture warnings.""" + yield + if len(recwarn) > 0: + for w in recwarn: + assert isinstance(w.message, qml.PennyLaneDeprecationWarning) + assert "QubitStateVector is deprecated" in str(w.message) + def test_all_marked_operations_are_tested(self): """Test that the subsets of the ``supports_broadcasting`` attribute defined above cover the entire attribute.""" From 2a889e910f3d5132b63870c2511bd58a06206154 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:30:29 -0400 Subject: [PATCH 04/10] Update test_assert_valid.py --- tests/ops/functions/test_assert_valid.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/ops/functions/test_assert_valid.py b/tests/ops/functions/test_assert_valid.py index 4e770db56fd..145f687dbff 100644 --- a/tests/ops/functions/test_assert_valid.py +++ b/tests/ops/functions/test_assert_valid.py @@ -360,15 +360,10 @@ def create_op_instance(c, str_wires=False): def capture_warnings(recwarn): """Capture warnings.""" yield - if len(recwarn) > 0: - for w in recwarn: - assert isinstance(w.message, qml.PennyLaneDeprecationWarning) - assert "QubitStateVector is deprecated" in str(w.message) @pytest.mark.parametrize("str_wires", (True, False)) def test_generated_list_of_ops(class_to_validate, str_wires): - """Test every auto-generated operator instance.""" """Test every auto-generated operator instance.""" if class_to_validate.__module__[14:20] == "qutrit": pytest.xfail(reason="qutrit ops fail matrix validation") From c74e9075724730d1aa55398b621bf6ba0bd2b73d Mon Sep 17 00:00:00 2001 From: Guillermo Alonso-Linaje <65235481+KetpuntoG@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:37:10 -0400 Subject: [PATCH 05/10] Update tests/ops/functions/test_assert_valid.py --- tests/ops/functions/test_assert_valid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ops/functions/test_assert_valid.py b/tests/ops/functions/test_assert_valid.py index 145f687dbff..d74a7674f5a 100644 --- a/tests/ops/functions/test_assert_valid.py +++ b/tests/ops/functions/test_assert_valid.py @@ -400,5 +400,4 @@ def test_explicit_list_of_failing_ops(invalid_instance_and_error): """Test instances of ops that fail validation.""" op, exc_type = invalid_instance_and_error with pytest.raises(exc_type): - print(op) assert_valid(op) From a19f5284f10631e6a741b1fa1b14ec8312702e28 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:42:24 -0400 Subject: [PATCH 06/10] isort --- pennylane/ops/qubit/state_preparation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pennylane/ops/qubit/state_preparation.py b/pennylane/ops/qubit/state_preparation.py index f3b916ac3e8..510b8c47a2b 100644 --- a/pennylane/ops/qubit/state_preparation.py +++ b/pennylane/ops/qubit/state_preparation.py @@ -15,11 +15,11 @@ This submodule contains the discrete-variable quantum operations concerned with preparing a certain state on the device. """ +import warnings + # pylint:disable=too-many-branches,abstract-method,arguments-differ,protected-access,no-member from typing import Optional -import warnings - import numpy as np import pennylane as qml From a327fc3fb31b6bf4c74ba417100dbe91ac97f159 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:09:22 -0400 Subject: [PATCH 07/10] removing some test for QubitStateVector --- tests/ops/functions/conftest.py | 1 + tests/ops/functions/test_assert_valid.py | 12 +----------- tests/ops/qubit/test_attributes.py | 10 ---------- 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/tests/ops/functions/conftest.py b/tests/ops/functions/conftest.py index c32c9d228a2..692745b48b6 100644 --- a/tests/ops/functions/conftest.py +++ b/tests/ops/functions/conftest.py @@ -136,6 +136,7 @@ PowOpObs, PowOperation, PowObs, + qml.QubitStateVector, } """Types that should not have actual instances created.""" diff --git a/tests/ops/functions/test_assert_valid.py b/tests/ops/functions/test_assert_valid.py index 71705df941d..e426b91a405 100644 --- a/tests/ops/functions/test_assert_valid.py +++ b/tests/ops/functions/test_assert_valid.py @@ -342,11 +342,7 @@ def create_op_instance(c, str_wires=False): if dim == 0: params = [1] * len(ndim_params) elif dim == 1: - - if c == qml.QubitStateVector: - params = [[1] + [0] * (2**n_wires - 1)] * len(ndim_params) - else: - params = [[1] * 2**n_wires] * len(ndim_params) + params = [[1] * 2**n_wires] * len(ndim_params) elif dim == 2: params = [np.eye(2)] * len(ndim_params) else: @@ -356,12 +352,6 @@ def create_op_instance(c, str_wires=False): @pytest.mark.jax -@pytest.fixture(scope="function", autouse=True) -def capture_warnings(recwarn): - """Capture warnings.""" - yield - - @pytest.mark.parametrize("str_wires", (True, False)) def test_generated_list_of_ops(class_to_validate, str_wires): """Test every auto-generated operator instance.""" diff --git a/tests/ops/qubit/test_attributes.py b/tests/ops/qubit/test_attributes.py index fadd08c7b3b..9808c07d71d 100644 --- a/tests/ops/qubit/test_attributes.py +++ b/tests/ops/qubit/test_attributes.py @@ -137,7 +137,6 @@ def test_tensor_check(self): "SpecialUnitary", "PauliRot", "MultiRZ", - "QubitStateVector", "StatePrep", "AmplitudeEmbedding", "AngleEmbedding", @@ -151,15 +150,6 @@ class TestSupportsBroadcasting: """Test that all operations in the ``supports_broadcasting`` attribute actually support broadcasting.""" - @pytest.fixture(scope="function", autouse=False) - def capture_warnings(self, recwarn): - """Capture warnings.""" - yield - if len(recwarn) > 0: - for w in recwarn: - assert isinstance(w.message, qml.PennyLaneDeprecationWarning) - assert "QubitStateVector is deprecated" in str(w.message) - def test_all_marked_operations_are_tested(self): """Test that the subsets of the ``supports_broadcasting`` attribute defined above cover the entire attribute.""" From dfd653cac81223c0c9cdfddbed36a33eb9ef2fe5 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:37:03 -0400 Subject: [PATCH 08/10] Update test_attributes.py --- tests/ops/qubit/test_attributes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ops/qubit/test_attributes.py b/tests/ops/qubit/test_attributes.py index 9808c07d71d..c0f71afec62 100644 --- a/tests/ops/qubit/test_attributes.py +++ b/tests/ops/qubit/test_attributes.py @@ -143,6 +143,7 @@ def test_tensor_check(self): "IQPEmbedding", "QAOAEmbedding", "PCPhase", + "QubitStateVector", ] From 8baae198a6009b26686eaaed419e60a3ebfc6261 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:53:38 -0400 Subject: [PATCH 09/10] Update test_attributes.py --- tests/ops/qubit/test_attributes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ops/qubit/test_attributes.py b/tests/ops/qubit/test_attributes.py index c0f71afec62..d29acd4c509 100644 --- a/tests/ops/qubit/test_attributes.py +++ b/tests/ops/qubit/test_attributes.py @@ -137,13 +137,13 @@ def test_tensor_check(self): "SpecialUnitary", "PauliRot", "MultiRZ", + "QubitStateVector"", "StatePrep", "AmplitudeEmbedding", "AngleEmbedding", "IQPEmbedding", "QAOAEmbedding", "PCPhase", - "QubitStateVector", ] From d38fa2f7f95fd640d4da6886e37186dbdaf276b5 Mon Sep 17 00:00:00 2001 From: KetpuntoG <65235481+KetpuntoG@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:53:53 -0400 Subject: [PATCH 10/10] Update test_attributes.py --- tests/ops/qubit/test_attributes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ops/qubit/test_attributes.py b/tests/ops/qubit/test_attributes.py index d29acd4c509..fd20efacc13 100644 --- a/tests/ops/qubit/test_attributes.py +++ b/tests/ops/qubit/test_attributes.py @@ -137,7 +137,7 @@ def test_tensor_check(self): "SpecialUnitary", "PauliRot", "MultiRZ", - "QubitStateVector"", + "QubitStateVector", "StatePrep", "AmplitudeEmbedding", "AngleEmbedding",