Skip to content

Commit

Permalink
Merge pull request #207 from jakelishman/fix-old-scipy
Browse files Browse the repository at this point in the history
Fix 'gmres' calls on old Scipy versions
  • Loading branch information
mtreinish authored Feb 9, 2024
2 parents 34acff2 + ac1e9b0 commit ced1635
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-20.04, windows-2019, macos-10.15]
os: [ubuntu-latest, windows-latest, macos-11]

steps:
- uses: actions/checkout@v2
Expand All @@ -21,15 +21,16 @@ jobs:
uses: pypa/[email protected]
env:
CIBW_SKIP: "cp36-* cp37-* pp* *musl*"
CIBW_TEST_SKIP: "*-macosx_arm64 *-macosx_universal2:arm64 *-win32 *-manylinux_i686 cp310-manylinux*"
CIBW_TEST_SKIP: "*-macosx_arm64 *-macosx_universal2:arm64 *-win32 *-win_amd64 *-manylinux_i686 cp310-manylinux*"
CIBW_MANYLINUX_X86_64_IMAGE: quay.io/pypa/manylinux2014_x86_64:latest
CIBW_MANYLINUX_I686_IMAGE: quay.io/pypa/manylinux2014_i686:latest
CIBW_ARCHS_MACOS: x86_64 universal2
CIBW_TEST_REQUIRES: pytest qiskit-aer
CIBW_TEST_REQUIRES: pytest qiskit-aer setuptools
CIBW_TEST_COMMAND_LINUX: pytest -p no:warnings /mthree_test
CIBW_TEST_COMMAND_WINDOWS: pytest -p no:warnings C:\Users\RUNNER~1\AppData\Local\Temp\mthree_test
CIBW_TEST_COMMAND_MACOS: pytest -p no:warnings /tmp/mthree_test
CIBW_ENVIRONMENT_LINUX: MTHREE_OPENMP=1
CIBW_ENVIRONMENT_MACOS: MACOSX_DEPLOYMENT_TARGET=10.15
CIBW_BEFORE_TEST_LINUX: rm -rf /mthree_test && cp -r {project}/mthree/test /mthree_test && rm -f /mthree_test/test_converters.py && rm -f /mthree_test/test_columns.py
CIBW_BEFORE_TEST_WINDOWS: rm -rf C:\Users\RUNNER~1\AppData\Local\Temp\mthree_test && cp -r {project}/mthree/test C:\Users\RUNNER~1\AppData\Local\Temp\mthree_test && rm -f C:\Users\RUNNER~1\AppData\Local\Temp\mthree_test\test_converters.py && rm -f C:\Users\RUNNER~1\AppData\Local\Temp\mthree_test\test_columns.py && pip install --prefer-binary orjson
CIBW_BEFORE_TEST_MACOS: rm -rf /tmp/mthree_test && cp -r {project}/mthree/test /tmp/mthree_test && rm -f /tmp/mthree_test/test_converters.py && rm -f /tmp/mthree_test/test_columns.py
Expand Down
4 changes: 2 additions & 2 deletions mthree/mitigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
balanced_cal_circuits,
)
from mthree.matrix import _reduced_cal_matrix, sdd_check
from mthree.utils import counts_to_vector, vector_to_quasiprobs
from mthree.utils import counts_to_vector, vector_to_quasiprobs, gmres
from mthree.norms import ainv_onenorm_est_lu, ainv_onenorm_est_iter
from mthree.matvec import M3MatVec
from mthree.exceptions import M3Error
Expand Down Expand Up @@ -779,7 +779,7 @@ def precond_matvec(x):

P = spla.LinearOperator((M.num_elems, M.num_elems), precond_matvec)
vec = counts_to_vector(M.sorted_counts)
out, error = spla.gmres(
out, error = gmres(
L,
vec,
rtol=tol,
Expand Down
22 changes: 12 additions & 10 deletions mthree/norms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import numpy as np
import scipy.linalg as la
import scipy.sparse.linalg as spla

from mthree.exceptions import M3Error
from mthree.utils import gmres


def ainv_onenorm_est_lu(A, LU=None):
Expand Down Expand Up @@ -134,14 +136,14 @@ def precond_matvec(x):
v = (1.0/dims)*np.ones(dims, dtype=float)

# Initial solve
v, error = spla.gmres(L, v, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
v, error = gmres(L, v, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
if error:
raise M3Error('Iterative solver error {}'.format(error))
gamma = la.norm(v, 1)
eta = np.sign(v)
x, error = spla.gmres(LT, eta, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
x, error = gmres(LT, eta, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
if error:
raise M3Error('Iterative solver error {}'.format(error))
# loop over reasonable number of trials
Expand All @@ -151,8 +153,8 @@ def precond_matvec(x):
idx = np.where(np.abs(x) == x_nrm)[0][0]
v = np.zeros(dims, dtype=float)
v[idx] = 1
v, error = spla.gmres(L, v, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
v, error = gmres(L, v, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
if error:
raise M3Error('Iterative solver error {}'.format(error))
gamma_prime = gamma
Expand All @@ -162,8 +164,8 @@ def precond_matvec(x):
break

eta = np.sign(v)
x, error = spla.gmres(LT, eta, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
x, error = gmres(LT, eta, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
if error:
raise M3Error('Iterative solver error {}'.format(error))
if la.norm(x, np.inf) == x[idx]:
Expand All @@ -174,8 +176,8 @@ def precond_matvec(x):
x = np.arange(1, dims+1)
x = (-1)**(x+1)*(1+(x-1)/(dims-1))

x, error = spla.gmres(L, x, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
x, error = gmres(L, x, rtol=tol, atol=tol, maxiter=max_iter,
M=P)
if error:
raise M3Error('Iterative solver error {}'.format(error))

Expand Down
17 changes: 17 additions & 0 deletions mthree/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,30 @@
marginal_distribution
"""
import functools
import numpy as np
import scipy.sparse.linalg

from qiskit.result import marginal_distribution as marg_dist
from mthree.exceptions import M3Error
from mthree.classes import (QuasiDistribution, ProbDistribution,
QuasiCollection, ProbCollection)

# This dynamic switch on keyword arguments in 'gmres' can be removed once Scipy 1.11 is
# the minimum supported version.

SCIPY_MAJOR, SCIPY_MINOR, *_ = scipy.__version__.split(".", 2)
if (int(SCIPY_MAJOR), int(SCIPY_MINOR)) >= (1, 11):
gmres = scipy.sparse.linalg.gmres
else:
@functools.wraps(scipy.sparse.linalg.gmres)
def gmres(*args, **kwargs):
"""Compatibility wrapper around Scipy's `gmres` to convert the new-style 'rtol'
argument into the old-style 'tol' for old SciPys."""
if "rtol" in kwargs:
kwargs["tol"] = kwargs.pop("rtol")
return scipy.sparse.linalg.gmres(*args, **kwargs)


def final_measurement_mapping(circuit):
"""Return the final measurement mapping for the circuit.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

MAJOR = 2
MINOR = 6
MICRO = 2
MICRO = 3

ISRELEASED = True
VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)
Expand Down

0 comments on commit ced1635

Please sign in to comment.