|
21 | 21 | from qiskit.quantum_info import SparsePauliOp |
22 | 22 | warnings.simplefilter('always', UserWarning) |
23 | 23 |
|
| 24 | +from qiskit._accelerate.sparse_pauli_op import ( |
| 25 | + ZXPaulis, |
| 26 | + to_matrix_sparse, |
| 27 | +) |
| 28 | + |
24 | 29 | from numba.core.errors import NumbaDeprecationWarning, NumbaPendingDeprecationWarning |
25 | 30 | warnings.simplefilter('ignore', category=NumbaDeprecationWarning) |
26 | 31 | warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning) |
@@ -1464,33 +1469,45 @@ def to_sparse_matrix(self) -> csr_matrix: |
1464 | 1469 | if self.n_qubits == 0: |
1465 | 1470 | return csr_matrix(self.coeff_vec) |
1466 | 1471 |
|
1467 | | - if self.n_qubits>15: |
1468 | | - from symmer.utils import get_sparse_matrix_large_pauliwordop |
1469 | | - sparse_matrix = get_sparse_matrix_large_pauliwordop(self) |
1470 | | - return sparse_matrix |
1471 | | - else: |
1472 | | - x_int = binary_array_to_int(self.X_block).reshape(-1, 1) |
1473 | | - z_int = binary_array_to_int(self.Z_block).reshape(-1, 1) |
| 1472 | + # if self.n_qubits>15: |
| 1473 | + # from symmer.utils import get_sparse_matrix_large_pauliwordop |
| 1474 | + # sparse_matrix = get_sparse_matrix_large_pauliwordop(self) |
| 1475 | + # return sparse_matrix |
| 1476 | + # else: |
| 1477 | + # x_int = binary_array_to_int(self.X_block).reshape(-1, 1) |
| 1478 | + # z_int = binary_array_to_int(self.Z_block).reshape(-1, 1) |
1474 | 1479 |
|
1475 | | - Y_number = np.sum(np.bitwise_and(self.X_block, self.Z_block), axis=1) |
1476 | | - global_phase = (-1j) ** Y_number |
| 1480 | + # Y_number = np.sum(np.bitwise_and(self.X_block, self.Z_block), axis=1) |
| 1481 | + # global_phase = (-1j) ** Y_number |
1477 | 1482 |
|
1478 | | - dimension = 2 ** self.n_qubits |
1479 | | - row_ind = np.repeat(np.arange(dimension).reshape(1, -1), self.X_block.shape[0], axis=0) |
1480 | | - col_ind = np.bitwise_xor(row_ind, x_int) |
| 1483 | + # dimension = 2 ** self.n_qubits |
| 1484 | + # row_ind = np.repeat(np.arange(dimension).reshape(1, -1), self.X_block.shape[0], axis=0) |
| 1485 | + # col_ind = np.bitwise_xor(row_ind, x_int) |
1481 | 1486 |
|
1482 | | - row_inds_and_Zint = np.bitwise_and(row_ind, z_int) |
1483 | | - vals = global_phase.reshape(-1, 1) * (-1) ** ( |
1484 | | - count1_in_int_bitstring(row_inds_and_Zint) % 2) # .astype(complex)) |
| 1487 | + # row_inds_and_Zint = np.bitwise_and(row_ind, z_int) |
| 1488 | + # vals = global_phase.reshape(-1, 1) * (-1) ** ( |
| 1489 | + # count1_in_int_bitstring(row_inds_and_Zint) % 2) # .astype(complex)) |
1485 | 1490 |
|
1486 | | - values_and_coeff = np.einsum('ij,i->ij', vals, self.coeff_vec) |
| 1491 | + # values_and_coeff = np.einsum('ij,i->ij', vals, self.coeff_vec) |
1487 | 1492 |
|
1488 | | - sparse_matrix = csr_matrix( |
1489 | | - (values_and_coeff.flatten(), (row_ind.flatten(), col_ind.flatten())), |
1490 | | - shape=(dimension, dimension), |
1491 | | - dtype=complex |
1492 | | - ) |
1493 | | - return sparse_matrix |
| 1493 | + # sparse_matrix = csr_matrix( |
| 1494 | + # (values_and_coeff.flatten(), (row_ind.flatten(), col_ind.flatten())), |
| 1495 | + # shape=(dimension, dimension), |
| 1496 | + # dtype=complex |
| 1497 | + # ) |
| 1498 | + # return sparse_matrix |
| 1499 | + |
| 1500 | + phase = np.zeros(self.n_terms, dtype=np.uint8) |
| 1501 | + zx = ZXPaulis( |
| 1502 | + self.X_block[:,::-1], |
| 1503 | + self.Z_block[:,::-1], |
| 1504 | + phase, |
| 1505 | + self.coeff_vec, |
| 1506 | + ) |
| 1507 | + |
| 1508 | + data, indices, indptr = to_matrix_sparse(zx, force_serial=False) |
| 1509 | + side = 1 << self.n_qubits |
| 1510 | + return csr_matrix((data, indices, indptr), shape=(side, side)) |
1494 | 1511 |
|
1495 | 1512 | def conjugate_op(self, R: 'PauliwordOp') -> 'PauliwordOp': |
1496 | 1513 | """ |
|
0 commit comments