99
1010
1111class IsingHamiltonianSimulator :
12- def __init__ (self , anneal_schedule = None , time_steps = 100 , dtype = np .float64 ):
12+ def __init__ (self , anneal_schedule = None , time_steps = 100 , sparse_format = "csr" , sparse_dtype = np .float64 ):
1313 self .anneal_schedule = self .default_anneal_schedule if anneal_schedule is None else anneal_schedule
1414 self .time_steps = time_steps
15- self .dtype = dtype
15+ self .sparse_format = sparse_format
16+ self .sparse_dtype = sparse_dtype
1617
1718 @staticmethod
1819 def default_anneal_schedule (s ):
@@ -22,18 +23,18 @@ def default_anneal_schedule(s):
2223
2324 def get_H_init (self , num_qubits ):
2425 size = 2 ** num_qubits
25- H_init = sparse .csr_array ((size , size ), dtype = self .dtype )
26- i_sparse = sparse .csc_array (identity , dtype = self .dtype )
27- x_sparse = sparse .csc_array (sigma_x , dtype = self .dtype )
26+ H_init = sparse .csr_array ((size , size ), dtype = self .sparse_dtype )
27+ i_sparse = sparse .csc_array (identity , dtype = self .sparse_dtype )
28+ x_sparse = sparse .csc_array (sigma_x , dtype = self .sparse_dtype )
2829 for i in range (num_qubits ):
2930 terms = [i_sparse ] * num_qubits
3031 terms [i ] = x_sparse
31- H_init -= functools .reduce (lambda A , B : sparse .kron (A , B , format = "csr" ), terms )
32+ H_init -= functools .reduce (lambda A , B : sparse .kron (A , B , format = self . sparse_format ), terms )
3233 return H_init
3334
3435 def get_H_final (self , num_qubits , h , J ):
3536 size = 2 ** num_qubits
36- diag = np .zeros (size , dtype = self .dtype )
37+ diag = np .zeros (size , dtype = self .sparse_dtype )
3738 i_diag = np .diag (identity )
3839 z_diag = np .diag (sigma_z )
3940 for i in range (num_qubits ):
@@ -46,7 +47,7 @@ def get_H_final(self, num_qubits, h, J):
4647 terms [i ] = z_diag
4748 terms [j ] = z_diag
4849 diag += J [i , j ] * functools .reduce (np .outer , terms ).ravel ()
49- H_final = sparse .dia_array (( diag , [ 0 ]), shape = ( size , size ) )
50+ H_final = sparse .diags ( diag , offsets = 0 , format = self . sparse_format )
5051 return H_final
5152
5253 def run (self , bqm ):
0 commit comments