From dc8cf6f0a51b7745406bc61d782f1acde2042d87 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Tue, 8 Oct 2024 12:35:23 -0400 Subject: [PATCH] updated to_sparse_matrix --- .DS_Store | Bin 14340 -> 14340 bytes symmer/.DS_Store | Bin 6148 -> 6148 bytes symmer/operators/base.py | 61 +++++++++++++++++++++++++-------------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/.DS_Store b/.DS_Store index 2ba982178801afe55f025ce7b77834d8f2022dc3..97f546b3c3d9dface9fd06f3f58832c7df4371ec 100644 GIT binary patch delta 38 ucmZoEXerpBE--nGaw&(gp{0(3p@rq-{fg3)k145bej=d8znNR%r#JuwZ4EO3 delta 33 pcmZoEXerpBE-=|yv6S7=*g!|Y#A5P(Mf=T<1vL0Kb1D212LQew3k(1N diff --git a/symmer/.DS_Store b/symmer/.DS_Store index 1b6e71e6473705b7a7deb30bdce84db2f10761cb..810b0bd9639ef9641f99cfae86e65e0b5c5f619d 100644 GIT binary patch delta 47 zcmZoMXffE}%gVT8av)0~w?uWdv7x1of}w@w@aAos(a&$TN0rW@BSy+RVoBj~@U` CZVjOT diff --git a/symmer/operators/base.py b/symmer/operators/base.py index 5eaaced..c1fb59e 100644 --- a/symmer/operators/base.py +++ b/symmer/operators/base.py @@ -21,6 +21,11 @@ from qiskit.quantum_info import SparsePauliOp warnings.simplefilter('always', UserWarning) +from qiskit._accelerate.sparse_pauli_op import ( + ZXPaulis, + to_matrix_sparse, +) + from numba.core.errors import NumbaDeprecationWarning, NumbaPendingDeprecationWarning warnings.simplefilter('ignore', category=NumbaDeprecationWarning) warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning) @@ -1464,33 +1469,45 @@ def to_sparse_matrix(self) -> csr_matrix: if self.n_qubits == 0: return csr_matrix(self.coeff_vec) - if self.n_qubits>15: - from symmer.utils import get_sparse_matrix_large_pauliwordop - sparse_matrix = get_sparse_matrix_large_pauliwordop(self) - return sparse_matrix - else: - x_int = binary_array_to_int(self.X_block).reshape(-1, 1) - z_int = binary_array_to_int(self.Z_block).reshape(-1, 1) + # if self.n_qubits>15: + # from symmer.utils import get_sparse_matrix_large_pauliwordop + # sparse_matrix = get_sparse_matrix_large_pauliwordop(self) + # return sparse_matrix + # else: + # x_int = binary_array_to_int(self.X_block).reshape(-1, 1) + # z_int = binary_array_to_int(self.Z_block).reshape(-1, 1) - Y_number = np.sum(np.bitwise_and(self.X_block, self.Z_block), axis=1) - global_phase = (-1j) ** Y_number + # Y_number = np.sum(np.bitwise_and(self.X_block, self.Z_block), axis=1) + # global_phase = (-1j) ** Y_number - dimension = 2 ** self.n_qubits - row_ind = np.repeat(np.arange(dimension).reshape(1, -1), self.X_block.shape[0], axis=0) - col_ind = np.bitwise_xor(row_ind, x_int) + # dimension = 2 ** self.n_qubits + # row_ind = np.repeat(np.arange(dimension).reshape(1, -1), self.X_block.shape[0], axis=0) + # col_ind = np.bitwise_xor(row_ind, x_int) - row_inds_and_Zint = np.bitwise_and(row_ind, z_int) - vals = global_phase.reshape(-1, 1) * (-1) ** ( - count1_in_int_bitstring(row_inds_and_Zint) % 2) # .astype(complex)) + # row_inds_and_Zint = np.bitwise_and(row_ind, z_int) + # vals = global_phase.reshape(-1, 1) * (-1) ** ( + # count1_in_int_bitstring(row_inds_and_Zint) % 2) # .astype(complex)) - values_and_coeff = np.einsum('ij,i->ij', vals, self.coeff_vec) + # values_and_coeff = np.einsum('ij,i->ij', vals, self.coeff_vec) - sparse_matrix = csr_matrix( - (values_and_coeff.flatten(), (row_ind.flatten(), col_ind.flatten())), - shape=(dimension, dimension), - dtype=complex - ) - return sparse_matrix + # sparse_matrix = csr_matrix( + # (values_and_coeff.flatten(), (row_ind.flatten(), col_ind.flatten())), + # shape=(dimension, dimension), + # dtype=complex + # ) + # return sparse_matrix + + phase = np.zeros(self.n_terms, dtype=np.uint8) + zx = ZXPaulis( + self.X_block[:,::-1], + self.Z_block[:,::-1], + phase, + self.coeff_vec, + ) + + data, indices, indptr = to_matrix_sparse(zx, force_serial=False) + side = 1 << self.n_qubits + return csr_matrix((data, indices, indptr), shape=(side, side)) def conjugate_op(self, R: 'PauliwordOp') -> 'PauliwordOp': """