qiskit_optimization/converters/inequality_to_equality.py
def convert(self, problem: QuadraticProgram) -> QuadraticProgram:
        """Convert a problem with inequality constraints into one with only equality constraints.

        Args:
            problem: The problem to be solved, that may contain inequality constraints.

        Returns:
            The converted problem, that contain only equality constraints.

        Raises:
            QiskitOptimizationError: If a variable type is not supported.
            QiskitOptimizationError: If an unsupported mode is selected.
            QiskitOptimizationError: If an unsupported sense is specified.
        """
        self._src = copy.deepcopy(problem)
        self._dst = QuadraticProgram(name=problem.name)

        # set a converting mode
        mode = self._mode
        if mode not in ["integer", "continuous", "auto"]:
            raise QiskitOptimizationError(f"Unsupported mode is selected: {mode}")

        # Copy variables
        for x in self._src.variables:
            if x.vartype == Variable.Type.BINARY:
                self._dst.binary_var(name=x.name)
            elif x.vartype == Variable.Type.INTEGER:
                self._dst.integer_var(name=x.name, lowerbound=x.lowerbound, upperbound=x.upperbound)
            elif x.vartype == Variable.Type.CONTINUOUS:
                self._dst.continuous_var(
                    name=x.name, lowerbound=x.lowerbound, upperbound=x.upperbound
                )
            else:
                raise QiskitOptimizationError(f"Unsupported variable type {x.vartype}")

        # Copy the objective function
        constant = self._src.objective.constant
        linear = self._src.objective.linear.to_dict(use_name=True)
        quadratic = self._src.objective.quadratic.to_dict(use_name=True)
        if self._src.objective.sense == QuadraticObjective.Sense.MINIMIZE:
            self._dst.minimize(constant, linear, quadratic)
        else:
            self._dst.maximize(constant, linear, quadratic)

        # For linear constraints
        for lin_const in self._src.linear_constraints:
            if lin_const.sense == Constraint.Sense.EQ:
                self._dst.linear_constraint(
                    lin_const.linear.coefficients, lin_const.sense, lin_const.rhs, lin_const.name
                )
            elif lin_const.sense in [Constraint.Sense.LE, Constraint.Sense.GE]:
                self._add_slack_var_linear_constraint(lin_const)
            else:
                raise QiskitOptimizationError(
                    f"Internal error: type of sense in {lin_const.name} is not supported: "
                    f"{lin_const.sense}"
                )

        # For quadratic constraints
        for quad_const in self._src.quadratic_constraints:
            if quad_const.sense == Constraint.Sense.EQ:
                self._dst.quadratic_constraint(
                    quad_const.linear.coefficients,
                    quad_const.quadratic.coefficients,
                    quad_const.sense,
                    quad_const.rhs,
                    quad_const.name,
                )
            elif quad_const.sense in [Constraint.Sense.LE, Constraint.Sense.GE]:
                self._add_slack_var_quadratic_constraint(quad_const)
            else:
                raise QiskitOptimizationError(
                    f"Internal error: type of sense in {quad_const.name} is not supported: "
                    f"{quad_const.sense}"
                )

        return self._dst
Convert a problem with inequality constraints into one with only equality constraints.

        Args:
            problem: The problem to be solved, that may contain inequality constraints.

        Returns:
            The converted problem, that contain only equality constraints.

        Raises:
            QiskitOptimizationError: If a variable type is not supported.
            QiskitOptimizationError: If an unsupported mode is selected.
            QiskitOptimizationError: If an unsupported sense is specified.
        
Convert a problem with inequality constraints into one with only equality constraints.
Convert a problem with inequality constraints into one with only equality constraints .
def convert(self, problem: QuadraticProgram) -> QuadraticProgram:
        self._src = copy.deepcopy(problem)
        self._dst = QuadraticProgram(name=problem.name)
        mode = self._mode
        if mode not in ["integer", "continuous", "auto"]:
            raise QiskitOptimizationError(f"Unsupported mode is selected: {mode}")
        for x in self._src.variables:
            if x.vartype == Variable.Type.BINARY:
                self._dst.binary_var(name=x.name)
            elif x.vartype == Variable.Type.INTEGER:
                self._dst.integer_var(name=x.name, lowerbound=x.lowerbound, upperbound=x.upperbound)
            elif x.vartype == Variable.Type.CONTINUOUS:
                self._dst.continuous_var(
                    name=x.name, lowerbound=x.lowerbound, upperbound=x.upperbound
                )
            else:
                raise QiskitOptimizationError(f"Unsupported variable type {x.vartype}")
        constant = self._src.objective.constant
        linear = self._src.objective.linear.to_dict(use_name=True)
        quadratic = self._src.objective.quadratic.to_dict(use_name=True)
        if self._src.objective.sense == QuadraticObjective.Sense.MINIMIZE:
            self._dst.minimize(constant, linear, quadratic)
        else:
            self._dst.maximize(constant, linear, quadratic)
        for lin_const in self._src.linear_constraints:
            if lin_const.sense == Constraint.Sense.EQ:
                self._dst.linear_constraint(
                    lin_const.linear.coefficients, lin_const.sense, lin_const.rhs, lin_const.name
                )
            elif lin_const.sense in [Constraint.Sense.LE, Constraint.Sense.GE]:
                self._add_slack_var_linear_constraint(lin_const)
            else:
                raise QiskitOptimizationError(
                    f"Internal error: type of sense in {lin_const.name} is not supported: "
                    f"{lin_const.sense}"
                )
        for quad_const in self._src.quadratic_constraints:
            if quad_const.sense == Constraint.Sense.EQ:
                self._dst.quadratic_constraint(
                    quad_const.linear.coefficients,
                    quad_const.quadratic.coefficients,
                    quad_const.sense,
                    quad_const.rhs,
                    quad_const.name,
                )
            elif quad_const.sense in [Constraint.Sense.LE, Constraint.Sense.GE]:
                self._add_slack_var_quadratic_constraint(quad_const)
            else:
                raise QiskitOptimizationError(
                    f"Internal error: type of sense in {quad_const.name} is not supported: "
                    f"{quad_const.sense}"
                )
        return self._dst
def convert ( self , problem : QuadraticProgram ) -> QuadraticProgram : self . _src = copy . deepcopy ( problem ) self . _dst = QuadraticProgram ( name = problem . name ) mode = self . _mode if mode not in [ "integer" , "continuous" , "auto" ] : raise QiskitOptimizationError ( f"Unsupported mode is selected: {mode}" ) for x in self . _src . variables : if x . vartype == Variable . Type . BINARY : self . _dst . binary_var ( name = x . name ) elif x . vartype == Variable . Type . INTEGER : self . _dst . integer_var ( name = x . name , lowerbound = x . lowerbound , upperbound = x . upperbound ) elif x . vartype == Variable . Type . CONTINUOUS : self . _dst . continuous_var ( name = x . name , lowerbound = x . lowerbound , upperbound = x . upperbound ) else : raise QiskitOptimizationError ( f"Unsupported variable type {x.vartype}" ) constant = self . _src . objective . constant linear = self . _src . objective . linear . to_dict ( use_name = True ) quadratic = self . _src . objective . quadratic . to_dict ( use_name = True ) if self . _src . objective . sense == QuadraticObjective . Sense . MINIMIZE : self . _dst . minimize ( constant , linear , quadratic ) else : self . _dst . maximize ( constant , linear , quadratic ) for lin_const in self . _src . linear_constraints : if lin_const . sense == Constraint . Sense . EQ : self . _dst . linear_constraint ( lin_const . linear . coefficients , lin_const . sense , lin_const . rhs , lin_const . name ) elif lin_const . sense in [ Constraint . Sense . LE , Constraint . Sense . GE ] : self . _add_slack_var_linear_constraint ( lin_const ) else : raise QiskitOptimizationError ( f"Internal error: type of sense in {lin_const.name} is not supported: " f"{lin_const.sense}" ) for quad_const in self . _src . quadratic_constraints : if quad_const . sense == Constraint . Sense . EQ : self . _dst . quadratic_constraint ( quad_const . linear . coefficients , quad_const . quadratic . coefficients , quad_const . sense , quad_const . rhs , quad_const . name , ) elif quad_const . sense in [ Constraint . Sense . LE , Constraint . Sense . GE ] : self . _add_slack_var_quadratic_constraint ( quad_const ) else : raise QiskitOptimizationError ( f"Internal error: type of sense in {quad_const.name} is not supported: " f"{quad_const.sense}" ) return self . _dst
Convert a problem with inequality constraints into one with only equality constraints.
Convert a problem with inequality constraints into one with only equality constraints .
"""Convert a problem with inequality constraints into one with only equality constraints.

        Args:
            problem: The problem to be solved, that may contain inequality constraints.

        Returns:
            The converted problem, that contain only equality constraints.

        Raises:
            QiskitOptimizationError: If a variable type is not supported.
            QiskitOptimizationError: If an unsupported mode is selected.
            QiskitOptimizationError: If an unsupported sense is specified.
        """ # set a converting mode # Copy variables # Copy the objective function # For linear constraints # For quadratic constraints
The converted problem, that contain only equality constraints.
The converted problem that contain only equality constraints .
If a variable type is not supported.
If a variable type is not supported .
QiskitOptimizationError
If an unsupported mode is selected.
If an unsupported mode is selected .
QiskitOptimizationError
If an unsupported sense is specified.
If an unsupported sense is specified .
QiskitOptimizationError
self
problem
QuadraticProgram
The problem to be solved, that may contain inequality constraints.
The problem to be solved that may contain inequality constraints .