Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate basis state preparation #6116

Merged
merged 33 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c1bdcd0
Update deprecations.rst
KetpuntoG Aug 19, 2024
66d3790
Update changelog-dev.md
KetpuntoG Aug 19, 2024
035fd2c
Update test_templates.py
KetpuntoG Aug 19, 2024
7fe3ce4
Update basis.py
KetpuntoG Aug 19, 2024
806a3d3
Update basis.py
KetpuntoG Aug 19, 2024
52b8b4c
Update test_templates.py
KetpuntoG Aug 19, 2024
cbe8a27
Update test_state_prep.py
KetpuntoG Aug 19, 2024
f606845
Update test_basis_state_prep.py
KetpuntoG Aug 19, 2024
800d7aa
Update test_batch_input.py
KetpuntoG Aug 19, 2024
8fded68
Update test_batch_params.py
KetpuntoG Aug 19, 2024
849176c
Update test_defer_measurements.py
KetpuntoG Aug 19, 2024
f301ed3
black
KetpuntoG Aug 19, 2024
f280871
Update state_preparation.py
KetpuntoG Aug 19, 2024
ef20de5
Removing dependencies
KetpuntoG Aug 19, 2024
0ddb1f0
add error
KetpuntoG Aug 19, 2024
f7fbda4
Update state_preparation.py
KetpuntoG Aug 19, 2024
7e66db5
Update state_preparation.py
KetpuntoG Aug 19, 2024
ada2a3a
Merge branch 'master' into deprecate-BasisStatePreparation
KetpuntoG Aug 26, 2024
78315ce
Update doc/development/deprecations.rst
KetpuntoG Aug 26, 2024
904f504
Update pennylane/templates/state_preparations/basis.py
KetpuntoG Aug 26, 2024
6c0dc2a
Update pennylane/templates/state_preparations/basis.py
KetpuntoG Aug 26, 2024
9cefc08
Update test_basis_state_prep.py
KetpuntoG Sep 12, 2024
cc0863e
Merge branch 'master' into deprecate-BasisStatePreparation
KetpuntoG Sep 12, 2024
4ea5e33
[skip ci]
KetpuntoG Sep 12, 2024
5dbaae5
cleaning tests
KetpuntoG Sep 12, 2024
95e84a0
black
KetpuntoG Sep 12, 2024
bf04cfa
Update test_templates.py
KetpuntoG Sep 12, 2024
a3d4780
Merge branch 'master' into deprecate-BasisStatePreparation
KetpuntoG Sep 13, 2024
b094504
Update tests/templates/test_state_preparations/test_basis_state_prep.py
KetpuntoG Sep 13, 2024
5810c3b
Merge branch 'master' into deprecate-BasisStatePreparation
KetpuntoG Sep 13, 2024
7aa894e
Merge branch 'master' into deprecate-BasisStatePreparation
KetpuntoG Sep 17, 2024
a391deb
Merge branch 'master' into deprecate-BasisStatePreparation
KetpuntoG Sep 18, 2024
831b68a
Merge branch 'master' into deprecate-BasisStatePreparation
soranjh Sep 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ Pending deprecations
- Deprecated in v0.39
- Will be removed in v0.40

* The ``decomp_depth`` argument in ``qml.device`` is deprecated.

- Deprecated in v0.38
- Will be removed in v0.39

* The ``simplify`` argument in ``qml.Hamiltonian`` and ``qml.ops.LinearCombination`` is deprecated.
Instead, ``qml.simplify()`` can be called on the constructed operator.

- Deprecated in v0.37
- Will be removed in v0.39

* The :class:`~pennylane.BasisStatePreparation` template is deprecated.
Instead, use :class:`~pennylane.BasisState`.

* The ``QubitStateVector`` template is deprecated.
Instead, use ``StatePrep``.

Expand Down
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@

<h3>Deprecations 👋</h3>

* The `qml.BasisStatePreparation` template is deprecated.
Instead, use `qml.BasisState`.
[(#6021)](https://github.com/PennyLaneAI/pennylane/pull/6021)

* The `'ancilla'` argument for `qml.iterative_qpe` has been deprecated. Instead, use the `'aux_wire'` argument.
[(#6277)](https://github.com/PennyLaneAI/pennylane/pull/6277)

Expand Down
3 changes: 3 additions & 0 deletions pennylane/devices/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ def circuit():
[math.fidelity_statevector(circuit(), exp_state)], [1.0], atol=tol(dev.shots)
)

@pytest.mark.filterwarnings(
"ignore:BasisStatePreparation is deprecated:pennylane.PennyLaneDeprecationWarning"
)
def test_BasisStatePreparation(self, device, tol):
"""Test the BasisStatePreparation template."""
dev = device(4)
Expand Down
11 changes: 11 additions & 0 deletions pennylane/templates/state_preparations/basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
Contains the BasisStatePreparation template.
"""

import warnings

import numpy as np

import pennylane as qml
Expand All @@ -30,6 +32,8 @@ class BasisStatePreparation(Operation):
``basis_state`` influences the circuit architecture and is therefore incompatible with
gradient computations.

``BasisStatePreparation`` is deprecated and will be removed in version 0.40. Instead, please use ``BasisState``.

Args:
basis_state (array): Input array of shape ``(n,)``, where n is the number of wires
the state preparation acts on.
Expand Down Expand Up @@ -59,6 +63,13 @@ def circuit(basis_state):
ndim_params = (1,)

def __init__(self, basis_state, wires, id=None):

warnings.warn(
"BasisStatePreparation is deprecated and will be removed in version 0.40. "
"Instead, please use BasisState.",
qml.PennyLaneDeprecationWarning,
)

basis_state = qml.math.stack(basis_state)

# check if the `basis_state` param is batched
Expand Down
8 changes: 6 additions & 2 deletions tests/capture/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
jax = pytest.importorskip("jax")
jnp = jax.numpy

pytestmark = pytest.mark.jax

pytestmark = [
pytest.mark.jax,
pytest.mark.filterwarnings(
"ignore:BasisStatePreparation is deprecated:pennylane.PennyLaneDeprecationWarning"
),
]
original_op_bind_code = qml.operation.Operator._primitive_bind_call.__code__


Expand Down
10 changes: 10 additions & 0 deletions tests/templates/test_state_preparations/test_basis_state_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

import pennylane as qml

pytestmark = pytest.mark.filterwarnings(
"ignore:BasisStatePreparation is deprecated:pennylane.PennyLaneDeprecationWarning"
)


def test_standard_validity():
"""Check the operation using the assert_valid function."""
Expand All @@ -33,6 +37,12 @@ def test_standard_validity():
qml.ops.functions.assert_valid(op)


def test_BasisStatePreparation_is_deprecated():
"""Test that BasisStatePreparation is deprecated."""
with pytest.warns(qml.PennyLaneDeprecationWarning, match="BasisStatePreparation is deprecated"):
_ = qml.BasisStatePreparation([1, 0], wires=[0, 1])


class TestDecomposition:
"""Tests that the template defines the correct decomposition."""

Expand Down
4 changes: 2 additions & 2 deletions tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,15 +975,15 @@ def test_sampling_with_mcm(self, basis_state, mocker):
@qml.qnode(dev)
def cry_qnode(x):
"""QNode where we apply a controlled Y-rotation."""
qml.BasisStatePreparation(basis_state, wires=[0, 1])
qml.BasisState(basis_state, wires=[0, 1])
qml.CRY(x, wires=[0, 1])
return qml.sample(qml.PauliZ(1))

@qml.qnode(dev)
def conditional_ry_qnode(x):
"""QNode where the defer measurements transform is applied by
default under the hood."""
qml.BasisStatePreparation(basis_state, wires=[0, 1])
qml.BasisState(basis_state, wires=[0, 1])
m_0 = qml.measure(0)
qml.cond(m_0, qml.RY)(x, wires=1)
return qml.sample(qml.PauliZ(1))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_qnode_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,15 +890,15 @@ def test_sampling_with_mcm(self, basis_state, mocker):
@qml.qnode(dev)
def cry_qnode(x):
"""QNode where we apply a controlled Y-rotation."""
qml.BasisStatePreparation(basis_state, wires=[0, 1])
qml.BasisState(basis_state, wires=[0, 1])
qml.CRY(x, wires=[0, 1])
return qml.sample(qml.PauliZ(1))

@qml.qnode(dev)
def conditional_ry_qnode(x):
"""QNode where the defer measurements transform is applied by
default under the hood."""
qml.BasisStatePreparation(basis_state, wires=[0, 1])
qml.BasisState(basis_state, wires=[0, 1])
m_0 = qml.measure(0)
qml.cond(m_0, qml.RY)(x, wires=1)
return qml.sample(qml.PauliZ(1))
Expand Down
3 changes: 3 additions & 0 deletions tests/transforms/test_batch_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ def circuit2(data, weights):
assert np.allclose(res, indiv_res)


@pytest.mark.filterwarnings(
"ignore:BasisStatePreparation is deprecated:pennylane.PennyLaneDeprecationWarning"
)
def test_basis_state_preparation(mocker):
"""Test that batching works for BasisStatePreparation"""
dev = qml.device("default.qubit", wires=3)
Expand Down
3 changes: 3 additions & 0 deletions tests/transforms/test_batch_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def circuit2(data, weights):
assert np.allclose(res, indiv_res)


@pytest.mark.filterwarnings(
"ignore:BasisStatePreparation is deprecated:pennylane.PennyLaneDeprecationWarning"
)
def test_basis_state_preparation(mocker):
"""Test that batching works for BasisStatePreparation"""
dev = qml.device("default.qubit", wires=4)
Expand Down
3 changes: 3 additions & 0 deletions tests/transforms/test_defer_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,9 @@ def quantum_control_circuit(rads):
class TestTemplates:
"""Tests templates being conditioned on mid-circuit measurement outcomes."""

@pytest.mark.filterwarnings(
"ignore:BasisStatePreparation is deprecated:pennylane.PennyLaneDeprecationWarning"
)
def test_basis_state_prep(self):
"""Test the basis state prep template conditioned on mid-circuit
measurement outcomes."""
Expand Down
Loading