Skip to content

Commit 498d72f

Browse files
committed
Remove declarative api from chemistry
1 parent 4a0161c commit 498d72f

File tree

7 files changed

+283
-674
lines changed

7 files changed

+283
-674
lines changed

chemistry/dictinput.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
# limitations under the License.
1616
# =============================================================================
1717

18-
import qiskit.chemistry
18+
from qiskit.aqua.algorithms import ExactEigensolver
19+
from qiskit.chemistry.drivers import PySCFDriver, UnitsType
20+
from qiskit.chemistry.core import Hamiltonian
21+
1922

2023
# An example of using a loop to vary inter-atomic distance. A dictionary is
2124
# created outside the loop, but inside the loop the 'atom' value is updated
@@ -26,15 +29,15 @@
2629
# substituted by format(). Note the negative sign preceding the first format
2730
# substitution point i.e. the {} brackets
2831
#
29-
input_dict = {
30-
'driver': {'name': 'PYSCF'},
31-
'PYSCF': {'atom': None, 'unit': 'Angstrom', 'charge': 0, 'spin': 0, 'basis': 'sto3g'},
32-
'algorithm': {'name': 'ExactEigensolver'},
33-
}
32+
3433
molecule = 'H .0 .0 -{0}; H .0 .0 {0}'
3534
for i in range(21):
3635
d = (0.5 + i * 0.5 / 20) / 2
37-
input_dict['PYSCF']['atom'] = molecule.format(d)
38-
solver = qiskit.chemistry.QiskitChemistry()
39-
result = solver.run(input_dict)
36+
driver = PySCFDriver(molecule.format(d), unit=UnitsType.ANGSTROM,
37+
charge=0, spin=0, basis='sto3g')
38+
qmolecule = driver.run()
39+
operator = Hamiltonian()
40+
qubit_op, aux_ops = operator.run(qmolecule)
41+
result = ExactEigensolver(qubit_op).run()
42+
_, result = operator.process_algorithm_result(result)
4043
print('{:.4f} : {}'.format(d * 2, result['energy']))

chemistry/h2_particle_hole.ipynb

Lines changed: 89 additions & 72 deletions
Large diffs are not rendered by default.

chemistry/h2_qpe.ipynb

Lines changed: 15 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": [
77
"## _*H2 ground state energy computation using Quantum Phase Estimation*_\n",
88
"\n",
9-
"This notebook demonstrates using Qiskit Chemistry to compute ground state energy of the Hydrogen (H2) molecule using QPE (Quantum Phase Estimation) algorithm. Let's first look at how to carry out such computation programmatically. Afterwards, we will illustrate how the computation can also be carried out using json configuration dictionaries.\n",
9+
"This notebook demonstrates using Qiskit Chemistry to compute ground state energy of the Hydrogen (H2) molecule using QPE (Quantum Phase Estimation) algorithm. Let's look at how to carry out such computation programmatically.\n",
1010
"\n",
1111
"This notebook has been written to use the PYSCF chemistry driver."
1212
]
@@ -22,7 +22,16 @@
2222
"cell_type": "code",
2323
"execution_count": 1,
2424
"metadata": {},
25-
"outputs": [],
25+
"outputs": [
26+
{
27+
"name": "stdout",
28+
"output_type": "stream",
29+
"text": [
30+
"Couldn't find cython int routine\n",
31+
"Couldn't find cython int routine\n"
32+
]
33+
}
34+
],
2635
"source": [
2736
"from collections import OrderedDict\n",
2837
"import time\n",
@@ -37,7 +46,6 @@
3746
"from qiskit.aqua.algorithms import QPE\n",
3847
"from qiskit.aqua.components.iqfts import Standard\n",
3948
"from qiskit.chemistry import FermionicOperator\n",
40-
"from qiskit.chemistry import QiskitChemistry\n",
4149
"from qiskit.chemistry.components.initial_states import HartreeFock\n",
4250
"from qiskit.chemistry.drivers import PySCFDriver, UnitsType\n",
4351
"\n",
@@ -67,7 +75,7 @@
6775
"name": "stdout",
6876
"output_type": "stream",
6977
"text": [
70-
"The exact ground state energy is: -1.8572750302023802\n"
78+
"The exact ground state energy is: -1.857275030202377\n"
7179
]
7280
}
7381
],
@@ -94,7 +102,7 @@
94102
"name": "stdout",
95103
"output_type": "stream",
96104
"text": [
97-
"The ground state energy as computed by QPE is: -1.8571368753258861\n"
105+
"The ground state energy as computed by QPE is: -1.8571368753258857\n"
98106
]
99107
}
100108
],
@@ -123,91 +131,8 @@
123131
"cell_type": "markdown",
124132
"metadata": {},
125133
"source": [
126-
"As can be easily seen, the QPE computed energy is quite close to the groundtruth value we computed earlier.\n",
127-
"\n",
128-
"Next we demonstrate how the same computation can be carried out using json dictionaries to drive the qiskit.chemistry stack. Such a dictionary can of course also be manipulated programmatically. An sibling notebook `h2_iqpe` is also provided, which showcases how the ground state energies over a range of inter-atomic distances can be computed and then plotted as well."
129-
]
130-
},
131-
{
132-
"cell_type": "code",
133-
"execution_count": 4,
134-
"metadata": {},
135-
"outputs": [],
136-
"source": [
137-
"molecule = 'H .0 .0 0; H .0 .0 {}'.format(distance)\n",
138-
"\n",
139-
"# Input dictionary to configure Qiskit Chemistry for the chemistry problem.\n",
140-
"qiskit_chemistry_qpe_dict = {\n",
141-
" 'driver': {'name': 'PYSCF'},\n",
142-
" 'PYSCF': {\n",
143-
" 'atom': molecule, \n",
144-
" 'basis': 'sto3g'\n",
145-
" },\n",
146-
" 'operator': {'name': 'hamiltonian', 'transformation': 'full', 'qubit_mapping': 'parity'},\n",
147-
" 'algorithm': {\n",
148-
" 'name': 'QPE',\n",
149-
" 'num_ancillae': 9,\n",
150-
" 'num_time_slices': 1,\n",
151-
" 'expansion_mode': 'suzuki',\n",
152-
" 'expansion_order': 2,\n",
153-
" },\n",
154-
" 'initial_state': {'name': 'HartreeFock'},\n",
155-
" 'backend': {'shots': 100}\n",
156-
"}\n",
157-
"\n",
158-
"qiskit_chemistry_ees_dict = {\n",
159-
" 'driver': {'name': 'PYSCF'},\n",
160-
" 'PYSCF': {'atom': molecule, 'basis': 'sto3g'},\n",
161-
" 'operator': {'name': 'hamiltonian', 'transformation': 'full', 'qubit_mapping': 'parity'},\n",
162-
" 'algorithm': {\n",
163-
" 'name': 'ExactEigensolver',\n",
164-
" }\n",
165-
"}"
166-
]
167-
},
168-
{
169-
"cell_type": "markdown",
170-
"metadata": {},
171-
"source": [
172-
"With the two algorithms configured, we can then run them and check the results, as follows."
134+
"As can be easily seen, the QPE computed energy is quite close to the groundtruth value we computed earlier."
173135
]
174-
},
175-
{
176-
"cell_type": "code",
177-
"execution_count": 5,
178-
"metadata": {},
179-
"outputs": [
180-
{
181-
"name": "stdout",
182-
"output_type": "stream",
183-
"text": [
184-
"The groundtruth total ground state energy is -1.8572750302023793.\n",
185-
"The total ground state energy as computed by QPE is -1.8571368753258861.\n",
186-
"In comparison, the Hartree-Fock ground state energy is -1.8369679912029846.\n"
187-
]
188-
}
189-
],
190-
"source": [
191-
"result_qpe = QiskitChemistry().run(qiskit_chemistry_qpe_dict, backend=backend)\n",
192-
"result_ees = QiskitChemistry().run(qiskit_chemistry_ees_dict)\n",
193-
"\n",
194-
"print('The groundtruth total ground state energy is {}.'.format(\n",
195-
" result_ees['energy'] - result_ees['nuclear_repulsion_energy']\n",
196-
"))\n",
197-
"print('The total ground state energy as computed by QPE is {}.'.format(\n",
198-
" result_qpe['energy'] - result_qpe['nuclear_repulsion_energy']\n",
199-
"))\n",
200-
"print('In comparison, the Hartree-Fock ground state energy is {}.'.format(\n",
201-
" result_ees['hf_energy'] - result_ees['nuclear_repulsion_energy']\n",
202-
"))"
203-
]
204-
},
205-
{
206-
"cell_type": "code",
207-
"execution_count": null,
208-
"metadata": {},
209-
"outputs": [],
210-
"source": []
211136
}
212137
],
213138
"metadata": {
@@ -226,7 +151,7 @@
226151
"name": "python",
227152
"nbconvert_exporter": "python",
228153
"pygments_lexer": "ipython3",
229-
"version": "3.6.8"
154+
"version": "3.7.4"
230155
}
231156
},
232157
"nbformat": 4,

chemistry/h2_swaprz.ipynb

Lines changed: 66 additions & 50 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)