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

Symmer import speed up #165

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions symmer/operators/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import quimb
from symmer.operators.utils import *
import ray
from ray import put, get, remote
import numpy as np
import pandas as pd
import networkx as nx
Expand Down Expand Up @@ -829,8 +829,8 @@ def expval(self, psi: "QuantumState") -> complex:
"""
if self.n_terms > 1:
# parallelize if number of terms greater than one
psi_ray_store = ray.put(psi)
expvals = np.array(ray.get(
psi_ray_store = put(psi)
expvals = np.array(get(
[single_term_expval.remote(P, psi_ray_store) for P in self]))
else:
expvals = np.array(single_term_expval(self, psi))
Expand Down Expand Up @@ -2399,7 +2399,7 @@ def get_ij_operator(i:int, j:int, n_qubits:int,
else:
return ij_symp_matrix, coeffs

@ray.remote(num_cpus=os.cpu_count(),
@remote(num_cpus=os.cpu_count(),
runtime_env={
"env_vars": {
"NUMBA_NUM_THREADS": os.getenv("NUMBA_NUM_THREADS"),
Expand Down
6 changes: 3 additions & 3 deletions symmer/operators/independent_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import multiprocessing as mp
from symmer.operators.utils import _rref_binary, _cref_binary, check_independent
from symmer.operators import PauliwordOp, QuantumState, symplectic_to_string, single_term_expval
import ray
from ray import put, get, remote

class IndependentOp(PauliwordOp):
"""
Expand Down Expand Up @@ -296,8 +296,8 @@ def update_sector(self,
# self.coeff_vec = np.array(
# list(pool.starmap(assign_value, [(S, ref_state, threshold) for S in self]))
# )
ref_state_ray_store = ray.put(ref_state)
self.coeff_vec = np.array(ray.get(
ref_state_ray_store = put(ref_state)
self.coeff_vec = np.array(get(
[single_term_expval.remote(S, ref_state_ray_store) for S in self]))
# set anything below threshold to be zero
self.coeff_vec[np.abs(self.coeff_vec)<threshold] = 0
Expand Down
8 changes: 4 additions & 4 deletions symmer/operators/noncontextual_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from scipy.optimize import differential_evolution, shgo
from symmer.operators import PauliwordOp, IndependentOp, AntiCommutingOp, QuantumState
from symmer.operators.utils import binomial_coefficient, perform_noncontextual_sweep
import ray
from ray import put, get, remote
from symmer.utils import random_anitcomm_2n_1_PauliwordOp

class NoncontextualOp(PauliwordOp):
Expand Down Expand Up @@ -748,8 +748,8 @@ def energy_via_brute_force(self) -> Tuple[float, np.array, np.array]:
nu_list[:,~self.fixed_ev_mask] = np.array(list(itertools.product([-1,1],repeat=np.sum(~self.fixed_ev_mask))))

# # optimize over all discrete value assignments of nu in parallel
noncon_H = ray.put(self.NC_op)
tracker = np.array(ray.get(
noncon_H = put(self.NC_op)
tracker = np.array(get(
[get_noncon_energy.remote(noncon_H, nu_vec) for nu_vec in nu_list]))

# with mp.Pool(mp.cpu_count()) as pool:
Expand Down Expand Up @@ -879,7 +879,7 @@ def energy_xUSO(self) -> Tuple[float, np.array, np.array]:
return self.NC_op.get_energy(nu_vec), nu_vec


@ray.remote(num_cpus=os.cpu_count(),
@remote(num_cpus=os.cpu_count(),
runtime_env={
"env_vars": {
"NUMBA_NUM_THREADS": os.getenv("NUMBA_NUM_THREADS"),
Expand Down
8 changes: 4 additions & 4 deletions symmer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from scipy.sparse import kron as sparse_kron
from symmer.operators.utils import _rref_binary
import multiprocessing as mp
import ray
from ray import put, get, remote
import os
# from psutil import cpu_count

Expand Down Expand Up @@ -270,20 +270,20 @@ def get_sparse_matrix_large_pauliwordop(P_op: PauliwordOp) -> csr_matrix:
n_chunks = os.cpu_count()
if (n_chunks<=1) or (P_op.n_terms<=1):
# no multiprocessing possible
mat = ray.get(_get_sparse_matrix_large_pauliwordop.remote(P_op))
mat = get(_get_sparse_matrix_large_pauliwordop.remote(P_op))
else:
# plus one below due to indexing (actual number of chunks ignores this value)
n_chunks += 1
P_op_chunks_inds = np.rint(np.linspace(0, P_op.n_terms, min(n_chunks, P_op.n_terms+1))).astype(set).astype(int)
P_op_chunks = [P_op[P_op_chunks_inds[ind_i]: P_op_chunks_inds[ind_i + 1]] for ind_i, _ in
enumerate(P_op_chunks_inds[1:])]
tracker = np.array(ray.get(
tracker = np.array(get(
[_get_sparse_matrix_large_pauliwordop.remote(op) for op in P_op_chunks]))
mat = reduce(lambda x, y: x + y, tracker)

return mat

@ray.remote(num_cpus=os.cpu_count(),
@remote(num_cpus=os.cpu_count(),
runtime_env={
"env_vars": {
"NUMBA_NUM_THREADS": os.getenv("NUMBA_NUM_THREADS"),
Expand Down
Loading