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

Miscellaneous Tensor deprecation fixes #6365

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
`qml.ops.LinearCombination`; this behaviour is not deprecated. For more information, check out the
[updated operator troubleshooting page](https://docs.pennylane.ai/en/stable/news/new_opmath.html).
[(#6287)](https://github.com/PennyLaneAI/pennylane/pull/6287)
[(#6365)](https://github.com/PennyLaneAI/pennylane/pull/6365)

* `qml.pauli.PauliSentence.hamiltonian` and `qml.pauli.PauliWord.hamiltonian` are deprecated. Instead, please use
`qml.pauli.PauliSentence.operation` and `qml.pauli.PauliWord.operation` respectively.
Expand Down
3 changes: 2 additions & 1 deletion pennylane/data/attributes/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ def _hdf5_to_ops(self, bind: HDF5Group) -> list[Operator]:

op_cls = self._supported_ops_dict()[op_class_name]
if op_cls is Tensor:
ops.append(Tensor(*self._hdf5_to_ops(bind[op_key])))
prod_op = qml.ops.Prod if qml.operation.active_new_opmath() else Tensor
ops.append(prod_op(*self._hdf5_to_ops(bind[op_key])))
elif op_cls in (qml.ops.Hamiltonian, qml.ops.LinearCombination):
ops.append(
qml.Hamiltonian(
Expand Down
9 changes: 7 additions & 2 deletions pennylane/qaoa/mixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,14 @@ def bit_flip_mixer(graph: Union[nx.Graph, rx.PyGraph], b: int):
n_terms = [[qml.X(get_nvalue(i))]] + [
[qml.Identity(get_nvalue(n)), qml.Z(get_nvalue(n))] for n in neighbours
]
n_coeffs = [[1, sign] for n in neighbours]
n_coeffs = [[1, sign] for _ in neighbours]

final_terms = (
[qml.prod(*list(m)).simplify() for m in itertools.product(*n_terms)]
if qml.operation.active_new_opmath()
else [qml.operation.Tensor(*list(m)).prune() for m in itertools.product(*n_terms)]
)

final_terms = [qml.operation.Tensor(*list(m)).prune() for m in itertools.product(*n_terms)]
final_coeffs = [
(0.5**degree) * functools.reduce(lambda x, y: x * y, list(m), 1)
for m in itertools.product(*n_coeffs)
Expand Down
5 changes: 2 additions & 3 deletions pennylane/tape/tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ def _validate_computational_basis_sampling(tape):
with (
QueuingManager.stop_recording()
): # stop recording operations - the constructed operator is just aux
prod_op = qml.ops.Prod if qml.operation.active_new_opmath() else qml.operation.Tensor
pauliz_for_cb_obs = (
qml.Z(all_wires)
if len(all_wires) == 1
else qml.operation.Tensor(*[qml.Z(w) for w in all_wires])
qml.Z(all_wires) if len(all_wires) == 1 else prod_op(*[qml.Z(w) for w in all_wires])
)

for obs in non_comp_basis_sampling_obs:
Expand Down
39 changes: 32 additions & 7 deletions tests/data/attributes/operator/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@
tensors = [Tensor(qml.PauliX(1), qml.PauliY(2))]


@pytest.mark.usefixtures("use_legacy_and_new_opmath")
@pytest.mark.parametrize("attribute_cls", [DatasetOperator, DatasetPyTree])
@pytest.mark.parametrize("obs_in", [*hermitian_ops, *pauli_ops, *identity, *hamiltonians, *tensors])
class TestDatasetOperatorObservable:
"""Tests serializing Observable operators using the ``compare()`` method."""

def test_value_init(self, attribute_cls, obs_in):
def test_value_init(self, attribute_cls, obs_in, recwarn):
"""Test that a DatasetOperator can be value-initialized
from an observable, and that the deserialized operator
is equivalent."""
Expand All @@ -92,10 +93,22 @@ def test_value_init(self, attribute_cls, obs_in):
assert dset_op.info["py_type"] == get_type_str(type(obs_in))

obs_out = dset_op.get_value()
qml.assert_equal(obs_out, obs_in)
assert obs_in.compare(obs_out)

def test_bind_init(self, attribute_cls, obs_in):
if (
qml.operation.active_new_opmath()
and isinstance(obs_in, Tensor)
and attribute_cls is DatasetOperator
):
assert isinstance(obs_out, qml.ops.Prod)
for o1, o2 in zip(obs_in.obs, obs_out.operands):
qml.assert_equal(o1, o2)

# No Tensor deprecation warnings are raised
assert len(recwarn) == 0
else:
qml.assert_equal(obs_out, obs_in)
assert obs_in.compare(obs_out)

def test_bind_init(self, attribute_cls, obs_in, recwarn):
"""Test that DatasetOperator can be initialized from a HDF5 group
that contains a operator attribute."""
if not qml.operation.active_new_opmath() and isinstance(obs_in, qml.ops.LinearCombination):
Expand All @@ -109,8 +122,20 @@ def test_bind_init(self, attribute_cls, obs_in):
assert dset_op.info["py_type"] == get_type_str(type(obs_in))

obs_out = dset_op.get_value()
qml.assert_equal(obs_out, obs_in)
assert obs_in.compare(obs_out)
if (
qml.operation.active_new_opmath()
and isinstance(obs_in, Tensor)
and attribute_cls is DatasetOperator
):
assert isinstance(obs_out, qml.ops.Prod)
for o1, o2 in zip(obs_in.obs, obs_out.operands):
qml.assert_equal(o1, o2)

# No Tensor deprecation warnings are raised
assert len(recwarn) == 0
else:
qml.assert_equal(obs_out, obs_in)
assert obs_in.compare(obs_out)


@pytest.mark.parametrize("attribute_cls", [DatasetOperator, DatasetPyTree])
Expand Down
Loading