Converters for Quadratic Programs
Overview
# --- Setup cell added by QCR (not part of the original tutorial) ---
# Source: qiskit-community/qiskit-optimization @ 0.7.0, Apache License 2.0.
# Installs the example's dependencies. If a later cell still reports a missing
# package, restart the runtime/kernel and run again from the top.
%pip install -q qiskit-optimization==0.7.0 cplex
Converters for Quadratic Programs
Optimization problems in Qiskit optimization module are represented with the QuadraticProgram class, which is a generic and powerful representation for optimization problems. In general, optimization algorithms are defined for a certain formulation of a quadratic program, and we need to convert our problem to the right type.
For instance, Qiskit optimization provides several optimization algorithms that can handle Quadratic Unconstrained Binary Optimization (QUBO) problems. These are mapped to Ising Hamiltonians, for which Qiskit optimization uses the qiskit.quantum_info.SparsePauliOp object, and then their ground state is approximated. For this optimization, commonly known algorithms such as VQE or QAOA can be used as underlying routine. See the following tutorial about the Minimum Eigen Optimizer for more detail. Note that also other algorithms exist that work differently, such as the GroverOptimizer.
To map a problem to the correct input format, the optimization module of Qiskit optimization offers a variety of converters. In this tutorial we're providing an overview on this functionality. Currently, Qiskit optimization contains the following converters.
InequalityToEquality: convert inequality constraints into equality constraints with additional slack variables.IntegerToBinary: convert integer variables into binary variables and corresponding coefficients.LinearEqualityToPenalty: convert equality constraints into additional terms of the objective function.LinearInequalityToPenalty: convert inequality constraints into additional terms of the objective function.MaximizeToMinimize: convert to the equivalent minimization problem.MinimizeToMaximize: convert to the equivalent maximization problem.QuadraticProgramToQubo: a wrapper that includesInequalityToEquality,IntegerToBinary,LinearEqualityToPenalty,LinearInequalityToPenalty, andMaximizeToMinimizefor convenience.
InequalityToEquality
InequalityToEqualityConverter converts inequality constraints into equality constraints with additional slack variables to remove inequality constraints from QuadraticProgram. The upper bounds and the lower bounds of slack variables will be calculated from the difference between the left sides and the right sides of constraints. Signs of slack variables depend on symbols in constraints such as
The following is an example of a maximization problem with two inequality constraints. Variable
\begin{aligned} & \text{maximize} & 3x + 2y + z\ & \text{subject to:} & x+y+z \leq 5.5\ & & x+y+z \geq 2.5\ & & x, y \in {0,1}\ & & z \in {0,1,2,3,4,5,6,7} \ \end{aligned}
With QuadraticProgram, an optimization model of the problem is written as follows.
from qiskit_optimization import QuadraticProgram
from qiskit_optimization.translators.docplex_mp import to_docplex_mpqp = QuadraticProgram()
qp.binary_var("x")
qp.binary_var("y")
qp.integer_var(lowerbound=0, upperbound=7, name="z")
qp.maximize(linear={"x": 3, "y": 2, "z": 1})
qp.linear_constraint(linear={"x": 1, "y": 1, "z": 1}, sense="LE", rhs=5.5, name="xyz_leq")
qp.linear_constraint(linear={"x": 1, "y": 1, "z": 1}, sense="GE", rhs=2.5, name="xyz_geq")
print(qp.prettyprint())Problem name:
Maximize
3*x + 2*y + z
Subject to
Linear constraints (2)
x + y + z <= 5.5 'xyz_leq'
x + y + z >= 2.5 'xyz_geq'
Integer variables (1)
0 <= z <= 7
Binary variables (2)
x y
Call convert method of InequalityToEquality to convert.
from qiskit_optimization.converters import InequalityToEqualityineq2eq = InequalityToEquality()
qp_eq = ineq2eq.convert(qp)
print(qp_eq.prettyprint())Problem name:
Maximize
3*x + 2*y + z
Subject to
Linear constraints (2)
x + xyz_leq@int_slack + y + z == 5 'xyz_leq'
x - xyz_geq@int_slack + y + z == 3 'xyz_geq'
Integer variables (3)
0 <= z <= 7
0 <= xyz_leq@int_slack <= 5
0 <= xyz_geq@int_slack <= 6
Binary variables (2)
x y
After converting, the formulation of the problem looks like the above output. As we can see, the inequality constraints are replaced with equality constraints with additional integer slack variables,
Let us explain how the conversion works. For example, the lower bound of the left side of the first constraint is
\begin{aligned} & \text{maximize} & 3x + 2y + z\ & \text{subject to:} & x+y+z+ xyz_leg\text{@}int_slack= 5\ & & x+y+z+xyz_geq\text{@}int_slack= 3\ & & x, y \in {0,1}\ & & z \in {0,1,2,3,4,5,6,7} \ & & xyz_leg\text{@}int_slack \in {0,1,2,3,4,5} \ & & xyz_geq\text{@}int_slack \in {0,1,2,3,4,5,6} \ \end{aligned}
Let us see how the interpret method works. The purpose of this method is to convert the solution of the converted problem back to that of the original problem. To use this method, we would first require to solve the problem. For the purpose of this tutorial, we will use docplex to solve. We will first translate the quadratic problem into a docplex.mp model.
from qiskit_optimization.algorithms import CplexOptimizer
cplex_optimizer = CplexOptimizer()result_orig = cplex_optimizer.solve(qp)
print(result_orig)fval=8.0, x=1.0, y=1.0, z=3.0, status=SUCCESS
result_eq = cplex_optimizer.solve(qp_eq)
print(result_eq)fval=8.0, x=1.0, y=1.0, z=3.0, xyz_leq@int_slack=0.0, xyz_geq@int_slack=2.0, status=SUCCESS
The result result_eq of qp_eq has 5 variable values (x=1.0, y=1.0, z=3.0, xyz_leq@int_slack=0.0, xyz_geq@int_slack=2.0) while result result_orig of the original qp has three values (x=1.0, y=1.0, z=3.0).
We can call InequalityToEquality.interpret method by passing a list or an array to the method that has values of qp_eq.variables as follows.
result_eq.x has the list of values that each variable takes in the solution in correspondence to their position in the variable list qp_eq.variables.
print("interpreting values of result_eq:", ineq2eq.interpret(result_eq.x))
print("values of result_orig:", result_orig.x)interpreting values of result_eq: [1. 1. 3.] values of result_orig: [1. 1. 3.]
We notice that
IntegerToBinary
IntegerToBinary converts integer variables into binary variables and coefficients to remove integer variables from QuadraticProgram. For converting, bounded-coefficient encoding proposed in arxiv:1706.01945 (Eq. (5)) is used. For more detail of the encoding method, please see the paper.
We use the output of InequalityToEquality as a starting point. Variables
print(qp_eq.prettyprint())Problem name:
Maximize
3*x + 2*y + z
Subject to
Linear constraints (2)
x + xyz_leq@int_slack + y + z == 5 'xyz_leq'
x - xyz_geq@int_slack + y + z == 3 'xyz_geq'
Integer variables (3)
0 <= z <= 7
0 <= xyz_leq@int_slack <= 5
0 <= xyz_geq@int_slack <= 6
Binary variables (2)
x y
Call convert method of IntegerToBinary to convert.
from qiskit_optimization.converters import IntegerToBinaryint2bin = IntegerToBinary()
qp_eq_bin = int2bin.convert(qp_eq)
print(qp_eq_bin.prettyprint())Problem name:
Maximize
3*x + 2*y + z@0 + 2*z@1 + 4*z@2
Subject to
Linear constraints (2)
x + xyz_leq@int_slack@0 + 2*xyz_leq@int_slack@1 + 2*xyz_leq@int_slack@2 + y
+ z@0 + 2*z@1 + 4*z@2 == 5 'xyz_leq'
x - xyz_geq@int_slack@0 - 2*xyz_geq@int_slack@1 - 3*xyz_geq@int_slack@2 + y
+ z@0 + 2*z@1 + 4*z@2 == 3 'xyz_geq'
Binary variables (11)
x y z@0 z@1 z@2 xyz_leq@int_slack@0 xyz_leq@int_slack@1 xyz_leq@int_slack@2
xyz_geq@int_slack@0 xyz_geq@int_slack@1 xyz_geq@int_slack@2
After converting, the integer variable InequalityToEquality are also both replaced with three binary variables with coefficients 1, 2, 2, and 1, 2, 3, respectively.
Note: Essentially the coefficients mean that the sum of these binary variables with coefficients can be the sum of a subset of
IntegerToBinary also provides interpret method that is the functionality to translate a given binary result back to the original integer representation. Let us see how the interpret method works.
result_eq = cplex_optimizer.solve(qp_eq)
print(result_eq)fval=8.0, x=1.0, y=1.0, z=3.0, xyz_leq@int_slack=0.0, xyz_geq@int_slack=2.0, status=SUCCESS
result_eq_bin = cplex_optimizer.solve(qp_eq_bin)
print(result_eq_bin)fval=8.0, x=1.0, y=1.0, z@0=1.0, z@1=1.0, z@2=0.0, xyz_leq@int_slack@0=0.0, xyz_leq@int_slack@1=0.0, xyz_leq@int_slack@2=0.0, xyz_geq@int_slack@0=0.0, xyz_geq@int_slack@1=1.0, xyz_geq@int_slack@2=0.0, status=SUCCESS
result_eq_bin has more binary variables due to the conversion by IntegerToBinary.convert.
IntegerToBinary.interpret translates them back to the integer values by aggregating binary variables values associated with the original integer variables of qp_eq.
print("interpreting values of result_eq_bin:", int2bin.interpret(result_eq_bin.x))
print("values of result_eq:", result_eq.x)interpreting values of result_eq_bin: [1. 1. 3. 0. 2.] values of result_eq: [1. 1. 3. 0. 2.]
LinearEqualityToPenalty
LinearEqualityToPenalty converts linear equality constraints into additional quadratic penalty terms of the objective function to map QuadraticProgram to an unconstrained form.
An input to the converter has to be a QuadraticProgram with only linear equality constraints. Those equality constraints, e.g.
We use the output of IntegerToBinary as a starting point, where all variables are binary variables and all inequality constraints have been mapped to equality constraints.
We print the problem again for reference.
print(qp_eq_bin.prettyprint())Problem name:
Maximize
3*x + 2*y + z@0 + 2*z@1 + 4*z@2
Subject to
Linear constraints (2)
x + xyz_leq@int_slack@0 + 2*xyz_leq@int_slack@1 + 2*xyz_leq@int_slack@2 + y
+ z@0 + 2*z@1 + 4*z@2 == 5 'xyz_leq'
x - xyz_geq@int_slack@0 - 2*xyz_geq@int_slack@1 - 3*xyz_geq@int_slack@2 + y
+ z@0 + 2*z@1 + 4*z@2 == 3 'xyz_geq'
Binary variables (11)
x y z@0 z@1 z@2 xyz_leq@int_slack@0 xyz_leq@int_slack@1 xyz_leq@int_slack@2
xyz_geq@int_slack@0 xyz_geq@int_slack@1 xyz_geq@int_slack@2
Call convert method of LinearEqualityToPenalty to convert.
from qiskit_optimization.converters import LinearEqualityToPenaltylineq2penalty = LinearEqualityToPenalty()
qubo = lineq2penalty.convert(qp_eq_bin)
print(qubo.prettyprint())Problem name:
Maximize
-26*x^2 + 26*x*xyz_geq@int_slack@0 + 52*x*xyz_geq@int_slack@1
+ 78*x*xyz_geq@int_slack@2 - 26*x*xyz_leq@int_slack@0
- 52*x*xyz_leq@int_slack@1 - 52*x*xyz_leq@int_slack@2 - 52*x*y - 52*x*z@0
- 104*x*z@1 - 208*x*z@2 - 13*xyz_geq@int_slack@0^2
- 52*xyz_geq@int_slack@0*xyz_geq@int_slack@1
- 78*xyz_geq@int_slack@0*xyz_geq@int_slack@2 - 52*xyz_geq@int_slack@1^2
- 156*xyz_geq@int_slack@1*xyz_geq@int_slack@2 - 117*xyz_geq@int_slack@2^2
- 13*xyz_leq@int_slack@0^2 - 52*xyz_leq@int_slack@0*xyz_leq@int_slack@1
- 52*xyz_leq@int_slack@0*xyz_leq@int_slack@2 - 52*xyz_leq@int_slack@1^2
- 104*xyz_leq@int_slack@1*xyz_leq@int_slack@2 - 52*xyz_leq@int_slack@2^2
+ 26*y*xyz_geq@int_slack@0 + 52*y*xyz_geq@int_slack@1
+ 78*y*xyz_geq@int_slack@2 - 26*y*xyz_leq@int_slack@0
- 52*y*xyz_leq@int_slack@1 - 52*y*xyz_leq@int_slack@2 - 26*y^2 - 52*y*z@0
- 104*y*z@1 - 208*y*z@2 + 26*z@0*xyz_geq@int_slack@0
+ 52*z@0*xyz_geq@int_slack@1 + 78*z@0*xyz_geq@int_slack@2
- 26*z@0*xyz_leq@int_slack@0 - 52*z@0*xyz_leq@int_slack@1
- 52*z@0*xyz_leq@int_slack@2 - 26*z@0^2 - 104*z@0*z@1 - 208*z@0*z@2
+ 52*z@1*xyz_geq@int_slack@0 + 104*z@1*xyz_geq@int_slack@1
+ 156*z@1*xyz_geq@int_slack@2 - 52*z@1*xyz_leq@int_slack@0
- 104*z@1*xyz_leq@int_slack@1 - 104*z@1*xyz_leq@int_slack@2 - 104*z@1^2
- 416*z@1*z@2 + 104*z@2*xyz_geq@int_slack@0 + 208*z@2*xyz_geq@int_slack@1
+ 312*z@2*xyz_geq@int_slack@2 - 104*z@2*xyz_leq@int_slack@0
- 208*z@2*xyz_leq@int_slack@1 - 208*z@2*xyz_leq@int_slack@2 - 416*z@2^2
+ 211*x - 78*xyz_geq@int_slack@0 - 156*xyz_geq@int_slack@1
- 234*xyz_geq@int_slack@2 + 130*xyz_leq@int_slack@0 + 260*xyz_leq@int_slack@1
+ 260*xyz_leq@int_slack@2 + 210*y + 209*z@0 + 418*z@1 + 836*z@2 - 442
Subject to
No constraints
Binary variables (11)
x y z@0 z@1 z@2 xyz_leq@int_slack@0 xyz_leq@int_slack@1 xyz_leq@int_slack@2
xyz_geq@int_slack@0 xyz_geq@int_slack@1 xyz_geq@int_slack@2
After converting, the equality constraints are added to the objective function as additional terms with the default penalty factor provided by Qiskit optimization. The resulting problem is now a QUBO and compatible with many quantum optimization algorithms such as VQE, QAOA and so on.
This gives the same result as before.
Like we did for the other converters, let us see how interpret method works for this case.
result_eq_bin = cplex_optimizer.solve(qp_eq_bin)
print(result_eq_bin)fval=8.0, x=1.0, y=1.0, z@0=1.0, z@1=1.0, z@2=0.0, xyz_leq@int_slack@0=0.0, xyz_leq@int_slack@1=0.0, xyz_leq@int_slack@2=0.0, xyz_geq@int_slack@0=0.0, xyz_geq@int_slack@1=1.0, xyz_geq@int_slack@2=0.0, status=SUCCESS
result_qubo = cplex_optimizer.solve(qubo)
print(result_qubo)fval=8.0, x=1.0, y=1.0, z@0=1.0, z@1=1.0, z@2=0.0, xyz_leq@int_slack@0=0.0, xyz_leq@int_slack@1=0.0, xyz_leq@int_slack@2=0.0, xyz_geq@int_slack@0=0.0, xyz_geq@int_slack@1=1.0, xyz_geq@int_slack@2=0.0, status=SUCCESS
print("interpreting values of result_eq_bin:", lineq2penalty.interpret(result_qubo.x))
print("values of result_eq_bin:", result_eq_bin.x)interpreting values of result_eq_bin: [1. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0.] values of result_eq_bin: [1. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0.]
We can see that the result of the interpret method implies that both the original and converted problems have exactly the same solution. This is expected because the converted problem has exactly the same variables as the original problem, the objective has been modified in such a way that we do not have the constraints anymore in the converted problem.
Finally, let us see how we interpret the result of QUBO back to the solution of the original problem qp. The following code shows that the interpreted values are equivalent to the result of the original problem qp.
print("result_orig.x", result_orig.x)
x = result_qubo.x
for conv in [lineq2penalty, int2bin, ineq2eq]:
x = conv.interpret(x)
print("interpreting result_qubo.x", x)result_orig.x [1. 1. 3.] interpreting result_qubo.x [1. 1. 3.]
import tutorial_magics
%qiskit_version_table
%qiskit_copyrightVersion Information
| Software | Version |
|---|---|
qiskit | 2.1.1 |
qiskit_optimization | 0.7.0 |
| System information | |
| Python version | 3.11.12 |
| OS | Darwin |
| Sat Aug 09 16:56:55 2025 JST | |
This code is a part of a Qiskit project
© Copyright IBM 2017, 2025.
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.
This entry was created automatically from publicly available records. QCR links to public sources and only stores repository content where the license permits redistribution.
Publication
doi:10.48550/arxiv.1706.01945Sahar Karimi, Pooya Ronagh
Versions
Cite all versions? Use the base QCR ID to always reference the latest version of this entry.
Join the Discussion
Comments (0)
No comments yet. Be the first to share your thoughts!