Skip to content

Commit

Permalink
Add support for GPU parallelization of diag function via PyTorch spar…
Browse files Browse the repository at this point in the history
…se library
  • Loading branch information
Alex McKeehan Boulton committed Aug 14, 2024
1 parent 4818dd2 commit f43ce4b
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions SQcircuit/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1917,16 +1917,37 @@ def _diag_np(
"""
hamil = self.hamiltonian()

# get the data out of qutip variable and use sparse SciPy
# eigensolver which is faster.
try:
efreqs, evecs = scipy.sparse.linalg.eigs(
hamil.data_as('csr_matrix'), k=n_eig, which='SR'
)
except ArpackNoConvergence:
efreqs, evecs = scipy.sparse.linalg.eigs(
hamil.data_as('csr_matrix'), k=n_eig, ncv=10*n_eig, which='SR'
)


import time
t0 = time.time()

if torch.cuda.is_available():
csr = hamil.data_as('csr_matrix')
coo = csr.tocoo()

indices = torch.tensor([coo.row, coo.col], dtype=torch.int64)
values = torch.tensor(coo.data, dtype=torch.float32)
sparse_tensor = torch.sparse_coo_tensor(indices, values, coo.shape).to('cuda:0')

efreqs, evecs = torch.lobpcg(sparse_tensor, k=n_eig)
efreqs = efreqs.to('cpu')
evecs = evecs.to('cpu')
else:
# get the data out of qutip variable and use sparse SciPy
# eigensolver which is faster.
try:
efreqs, evecs = scipy.sparse.linalg.eigs(
hamil.data_as('csr_matrix'), k=n_eig, which='SR'
)
except ArpackNoConvergence:
efreqs, evecs = scipy.sparse.linalg.eigs(
hamil.data_as('csr_matrix'), k=n_eig, ncv=10*n_eig, which='SR'
)

t1 = time.time()
print(f"total: {t1 - t0}")

# the output of eigen solver is not sorted
efreqs_sorted = np.sort(efreqs.real)

Expand Down

0 comments on commit f43ce4b

Please sign in to comment.