-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from nonhermitian/move-solvers
Move solvers to own modules
- Loading branch information
Showing
10 changed files
with
301 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# This code is part of Mthree. | ||
# | ||
# (C) Copyright IBM 2024. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
# pylint: disable=no-name-in-module, invalid-name | ||
"""Direct solver routines""" | ||
import scipy.linalg as la | ||
|
||
from mthree.matrix import _reduced_cal_matrix | ||
from mthree.utils import counts_to_vector, vector_to_quasiprobs | ||
from mthree.norms import ainv_onenorm_est_lu | ||
from mthree.exceptions import M3Error | ||
|
||
|
||
def reduced_cal_matrix(mitigator, counts, qubits, distance=None): | ||
"""Return the reduced calibration matrix used in the solution. | ||
Parameters: | ||
counts (dict): Input counts dict. | ||
qubits (array_like): Qubits on which measurements applied. | ||
distance (int): Distance to correct for. Default=num_bits | ||
Returns: | ||
ndarray: 2D array of reduced calibrations. | ||
dict: Counts in order they are displayed in matrix. | ||
Raises: | ||
M3Error: If bit-string length does not match passed number | ||
of qubits. | ||
""" | ||
counts = dict(counts) | ||
# If distance is None, then assume max distance. | ||
num_bits = len(qubits) | ||
if distance is None: | ||
distance = num_bits | ||
|
||
# check if len of bitstrings does not equal number of qubits passed. | ||
bitstring_len = len(next(iter(counts))) | ||
if bitstring_len != num_bits: | ||
raise M3Error( | ||
"Bitstring length ({}) does not match".format(bitstring_len) | ||
+ " number of qubits ({})".format(num_bits) | ||
) | ||
|
||
cals = mitigator._form_cals(qubits) | ||
A, counts, _ = _reduced_cal_matrix(counts, cals, num_bits, distance) | ||
return A, counts | ||
|
||
|
||
def direct_solver( | ||
mitigator, counts, qubits, distance=None, return_mitigation_overhead=False | ||
): | ||
"""Apply the mitigation using direct LU factorization. | ||
Parameters: | ||
counts (dict): Input counts dict. | ||
qubits (int): Qubits over which to calibrate. | ||
distance (int): Distance to correct for. Default=num_bits | ||
return_mitigation_overhead (bool): Returns the mitigation overhead, default=False. | ||
Returns: | ||
QuasiDistribution: dict of Quasiprobabilites | ||
""" | ||
cals = mitigator._form_cals(qubits) | ||
num_bits = len(qubits) | ||
A, sorted_counts, col_norms = _reduced_cal_matrix(counts, cals, num_bits, distance) | ||
vec = counts_to_vector(sorted_counts) | ||
LU = la.lu_factor(A, check_finite=False) | ||
x = la.lu_solve(LU, vec, check_finite=False) | ||
gamma = None | ||
if return_mitigation_overhead: | ||
gamma = ainv_onenorm_est_lu(A, LU) | ||
out = vector_to_quasiprobs(x, sorted_counts) | ||
return out, col_norms, gamma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# This code is part of Mthree. | ||
# | ||
# (C) Copyright IBM 2024. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
# pylint: disable=no-name-in-module, invalid-name | ||
"""Iterative solver routines""" | ||
import numpy as np | ||
import scipy.sparse.linalg as spla | ||
|
||
from mthree.norms import ainv_onenorm_est_iter | ||
from mthree.matvec import M3MatVec | ||
from mthree.utils import counts_to_vector, vector_to_quasiprobs | ||
from mthree.exceptions import M3Error | ||
|
||
|
||
def iterative_solver( | ||
mitigator, | ||
counts, | ||
qubits, | ||
distance, | ||
tol=1e-5, | ||
max_iter=25, | ||
details=0, | ||
callback=None, | ||
return_mitigation_overhead=False, | ||
): | ||
"""Compute solution using GMRES and Jacobi preconditioning. | ||
Parameters: | ||
counts (dict): Input counts dict. | ||
qubits (int): Qubits over which to calibrate. | ||
tol (float): Tolerance to use. | ||
max_iter (int): Maximum number of iterations to perform. | ||
distance (int): Distance to correct for. Default=num_bits | ||
details (bool): Return col norms. | ||
callback (callable): Callback function to record iteration count. | ||
return_mitigation_overhead (bool): Returns the mitigation overhead, default=False. | ||
Returns: | ||
QuasiDistribution: dict of Quasiprobabilites | ||
Raises: | ||
M3Error: Solver did not converge. | ||
""" | ||
cals = mitigator._form_cals(qubits) | ||
M = M3MatVec(dict(counts), cals, distance) | ||
L = spla.LinearOperator( | ||
(M.num_elems, M.num_elems), | ||
matvec=M.matvec, | ||
rmatvec=M.rmatvec, | ||
dtype=np.float32, | ||
) | ||
diags = M.get_diagonal() | ||
|
||
def precond_matvec(x): | ||
out = x / diags | ||
return out | ||
|
||
P = spla.LinearOperator( | ||
(M.num_elems, M.num_elems), precond_matvec, dtype=np.float32 | ||
) | ||
vec = counts_to_vector(M.sorted_counts) | ||
|
||
out, error = spla.gmres( | ||
L, | ||
vec, | ||
rtol=tol, | ||
atol=tol, | ||
maxiter=max_iter, | ||
M=P, | ||
callback=callback, | ||
callback_type="legacy", | ||
) | ||
if error: | ||
raise M3Error("GMRES did not converge: {}".format(error)) | ||
|
||
gamma = None | ||
if return_mitigation_overhead: | ||
gamma = ainv_onenorm_est_iter(M, tol=tol, max_iter=max_iter) | ||
|
||
quasi = vector_to_quasiprobs(out, M.sorted_counts) | ||
if details: | ||
return quasi, M.get_col_norms(), gamma | ||
return quasi, gamma |
Oops, something went wrong.