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

fix expand_matrix for qudit matrices #6398

Merged
merged 15 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@

<h3>Bug fixes 🐛</h3>

* Fixes the matrices for qutrit operators when expanded to more wires.
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
albi3ro marked this conversation as resolved.
Show resolved Hide resolved

* `MeasurementValue` now raises an error when it is used as a boolean.
[(#6386)](https://github.com/PennyLaneAI/pennylane/pull/6386)

Expand Down
20 changes: 13 additions & 7 deletions pennylane/math/matrix_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ def expand_matrix(mat, wires, wire_order=None, sparse_format="csr"):

"""
albi3ro marked this conversation as resolved.
Show resolved Hide resolved

qudit_dim = int(qml.math.shape(mat)[-1] ** 1 / (len(wires)))
isaacdevlugt marked this conversation as resolved.
Show resolved Hide resolved

if (wire_order is None) or (wire_order == wires):
return mat

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

Expand All @@ -118,8 +120,8 @@ def expand_matrix(mat, wires, wire_order=None, sparse_format="csr"):

def eye_interface(dim):
if interface == "scipy":
return eye(2**dim, format="coo")
return qml.math.cast_like(qml.math.eye(2**dim, like=interface), mat)
return eye(qudit_dim**dim, format="coo")
return qml.math.cast_like(qml.math.eye(qudit_dim**dim, like=interface), mat)

def kron_interface(mat1, mat2):
if interface == "scipy":
Expand Down Expand Up @@ -154,7 +156,9 @@ def kron_interface(mat1, mat2):
if interface == "scipy":
mat = _permute_sparse_matrix(mat, expanded_wires, subset_wire_order)
else:
mat = _permute_dense_matrix(mat, expanded_wires, subset_wire_order, batch_dim)
mat = _permute_dense_matrix(
mat, expanded_wires, subset_wire_order, batch_dim, qudit_dim=qudit_dim
)

# expand the matrix even further if needed
if len(expanded_wires) < len(wire_order):
Expand Down Expand Up @@ -201,7 +205,7 @@ def _permute_sparse_matrix(matrix, wires, wire_order):
return matrix


def _permute_dense_matrix(matrix, wires, wire_order, batch_dim):
def _permute_dense_matrix(matrix, wires, wire_order, batch_dim, qudit_dim: int = 2):
"""Permute the matrix to match the wires given in `wire_order`.

Args:
Expand All @@ -228,12 +232,14 @@ def _permute_dense_matrix(matrix, wires, wire_order, batch_dim):

# reshape matrix to match wire values e.g. mat[0, 0, 0, 0] = <00|mat|00>
# with this reshape we can easily swap wires
shape = [batch_dim] + [2] * (num_wires * 2) if batch_dim else [2] * (num_wires * 2)
shape = (
[batch_dim] + [qudit_dim] * (num_wires * 2) if batch_dim else [qudit_dim] * (num_wires * 2)
)
matrix = qml.math.reshape(matrix, shape)
# transpose matrix
matrix = qml.math.transpose(matrix, axes=perm)
# reshape back
shape = [batch_dim] + [2**num_wires] * 2 if batch_dim else [2**num_wires] * 2
shape = [batch_dim] + [qudit_dim * num_wires] * 2 if batch_dim else [qudit_dim**num_wires] * 2
return qml.math.reshape(matrix, shape)


Expand Down