Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

BOPES Sampler algorithm for Chemistry #1352

Merged
merged 264 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
264 commits
Select commit Hold shift + click to select a range
d10f5f1
naming
Sep 21, 2020
a83e354
compute eigenvalue MinEigensolver works
Sep 21, 2020
e73d4aa
return raw results until I make the GroundStateResult
Sep 21, 2020
1b45a92
now the fermionic transformation returns a molecular ground state res…
Sep 21, 2020
1019641
added TODOs
Sep 21, 2020
65ec2cc
rename GSC files
Cryoris Sep 22, 2020
98756e2
add init to qubit_transformations
Cryoris Sep 22, 2020
945ec04
fix lint in bosonic_trafo and qubit_op_trafo
Cryoris Sep 22, 2020
cd0b613
update (MES)GSC to new structure
Cryoris Sep 22, 2020
00cf578
rm empty orbital optimization file
Cryoris Sep 22, 2020
1c6105d
Fix init
mrossinek Sep 24, 2020
5d193f6
Implement returns_groundstate and fix some lints
mrossinek Sep 24, 2020
bd65fff
Rewrite AdaptVQEGSCalc. deriving from MESGSCalc.
mrossinek Sep 24, 2020
90c5f60
Add TODO about deprecating qiskit.chemistry.algorithms module
mrossinek Sep 24, 2020
9049480
Revert "Add MinimumEigensolver interface to VQEAdapt"
mrossinek Sep 24, 2020
6876b67
Merge branch 'master' into refactor-interfaces-dev
mrossinek Sep 24, 2020
3045616
Properly deprecate VQEAdapt
mrossinek Sep 24, 2020
5b85722
Port VQEAdapt to AdaptVQE (based on a GroundStateCalculation)
mrossinek Sep 24, 2020
cc03798
Some minor corrections/updates
mrossinek Sep 24, 2020
d8b5f07
Rename AdaptVQE files
mrossinek Sep 24, 2020
c60bc56
Add optional custom_excitation_pool argument to AdaptVQE computation …
mrossinek Sep 24, 2020
dce955a
Work on FermionicTransformation and related objs
Cryoris Sep 28, 2020
80efb85
Merge branch 'refactor-interfaces-dev' of github.com:pbark/qiskit-aqu…
Cryoris Sep 28, 2020
2f80943
Merge branch 'master' into interfaces_dev
Cryoris Sep 28, 2020
d918f03
Merge branch 'interfaces_dev' into refactor-interfaces-dev
Cryoris Sep 28, 2020
c629783
Merge pull request #4 from pbark/refactor-interfaces-dev
pbark Sep 28, 2020
46b4c5d
documentation added
Sep 28, 2020
ec1d697
fixes in ground state
Sep 28, 2020
1133cb1
patch MES result to GSC result instead of returning a tuple
Cryoris Sep 29, 2020
4b881c1
Merge branch 'interfaces_dev' into ground_state_interface
Sep 29, 2020
f24c35d
Merge branch 'master' into interfaces_dev
Sep 29, 2020
ca4aa17
Merge branch 'interfaces_dev' of github.com:pbark/qiskit-aqua into in…
Sep 29, 2020
7ac5ac4
Merge branch 'interfaces_dev' into ground_state_interface
Sep 29, 2020
e3d9bf4
files fixes for GSC interface
Sep 29, 2020
03d6c30
rm utf8 headers
Cryoris Sep 29, 2020
72b2716
fix spell, lint, mypy
manoelmarques Sep 29, 2020
c631ccd
Merge branch 'master' into ground_state_interface
Sep 29, 2020
0ee83fa
Remove `custom_excitation_pool` from AdaptVQE `compute_ground_state` …
mrossinek Sep 29, 2020
66e74b8
fix mypy
manoelmarques Sep 29, 2020
1f42a6c
fix lint
manoelmarques Sep 29, 2020
cde79fc
Re-enable DeprecationWarnings after VQEAdapt has been initialized
mrossinek Sep 30, 2020
74e26ea
Fixes several minor nit-picks
mrossinek Sep 30, 2020
eb97fb7
make MESFactory an interface only
Cryoris Sep 30, 2020
346f28d
fix copyright years
Cryoris Sep 30, 2020
0e5abdd
derive from MESFactory and update docs
Cryoris Sep 30, 2020
c93c4fa
update docstrings and add molecule_info
Cryoris Sep 30, 2020
d97c224
consistent naming in ``ground(_)state``
Cryoris Sep 30, 2020
1534184
fix spell
Sep 30, 2020
0d1d523
more spell, deprecate MGSE
Sep 30, 2020
229af18
fix deprecation
Sep 30, 2020
3d0a728
more style
Sep 30, 2020
c67567a
test fixes
Sep 30, 2020
6d3d254
fixes
Sep 30, 2020
60d89d3
Merge branch 'master' into ground_state_interface
Sep 30, 2020
36d0045
[WIP] basic Result-interfaces
mrossinek Sep 30, 2020
f67079a
Remove BosonicTransformation from this PR
mrossinek Sep 30, 2020
f5b177d
AdaptVQE actually supports aux_operators
mrossinek Sep 30, 2020
71bdbd6
[wip] resolve cyclic import problems
mrossinek Sep 30, 2020
c1c9dfa
Make AdaptVQE use the FermionicGSResult
mrossinek Sep 30, 2020
1d9413a
Use FermionicGroundStateResult in MinimumEigensolverGroundStateCalcul…
mrossinek Sep 30, 2020
1f0da50
Fix mypy
mrossinek Sep 30, 2020
e751b79
Filter DeprecationWarnings in unittests
mrossinek Sep 30, 2020
85c97d0
Add TODO in AdaptVQE unittest
mrossinek Sep 30, 2020
dd6bbb3
Add missing unittest imports
mrossinek Sep 30, 2020
04ff1ff
mypy fix
adekusar-drl Sep 30, 2020
388c999
Merge branch 'ground_state_interface' of https://github.com/pbark/qis…
adekusar-drl Sep 30, 2020
3aebb4d
Merge branch 'master' into ground_state_interface
Cryoris Oct 1, 2020
5e23835
unused code
adekusar-drl Oct 1, 2020
05e10c9
Merge remote-tracking branch 'pbark/ground_state_interface' into grou…
adekusar-drl Oct 1, 2020
fee3206
Merge branch 'master' into ground_state_interface
adekusar-drl Oct 1, 2020
affac21
Create qiskit.chemistry.results module
mrossinek Oct 1, 2020
df8d717
Replace interpret() with add_context() in QubitOperatorTransformations
mrossinek Oct 1, 2020
7a8f1cd
Fix GroundStateCalculation classes to work with the results interface
mrossinek Oct 1, 2020
a07d36f
Revert faulty change in github workflow
mrossinek Oct 1, 2020
f0c0c98
Merge branch 'master' into ground_state_interface
manoelmarques Oct 2, 2020
e842598
Merge branch 'master' into ground_state_interface
manoelmarques Oct 2, 2020
3de3c90
unitests WIP first commit
Oct 5, 2020
6800911
remove deprecation suppressing from new tests
Oct 5, 2020
a5704bd
support additional aux ops and conv to dict
Cryoris Oct 6, 2020
c2cd78b
fix aux_ops=None
Cryoris Oct 6, 2020
9070c6e
lint, style
adekusar-drl Oct 6, 2020
699751b
more test
Oct 6, 2020
f82a78a
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
Oct 6, 2020
d72be75
fixes in test
Oct 6, 2020
ac6db57
fix style
manoelmarques Oct 6, 2020
250b3d9
Merge branch 'master' into ground_state_interface
manoelmarques Oct 6, 2020
4bc6404
fix mypy
manoelmarques Oct 6, 2020
b3bc18d
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
manoelmarques Oct 6, 2020
f5f1914
fix mypy
manoelmarques Oct 6, 2020
74ce025
fixes in fermionic trafo
Oct 6, 2020
9021533
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
Oct 6, 2020
39a3214
Merge branch 'master' into ground_state_interface
manoelmarques Oct 6, 2020
b727562
remove editor created redundant file
Oct 7, 2020
dc6f853
resolved tests
Oct 7, 2020
cc49925
docstring fix
Oct 7, 2020
371c7e1
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
Oct 7, 2020
37f71f8
fix error & specify type hint
Cryoris Oct 7, 2020
e9188bf
Merge branch 'ground_state_interface' into aux_operators
Cryoris Oct 7, 2020
b9da2bf
change typehint Any -> FermionicOp
Cryoris Oct 7, 2020
534de13
add missing imports
Cryoris Oct 7, 2020
37e54b3
spell
Oct 7, 2020
65fbe05
suggestions from code review
Cryoris Oct 7, 2020
e8b78f1
consistent use in .keys()
Cryoris Oct 7, 2020
5cf7702
type hints, minor improvements
adekusar-drl Oct 7, 2020
19b704a
Rethink the qiskit.chemistry.results
mrossinek Oct 7, 2020
7361c90
Remove unneeded DeprecationWarning from AdaptVQEResult
mrossinek Oct 7, 2020
a877e98
Update some docstrings
mrossinek Oct 7, 2020
a127cf3
fix lint
manoelmarques Oct 7, 2020
af6a9b8
revert back to lists of aux_ops
Cryoris Oct 7, 2020
3343861
Merge branch 'ground_state_interface' into aux_operators
Cryoris Oct 7, 2020
d08d750
Update qiskit/chemistry/ground_state_calculation/adapt_vqe.py
Cryoris Oct 8, 2020
7d5799a
apply suggestions from code review
Cryoris Oct 8, 2020
e41cd60
Merge branch 'aux_operators' of github.com:Cryoris/qiskit-aqua into a…
Cryoris Oct 8, 2020
d0f39e8
change typehint to List[float]
Cryoris Oct 8, 2020
a15326d
Merge pull request #5 from Cryoris/aux_operators
pbark Oct 8, 2020
b56b1b5
Update qiskit/chemistry/core/hamiltonian.py
Cryoris Oct 8, 2020
5a5f1be
test fixes after code review
Oct 8, 2020
bbbbbd4
test fixes after code review
Oct 8, 2020
33da63a
Merge branch 'master' into ground_state_interface
manoelmarques Oct 8, 2020
63196b5
Rename aux_values to aux_operator_eigenvalues
mrossinek Oct 8, 2020
fb916b4
Remove leftover from dict-style aux_operators
mrossinek Oct 8, 2020
e3fc416
Add an `evaluate_operators` method to the GroundStateCalculation
mrossinek Oct 8, 2020
ebae7aa
Merge branch 'master' into ground_state_interface
manoelmarques Oct 8, 2020
842ca9c
fix lint
manoelmarques Oct 8, 2020
d9f5ba1
Merge branch 'master' into ground_state_interface
manoelmarques Oct 8, 2020
783a0e4
Remove erronous TODO
mrossinek Oct 9, 2020
acdd705
Enforce a FermionicTransformation in the VQEUCCSDFactory
mrossinek Oct 9, 2020
7ab4a39
Do not enforce VQEUCCSDFactory type in AdaptVQE
mrossinek Oct 9, 2020
960ec95
Reuse the VQE object during AdaptVQE
mrossinek Oct 9, 2020
0f28076
Expose molecule_info and qubit_mapping publicly
mrossinek Oct 9, 2020
9cf0327
fix lint
manoelmarques Oct 9, 2020
26d7362
Merge branch 'master' into ground_state_interface
manoelmarques Oct 9, 2020
b7eeb75
Merge branch 'master' into ground_state_interface
manoelmarques Oct 9, 2020
13e6e42
Merge branch 'master' into ground_state_interface
manoelmarques Oct 10, 2020
1444474
fixes in tests
Oct 12, 2020
db1ad81
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
Oct 12, 2020
b0e877b
fixes in tests and revert to molecule_methods
Oct 12, 2020
26d0105
remove two duplicate tests
Oct 12, 2020
e95c509
Merge branch 'master' into ground_state_interface
manoelmarques Oct 12, 2020
97a3933
Expose some VQE arguments in the VQEUCCSDFactory
mrossinek Oct 12, 2020
4c9e903
Merge branch 'master' into ground_state_interface
manoelmarques Oct 12, 2020
b0d100d
fix spell
manoelmarques Oct 12, 2020
20b3bcf
reverted due to missing part of the test uccsd_HF
Oct 12, 2020
1e2679b
remove test EOM todos
Oct 12, 2020
6da9700
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
Oct 12, 2020
8bae6ea
lint
Oct 12, 2020
0c9b39c
more lint
Oct 12, 2020
f8233ba
more lint
Oct 12, 2020
a9698d5
Prepare EigenstateResult to handle ground and excited states
mrossinek Oct 13, 2020
8cc1d6b
Deprecate the Enums in the chemistry.core module
mrossinek Oct 13, 2020
fa7f436
Also populate eigenstates in ground state calculations
mrossinek Oct 13, 2020
1622eb8
Add factory for NumPyMinimumEigensolver
mrossinek Oct 13, 2020
7ed6508
Implement `_filter_criterion` in FermionicTransformation
mrossinek Oct 13, 2020
dfcdb7a
Extract default filter_criterion into interface
mrossinek Oct 13, 2020
634538f
Make use of default filter criterion configurable
mrossinek Oct 13, 2020
ce3e3fb
Check state's type before making at a StateFn
mrossinek Oct 13, 2020
fdfb057
Use `supports_aux_ops` in `returns_groundstate`
mrossinek Oct 13, 2020
43382e2
Replace Any type-hint where not really necessary
mrossinek Oct 13, 2020
ec268ab
Do not use legacy Operator in new interface
mrossinek Oct 13, 2020
2bce4e6
Except (Minimum)EigensolverResults in interpret()
mrossinek Oct 13, 2020
f10e41f
Fix tests to work with OperatorBase rather than WeightedPauliOperator
mrossinek Oct 13, 2020
850c799
rm WPO support in trafo, add test for aux_ops
Cryoris Oct 14, 2020
1df361d
Fix docstring
mrossinek Oct 14, 2020
946682c
Fix aux_op particle-hole conversion
mrossinek Oct 14, 2020
a9af92a
Merge branch 'master' into ground_state_interface
manoelmarques Oct 14, 2020
3ec6cf9
added reno
Oct 14, 2020
947567b
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
Oct 14, 2020
176fdfb
initial OOVQE commit
Brogis1 Oct 14, 2020
cc35b24
minor fixes
Brogis1 Oct 14, 2020
25da57e
fix style
adekusar-drl Oct 14, 2020
a4adf1b
Merge pull request #6 from pbark/gsi_oovqe
pbark Oct 14, 2020
8a1fe42
renaming
manoelmarques Oct 14, 2020
58d3581
Merge branch 'master' into ground_state_interface
manoelmarques Oct 14, 2020
6316193
Merge branch 'master' into ground_state_interface
manoelmarques Oct 14, 2020
386c435
typo in the properties
adekusar-drl Oct 14, 2020
4e435a8
fixes in OOVQE
adekusar-drl Oct 14, 2020
578333e
update reno
Oct 14, 2020
85d5594
update reno2
Oct 14, 2020
ba21131
update reno 3
Oct 14, 2020
e25fb6f
fix spell
manoelmarques Oct 14, 2020
83e952e
Merge branch 'master' into ground_state_interface
manoelmarques Oct 14, 2020
de7ff10
moving oovqe to a separate PR
Oct 14, 2020
be4d237
Merge branch 'ground_state_interface' of github.com:pbark/qiskit-aqua…
Oct 14, 2020
8a5a49a
Merge branch 'master' into ground_state_interface
Cryoris Oct 14, 2020
3ff9312
bopessampler added
Oct 14, 2020
41b1892
fix mypy
manoelmarques Oct 14, 2020
ab324a7
test bopes and potentials
Oct 14, 2020
25972e0
Update qiskit/chemistry/algorithms/ground_state_solvers/minimum_eigen…
manoelmarques Oct 14, 2020
5e0ee8b
bopes result and constants
Oct 14, 2020
a3ccb85
fix spell
manoelmarques Oct 14, 2020
bec2fae
test_extrapolatrors added
Oct 14, 2020
8b68259
Merge branch 'ground_state_interface' into bopessampler_f
Oct 14, 2020
577eb1a
linting
adekusar-drl Oct 14, 2020
ff34e6d
linting
adekusar-drl Oct 14, 2020
974e943
linting
adekusar-drl Oct 14, 2020
bd92822
linting
adekusar-drl Oct 14, 2020
3de5b79
merge and fix spell, style and some lint
manoelmarques Oct 14, 2020
64c0f1b
merge fix style
manoelmarques Oct 15, 2020
e90ecf4
fix copyright
manoelmarques Oct 15, 2020
10c480d
remove test oovqe
manoelmarques Oct 15, 2020
c7ea986
Merge branch 'bopessampler_f' of github.com:pbark/qiskit-aqua into bo…
Oct 15, 2020
219e770
remove redundant tests
Oct 15, 2020
0301065
BOPESSampler refactoring (renaming)
stefan-woerner Oct 15, 2020
302f425
Merge branch 'master' into bopessampler_f
Oct 15, 2020
102b68e
remove duplicate test
Oct 15, 2020
52ea961
clean up
Oct 15, 2020
21abc54
fix potentials
Oct 15, 2020
6775a14
added init
Oct 15, 2020
7408d88
linting
adekusar-drl Oct 15, 2020
2c03da2
linting
adekusar-drl Oct 15, 2020
ad25e45
Merge branch 'master' into bopessampler_f
Oct 15, 2020
b9956c1
fix spell
manoelmarques Oct 15, 2020
1889a54
Merge branch 'master' into bopessampler_f
manoelmarques Oct 15, 2020
7a249f9
removed unused code, more docstrings.
adekusar-drl Oct 15, 2020
0a4f31a
Merge branch 'master' into bopessampler_f
manoelmarques Oct 15, 2020
5ca21f8
Update qiskit/chemistry/algorithms/pes_samplers/potentials/harmonic_p…
adekusar-drl Oct 15, 2020
c06d342
Update qiskit/chemistry/algorithms/pes_samplers/bopes_sampler.py
adekusar-drl Oct 15, 2020
73ac78f
Update qiskit/chemistry/algorithms/pes_samplers/potentials/morse_pote…
adekusar-drl Oct 15, 2020
91205d8
converted to static
adekusar-drl Oct 15, 2020
cdced4c
Merge remote-tracking branch 'pbark/bopessampler_f' into bopessampler_f
adekusar-drl Oct 15, 2020
ebc73f4
code review fixes
adekusar-drl Oct 15, 2020
dfbbb3b
gsc -> gss
adekusar-drl Oct 15, 2020
4956f5f
added dicstrings fixed missing args
Oct 15, 2020
2cc10d2
added dicstrings fixed missing args
Oct 15, 2020
4352045
test fix
Oct 15, 2020
77e643e
one more docstring
Oct 15, 2020
2602521
fix typehints
Oct 15, 2020
32bfa28
more fix typehints
Oct 15, 2020
e6bd66d
fixed comments
davindratulsi Oct 15, 2020
f46f26f
typehint fix
Oct 15, 2020
c46f824
Merge branch 'bopessampler_f' of github.com:pbark/qiskit-aqua into bo…
Oct 15, 2020
82163bf
more typehint fix
Oct 15, 2020
9c0165c
more typehint fix 2
Oct 15, 2020
cb0dc6c
some lint
Oct 15, 2020
2c566de
fix first round of lints
Cryoris Oct 15, 2020
2cc12d9
Merge branch 'bopessampler_f' of github.com:pbark/qiskit-aqua into bo…
Cryoris Oct 15, 2020
ce81016
fix mypy
Cryoris Oct 15, 2020
4217d57
Merge branch 'master' into bopessampler_f
Oct 15, 2020
66ffdd6
lint more
Oct 15, 2020
54a8f26
Merge branch 'bopessampler_f' of github.com:pbark/qiskit-aqua into bo…
Oct 15, 2020
b875b7a
add check for masses
Oct 15, 2020
1b065fa
fix
Oct 15, 2020
f0356b4
fix docstring
Oct 15, 2020
87d6bfb
Merge branch 'master' into bopessampler_f
Oct 15, 2020
ff13176
spelling, style
adekusar-drl Oct 15, 2020
1f6770a
update bopes sampler
stefan-woerner Oct 15, 2020
ebdfee8
Merge branch 'bopessampler_f' of github.com:pbark/qiskit-aqua into bo…
stefan-woerner Oct 15, 2020
9d706d1
Merge branch 'master' into bopessampler_f
woodsp-ibm Oct 15, 2020
58f6606
Update harmonic_potential.py
stefan-woerner Oct 15, 2020
5ed33fa
Merge branch 'bopessampler_f' of github.com:pbark/qiskit-aqua into bo…
stefan-woerner Oct 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .pylintdict
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ babbush
backend
backends
barkoutsos
basedriver
bb
benchmarking
bergholm
Expand All @@ -56,6 +57,7 @@ bohr
bool
boolean
bools
bopes
bosonic
bpa
brassard
Expand Down Expand Up @@ -169,6 +171,7 @@ ee
egger
eigen
eigenenergies
eigensolution
eigensolver
eigensolvers
eigenstate
Expand Down Expand Up @@ -205,10 +208,14 @@ exponentials
exponentiated
exponentiations
expr
extrapolator
extrapolators
extrapolator's
factorizers
factr
fcidump
fcompiler
fd
fermionic
fermionicoperator
fermionicdriver
Expand Down Expand Up @@ -251,8 +258,10 @@ grinko
groundenergy
groundstates
grover
gsc
gset
gsls
gss
gto
gtol
hadamard
Expand All @@ -261,6 +270,7 @@ hamiltonian
hamiltonians
hardcoded
hartree
hartrees
hassidim
hb
hessians
Expand Down Expand Up @@ -299,6 +309,7 @@ inreg
instantiation
intelvem
interatomic
internuclear
ints
invertible
iprint
Expand Down Expand Up @@ -367,6 +378,7 @@ lnot
loglik
loglikelihood
logn
lookback
lor
lowerbound
lp
Expand Down Expand Up @@ -410,6 +422,10 @@ mohij
mohijkl
mol
moller
momentums
monomial
monomials
morse
mosca
mprev
msb
Expand Down Expand Up @@ -676,6 +692,7 @@ terra
terra's
tgt
th
thermo
tiajb
tibja
timelimit
Expand Down Expand Up @@ -755,6 +772,7 @@ workq
wrt
wvf
xatol
xdata
xixj
xopt
xs
Expand All @@ -764,6 +782,8 @@ xx
xyz
yamamoto
yao
ydata
YFinance
yfinance
yu
yy
Expand Down
18 changes: 18 additions & 0 deletions qiskit/chemistry/algorithms/pes_samplers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Potential energy surface samplers"""

from .bopes_sampler import BOPESSampler

__all__ = ['BOPESSampler'
]
195 changes: 195 additions & 0 deletions qiskit/chemistry/algorithms/pes_samplers/bopes_sampler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""The calculation of points on the Born-Oppenheimer Potential Energy Surface (BOPES)."""

import logging
from typing import Optional, List, Dict

import numpy as np
from qiskit.aqua import AquaError
from qiskit.aqua.algorithms import VQAlgorithm
from qiskit.chemistry.drivers import BaseDriver
from qiskit.chemistry.algorithms.ground_state_solvers import GroundStateSolver
from qiskit.chemistry.results.bopes_sampler_result import BOPESSamplerResult
from qiskit.chemistry.algorithms.pes_samplers.extrapolator import Extrapolator, WindowExtrapolator
from qiskit.chemistry.results import EigenstateResult

logger = logging.getLogger(__name__)


class BOPESSampler:
"""Class to evaluate the Born-Oppenheimer Potential Energy Surface (BOPES)."""

def __init__(self,
gss: GroundStateSolver,
tolerance: float = 1e-3,
bootstrap: bool = True,
num_bootstrap: Optional[int] = None,
extrapolator: Optional[Extrapolator] = None) -> None:
"""
Args:
gss: GroundStateSolver
tolerance: Tolerance desired for minimum energy.
bootstrap: Whether to warm-start the solution of variational minimum eigensolvers.
num_bootstrap: Number of previous points for extrapolation
and bootstrapping. If None and a list of extrapolators is defined,
the first two points will be used for bootstrapping.
If no extrapolator is defined and bootstrap is True,
all previous points will be used for bootstrapping.
extrapolator: Extrapolator objects that define space/window
and method to extrapolate variational parameters.

Raises:
AquaError: If ``num_boostrap`` is an integer smaller than 2, or
if ``num_boostrap`` is larger than 2 and the extrapolator is not an instance of
``WindowExtrapolator``.
"""

self._gss = gss
self._tolerance = tolerance
self._bootstrap = bootstrap
self._driver = None # type: Optional[BaseDriver]
self._points = None # type: Optional[List[float]]
self._energies = None # type: Optional[List[float]]
self._raw_results = None # type: Optional[Dict[float, EigenstateResult]]
self._points_optparams = None # type: Optional[Dict[float, List[float]]]
self._num_bootstrap = num_bootstrap
self._extrapolator = extrapolator

if self._extrapolator:
if num_bootstrap is None:
# set default number of bootstrapping points to 2
self._num_bootstrap = 2
elif num_bootstrap >= 2:
if not isinstance(self._extrapolator, WindowExtrapolator):
raise AquaError(
'If num_bootstrap >= 2 then the extrapolator must be an instance '
'of WindowExtrapolator, got {} instead'.format(self._extrapolator))
self._num_bootstrap = num_bootstrap
self._extrapolator.window = num_bootstrap # window for extrapolator
else:
raise AquaError(
'num_bootstrap must be None or an integer greater than or equal to 2')

if isinstance(self._gss.solver, VQAlgorithm):
# Save initial point passed to min_eigensolver;
# this will be used when NOT bootstrapping
self._initial_point = self._gss.solver.initial_point

def sample(self, driver: BaseDriver, points: List[float]) -> BOPESSamplerResult:
"""Run the sampler at the given points, potentially with repetitions.

Args:
driver: BaseDriver specific for the problem. The driver should be based on
a Molecule object that has perturbations to be varied.
points: The points along the degrees of freedom to evaluate.

Returns:
BOPES Sampler Result

Raises:
AquaError: if the driver does not have a molecule specified.
"""

self._driver = driver

if self._driver.molecule is None:
raise AquaError('Driver MUST be configured with a Molecule.')

# full dictionary of points
self._raw_results = self._run_points(points)
# create results dictionary with (point, energy)
self._points = list(self._raw_results.keys())
self._energies = []
for key in self._raw_results:
energy = self._raw_results[key].computed_electronic_energy + \
self._raw_results[key].nuclear_repulsion_energy
self._energies.append(energy)

result = BOPESSamplerResult(self._points, self._energies, self._raw_results)

return result

def _run_points(self, points: List[float]) -> Dict[float, EigenstateResult]:
"""Run the sampler at the given points.

Args:
points: the points along the single degree of freedom to evaluate

Returns:
The results for all points.
"""
raw_results = dict() # type: Dict[float, EigenstateResult]
if isinstance(self._gss.solver, VQAlgorithm):
self._points_optparams = dict()
self._gss.solver.initial_point = self._initial_point

# Iterate over the points
for i, point in enumerate(points):
logger.info('Point %s of %s', i + 1, len(points))
raw_result = self._run_single_point(point) # dict of results
raw_results[point] = raw_result

return raw_results

def _run_single_point(self, point: float) -> EigenstateResult:
"""Run the sampler at the given single point

Args:
point: The value of the degree of freedom to evaluate.

Returns:
Results for a single point.
"""

# update molecule geometry and thus resulting Hamiltonian based on specified point
self._driver.molecule.perturbations = [point]

# find closest previously run point and take optimal parameters
if isinstance(self._gss.solver, VQAlgorithm) and self._bootstrap:
prev_points = list(self._points_optparams.keys())
prev_params = list(self._points_optparams.values())
n_pp = len(prev_points)

# set number of points to bootstrap
if self._extrapolator is None:
n_boot = len(prev_points) # bootstrap all points
else:
n_boot = self._num_bootstrap

# Set initial params # if prev_points not empty
if prev_points:
if n_pp <= n_boot:
distances = np.array(point) - \
np.array(prev_points).reshape(n_pp, -1)
# find min 'distance' from point to previous points
min_index = np.argmin(np.linalg.norm(distances, axis=1))
# update initial point
self._gss.solver.initial_point = prev_params[min_index]
else: # extrapolate using saved parameters
opt_params = self._points_optparams
param_sets = self._extrapolator.extrapolate(points=[point],
param_dict=opt_params)
# update initial point, note param_set is a dictionary
self._gss.solver.initial_point = param_sets.get(point)

# the output is an instance of EigenstateResult
result = self._gss.solve(self._driver)

# Save optimal point to bootstrap
if isinstance(self._gss.solver, VQAlgorithm):
# at every point evaluation, the optimal params are updated
optimal_params = self._gss.solver.optimal_params
self._points_optparams[point] = optimal_params

return result
Loading