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

Add a helper in math/quantum to calculate the matrix square root of sparse, hermitian matrices #6976

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from all 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
39 changes: 39 additions & 0 deletions pennylane/math/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import itertools
from string import ascii_letters as ABC

import scipy as sp
import scipy.sparse.linalg as spla
from autoray import numpy as np
from numpy import float64 # pylint:disable=wrong-import-order
from scipy.sparse import csc_matrix, issparse

import pennylane as qml

Expand Down Expand Up @@ -972,6 +975,42 @@
return vecs @ qml.math.diag(qml.math.sqrt(evs)) @ qml.math.conj(qml.math.transpose(vecs))


def sqrt_matrix_sparse(density_matrix):
r"""Compute the square root matrix of a sparse density matrix where :math:`\rho = \sqrt{\rho} \times \sqrt{\rho}`

Args:
density_matrix (sparse): 2D sparse density matrix of the quantum system.

Returns:
(sparse): Square root of the density matrix. Even thought type as `csr_matrix` or `csc_matrix`, the output is not guaranteed to be sparse as well.
"""
if not issparse(density_matrix):
raise TypeError(
"Only use this method for sparse matrices, or there will be an inevitable performance hit and divergence risk."
)

Check notice on line 990 in pennylane/math/quantum.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/math/quantum.py#L990

Unused argument 'mat' (unused-argument)
return _denman_beavers_iterations(density_matrix, max_iter=100, tol=1e-10)


def _denman_beavers_iterations(mat, max_iter=100, tol=1e-10):
mat = csc_matrix(mat)
Ynew = mat

Znew = sp.sparse.eye(mat.shape[0]).tocsc()
for _ in range(max_iter):
Y = Ynew
Z = Znew
# TODO: implement the tol control logic
# basic idea: check norm_diff every 10 iter. If norm_diff < tol, break
Ynew = 0.5 * (Y + spla.inv(Z))
Znew = 0.5 * (Z + spla.inv(Y))
# TODO: common error catch to be here -- failure of spla.inv


X = spla.inv(Z)
B = Znew
return 2 * X - X @ B @ X


def _compute_relative_entropy(rho, sigma, base=None):
r"""
Compute the quantum relative entropy of density matrix rho with respect to sigma.
Expand Down