From ead01aa4850574d96a7676ef6f266217aeaa7452 Mon Sep 17 00:00:00 2001 From: Jim Garrison Date: Fri, 16 Jun 2023 22:27:23 -0400 Subject: [PATCH] Fix doctest discovery when DOcplex is not available --- circuit_knitting/cutting/cutqc/mip_model.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/circuit_knitting/cutting/cutqc/mip_model.py b/circuit_knitting/cutting/cutqc/mip_model.py index cf013ac9f..9b03a9124 100644 --- a/circuit_knitting/cutting/cutqc/mip_model.py +++ b/circuit_knitting/cutting/cutqc/mip_model.py @@ -17,15 +17,6 @@ import numpy as np -try: - from docplex.mp.model import Model - from docplex.mp.utils import DOcplexException -except ModuleNotFoundError as ex: # pragma: no cover - raise ModuleNotFoundError( - "DOcplex is not installed. For automatic cut finding to work, both " - "DOcplex and cplex must be available." - ) from ex - class MIPModel(object): """ @@ -100,6 +91,14 @@ def __init__( num_in_qubits += 1 self.vertex_weight[node] = num_in_qubits + try: + from docplex.mp.model import Model + except ModuleNotFoundError as ex: # pragma: no cover + raise ModuleNotFoundError( + "DOcplex is not installed. For automatic cut finding to work, both " + "DOcplex and cplex must be available." + ) from ex + self.model = Model("docplex_cutter") self.model.log_output = False self._add_variables() @@ -465,6 +464,8 @@ def solve(self, min_postprocessing_cost: float) -> bool: # print('solving for %d subcircuits'%self.num_subcircuit) # print('model has %d variables, %d linear constraints,%d quadratic constraints, %d general constraints' # % (self.model.NumVars,self.model.NumConstrs, self.model.NumQConstrs, self.model.NumGenConstrs)) + from docplex.mp.utils import DOcplexException + print( "Exporting as a LP file to let you check the model that will be solved : ", min_postprocessing_cost,