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 19 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
6 changes: 6 additions & 0 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ Pending deprecations
- Deprecated in v0.37
- Will be removed in v0.39

* The ``BasisStatePreparation`` template is deprecated.
Instead, use ``BasisState``.
KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved

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

New operator arithmetic deprecations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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 @@ -10,6 +10,10 @@

<h3>Deprecations 👋</h3>

* The ``BasisStatePreparation`` template is deprecated.
Instead, use ``BasisState``.
KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
[(#6021)](https://github.com/PennyLaneAI/pennylane/pull/6021)

<h3>Documentation 📝</h3>

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

@pytest.fixture(scope="function", autouse=False)
soranjh marked this conversation as resolved.
Show resolved Hide resolved
def capture_warnings(self, recwarn):
"""Capture warnings."""
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
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.39. Instead, please use ``BasisState``.
KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved

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.39. "
KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
"Instead, please use BasisState.",
qml.PennyLaneDeprecationWarning,
)

basis_state = qml.math.stack(basis_state)

# check if the `basis_state` param is batched
Expand Down
9 changes: 9 additions & 0 deletions tests/capture/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ def enable_disable_plxpr():
]


@pytest.fixture(scope="function", autouse=True)
def capture_warnings(recwarn):
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)


KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize("template, args, kwargs", unmodified_templates_cases)
def test_unmodified_templates(template, args, kwargs):
"""Test that templates with unmodified primitive binds are captured as expected."""
Expand Down
6 changes: 6 additions & 0 deletions tests/ops/qubit/test_state_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def test_adjoint_error_exception(op):
op.adjoint()


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


KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize(
"op, mat, base",
[
Expand Down
89 changes: 89 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 @@ -22,6 +22,15 @@
import pennylane as qml


@pytest.fixture(scope="function", autouse=True)
def capture_warnings(recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)


def test_standard_validity():
"""Check the operation using the assert_valid function."""

Expand All @@ -36,6 +45,14 @@ def test_standard_validity():
class TestDecomposition:
"""Tests that the template defines the correct decomposition."""

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

# fmt: off
@pytest.mark.parametrize("basis_state,wires,target_wires", [
([0], [0], []),
Expand All @@ -61,6 +78,14 @@ def test_correct_pl_gates(self, basis_state, wires, target_wires):
assert gate.name == "PauliX"
assert gate.wires.tolist() == [target_wires[id]]

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

# fmt: off
@pytest.mark.parametrize("basis_state,wires,target_state", [
([0], [0], [0, 0, 0]),
Expand Down Expand Up @@ -90,6 +115,14 @@ def circuit():

assert np.allclose(output_state, target_state, atol=tol, rtol=0)

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

@pytest.mark.jax
@pytest.mark.parametrize(
"basis_state,wires,target_state",
Expand Down Expand Up @@ -117,6 +150,14 @@ def circuit(state):

assert np.allclose(output_state, target_state, atol=tol, rtol=0)

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

@pytest.mark.tf
@pytest.mark.parametrize(
"basis_state,wires,target_state",
Expand All @@ -143,6 +184,14 @@ def circuit(state):

assert np.allclose(output_state, target_state, atol=tol, rtol=0)

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

def test_custom_wire_labels(self, tol):
"""Test that template can deal with non-numeric, nonconsecutive wire labels."""
basis_state = [0, 1, 0]
Expand All @@ -166,6 +215,14 @@ def circuit2():
assert np.allclose(res1, res2, atol=tol, rtol=0)
assert np.allclose(state1, state2, atol=tol, rtol=0)

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

def test_batched_decomposition_fails(self):
"""Test that attempting to decompose a BasisStatePreparation operation with
broadcasting raises an error."""
Expand All @@ -182,6 +239,14 @@ def test_batched_decomposition_fails(self):
class TestInputs:
"""Test inputs and pre-processing."""

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

# fmt: off
@pytest.mark.parametrize("basis_state,wires", [
([0], [0, 1]),
Expand All @@ -195,6 +260,14 @@ def test_error_num_qubits(self, basis_state, wires):
with pytest.raises(ValueError, match="Basis states must be of (shape|length)"):
qml.BasisStatePreparation(basis_state, wires)

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

# fmt: off
@pytest.mark.parametrize("basis_state,wires", [
([3], [0]),
Expand All @@ -208,6 +281,14 @@ def test_error_basis_state_format(self, basis_state, wires):
with pytest.raises(ValueError, match="Basis states must only (contain|consist)"):
qml.BasisStatePreparation(basis_state, wires)

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

def test_exception_wrong_dim(self):
"""Verifies that exception is raised if the
number of dimensions of features is incorrect."""
Expand All @@ -230,6 +311,14 @@ def circuit(basis_state):
basis_state = np.array([0, 2])
circuit(basis_state)

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn): # pylint: disable=function-redefined
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

def test_id(self):
"""Tests that the id attribute can be set."""
template = qml.BasisStatePreparation(np.array([0, 1]), wires=[0, 1], id="a")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,15 +901,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 @@ -1016,15 +1016,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
9 changes: 9 additions & 0 deletions tests/transforms/test_batch_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ def circuit2(data, weights):
assert np.allclose(res, indiv_res)


@pytest.fixture(scope="function", autouse=True)
def capture_warnings(recwarn):
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)


KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
def test_basis_state_preparation(mocker):
"""Test that batching works for BasisStatePreparation"""
dev = qml.device("default.qubit", wires=3)
Expand Down
9 changes: 9 additions & 0 deletions tests/transforms/test_batch_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def circuit2(data, weights):
assert np.allclose(res, indiv_res)


@pytest.fixture(scope="function", autouse=True)
def capture_warnings(recwarn):
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)


KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
def test_basis_state_preparation(mocker):
"""Test that batching works for BasisStatePreparation"""
dev = qml.device("default.qubit", wires=4)
Expand Down
8 changes: 8 additions & 0 deletions tests/transforms/test_defer_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,14 @@ def quantum_control_circuit(rads):
class TestTemplates:
"""Tests templates being conditioned on mid-circuit measurement outcomes."""

@pytest.fixture(scope="function", autouse=True)
def capture_warnings(self, recwarn):
yield
if len(recwarn) > 0:
for w in recwarn:
assert isinstance(w.message, qml.PennyLaneDeprecationWarning)
assert "BasisStatePreparation is deprecated" in str(w.message)

KetpuntoG marked this conversation as resolved.
Show resolved Hide resolved
def test_basis_state_prep(self):
"""Test the basis state prep template conditioned on mid-circuit
measurement outcomes."""
Expand Down
Loading