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

Remove remaining references to legacy operator arithmetic #997

Merged
merged 9 commits into from
Nov 18, 2024
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

* Handling for the legacy operator arithmetic (the `Hamiltonian` and `Tensor` classes in PennyLane) is removed.
[(#994)](https://github.com/PennyLaneAI/pennylane-lightning/pull/994)
[(#997)](https://github.com/PennyLaneAI/pennylane-lightning/pull/997)

### Improvements

Expand Down
3 changes: 1 addition & 2 deletions pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
Version number (major.minor.patch[-label])
"""


__version__ = "0.40.0-dev10"
__version__ = "0.40.0-dev11"
6 changes: 1 addition & 5 deletions pennylane_lightning/lightning_gpu/_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@
matrix = measurementprocess.obs.matrix()
return self._measurement_lightning.expval(matrix, observable_wires)

if (
isinstance(measurementprocess.obs, qml.ops.Hamiltonian)
or (measurementprocess.obs.arithmetic_depth > 0)
or isinstance(measurementprocess.obs.name, List)
):
if measurementprocess.obs.arithmetic_depth:

Check warning on line 189 in pennylane_lightning/lightning_gpu/_measurements.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_gpu/_measurements.py#L189

Added line #L189 was not covered by tests
# pylint: disable=protected-access
ob_serialized = QuantumScriptSerializer(
self._qubit_state.device_name, self.dtype == np.complex64, self._use_mpi
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_gpu/lightning_gpu.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ PauliY = { properties = [ "differentiable" ] }
PauliZ = { properties = [ "differentiable" ] }
Hadamard = { properties = [ "differentiable" ] }
Hermitian = { properties = [ "differentiable" ] }
Hamiltonian = { properties = [ "differentiable" ] }
SparseHamiltonian = { properties = [ "differentiable" ] }
Sum = { properties = [ "differentiable" ] }
SProd = { properties = [ "differentiable" ] }
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_kokkos/lightning_kokkos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ PauliY = { properties = [ "differentiable" ] }
PauliZ = { properties = [ "differentiable" ] }
Hadamard = { properties = [ "differentiable" ] }
Hermitian = { properties = [ "differentiable" ] }
Hamiltonian = { properties = [ "differentiable" ] }
SparseHamiltonian = { properties = [ "differentiable" ] }
Sum = { properties = [ "differentiable" ] }
SProd = { properties = [ "differentiable" ] }
Expand Down
6 changes: 3 additions & 3 deletions pennylane_lightning/lightning_tensor/_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
StateMeasurement,
VarianceMP,
)
from pennylane.ops import Hamiltonian, SparseHamiltonian, Sum
from pennylane.ops import SparseHamiltonian, Sum
from pennylane.tape import QuantumScript
from pennylane.typing import Result, TensorLike
from pennylane.wires import Wires
Expand Down Expand Up @@ -268,8 +268,8 @@
raise TypeError(
"ExpectationMP/VarianceMP(SparseHamiltonian) cannot be computed with samples."
)
if isinstance(group[0], VarianceMP) and isinstance(group[0].obs, (Hamiltonian, Sum)):
raise TypeError("VarianceMP(Hamiltonian/Sum) cannot be computed with samples.")
if isinstance(group[0], VarianceMP) and isinstance(group[0].obs, Sum):
raise TypeError("VarianceMP(Sum) cannot be computed with samples.")

Check warning on line 272 in pennylane_lightning/lightning_tensor/_measurements.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_measurements.py#L271-L272

Added lines #L271 - L272 were not covered by tests
if isinstance(group[0], (ClassicalShadowMP, ShadowExpvalMP)):
raise TypeError(
"ExpectationMP(ClassicalShadowMP, ShadowExpvalMP) cannot be computed with samples."
Expand Down
4 changes: 0 additions & 4 deletions tests/lightning_qubit/test_jacobian_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ def test_derivatives_single_expval(
self, theta, phi, obs, execute_and_derivatives, lightning_sv
):
"""Test that the jacobian is correct when a tape has a single expectation value"""
if isinstance(obs, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs = qml.operation.convert_to_legacy_H(obs)

qs = QuantumScript(
[qml.RX(theta, 0), qml.CNOT([0, 1]), qml.RY(phi, 1)],
Expand Down Expand Up @@ -155,8 +153,6 @@ def process_and_execute(statevector, tape, dy, execute_and_derivatives=False):
@pytest.mark.parametrize("execute_and_derivatives", [True, False])
def test_vjp_single_expval(self, theta, phi, obs, execute_and_derivatives, lightning_sv):
"""Test that the VJP is correct when a tape has a single expectation value"""
if isinstance(obs, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs = qml.operation.convert_to_legacy_H(obs)

qs = QuantumScript(
[qml.RX(theta, 0), qml.CNOT([0, 1]), qml.RY(phi, 1)],
Expand Down
20 changes: 0 additions & 20 deletions tests/new_api/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,6 @@ def test_execute_single_measurement(self, theta, phi, mp, dev):
if isinstance(mp.obs, qml.SparseHamiltonian) or isinstance(mp.obs, qml.Projector):
pytest.skip("SparseHamiltonian/Projector obs not supported in lightning.tensor")

if isinstance(mp.obs, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
mp.obs = qml.operation.convert_to_legacy_H(mp.obs)

if isinstance(mp.obs, qml.SparseHamiltonian) and dev.dtype == np.complex64:
pytest.skip(
reason="The conversion from qml.Hamiltonian to SparseHamiltonian is only possible with np.complex128"
Expand Down Expand Up @@ -507,8 +504,6 @@ def test_execute_single_measurement(self, theta, phi, mp, dev):
)
def test_execute_multi_measurement(self, theta, phi, dev, mp1, mp2):
"""Test that execute returns the correct results with multiple measurements."""
if isinstance(mp2.obs, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
mp2.obs = qml.operation.convert_to_legacy_H(mp2.obs)

qs = QuantumScript(
[
Expand Down Expand Up @@ -679,9 +674,6 @@ def test_derivatives_single_expval(
reason="The conversion from qml.Hamiltonian to SparseHamiltonian is only possible with np.complex128"
)

if isinstance(obs, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs = qml.operation.convert_to_legacy_H(obs)

qs = QuantumScript(
[qml.RX(theta, 0), qml.CNOT([0, 1]), qml.RY(phi, 1)],
[qml.expval(obs)],
Expand Down Expand Up @@ -740,11 +732,6 @@ def test_derivatives_multi_expval(
reason="The conversion from qml.Hamiltonian to SparseHamiltonian is only possible with np.complex128"
)

if isinstance(obs1, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs1 = qml.operation.convert_to_legacy_H(obs1)
if isinstance(obs2, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs2 = qml.operation.convert_to_legacy_H(obs2)

qs = QuantumScript(
[
qml.RX(theta, 0),
Expand Down Expand Up @@ -1051,8 +1038,6 @@ def test_supports_vjp(self, dev, config, tape, expected, batch_obs):
@pytest.mark.parametrize("execute_and_derivatives", [True, False])
def test_vjp_single_expval(self, theta, phi, dev, obs, execute_and_derivatives, batch_obs):
"""Test that the VJP is correct when a tape has a single expectation value"""
if isinstance(obs, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs = qml.operation.convert_to_legacy_H(obs)

qs = QuantumScript(
[qml.RX(theta, 0), qml.CNOT([0, 1]), qml.RY(phi, 1)],
Expand Down Expand Up @@ -1113,11 +1098,6 @@ def test_vjp_multi_expval(
reason="The conversion from qml.Hamiltonian to SparseHamiltonian is only possible with np.complex128"
)

if isinstance(obs1, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs1 = qml.operation.convert_to_legacy_H(obs1)
if isinstance(obs2, qml.ops.LinearCombination) and not qml.operation.active_new_opmath():
obs2 = qml.operation.convert_to_legacy_H(obs2)

qs = QuantumScript(
[
qml.RX(theta, 0),
Expand Down
5 changes: 0 additions & 5 deletions tests/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,6 @@ def circuit():
)
def test_expval_hamiltonian(self, obs, coeffs, res, tol, dev):
"""Test expval with Hamiltonian"""
if not qml.operation.active_new_opmath():
obs = [
qml.operation.convert_to_legacy_H(o).ops[0] if isinstance(o, qml.ops.Prod) else o
for o in obs
]
ham = qml.Hamiltonian(coeffs, obs)

@qml.qnode(dev)
Expand Down
Loading