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",
+ " terms | \n",
+ " weights | \n",
+ " constant | \n",
+ " n | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " [4, 5] | \n",
+ " 10.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " [4, 6] | \n",
+ " 20.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " [4, 7] | \n",
+ " 40.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " [8, 4] | \n",
+ " 60.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " [5, 6] | \n",
+ " 5.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 5 | \n",
+ " [5, 7] | \n",
+ " 10.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 6 | \n",
+ " [8, 5] | \n",
+ " 15.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 7 | \n",
+ " [6, 7] | \n",
+ " 20.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 8 | \n",
+ " [8, 6] | \n",
+ " 30.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 9 | \n",
+ " [8, 7] | \n",
+ " 60.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 10 | \n",
+ " [0] | \n",
+ " 0.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 11 | \n",
+ " [1] | \n",
+ " 0.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 12 | \n",
+ " [2] | \n",
+ " 0.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 13 | \n",
+ " [3] | \n",
+ " 0.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 14 | \n",
+ " [4] | \n",
+ " 92.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 15 | \n",
+ " [5] | \n",
+ " 22.5 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 16 | \n",
+ " [6] | \n",
+ " 45.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 17 | \n",
+ " [7] | \n",
+ " 90.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ " 18 | \n",
+ " [8] | \n",
+ " 135.0 | \n",
+ " 190.5 | \n",
+ " 9 | \n",
+ "
\n",
+ " \n",
+ "
\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",
+ " solutions_bitstrings | \n",
+ " bitstrings_energies | \n",
+ " probabilities | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " 100011101 | \n",
+ " -4.0 | \n",
+ " 0.001661 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " 010011101 | \n",
+ " -4.0 | \n",
+ " 0.001661 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " 000011101 | \n",
+ " -4.0 | \n",
+ " 0.001661 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " 111111101 | \n",
+ " -4.0 | \n",
+ " 0.001661 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " 011111101 | \n",
+ " -4.0 | \n",
+ " 0.001661 | \n",
+ "
\n",
+ " \n",
+ "
\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",
+ " solutions_bitstrings | \n",
+ " bitstrings_energies | \n",
+ " probabilities | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " 101111101 | \n",
+ " -4.0 | \n",
+ " 0.02 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " 100111101 | \n",
+ " -4.0 | \n",
+ " 0.01 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " 000111101 | \n",
+ " -4.0 | \n",
+ " 0.01 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " 001011101 | \n",
+ " -4.0 | \n",
+ " 0.01 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " 010111101 | \n",
+ " -4.0 | \n",
+ " 0.01 | \n",
+ "
\n",
+ " \n",
+ "
\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,