{ "cells": [ { "cell_type": "markdown", "id": "14a471c8-ca37-41a4-b5ca-1d849e34477b", "metadata": {}, "source": [ "# 02 - Converters for Quadratic Programs\n", "### Modified for use with Quantum Rings toolkit for Qiskit 2.x" ] }, { "cell_type": "code", "execution_count": 1, "id": "9b76902b-00bd-4071-9042-92e98f595a53", "metadata": {}, "outputs": [], "source": [ "# This code is from:\n", "# https://qiskit-community.github.io/qiskit-optimization/tutorials/02_converters_for_quadratic_programs.html\n", "\n", "# Modified for use with Quantum Rings SDK" ] }, { "cell_type": "code", "execution_count": 2, "id": "402d3603-e283-4094-aa86-52880a7736c8", "metadata": {}, "outputs": [], "source": [ "#\n", "# Setup your account\n", "# You can also save your account locally using the class method QrRuntimeService.save_account(...) and\n", "# invoke the QrRuntimeService class constructor without any arguments.\n", "#\n", "\n", "import os\n", "my_token = os.environ[\"QR_TOKEN\"]\n", "my_name = os.environ[\"QR_ACCOUNT\"]\n", "\n", "#\n", "# Set the backend of your choice, depending upon the task and your hardware configuration.\n", "# See SDK documentation for additional help.\n", "#\n", "\n", "my_backend = \"scarlet_quantum_rings\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "f0a7b4ab-ee4c-4468-ba27-a031bd13b39a", "metadata": {}, "outputs": [], "source": [ "# Import from Quantum Rings Toolkit\n", "from quantumrings.toolkit.qiskit import QrRuntimeService\n", "\n", "# Acquire Quantum Rings backend\n", "qr_services = QrRuntimeService(name = my_name, token = my_token)\n", "qr_backend = qr_services.backend(name = my_backend, precision = \"single\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "f01f7911-8fcb-4622-9e9d-8aa941f6c10a", "metadata": {}, "outputs": [], "source": [ "from qiskit_optimization import QuadraticProgram\n", "from qiskit_optimization.translators.docplex_mp import to_docplex_mp" ] }, { "cell_type": "code", "execution_count": 5, "id": "bda8f072-a304-470e-a299-532bac0253d7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: \n", "\n", "Maximize\n", " 3*x + 2*y + z\n", "\n", "Subject to\n", " Linear constraints (2)\n", " x + y + z <= 5.5 'xyz_leq'\n", " x + y + z >= 2.5 'xyz_geq'\n", "\n", " Integer variables (1)\n", " 0 <= z <= 7\n", "\n", " Binary variables (2)\n", " x y\n", "\n" ] } ], "source": [ "qp = QuadraticProgram()\n", "qp.binary_var(\"x\")\n", "qp.binary_var(\"y\")\n", "qp.integer_var(lowerbound=0, upperbound=7, name=\"z\")\n", "\n", "qp.maximize(linear={\"x\": 3, \"y\": 2, \"z\": 1})\n", "qp.linear_constraint(linear={\"x\": 1, \"y\": 1, \"z\": 1}, sense=\"LE\", rhs=5.5, name=\"xyz_leq\")\n", "qp.linear_constraint(linear={\"x\": 1, \"y\": 1, \"z\": 1}, sense=\"GE\", rhs=2.5, name=\"xyz_geq\")\n", "print(qp.prettyprint())" ] }, { "cell_type": "code", "execution_count": 6, "id": "60f5246d-9582-4f30-8503-17e64421c9be", "metadata": {}, "outputs": [], "source": [ "from qiskit_optimization.converters import InequalityToEquality" ] }, { "cell_type": "code", "execution_count": 7, "id": "6b109355-afc7-4eea-abe1-1ca8f487082d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: \n", "\n", "Maximize\n", " 3*x + 2*y + z\n", "\n", "Subject to\n", " Linear constraints (2)\n", " x + xyz_leq@int_slack + y + z == 5 'xyz_leq'\n", " x - xyz_geq@int_slack + y + z == 3 'xyz_geq'\n", "\n", " Integer variables (3)\n", " 0 <= z <= 7\n", " 0 <= xyz_leq@int_slack <= 5\n", " 0 <= xyz_geq@int_slack <= 6\n", "\n", " Binary variables (2)\n", " x y\n", "\n" ] } ], "source": [ "ineq2eq = InequalityToEquality()\n", "qp_eq = ineq2eq.convert(qp)\n", "print(qp_eq.prettyprint())" ] }, { "cell_type": "code", "execution_count": 8, "id": "d3ed6cd7-b425-4b90-a468-2218a6c2f681", "metadata": {}, "outputs": [], "source": [ "from qiskit_optimization.algorithms import CplexOptimizer\n", "\n", "cplex_optimizer = CplexOptimizer()" ] }, { "cell_type": "code", "execution_count": 9, "id": "dd82976f-e5b8-43b4-a0c0-4cd8e63ad591", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fval=8.0, x=1.0, y=1.0, z=3.0, status=SUCCESS\n" ] } ], "source": [ "result_orig = cplex_optimizer.solve(qp)\n", "print(result_orig)" ] }, { "cell_type": "code", "execution_count": 10, "id": "ccd9e8b8-dff8-4aaa-a24f-efc9ef376bab", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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\n" ] } ], "source": [ "result_eq = cplex_optimizer.solve(qp_eq)\n", "print(result_eq)" ] }, { "cell_type": "code", "execution_count": 11, "id": "904444d0-af26-416d-9959-ae0f97e0609c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "interpreting values of result_eq: [1. 1. 3.]\n", "values of result_orig: [1. 1. 3.]\n" ] } ], "source": [ "print(\"interpreting values of result_eq:\", ineq2eq.interpret(result_eq.x))\n", "print(\"values of result_orig:\", result_orig.x)" ] }, { "cell_type": "code", "execution_count": 12, "id": "c6d654d7-657e-4227-874d-703d43fdb6b6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: \n", "\n", "Maximize\n", " 3*x + 2*y + z\n", "\n", "Subject to\n", " Linear constraints (2)\n", " x + xyz_leq@int_slack + y + z == 5 'xyz_leq'\n", " x - xyz_geq@int_slack + y + z == 3 'xyz_geq'\n", "\n", " Integer variables (3)\n", " 0 <= z <= 7\n", " 0 <= xyz_leq@int_slack <= 5\n", " 0 <= xyz_geq@int_slack <= 6\n", "\n", " Binary variables (2)\n", " x y\n", "\n" ] } ], "source": [ "print(qp_eq.prettyprint())" ] }, { "cell_type": "code", "execution_count": 13, "id": "facf2aa4-da0b-4f6a-af4d-735ca02c6f77", "metadata": {}, "outputs": [], "source": [ "from qiskit_optimization.converters import IntegerToBinary" ] }, { "cell_type": "code", "execution_count": 14, "id": "2caf3011-e077-4152-abe0-a1dcd82fe614", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: \n", "\n", "Maximize\n", " 3*x + 2*y + z@0 + 2*z@1 + 4*z@2\n", "\n", "Subject to\n", " Linear constraints (2)\n", " x + xyz_leq@int_slack@0 + 2*xyz_leq@int_slack@1 + 2*xyz_leq@int_slack@2 + y\n", " + z@0 + 2*z@1 + 4*z@2 == 5 'xyz_leq'\n", " x - xyz_geq@int_slack@0 - 2*xyz_geq@int_slack@1 - 3*xyz_geq@int_slack@2 + y\n", " + z@0 + 2*z@1 + 4*z@2 == 3 'xyz_geq'\n", "\n", " Binary variables (11)\n", " x y z@0 z@1 z@2 xyz_leq@int_slack@0 xyz_leq@int_slack@1 xyz_leq@int_slack@2\n", " xyz_geq@int_slack@0 xyz_geq@int_slack@1 xyz_geq@int_slack@2\n", "\n" ] } ], "source": [ "int2bin = IntegerToBinary()\n", "qp_eq_bin = int2bin.convert(qp_eq)\n", "print(qp_eq_bin.prettyprint())" ] }, { "cell_type": "code", "execution_count": 15, "id": "90a98fc7-5a6f-43a9-9995-468766c09953", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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\n" ] } ], "source": [ "result_eq = cplex_optimizer.solve(qp_eq)\n", "print(result_eq)" ] }, { "cell_type": "code", "execution_count": 16, "id": "ab1c326f-9af2-4aeb-ba48-9a1676a9ff9e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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\n" ] } ], "source": [ "result_eq_bin = cplex_optimizer.solve(qp_eq_bin)\n", "print(result_eq_bin)" ] }, { "cell_type": "code", "execution_count": 17, "id": "79b4343a-eb71-46cd-9381-de7e28424bb7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "interpreting values of result_eq_bin: [1. 1. 3. 0. 2.]\n", "values of result_eq: [1. 1. 3. 0. 2.]\n" ] } ], "source": [ "print(\"interpreting values of result_eq_bin:\", int2bin.interpret(result_eq_bin.x))\n", "print(\"values of result_eq:\", result_eq.x)" ] }, { "cell_type": "code", "execution_count": 18, "id": "36b92dab-a2f9-469e-a791-2c90ce7375e9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: \n", "\n", "Maximize\n", " 3*x + 2*y + z@0 + 2*z@1 + 4*z@2\n", "\n", "Subject to\n", " Linear constraints (2)\n", " x + xyz_leq@int_slack@0 + 2*xyz_leq@int_slack@1 + 2*xyz_leq@int_slack@2 + y\n", " + z@0 + 2*z@1 + 4*z@2 == 5 'xyz_leq'\n", " x - xyz_geq@int_slack@0 - 2*xyz_geq@int_slack@1 - 3*xyz_geq@int_slack@2 + y\n", " + z@0 + 2*z@1 + 4*z@2 == 3 'xyz_geq'\n", "\n", " Binary variables (11)\n", " x y z@0 z@1 z@2 xyz_leq@int_slack@0 xyz_leq@int_slack@1 xyz_leq@int_slack@2\n", " xyz_geq@int_slack@0 xyz_geq@int_slack@1 xyz_geq@int_slack@2\n", "\n" ] } ], "source": [ "print(qp_eq_bin.prettyprint())" ] }, { "cell_type": "code", "execution_count": 19, "id": "db60ee2a-0057-412a-aae2-e262938f2efc", "metadata": {}, "outputs": [], "source": [ "from qiskit_optimization.converters import LinearEqualityToPenalty" ] }, { "cell_type": "code", "execution_count": 20, "id": "6bce425b-1111-42ef-96e0-5bacb42ac617", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Problem name: \n", "\n", "Maximize\n", " -26*x^2 + 26*x*xyz_geq@int_slack@0 + 52*x*xyz_geq@int_slack@1\n", " + 78*x*xyz_geq@int_slack@2 - 26*x*xyz_leq@int_slack@0\n", " - 52*x*xyz_leq@int_slack@1 - 52*x*xyz_leq@int_slack@2 - 52*x*y - 52*x*z@0\n", " - 104*x*z@1 - 208*x*z@2 - 13*xyz_geq@int_slack@0^2\n", " - 52*xyz_geq@int_slack@0*xyz_geq@int_slack@1\n", " - 78*xyz_geq@int_slack@0*xyz_geq@int_slack@2 - 52*xyz_geq@int_slack@1^2\n", " - 156*xyz_geq@int_slack@1*xyz_geq@int_slack@2 - 117*xyz_geq@int_slack@2^2\n", " - 13*xyz_leq@int_slack@0^2 - 52*xyz_leq@int_slack@0*xyz_leq@int_slack@1\n", " - 52*xyz_leq@int_slack@0*xyz_leq@int_slack@2 - 52*xyz_leq@int_slack@1^2\n", " - 104*xyz_leq@int_slack@1*xyz_leq@int_slack@2 - 52*xyz_leq@int_slack@2^2\n", " + 26*y*xyz_geq@int_slack@0 + 52*y*xyz_geq@int_slack@1\n", " + 78*y*xyz_geq@int_slack@2 - 26*y*xyz_leq@int_slack@0\n", " - 52*y*xyz_leq@int_slack@1 - 52*y*xyz_leq@int_slack@2 - 26*y^2 - 52*y*z@0\n", " - 104*y*z@1 - 208*y*z@2 + 26*z@0*xyz_geq@int_slack@0\n", " + 52*z@0*xyz_geq@int_slack@1 + 78*z@0*xyz_geq@int_slack@2\n", " - 26*z@0*xyz_leq@int_slack@0 - 52*z@0*xyz_leq@int_slack@1\n", " - 52*z@0*xyz_leq@int_slack@2 - 26*z@0^2 - 104*z@0*z@1 - 208*z@0*z@2\n", " + 52*z@1*xyz_geq@int_slack@0 + 104*z@1*xyz_geq@int_slack@1\n", " + 156*z@1*xyz_geq@int_slack@2 - 52*z@1*xyz_leq@int_slack@0\n", " - 104*z@1*xyz_leq@int_slack@1 - 104*z@1*xyz_leq@int_slack@2 - 104*z@1^2\n", " - 416*z@1*z@2 + 104*z@2*xyz_geq@int_slack@0 + 208*z@2*xyz_geq@int_slack@1\n", " + 312*z@2*xyz_geq@int_slack@2 - 104*z@2*xyz_leq@int_slack@0\n", " - 208*z@2*xyz_leq@int_slack@1 - 208*z@2*xyz_leq@int_slack@2 - 416*z@2^2\n", " + 211*x - 78*xyz_geq@int_slack@0 - 156*xyz_geq@int_slack@1\n", " - 234*xyz_geq@int_slack@2 + 130*xyz_leq@int_slack@0 + 260*xyz_leq@int_slack@1\n", " + 260*xyz_leq@int_slack@2 + 210*y + 209*z@0 + 418*z@1 + 836*z@2 - 442\n", "\n", "Subject to\n", " No constraints\n", "\n", " Binary variables (11)\n", " x y z@0 z@1 z@2 xyz_leq@int_slack@0 xyz_leq@int_slack@1 xyz_leq@int_slack@2\n", " xyz_geq@int_slack@0 xyz_geq@int_slack@1 xyz_geq@int_slack@2\n", "\n" ] } ], "source": [ "lineq2penalty = LinearEqualityToPenalty()\n", "qubo = lineq2penalty.convert(qp_eq_bin)\n", "print(qubo.prettyprint())" ] }, { "cell_type": "code", "execution_count": 21, "id": "b9eaabc2-8a5a-4575-91ce-36ea73a3616a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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\n" ] } ], "source": [ "result_eq_bin = cplex_optimizer.solve(qp_eq_bin)\n", "print(result_eq_bin)" ] }, { "cell_type": "code", "execution_count": 22, "id": "69756a70-092f-477c-b6f3-a36cf4f243a3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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\n" ] } ], "source": [ "result_qubo = cplex_optimizer.solve(qubo)\n", "print(result_qubo)" ] }, { "cell_type": "code", "execution_count": 23, "id": "6e37c5c6-dbba-4677-a957-fe3baa88a65f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "interpreting values of result_eq_bin: [1. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0.]\n", "values of result_eq_bin: [1. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0.]\n" ] } ], "source": [ "print(\"interpreting values of result_eq_bin:\", lineq2penalty.interpret(result_qubo.x))\n", "print(\"values of result_eq_bin:\", result_eq_bin.x)" ] }, { "cell_type": "code", "execution_count": 24, "id": "efa6fc69-1eda-48e0-b6aa-f798462e3a7c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "result_orig.x [1. 1. 3.]\n", "interpreting result_qubo.x [1. 1. 3.]\n" ] } ], "source": [ "print(\"result_orig.x\", result_orig.x)\n", "\n", "x = result_qubo.x\n", "for conv in [lineq2penalty, int2bin, ineq2eq]:\n", " x = conv.interpret(x)\n", "print(\"interpreting result_qubo.x\", x)" ] }, { "cell_type": "code", "execution_count": 25, "id": "39535c76-5c61-420a-87d5-019556e7d2df", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| Software | Version |
|---|---|
qiskit | 2.2.1 |
qiskit_optimization | 0.7.0 |
| System information | |
| Python version | 3.12.9 |
| OS | Windows |
| Tue Oct 21 15:14:32 2025 Mountain Daylight Time | |
| Software | Version |
|---|---|
QuantumRingsLib | 0.11.0 |
quantumrings-toolkit-qiskit | 0.2.0 |
© 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.
Modifications (c) Copyright Quantum Rings Inc, 2025
Modified from the originals
Added support for Quantum Rings toolkit for Qiskit.