From dc8cf6f0a51b7745406bc61d782f1acde2042d87 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Tue, 8 Oct 2024 12:35:23 -0400 Subject: [PATCH 1/5] 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': """ From 6eef95a864fb820f823af9b40c1d83c7f73713a0 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Tue, 8 Oct 2024 12:38:03 -0400 Subject: [PATCH 2/5] removing py3dmol --- poetry.lock | 16 +--------------- pyproject.toml | 1 - symmer/utils.py | 33 --------------------------------- 3 files changed, 1 insertion(+), 49 deletions(-) diff --git a/poetry.lock b/poetry.lock index 7409122..bf3ad99 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2558,20 +2558,6 @@ files = [ [package.extras] tests = ["pytest"] -[[package]] -name = "py3dmol" -version = "2.4.0" -description = "An IPython interface for embedding 3Dmol.js views in Jupyter notebooks" -optional = false -python-versions = "*" -files = [ - {file = "py3Dmol-2.4.0-py2.py3-none-any.whl", hash = "sha256:e7d27ffdf9326850e4c6dac55ec047ce023ffaf87d8032278982a4d3d78d3973"}, - {file = "py3Dmol-2.4.0.tar.gz", hash = "sha256:5cbeb125771abdedf051845bbb8df30306338a92a1852c9934c3ec85ba482a78"}, -] - -[package.extras] -ipython = ["IPython"] - [[package]] name = "pycodestyle" version = "2.8.0" @@ -3790,4 +3776,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "17a853a1287459499c42a8559227f2b5d1ef4ddedd10f531f02ffce9baa9ebb5" +content-hash = "0ebae7433e51c9b3d5a63553a4d6ab65144b44e911cd041d345d4b49e563cdc3" diff --git a/pyproject.toml b/pyproject.toml index 3a246ff..87b55f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,6 @@ ray = "2.10" sphinx-design = "^0.5.0" myst-nb = "^0.17.2" sphinx-copybutton = "^0.5.2" -py3dmol = "^2.0.4" scipy = "1.9.3" urllib3 = "1.26.6" diff --git a/symmer/utils.py b/symmer/utils.py index a961f97..82ca2ff 100644 --- a/symmer/utils.py +++ b/symmer/utils.py @@ -3,7 +3,6 @@ import scipy as sp from typing import List, Tuple, Union from functools import reduce -import py3Dmol from scipy.sparse import csr_matrix from scipy.sparse import kron as sparse_kron from symmer.operators.utils import _rref_binary @@ -226,38 +225,6 @@ def gram_schmidt_from_quantum_state(state:Union[np.array, list, QuantumState]) - return M -def Draw_molecule( - xyz_string: str, width: int = 400, height: int = 400, style: str = "sphere" - ) -> py3Dmol.view: - """Draw molecule from xyz string. - - Note if molecule has unrealistic bonds, then style should be sphere. Otherwise stick style can be used - which shows bonds. - - TODO: more styles at http://3dmol.csb.pitt.edu/doc/$3Dmol.GLViewer.html - - Args: - xyz_string (str): xyz string of molecule - width (int): width of image - height (int): Height of image - style (str): py3Dmol style ('sphere' or 'stick') - - Returns: - view (py3dmol.view object). Run view.show() method to print molecule. - """ - view = py3Dmol.view(width=width, height=height) - view.addModel(xyz_string, "xyz") - if style == "sphere": - view.setStyle({'sphere': {"radius": 0.2}}) - elif style == "stick": - view.setStyle({'stick': {}}) - else: - raise ValueError(f"unknown py3dmol style: {style}") - - view.zoomTo() - return view - - def get_sparse_matrix_large_pauliwordop(P_op: PauliwordOp) -> csr_matrix: """ In order to build the sparse matrix (e.g. above 18 qubits), this function goes through each pauli term From 422a0a75c4e202ebaaa5dba2f34e6e53e26d1ba3 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Tue, 8 Oct 2024 12:44:16 -0400 Subject: [PATCH 3/5] updated default multiprocessing as ray... instead tests for operating system --- symmer/process_handler.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/symmer/process_handler.py b/symmer/process_handler.py index aeccc93..b36d767 100644 --- a/symmer/process_handler.py +++ b/symmer/process_handler.py @@ -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', 'darwin']: + method = 'mp' + else: + method = 'ray' + verbose = False def __init__(self): From ddf1101ccd6fd2116ddf8ee3dc4abf8c601b53e4 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Tue, 8 Oct 2024 12:46:53 -0400 Subject: [PATCH 4/5] typo in process_handler.py fixed --- symmer/process_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/symmer/process_handler.py b/symmer/process_handler.py index b36d767..f438eba 100644 --- a/symmer/process_handler.py +++ b/symmer/process_handler.py @@ -12,7 +12,7 @@ class ProcessHandler: - if sys.platform.lower() in ['linux', 'darwin', 'linux2', 'darwin']: + if sys.platform.lower() in ['linux', 'darwin', 'linux2']: method = 'mp' else: method = 'ray' From 9e78d578a59c49ac557d5be052f098e35f1f402f Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Tue, 8 Oct 2024 13:22:43 -0400 Subject: [PATCH 5/5] removed cotengra UserWarning at import --- symmer/.DS_Store | Bin 6148 -> 6148 bytes symmer/__init__.py | 2 ++ 2 files changed, 2 insertions(+) diff --git a/symmer/.DS_Store b/symmer/.DS_Store index 810b0bd9639ef9641f99cfae86e65e0b5c5f619d..b80ade341af72c08f21945b5f85f6f850d2fc661 100644 GIT binary patch delta 86 zcmZoMXfc@J&&atkU^g=(=Vl(3U?!fV^5TM|octsP28JDz16c|u?`QRz?8T<2$i+~= oP{feWkj0S7kjzlRkO^exl?4~&<>cq3164ClES$fYo#QV*04VAhfdBvi delta 32 ocmZoMXfc@J&&a+pU^g=(`(_@NV5Z5|>^vJw=P+(&=lIJH0G?_Ip#T5? diff --git a/symmer/__init__.py b/symmer/__init__.py index 5252ee9..4f59688 100644 --- a/symmer/__init__.py +++ b/symmer/__init__.py @@ -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 \ No newline at end of file