Skip to content

Commit

Permalink
Making default.tensor compatible with new opmath disabled (#5751)
Browse files Browse the repository at this point in the history
**Context:** This PR makes the new `default.tensor` device compatible
with 'new_opmath' disabled.

**Description of the Change:** Simpy replacing the `matrix()` method
with the more general function `qml.matrix`, since `matrix()` does not
seem to work with `qml.ops.Hamiltonian`.

**Benefits:** Now, the user does not encounter an error with this device
if he/she explicitly disables the new opmath.

**Possible Drawbacks:** None that I can think of.

**Related GitHub Issues:** None

**Related Shortcut Stories.** [sc-64197]
  • Loading branch information
PietropaoloFrisoni authored May 29, 2024
1 parent 5b192e6 commit e1a1ae9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pennylane/devices/default_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ def _apply_operation(self, op: qml.operation.Operator) -> None:
op (Operator): The operation to apply.
"""

self._circuitMPS.apply_gate(op.matrix().astype(self._dtype), *op.wires, **self._gate_opts)
self._circuitMPS.apply_gate(
qml.matrix(op).astype(self._dtype), *op.wires, **self._gate_opts
)

def measurement(self, measurementprocess: MeasurementProcess) -> TensorLike:
"""Measure the measurement required by the circuit over the MPS.
Expand Down Expand Up @@ -434,7 +436,7 @@ def expval(self, measurementprocess: MeasurementProcess) -> float:

obs = measurementprocess.obs

result = self._local_expectation(obs.matrix(), tuple(obs.wires))
result = self._local_expectation(qml.matrix(obs), tuple(obs.wires))

return result

Expand All @@ -450,7 +452,7 @@ def var(self, measurementprocess: MeasurementProcess) -> float:

obs = measurementprocess.obs

obs_mat = obs.matrix()
obs_mat = qml.matrix(obs)
expect_op = self.expval(measurementprocess)
expect_squar_op = self._local_expectation(obs_mat @ obs_mat.conj().T, tuple(obs.wires))

Expand Down
6 changes: 3 additions & 3 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3017,7 +3017,7 @@ def enable_new_opmath(warn=True):
"""
if warn:
warnings.warn(
"Re-enabling the new Operator arithmetic system after disabling it is not advised."
"Re-enabling the new Operator arithmetic system after disabling it is not advised. "
"Please visit https://docs.pennylane.ai/en/stable/news/new_opmath.html for help troubleshooting.",
UserWarning,
)
Expand All @@ -3044,8 +3044,8 @@ def disable_new_opmath(warn=True):
"""
if warn:
warnings.warn(
"Disabling the new Operator arithmetic system for legacy support."
"If you need help troubleshooting your code, please visit"
"Disabling the new Operator arithmetic system for legacy support. "
"If you need help troubleshooting your code, please visit "
"https://docs.pennylane.ai/en/stable/news/new_opmath.html",
UserWarning,
)
Expand Down
1 change: 0 additions & 1 deletion tests/devices/default_tensor/test_default_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ def circuit(x):
return qml.expval(qml.Z(0))

weights = jax.numpy.array([0.2, 0.5, 0.1])
print(isinstance(dev, qml.Device))
qnode = qml.QNode(circuit, dev, interface="jax")
ref_qnode = qml.QNode(circuit, ref_dev, interface="jax")

Expand Down
2 changes: 0 additions & 2 deletions tests/devices/default_tensor/test_tensor_expval.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ def test_hermitian_expectation(self, theta, phi, tol, dev):

assert np.allclose(calculated_val, reference_val, tol)

@pytest.mark.usefixtures("new_opmath_only")
def test_hamiltonian_expectation(self, theta, phi, tol, dev):
"""Tests a Hamiltonian."""

Expand Down Expand Up @@ -355,7 +354,6 @@ def test_PauliZ_hadamard_PauliY(self, theta, phi, varphi, dev, tol):
assert np.allclose(calculated_val, reference_val, tol)


@pytest.mark.usefixtures("new_opmath_only")
@pytest.mark.parametrize("theta, phi", list(zip(THETA, PHI)))
def test_multi_qubit_gates(theta, phi, dev):
"""Tests a simple circuit with multi-qubit gates."""
Expand Down
3 changes: 2 additions & 1 deletion tests/devices/default_tensor/test_tensor_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ def test_hermitian_variance(self, theta, phi, dev):
tol = 1e-5 if dev.dtype == np.complex64 else 1e-7
assert np.allclose(calculated_val, reference_val, atol=tol, rtol=0)

@pytest.mark.usefixtures("new_opmath_only")
def test_hamiltonian_variance(self, theta, phi, dev):
"""Tests a Hamiltonian."""

Expand Down Expand Up @@ -362,6 +361,8 @@ def test_PauliZ_hadamard_PauliY(self, theta, phi, varphi, dev, tol):
assert np.allclose(calculated_val, reference_val, tol)


# This test is only for the new opmath since there is an error
# in the tape computation with `default.qubit`, that we use as reference.
@pytest.mark.usefixtures("new_opmath_only")
@pytest.mark.parametrize("theta, phi", list(zip(THETA, PHI)))
def test_multi_qubit_gates(theta, phi, dev):
Expand Down

0 comments on commit e1a1ae9

Please sign in to comment.