Skip to content

Commit

Permalink
Switch cvxopt usage to cvxpy
Browse files Browse the repository at this point in the history
This commit switches the usage of cvxopt to use cvxpy instead.
  • Loading branch information
mtreinish committed Nov 11, 2019
1 parent c5f3ec0 commit bd20530
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
28 changes: 12 additions & 16 deletions qiskit/providers/aer/noise/utils/noise_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import itertools
import numpy
import cvxpy

from qiskit.providers.aer.noise.errors import QuantumError
from qiskit.providers.aer.noise import NoiseModel
Expand Down Expand Up @@ -691,22 +692,13 @@ def solve_quadratic_program(self, P, q):
Returns:
list: The solution of the quadratic program (represents probabilites)
Raises:
ImportError: If cvxopt external module is not installed
Additional information
======================
This method is the only place in the code where we rely on the cvxopt library
This method is the only place in the code where we rely on the cvpy library
should we consider another library, only this method needs to change
"""
try:
import cvxopt
except ImportError:
raise ImportError(
"The CVXOPT library is required to use this module")

P = cvxopt.matrix(numpy.array(P).astype(float))
q = cvxopt.matrix(numpy.array(q).astype(float)).T
P = numpy.array(P).astype(float)
q = numpy.array(q).astype(float).T
n = len(q)
# G and h constrain:
# 1) sum of probs is less then 1
Expand All @@ -718,7 +710,11 @@ def solve_quadratic_program(self, P, q):
if self.fidelity_data is not None:
G_data.append(self.fidelity_data['coefficients'])
h_data.append(self.fidelity_data['goal'])
G = cvxopt.matrix(numpy.array(G_data).astype(float))
h = cvxopt.matrix(numpy.array(h_data).astype(float))
cvxopt.solvers.options['show_progress'] = False
return cvxopt.solvers.qp(P, q, G, h)['x']
G = numpy.array(G_data).astype(float)
h = numpy.array(h_data).astype(float)
x = cvxpy.Variable(n)
prob = cvxpy.Problem(
cvxpy.Minimize((1 / 2) * cvxpy.quad_form(x, P) + q.T@x),
[G@x <= h])
prob.solve()
return x.value
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake
scikit-build
cython
asv
cvxopt
cvxpy
pylint
pycodestyle
Sphinx>=1.8.3
Expand Down
16 changes: 0 additions & 16 deletions test/terra/noise/test_noise_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@
from qiskit.providers.aer.noise.errors.standard_errors import pauli_error
from qiskit.providers.aer.noise.errors.quantum_error import QuantumError

try:
import cvxopt
has_cvxopt = True
except ImportError:
has_cvxopt = False


@unittest.skipUnless(has_cvxopt, "Needs cvxopt to test")
class TestNoiseTransformer(unittest.TestCase):
def setUp(self):
self.ops = {
Expand Down Expand Up @@ -296,15 +289,6 @@ def test_errors(self):
"No information about noise type seven"):
approximate_quantum_error(error, operator_string="seven")

# let's pretend cvxopt does not exist; the script should raise ImportError with proper message
import unittest.mock
import sys
with unittest.mock.patch.dict(sys.modules, {'cvxopt': None}):
with self.assertRaisesRegex(
ImportError,
"The CVXOPT library is required to use this module"):
approximate_quantum_error(error, operator_string="reset")


if __name__ == '__main__':
unittest.main()

0 comments on commit bd20530

Please sign in to comment.