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

Fixing dismatch between dense matrix() and sparse repr sparse_matrix() of GlobalPhase #6940

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions pennylane/math/matrix_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@
if (wire_order is None) or (wire_order == wires):
return mat

if not wires and qml.math.shape(mat) == (qudit_dim, qudit_dim):
if not wires:
# global phase
wires = wire_order[0:1]
if qml.math.shape(mat) == (qudit_dim, qudit_dim):
wires = wire_order[0:1]

Check warning on line 119 in pennylane/math/matrix_manipulation.py

View check run for this annotation

Codecov / codecov/patch

pennylane/math/matrix_manipulation.py#L119

Added line #L119 was not covered by tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the line119 has never been visited by the whole codebase. This means it actually only serves for the previous wrong impelmentation of GlobalPhase(.., wires=None). So we will just delete this

elif qml.math.shape(mat) == (1, 1):
return complex(mat[0, 0])

wires = list(wires)
wire_order = list(wire_order)
Expand Down
4 changes: 4 additions & 0 deletions pennylane/ops/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ def matrix(self, wire_order=None):
n_wires = len(wire_order) if wire_order else len(self.wires)
return self.compute_matrix(self.data[0], n_wires=n_wires)

def sparse_matrix(self, wire_order=None):
n_wires = len(wire_order) if wire_order else len(self.wires)
return self.compute_sparse_matrix(self.data[0], n_wires=n_wires)

def adjoint(self):
return GlobalPhase(-1 * self.data[0], self.wires)

Expand Down
10 changes: 10 additions & 0 deletions tests/ops/qubit/test_parametric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
]

ALL_OPERATIONS = NON_PARAMETRIZED_OPERATIONS + PARAMETRIZED_OPERATIONS
SPARSE_OPERATIONS = [op for op in ALL_OPERATIONS if op.has_sparse_matrix]


def dot_broadcasted(a, b):
Expand All @@ -139,6 +140,15 @@ def multi_dot_broadcasted(matrices):
return reduce(dot_broadcasted, matrices)


class TestSparseOperators:
@pytest.mark.parametrize("op", SPARSE_OPERATIONS)
def test_validity(self, op):
"""Test that the operations are valid."""
assert np.allclose(
op.sparse_matrix().toarray(), qml.math.asarray(op.matrix(), like="numpy")
)


class TestOperations:
@pytest.mark.parametrize("op", ALL_OPERATIONS + BROADCASTED_OPERATIONS)
def test_parametrized_op_copy(self, op, tol):
Expand Down