Skip to content

Commit

Permalink
Towards a non-driver future (qiskit-community#796)
Browse files Browse the repository at this point in the history
* WIP: problem refactoring

This is a first step towards decoupling the BaseProblem and BaseDriver
interfaces. Instead of injecting drivers and transformers into a
problem, these become the output of our driver classes. In turn,
transformers now take entire problem instances as input and return them
as output.

This is an intermediate step towards the final form of the problem
classes and this commit is not working fully.

* refactor: make BaseProblem class instantiable

* refactor: integrate PropertiesContainer

* refactor: populate second_q.hamiltonians

This moves the "main operators" into the new hamiltonians module and
creates a base interface for these classes to follow.

* refactor: extract lattice constructor methods to Lattice

* refactor: remove unneeded Property subclasses

* fix: some linting

* fix: html generation

* fix: add missing hamiltonian.interpret call

* WIP: migrate driver to QCSchema

* refactor: molecule dataclass

- extracts a simple MoleculeInfo dataclass out of the original Molecule
- moves UnitsType to DistanceType in the new units module (generalizing
  the concept of enumerating units)

* fix: spelling

* fix: PySCFDriver

* fix HDF5Driver-based tests

* fix: GaussianDriver

* fix linters

* refactor transformer tests

* fix: handle optionals in unittests

* fix: GaussianDriver.from_matrix_file

* fix: README example

* fix: tutorials

* fix: slow PySCF-dependent test

* fix: revert minor change to PySCFDriver

* test: fix InitialPoint unittests

* docs: replace open TODOs in docstrings

* add reno

* refactor: use direct type hints for Hamiltonians

* fix: default to ANGSTROM unit in MoleculeInfo

* fix: do not copy LatticeModel.lattice

* fix: generalize MoleculeInfo.coords type

* fix: remove unused loggers

* refactor: enforce keyword arguments in drivers

* refactor: extract _QCSchemaData

* refactor: some QCSchema methods

* fix: update some docstrings

Co-authored-by: Steve Wood <[email protected]>

* refactor: update argument name in transformers

* refactor: extract np.asarray into reshape utility

Co-authored-by: Steve Wood <[email protected]>
  • Loading branch information
mrossinek and woodsp-ibm authored Sep 1, 2022
1 parent b39d013 commit 545d06c
Show file tree
Hide file tree
Showing 175 changed files with 5,996 additions and 9,131 deletions.
3 changes: 3 additions & 0 deletions .pylintdict
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ args
arrowheads
arrowstyle
arxiv
asarray
asdict
asparagine
aspartic
Expand Down Expand Up @@ -121,6 +122,7 @@ dimens
dimensionality
dirac
discoverable
distanceunit
distinguishability
dll
docstring
Expand Down Expand Up @@ -429,6 +431,7 @@ realise
rebranding
refactor
refactored
refactoring
regs
reinstall
repr
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ the ground-state (minimum) energy of a molecule.
```python
from qiskit_nature.settings import settings
from qiskit_nature.second_q.drivers import UnitsType
from qiskit_nature.units import DistanceUnit
from qiskit_nature.second_q.drivers import PySCFDriver
from qiskit_nature.second_q.problems import ElectronicStructureProblem

Expand All @@ -79,14 +79,14 @@ settings.dict_aux_operators = True
# package, to compute the one-body and two-body integrals in
# electronic-orbital basis, necessary to form the Fermionic operator
driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.735',
unit=UnitsType.ANGSTROM,
unit=DistanceUnit.ANGSTROM,
basis='sto3g')
problem = ElectronicStructureProblem(driver)
problem = driver.run()

# generate the second-quantized operators
main_op, _ = problem.second_q_ops()

particle_number = problem.grouped_property_transformed.get_property("ParticleNumber")
particle_number = problem.properties.particle_number

num_particles = (particle_number.num_alpha, particle_number.num_beta)
num_spin_orbitals = particle_number.num_spin_orbitals
Expand Down
19 changes: 19 additions & 0 deletions docs/apidocs/qiskit_nature.second_q.hamiltonians.lattices.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


lattices
=================================

.. automodule:: qiskit_nature.second_q.hamiltonians.lattices
:no-members:
:no-inherited-members:
:no-special-members:










108 changes: 41 additions & 67 deletions docs/tutorials/01_electronic_structure.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,17 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Couldn't find cython becke routine\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/manoel/opt/anaconda3/envs/Qiskitenv/lib/python3.8/site-packages/pyscf/lib/misc.py:46: H5pyDeprecationWarning: Using default_file_mode other than 'r' is deprecated. Pass the mode to h5py.File() instead.\n",
" h5py.get_config().default_file_mode = 'a'\n"
]
}
],
"outputs": [],
"source": [
"from qiskit_nature.second_q.drivers import UnitsType, Molecule\n",
"from qiskit_nature.second_q.drivers import (\n",
" ElectronicStructureDriverType,\n",
" ElectronicStructureMoleculeDriver,\n",
")\n",
"from qiskit_nature.units import DistanceUnit\n",
"from qiskit_nature.second_q.drivers import PySCFDriver\n",
"\n",
"molecule = Molecule(\n",
" geometry=[[\"H\", [0.0, 0.0, 0.0]], [\"H\", [0.0, 0.0, 0.735]]], charge=0, multiplicity=1\n",
")\n",
"driver = ElectronicStructureMoleculeDriver(\n",
" molecule, basis=\"sto3g\", driver_type=ElectronicStructureDriverType.PYSCF\n",
"driver = PySCFDriver(\n",
" atom=\"H 0 0 0; H 0 0 0.735\",\n",
" basis=\"sto3g\",\n",
" charge=0,\n",
" spin=0,\n",
" unit=DistanceUnit.ANGSTROM,\n",
")"
]
},
Expand Down Expand Up @@ -156,7 +138,6 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit_nature.second_q.problems import ElectronicStructureProblem\n",
"from qiskit_nature.second_q.mappers import QubitConverter\n",
"from qiskit_nature.second_q.mappers import JordanWignerMapper, ParityMapper"
]
Expand All @@ -170,25 +151,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
" +-+- * (0.18093119978423117+0j)\n",
"+ +--+ * (-0.18093119978423128+0j)\n",
"+ -++- * (-0.18093119978423128+0j)\n",
"+ -+-+ * (0.18093119978423136+0j)\n",
"+ IIIN * (-0.4718960072811401+0j)\n",
"+ IINI * (-1.2563390730032502+0j)\n",
"+ IINN * (0.4836505304710652+0j)\n",
"+ INII * (-0.4718960072811401+0j)\n",
"+ ININ * (0.6985737227320179+0j)\n",
"+ INNI * (0.6645817302552963+0j)\n",
"+ NIII * (-1.2563390730032502+0j)\n",
"+ NIIN * (0.6645817302552963+0j)\n",
"+ NINI * (0.6757101548035165+0j)\n",
"+ NNII * (0.4836505304710652+0j)\n"
"Fermionic Operator\n",
"register length=4, number terms=36\n",
" -1.2563390730032498 * ( +_0 -_0 )\n",
"+ -0.47189600728114245 * ( +_1 -_1 )\n",
"+ -1.2563390730032498 * ( +_2 -_2 )\n",
"+ -0.47189600728114245 * ( +_3 -_3 )\n",
"+ -0.33785507740175813 * ( +_0 +_0 -_0 -_0 )\n",
"+ -0. ...\n"
]
}
],
"source": [
"es_problem = ElectronicStructureProblem(driver)\n",
"es_problem = driver.run()\n",
"main_op, aux_ops = es_problem.second_q_ops()\n",
"print(main_op)"
]
Expand All @@ -209,21 +184,21 @@
"name": "stdout",
"output_type": "stream",
"text": [
"-0.8105479805373259 * IIII\n",
"- 0.2257534922240248 * ZIII\n",
"+ 0.17218393261915566 * IZII\n",
"+ 0.1209126326177663 * ZZII\n",
"- 0.2257534922240248 * IIZI\n",
"+ 0.17464343068300447 * ZIZI\n",
"+ 0.16614543256382408 * IZZI\n",
"+ 0.17218393261915566 * IIIZ\n",
"+ 0.16614543256382408 * ZIIZ\n",
"+ 0.16892753870087912 * IZIZ\n",
"+ 0.1209126326177663 * IIZZ\n",
"+ 0.04523279994605782 * XXXX\n",
"+ 0.04523279994605782 * YYXX\n",
"+ 0.04523279994605782 * XXYY\n",
"+ 0.04523279994605782 * YYYY\n"
"-0.8105479805373279 * IIII\n",
"+ 0.1721839326191554 * IIIZ\n",
"- 0.22575349222402372 * IIZI\n",
"+ 0.17218393261915543 * IZII\n",
"- 0.2257534922240237 * ZIII\n",
"+ 0.12091263261776627 * IIZZ\n",
"+ 0.16892753870087907 * IZIZ\n",
"+ 0.045232799946057826 * YYYY\n",
"+ 0.045232799946057826 * XXYY\n",
"+ 0.045232799946057826 * YYXX\n",
"+ 0.045232799946057826 * XXXX\n",
"+ 0.1661454325638241 * ZIIZ\n",
"+ 0.1661454325638241 * IZZI\n",
"+ 0.17464343068300453 * ZIZI\n",
"+ 0.12091263261776627 * ZZII\n"
]
}
],
Expand All @@ -249,11 +224,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"(-1.052373245772858+5.551115123125783e-17j) * II\n",
"+ (-0.39793742484318034+1.3877787807814457e-17j) * ZI\n",
"+ (0.3979374248431804-2.7755575615628914e-17j) * IZ\n",
"+ (-0.011280104256235449-1.3877787807814457e-17j) * ZZ\n",
"+ (0.18093119978423114-3.469446951953614e-18j) * XX\n"
"-1.0523732457728594 * II\n",
"+ 0.3979374248431778 * IZ\n",
"- 0.39793742484317923 * ZI\n",
"- 0.011280104256233658 * ZZ\n",
"+ 0.18093119978423122 * XX\n"
]
}
],
Expand Down Expand Up @@ -285,8 +260,7 @@
{
"data": {
"text/html": [
"<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td><code>qiskit-terra</code></td><td>0.19.0.dev0+105cba3</td></tr><tr><td><code>qiskit-aer</code></td><td>0.9.0</td></tr><tr><td><code>qiskit-ignis</code></td><td>0.7.0.dev0+9201ed8</td></tr><tr><td><code>qiskit-nature</code></td><td>0.2.0</td></tr><tr><td><code>qiskit-finance</code></td><td>0.3.0</td></tr><tr><td><code>qiskit-optimization</code></td><td>0.3.0</td></tr><tr><td><code>qiskit-machine-learning</code></td><td>0.3.0</td></tr><tr><th>System information</th></tr><tr><td>Python</td><td>3.8.10 (default, May 19 2021, 11:01:55) \n",
"[Clang 10.0.0 ]</td></tr><tr><td>OS</td><td>Darwin</td></tr><tr><td>CPUs</td><td>2</td></tr><tr><td>Memory (Gb)</td><td>12.0</td></tr><tr><td colspan='2'>Mon Jul 26 13:45:29 2021 EDT</td></tr></table>"
"<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td><code>qiskit-terra</code></td><td>0.22.0.dev0+82c85d8</td></tr><tr><td><code>qiskit-aer</code></td><td>0.11.0</td></tr><tr><td><code>qiskit-ibmq-provider</code></td><td>0.20.0.dev0+4f1f8c6</td></tr><tr><td><code>qiskit-nature</code></td><td>0.5.0</td></tr><tr><th>System information</th></tr><tr><td>Python version</td><td>3.9.13</td></tr><tr><td>Python compiler</td><td>GCC 12.1.1 20220507 (Red Hat 12.1.1-1)</td></tr><tr><td>Python build</td><td>main, Jun 9 2022 00:00:00</td></tr><tr><td>OS</td><td>Linux</td></tr><tr><td>CPUs</td><td>4</td></tr><tr><td>Memory (Gb)</td><td>14.845233917236328</td></tr><tr><td colspan='2'>Thu Aug 18 17:44:18 2022 CEST</td></tr></table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
Expand All @@ -298,7 +272,7 @@
{
"data": {
"text/html": [
"<div style='width: 100%; background-color:#d5d9e0;padding-left: 10px; padding-bottom: 10px; padding-right: 10px; padding-top: 5px'><h3>This code is a part of Qiskit</h3><p>&copy; Copyright IBM 2017, 2021.</p><p>This code is licensed under the Apache License, Version 2.0. You may<br>obtain a copy of this license in the LICENSE.txt file in the root directory<br> of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.<p>Any modifications or derivative works of this code must retain this<br>copyright notice, and modified files need to carry a notice indicating<br>that they have been altered from the originals.</p></div>"
"<div style='width: 100%; background-color:#d5d9e0;padding-left: 10px; padding-bottom: 10px; padding-right: 10px; padding-top: 5px'><h3>This code is a part of Qiskit</h3><p>&copy; Copyright IBM 2017, 2022.</p><p>This code is licensed under the Apache License, Version 2.0. You may<br>obtain a copy of this license in the LICENSE.txt file in the root directory<br> of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.<p>Any modifications or derivative works of this code must retain this<br>copyright notice, and modified files need to carry a notice indicating<br>that they have been altered from the originals.</p></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
Expand Down Expand Up @@ -332,7 +306,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 545d06c

Please sign in to comment.