From 4d3307966974b1137980963f8958bed6e37906d0 Mon Sep 17 00:00:00 2001 From: Yuri Han Date: Thu, 1 Jun 2023 00:00:26 +0900 Subject: [PATCH] solution to prob1 --- .../challenge-2023.04-apr.ipynb | 528 +++++++++++++++++- 1 file changed, 498 insertions(+), 30 deletions(-) diff --git a/challenge-2023.04-apr/challenge-2023.04-apr.ipynb b/challenge-2023.04-apr/challenge-2023.04-apr.ipynb index 8662b68..28ea912 100644 --- a/challenge-2023.04-apr/challenge-2023.04-apr.ipynb +++ b/challenge-2023.04-apr/challenge-2023.04-apr.ipynb @@ -82,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -109,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -120,56 +120,391 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "def Knapsack(values,weights, max_weight):\n", + "def Knapsack(values, weights, max_weight):\n", " \n", " #init a model\n", - "\n", + " mdl = Model(\"Basic Problem using maximize\")\n", + " \n", " # indicate the binary variables \n", - "\n", + " n_vars = len(values) # Number of variables\n", + " x = mdl.binary_var_list(n_vars, name=\"x\") # Using binary variables for this model\n", + " \n", " # define the objective function\n", + " obj_func = 0\n", + " for i in range(len(weights)):\n", + " obj_func = weights[i]*x[i]\n", + " # obj_func = weights[0]*x[0] + weights[1]*x[1] + weights[2]*x[2] + weights[3]*x[3] + weights[4]*x[4]\n", + " mdl.maximize(obj_func)\n", "\n", " # add the constraints\n", - " return #return model, check FromDocplex2IsingModel" + " mdl.add_constraint(obj_func <= max_weight)\n", + " \n", + " return mdl #return model, check FromDocplex2IsingModel" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
termsweightsconstantn
0[4, 5]10.0190.59
1[4, 6]20.0190.59
2[4, 7]40.0190.59
3[8, 4]60.0190.59
4[5, 6]5.0190.59
5[5, 7]10.0190.59
6[8, 5]15.0190.59
7[6, 7]20.0190.59
8[8, 6]30.0190.59
9[8, 7]60.0190.59
10[0]0.0190.59
11[1]0.0190.59
12[2]0.0190.59
13[3]0.0190.59
14[4]92.0190.59
15[5]22.5190.59
16[6]45.0190.59
17[7]90.0190.59
18[8]135.0190.59
\n", + "
" + ], + "text/plain": [ + " terms weights constant n\n", + "0 [4, 5] 10.0 190.5 9\n", + "1 [4, 6] 20.0 190.5 9\n", + "2 [4, 7] 40.0 190.5 9\n", + "3 [8, 4] 60.0 190.5 9\n", + "4 [5, 6] 5.0 190.5 9\n", + "5 [5, 7] 10.0 190.5 9\n", + "6 [8, 5] 15.0 190.5 9\n", + "7 [6, 7] 20.0 190.5 9\n", + "8 [8, 6] 30.0 190.5 9\n", + "9 [8, 7] 60.0 190.5 9\n", + "10 [0] 0.0 190.5 9\n", + "11 [1] 0.0 190.5 9\n", + "12 [2] 0.0 190.5 9\n", + "13 [3] 0.0 190.5 9\n", + "14 [4] 92.0 190.5 9\n", + "15 [5] 22.5 190.5 9\n", + "16 [6] 45.0 190.5 9\n", + "17 [7] 90.0 190.5 9\n", + "18 [8] 135.0 190.5 9" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "knapsack = Knapsack(values,weights, max_weight)\n", + "# Converting the Docplex model into its qubo representation\n", + "mdl = Knapsack(values, weights, max_weight)\n", + "qubo = FromDocplex2IsingModel(mdl)\n", "\n", - "# Ising encoding of the QUBO problem for binpacking problem\n", + "# Ising encoding of the QUBO problem\n", + "ising_encoding = qubo.ising_model \n", "\n", + "# Print in a df the ising encoding (we need to remove keys: 'problem_instance' and 'metadata')) \n", + "ising_encoding_dict = ising_encoding.asdict(exclude_keys=['problem_instance', 'metadata'])\n", + "pd.DataFrame(ising_encoding_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "// This file has been generated by DOcplex\n", + "// model name is: Copy of Copy of Basic Problem using maximize\n", + "// var contrainer section\n", + "dvar bool x[5];\n", + "dvar bool slack_C0[4];\n", + "\n", + "// single vars section\n", + "dvar bool x_0;\n", + "dvar bool x_1;\n", + "dvar bool x_2;\n", + "dvar bool x_3;\n", + "dvar bool x_4;\n", + "dvar bool slack_C0_0;\n", + "dvar bool slack_C0_1;\n", + "dvar bool slack_C0_2;\n", + "dvar bool slack_C0_3;\n", + "\n", + "minimize\n", + " - 524 x_4 - 130 slack_C0_0 - 260 slack_C0_1 - 520 slack_C0_2 - 780 slack_C0_3\n", + " [ 80 x_4^2 + 40 x_4*slack_C0_0 + 80 x_4*slack_C0_1 + 160 x_4*slack_C0_2\n", + " + 240 x_4*slack_C0_3 + 5 slack_C0_0^2 + 20 slack_C0_0*slack_C0_1\n", + " + 40 slack_C0_0*slack_C0_2 + 60 slack_C0_0*slack_C0_3 + 20 slack_C0_1^2\n", + " + 80 slack_C0_1*slack_C0_2 + 120 slack_C0_1*slack_C0_3 + 80 slack_C0_2^2\n", + " + 240 slack_C0_2*slack_C0_3 + 180 slack_C0_3^2 ] + 845;\n", + " \n", + "subject to {\n", + "\n", + "}\n" + ] + } + ], + "source": [ + "knapsack = Knapsack(values, weights, max_weight)\n", "\n", - "# Docplex encoding of the QUBO problem for binpacking problem\n", + "# Ising encoding of the QUBO problem for binpacking problem\n", + "# Converting the Docplex model of binpacking into its qubo representation\n", + "qubo_bin = FromDocplex2IsingModel(knapsack) \n", "\n", + "# Ising encoding of the QUBO problem for binpacking problem\n", + "ising_encoding_bin = qubo_bin.ising_model\n", "\n", + "# Docplex encoding of the QUBO problem for binpacking problem\n", + "mdl_qubo_docplex = qubo_bin.qubo_docplex\n", "\n", "mdl_qubo_docplex.prettyprint()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
solutions_bitstringsbitstrings_energiesprobabilities
0100011101-4.00.001661
1010011101-4.00.001661
2000011101-4.00.001661
3111111101-4.00.001661
4011111101-4.00.001661
\n", + "
" + ], + "text/plain": [ + " solutions_bitstrings bitstrings_energies probabilities\n", + "0 100011101 -4.0 0.001661\n", + "1 010011101 -4.0 0.001661\n", + "2 000011101 -4.0 0.001661\n", + "3 111111101 -4.0 0.001661\n", + "4 011111101 -4.0 0.001661" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Initilize the QAOA object\n", "qaoa = QAOA()\n", "\n", "# Set the parameters to work the QAOA algorithm\n", "# where n_shots =1024 and seed_simulator=1\n", - "\n", + "qaoa.set_backend_properties(n_shots=1024, seed_simulator=1)\n", "\n", "#p = 1, a custom type and range from 0 to pi\n", - "\n", - "qaoa.compile(ising_encoding)\n", + "rep = 1 \n", + "qaoa.set_circuit_properties(p=rep, init_type=\"custom\", variational_params_dict={\"betas\":rep*[0.01*np.pi],\"gammas\":rep*[0.01*np.pi]})\n", + "qaoa.compile(ising_encoding_bin)\n", "\n", "# Run the QAOA algorithm\n", "qaoa.optimize()\n", @@ -179,13 +514,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(-4.0,\n", + " ['000011101',\n", + " '100011101',\n", + " '010011101',\n", + " '110011101',\n", + " '001011101',\n", + " '101011101',\n", + " '011011101',\n", + " '111011101',\n", + " '000111101',\n", + " '100111101',\n", + " '010111101',\n", + " '110111101',\n", + " '001111101',\n", + " '101111101',\n", + " '011111101',\n", + " '111111101'])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# To find the correct answer using ground_state_hamiltonian\n", "# and the paremeter is a cost_hamiltonian\n", - "correct_solution = \n", + "correct_solution = ground_state_hamiltonian(qaoa.cost_hamil)\n", "correct_solution" ] }, @@ -198,9 +560,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "objective: -4.000\n", + "status: OPTIMAL_SOLUTION(2)\n", + " x_0=0\n", + " x_1=0\n", + " x_2=0\n", + " x_3=0\n", + " x_4=1\n", + " slack_C0_0=1\n", + " slack_C0_1=1\n", + " slack_C0_2=0\n", + " slack_C0_3=1\n" + ] + } + ], "source": [ "## docplex solution\n", "sol = mdl_qubo_docplex.solve()\n", @@ -225,14 +605,25 @@ "## implementation\n", "\n", "# Initilize the QAOA object and use a device\n", - "device = create_device(\"local\", 'qiskit.qasm_simulator')\n", - "\n", - "\n", + "device = create_device(\"local\", 'qiskit.aer_simulator')\n", "qaoa = QAOA(device)\n", "\n", "# Set the parameters to work the QAOA algorithm\n", "# play with the parameters values\n", + "knapsack = Knapsack(values, weights, max_weight)\n", + "\n", + "# Ising encoding of the QUBO problem for binpacking problem\n", + "# Converting the Docplex model of binpacking into its qubo representation\n", + "qubo_bin = FromDocplex2IsingModel(knapsack) \n", "\n", + "# Ising encoding of the QUBO problem for binpacking problem\n", + "ising_encoding_bin = qubo_bin.ising_model\n", + "\n", + "qaoa.set_backend_properties(n_shots=1024, seed_simulator=1)\n", + "\n", + "rep = 5\n", + "qaoa.set_circuit_properties(p=rep, init_type=\"custom\", variational_params_dict={\"betas\":rep*[0.01*np.pi],\"gammas\":rep*[0.01*np.pi]})\n", + "qaoa.compile(ising_encoding_bin)\n", "\n", "# Run the QAOA algorithm\n", "qaoa.optimize()\n", @@ -251,9 +642,84 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
solutions_bitstringsbitstrings_energiesprobabilities
0101111101-4.00.02
1100111101-4.00.01
2000111101-4.00.01
3001011101-4.00.01
4010111101-4.00.01
\n", + "
" + ], + "text/plain": [ + " solutions_bitstrings bitstrings_energies probabilities\n", + "0 101111101 -4.0 0.02\n", + "1 100111101 -4.0 0.01\n", + "2 000111101 -4.0 0.01\n", + "3 001011101 -4.0 0.01\n", + "4 010111101 -4.0 0.01" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "## implementation using a noise model using qiskit \n", "\n", @@ -271,6 +737,8 @@ "qaoa = QAOA(device)\n", "\n", "# Set the parameters to work the QAOA algorithm\n", + "rep = 1\n", + "qaoa.set_circuit_properties(p=rep, init_type=\"custom\", variational_params_dict={\"betas\":rep*[0.01*np.pi],\"gammas\":rep*[0.01*np.pi]})\n", "\n", "\n", "qaoa.compile(ising_encoding)\n", @@ -395,9 +863,9 @@ ], "metadata": { "kernelspec": { - "display_name": "mitiq-qaoa", + "display_name": "Python 3 [OpenQAOA]", "language": "python", - "name": "mitiq-qaoa" + "name": "python3_openqaoa_fozpr5" }, "language_info": { "codemirror_mode": { @@ -409,7 +877,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.9.10" } }, "nbformat": 4,