-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fa8b70aa", | ||
"metadata": {}, | ||
"source": [ | ||
"# To sync or not to sync\n", | ||
"\n", | ||
"`Estimator` and `Sampler` primitives currently only allow asynchronous calls:\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b6d26f95", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"est = Estimator()\n", | ||
"job = est.run([circuit], [operator], [values])\n", | ||
"result = job.result()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c88fc5eb", | ||
"metadata": {}, | ||
"source": [ | ||
"In context of the algorithms, we discussed whether we want to follow the same interface for advanced subroutines like gradients or fidelities, as" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2d32cec8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"grad = ParameterShiftEstimatorGradient()\n", | ||
"job = grad.run([circuit], [operator], [values])\n", | ||
"result = job.result()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "40dfd676", | ||
"metadata": {}, | ||
"source": [ | ||
" Alternatively we would only allow synchronous calls as" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e9495b9a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"grad = ParameterShiftEstimatorGradient()\n", | ||
"result = grad.evaluate([circuit], [operator], [values])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4fef8a91", | ||
"metadata": {}, | ||
"source": [ | ||
"### Pro sync\n", | ||
"\n", | ||
"* easier syntax\n", | ||
"* \"where do we draw the line?\" -- if subroutines have `.run` do algorithms also need it?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0a0a12e3", | ||
"metadata": {}, | ||
"source": [ | ||
"### Pro async\n", | ||
"\n", | ||
"* consistency with other estimator and sampler\n", | ||
"* subroutines and primitives are used in the same piece of code -- different signatures look weird" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "9c6cfc4e", | ||
"metadata": {}, | ||
"source": [ | ||
"### Examples" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "385ec5be", | ||
"metadata": {}, | ||
"source": [ | ||
"#### VQE \"sync\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "963dab2e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class VQE(MinimumEigensolver):\n", | ||
" ...\n", | ||
" \n", | ||
" def compute_minimum_eigenvalue(self, operator):\n", | ||
" \n", | ||
" def energy(theta):\n", | ||
" job = self.estimator.run([self.ansatz], [operator], [theta])\n", | ||
" return job.result()\n", | ||
" \n", | ||
" def gradient(theta):\n", | ||
" return self.gradient.evaluate([self.ansatz], [operator], [theta])\n", | ||
" \n", | ||
" result = self.optimizer.minimize(energy, initial_guess, jac=gradient)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "25f430b5", | ||
"metadata": {}, | ||
"source": [ | ||
"### VQE \"all async\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c10ca846", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class VQE(MinimumEigensolver):\n", | ||
" ...\n", | ||
" \n", | ||
" def compute_minimum_eigenvalue(self, operator):\n", | ||
" \n", | ||
" def energy(theta):\n", | ||
" job = self.run([self.ansatz], [operator], [theta])\n", | ||
" return job.result()\n", | ||
" \n", | ||
" def gradient(theta):\n", | ||
" job = self.gradient.run([self.ansatz], [operator], [theta])\n", | ||
" return job.result()\n", | ||
" \n", | ||
" result = self.optimizer.minimize(energy, initial_guess, jac=gradient)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "80102684", | ||
"metadata": {}, | ||
"source": [ | ||
"\n", | ||
"\n", | ||
"#### VQD \"sync\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0d2b6b00", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class VQD(Eigensolver):\n", | ||
" ...\n", | ||
" \n", | ||
" def energy_evaluation(operator, theta, penalty):\n", | ||
" job = self.estimator.run([self.ansatz], [operator], [theta])\n", | ||
" energy = job.result()\n", | ||
" \n", | ||
" overlap = self.fidelity.evaluate([self.ansatz], [self.ansatz], [self.optimal_point], [theta])\n", | ||
" \n", | ||
" return energy + penalty * overlap" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4d592842", | ||
"metadata": {}, | ||
"source": [ | ||
"#### VQD \"all async\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d2de0598", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class VQD(Eigensolver):\n", | ||
" ...\n", | ||
" \n", | ||
" def energy_evaluation(operator, theta, penalty):\n", | ||
" jobs = [\n", | ||
" self.estimator.run([self.ansatz], [operator], [theta]),\n", | ||
" self.fidelity.run([self.ansatz], [self.ansatz], [self.optimal_point], [theta])\n", | ||
" ]\n", | ||
" energy, overlap = [job.result() for job in jobs]\n", | ||
" \n", | ||
" return energy + penalty * overlap" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |