Skip to content

Commit

Permalink
Update measurements.py (#6352)
Browse files Browse the repository at this point in the history
**Context:**
revert the abstract deco for StateMeasurement.process_density_matrix to
avoid unwanted conflicts for any downstreaming implementation.
**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related ShortCut Story:**
[sc-75238]
  • Loading branch information
JerryChen97 authored Oct 7, 2024
1 parent 5c26faf commit cc65532
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
6 changes: 0 additions & 6 deletions pennylane/devices/tests/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,9 +1757,6 @@ class MyMeasurement(StateMeasurement):
def process_state(self, state, wire_order):
return 1

def process_density_matrix(self, density_matrix, wire_order):
return 1

@qml.qnode(dev)
def circuit():
qml.X(0)
Expand All @@ -1781,9 +1778,6 @@ class MyMeasurement(StateMeasurement):
def process_state(self, state, wire_order):
return 1

def process_density_matrix(self, density_matrix, wire_order):
return 1

@qml.qnode(dev)
def circuit():
qml.X(0)
Expand Down
2 changes: 1 addition & 1 deletion pennylane/measurements/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,6 @@ def process_state(self, state: Sequence[complex], wire_order: Wires):
dimension :math:`2^n` acts on a subspace of :math:`n` wires
"""

@abstractmethod
def process_density_matrix(self, density_matrix: TensorLike, wire_order: Wires):
"""
Process the given density matrix.
Expand All @@ -670,6 +669,7 @@ def process_density_matrix(self, density_matrix: TensorLike, wire_order: Wires):
the mapping of matrix dimensions to physical qubits, allowing the function to correctly
trace out the subsystems not involved in the measurement or operation.
"""
raise NotImplementedError


class MeasurementTransform(MeasurementProcess):
Expand Down
19 changes: 13 additions & 6 deletions tests/measurements/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,6 @@ class MyMeasurement(StateMeasurement):
def process_state(self, state, wire_order):
return qml.math.sum(state)

def process_density_matrix(self, density_matrix, wire_order):
return 1

@property
def return_type(self):
return State
Expand All @@ -646,9 +643,6 @@ class MyMeasurement(StateMeasurement):
def process_state(self, state, wire_order):
return qml.math.sum(state)

def process_density_matrix(self, density_matrix, wire_order):
return 1

@property
def return_type(self):
return State
Expand All @@ -667,6 +661,19 @@ def circuit():
):
circuit()

def test_state_measurement_process_density_matrix_not_implemented(self):
"""Test that the process_density_matrix method of StateMeasurement raises
NotImplementedError."""

class MyMeasurement(StateMeasurement):
def process_state(self, state, wire_order):
return qml.math.sum(state)

with pytest.raises(NotImplementedError):
MyMeasurement().process_density_matrix(
density_matrix=qml.math.array([[1, 0], [0, 0]]), wire_order=Wires([0, 1])
)


class TestMeasurementTransform:
"""Tests for the MeasurementTransform class."""
Expand Down
6 changes: 0 additions & 6 deletions tests/measurements/test_measurements_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ class MyMeasurement(StateMeasurement):
def process_state(self, state, wire_order):
return qml.math.sum(state)

def process_density_matrix(self, density_matrix, wire_order):
return qml.math.sum(density_matrix)

dev = qml.device("default.mixed", wires=2)

@qml.qnode(dev)
Expand All @@ -125,9 +122,6 @@ class MyMeasurement(StateMeasurement):
def process_state(self, state, wire_order):
return qml.math.sum(state)

def process_density_matrix(self, density_matrix, wire_order):
return qml.math.sum(density_matrix)

dev = qml.device("default.mixed", wires=2, shots=1000)

@qml.qnode(dev)
Expand Down

0 comments on commit cc65532

Please sign in to comment.