Skip to content

Commit 7684d90

Browse files
authored
Merge pull request #189 from UCL-CCS/updated_to_sparse
Updated to sparse
2 parents d33d50e + 9e78d57 commit 7684d90

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

.DS_Store

0 Bytes
Binary file not shown.

symmer/.DS_Store

0 Bytes
Binary file not shown.

symmer/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Main init for package."""
2+
import warnings
3+
warnings.filterwarnings('ignore', module='cotengra')
24
from symmer.process_handler import process
35
from symmer.operators import PauliwordOp, QuantumState
46
from symmer.projection import QubitTapering, ContextualSubspace, QubitSubspaceManager

symmer/operators/base.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
from qiskit.quantum_info import SparsePauliOp
2222
warnings.simplefilter('always', UserWarning)
2323

24+
from qiskit._accelerate.sparse_pauli_op import (
25+
ZXPaulis,
26+
to_matrix_sparse,
27+
)
28+
2429
from numba.core.errors import NumbaDeprecationWarning, NumbaPendingDeprecationWarning
2530
warnings.simplefilter('ignore', category=NumbaDeprecationWarning)
2631
warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning)
@@ -1464,33 +1469,45 @@ def to_sparse_matrix(self) -> csr_matrix:
14641469
if self.n_qubits == 0:
14651470
return csr_matrix(self.coeff_vec)
14661471

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)
14741479

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
14771482

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)
14811486

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))
14851490

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)
14871492

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))
14941511

14951512
def conjugate_op(self, R: 'PauliwordOp') -> 'PauliwordOp':
14961513
"""

symmer/process_handler.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
from ray import remote, put, get
66
from multiprocessing import Process, Queue, set_start_method
77

8-
if sys.platform.lower() in ['linux', 'darwin']:
8+
if sys.platform.lower() in ['linux', 'darwin', 'linux2']:
99
set_start_method('fork', force = True)
1010
else:
1111
set_start_method('spawn', force = True)
1212

1313
class ProcessHandler:
1414

15-
method = 'ray'
15+
if sys.platform.lower() in ['linux', 'darwin', 'linux2']:
16+
method = 'mp'
17+
else:
18+
method = 'ray'
19+
1620
verbose = False
1721

1822
def __init__(self):

0 commit comments

Comments
 (0)