Skip to content

Commit

Permalink
feat: Speedup unitary by using sparse matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-pasquale committed Jun 4, 2024
1 parent fc3a043 commit cb87952
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import math

import numpy as np
from scipy import sparse

from qibo import __version__
from qibo.backends import einsum_utils
Expand Down Expand Up @@ -119,7 +120,8 @@ def matrix_parametrized(self, gate):

def matrix_fused(self, fgate):
rank = len(fgate.target_qubits)
matrix = np.eye(2**rank)
matrix = sparse.eye(2**rank)

for gate in fgate.gates:
# transfer gate matrix to numpy as it is more efficient for
# small tensor calculations
Expand All @@ -141,8 +143,10 @@ def matrix_fused(self, fgate):
gmatrix = np.transpose(gmatrix, transpose_indices)
gmatrix = np.reshape(gmatrix, original_shape)
# fuse the individual gate matrix to the total ``FusedGate`` matrix
matrix = gmatrix @ matrix
return self.cast(matrix)
# we are using sparse matrices to improve perfomances
matrix = sparse.csr_matrix(gmatrix).dot(matrix)

return self.cast(matrix.toarray())

def control_matrix(self, gate):
if len(gate.control_qubits) > 1:
Expand Down

0 comments on commit cb87952

Please sign in to comment.