Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to sparse #189

Merged
merged 7 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file modified symmer/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions symmer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Main init for package."""
import warnings
warnings.filterwarnings('ignore', module='cotengra')
from symmer.process_handler import process
from symmer.operators import PauliwordOp, QuantumState
from symmer.projection import QubitTapering, ContextualSubspace, QubitSubspaceManager
61 changes: 39 additions & 22 deletions symmer/operators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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':
"""
Expand Down
8 changes: 6 additions & 2 deletions symmer/process_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
from ray import remote, put, get
from multiprocessing import Process, Queue, set_start_method

if sys.platform.lower() in ['linux', 'darwin']:
if sys.platform.lower() in ['linux', 'darwin', 'linux2']:
set_start_method('fork', force = True)
else:
set_start_method('spawn', force = True)

class ProcessHandler:

method = 'ray'
if sys.platform.lower() in ['linux', 'darwin', 'linux2']:
method = 'mp'
else:
method = 'ray'

verbose = False

def __init__(self):
Expand Down
Loading