Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
nonhermitian committed Jun 8, 2023
1 parent 2e8d7c0 commit 57fe3d3
Showing 1 changed file with 54 additions and 46 deletions.
100 changes: 54 additions & 46 deletions docs/tutorials/vqe_with_estimator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"metadata": {},
"outputs": [],
"source": [
"backend = service.get_backend('ibmq_qasm_simulator')"
"backend = service.get_backend(\"ibmq_qasm_simulator\")"
]
},
{
Expand All @@ -111,10 +111,9 @@
"metadata": {},
"outputs": [],
"source": [
"hamiltonian = SparsePauliOp.from_list([('YZ', 0.3980),\n",
" ('ZI', -0.3980),\n",
" ('ZZ', -0.0113),\n",
" ('XX', 0.1810)])"
"hamiltonian = SparsePauliOp.from_list(\n",
" [(\"YZ\", 0.3980), (\"ZI\", -0.3980), (\"ZZ\", -0.0113), (\"XX\", 0.1810)]\n",
")"
]
},
{
Expand Down Expand Up @@ -150,7 +149,7 @@
],
"source": [
"ansatz = EfficientSU2(hamiltonian.num_qubits)\n",
"ansatz.draw('mpl')"
"ansatz.draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -201,21 +200,21 @@
"outputs": [],
"source": [
"def cost_func(params, ansatz, hamiltonian, estimator):\n",
" \"\"\"Return estimate of energy from estimator\n",
" \"\"\"Return estimate of energy from estimator\n",
"\n",
" Parameters:\n",
" params (ndarray): Array of ansatz parameters\n",
" ansatz (QuantumCircuit): Parameterized ansatz circuit\n",
" hamiltonian (SparsePauliOp): Operator representation of Hamiltonian\n",
" estimator (Estimator): Estimator primitive instance\n",
" Parameters:\n",
" params (ndarray): Array of ansatz parameters\n",
" ansatz (QuantumCircuit): Parameterized ansatz circuit\n",
" hamiltonian (SparsePauliOp): Operator representation of Hamiltonian\n",
" estimator (Estimator): Estimator primitive instance\n",
"\n",
" Returns:\n",
" float: Energy estimate\n",
" \"\"\"\n",
" energy = estimator.run(ansatz, hamiltonian,\n",
" parameter_values=params).result().values[0]\n",
" return energy\n",
" "
" Returns:\n",
" float: Energy estimate\n",
" \"\"\"\n",
" energy = (\n",
" estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0]\n",
" )\n",
" return energy"
]
},
{
Expand All @@ -235,7 +234,7 @@
"metadata": {},
"outputs": [],
"source": [
"x0 = 2*np.pi*np.random.random(num_params)"
"x0 = 2 * np.pi * np.random.random(num_params)"
]
},
{
Expand All @@ -254,10 +253,10 @@
"outputs": [],
"source": [
"with Session(backend=backend):\n",
" estimator = Estimator(options={'shots':int(1e4)})\n",
" res = minimize(cost_func, x0,\n",
" args=(ansatz, hamiltonian, estimator),\n",
" method='cobyla')"
" estimator = Estimator(options={\"shots\": int(1e4)})\n",
" res = minimize(\n",
" cost_func, x0, args=(ansatz, hamiltonian, estimator), method=\"cobyla\"\n",
" )"
]
},
{
Expand Down Expand Up @@ -312,10 +311,8 @@
"metadata": {},
"outputs": [],
"source": [
"callback_dict = {'prev_vector': None,\n",
" 'iters': 0,\n",
" '_total_time': 0,\n",
" '_prev_time': None}\n",
"callback_dict = {\"prev_vector\": None, \"iters\": 0, \"_total_time\": 0, \"_prev_time\": None}\n",
"\n",
"\n",
"def callback(current_vector):\n",
" \"\"\"Callback function storing previous solution vector\n",
Expand All @@ -325,26 +322,34 @@
" Values are stored in pre-defined 'callback_dict' dictionary.\n",
"\n",
" Parameters:\n",
" current_vector (ndarray): Current vector of parameters \n",
" returned by optimizer \n",
" current_vector (ndarray): Current vector of parameters\n",
" returned by optimizer\n",
" \"\"\"\n",
" # Keep track of the number of iterations\n",
" callback_dict['iters'] += 1\n",
" callback_dict[\"iters\"] += 1\n",
" # Set the prev_vector to the latest one\n",
" callback_dict['prev_vector'] = current_vector\n",
" callback_dict[\"prev_vector\"] = current_vector\n",
" # Grab the current time\n",
" current_time = time.perf_counter()\n",
" # Find the total time of the execute (after the 1st iteration)\n",
" if callback_dict['iters'] > 1:\n",
" callback_dict['_total_time'] += current_time - callback_dict['_prev_time']\n",
" if callback_dict[\"iters\"] > 1:\n",
" callback_dict[\"_total_time\"] += current_time - callback_dict[\"_prev_time\"]\n",
" # Set the previous time to the current time\n",
" callback_dict['_prev_time'] = current_time\n",
" callback_dict[\"_prev_time\"] = current_time\n",
" # Compute the average time per iteration and round it\n",
" time_str = round(callback_dict['_total_time']/(callback_dict['iters']-1), 2) \\\n",
" if callback_dict['_total_time'] else '-'\n",
" time_str = (\n",
" round(callback_dict[\"_total_time\"] / (callback_dict[\"iters\"] - 1), 2)\n",
" if callback_dict[\"_total_time\"]\n",
" else \"-\"\n",
" )\n",
" # Print to screen on single line\n",
" print(\"Iters. done: {} [Avg. time per iter: {}]\".format(callback_dict['iters'],\n",
" time_str), end='\\r', flush=True)"
" print(\n",
" \"Iters. done: {} [Avg. time per iter: {}]\".format(\n",
" callback_dict[\"iters\"], time_str\n",
" ),\n",
" end=\"\\r\",\n",
" flush=True,\n",
" )"
]
},
{
Expand All @@ -371,11 +376,14 @@
],
"source": [
"with Session(backend=backend):\n",
" estimator = Estimator(options={'shots':int(1e4)})\n",
" res = minimize(cost_func, x0,\n",
" args=(ansatz, hamiltonian, estimator),\n",
" method='cobyla',\n",
" callback=callback)"
" estimator = Estimator(options={\"shots\": int(1e4)})\n",
" res = minimize(\n",
" cost_func,\n",
" x0,\n",
" args=(ansatz, hamiltonian, estimator),\n",
" method=\"cobyla\",\n",
" callback=callback,\n",
" )"
]
},
{
Expand Down Expand Up @@ -404,7 +412,7 @@
}
],
"source": [
"all(callback_dict['prev_vector'] == res.x)"
"all(callback_dict[\"prev_vector\"] == res.x)"
]
},
{
Expand All @@ -425,7 +433,7 @@
}
],
"source": [
"callback_dict['iters'] == res.nfev"
"callback_dict[\"iters\"] == res.nfev"
]
},
{
Expand Down

0 comments on commit 57fe3d3

Please sign in to comment.