Skip to content

Commit

Permalink
Switch to qiskit_algorithms (#291)
Browse files Browse the repository at this point in the history
* Switch to qiskit_algorithms

* Fix lint and black

* qiskit_algorithms 0.2.0 min, use its globals

* Update requirements.txt

Co-authored-by: Takashi Imamichi <[email protected]>

* Notebooks using Opt change to qiskit_algorithms

* Fixup qiskit-optimization links over to community or ecosystem

* Qiskit`s finance module to Qiskit Finance module

* Fix lint for long link (black was ok with it!)

---------

Co-authored-by: Takashi Imamichi <[email protected]>
  • Loading branch information
woodsp-ibm and t-imamichi authored Sep 6, 2023
1 parent ec584a1 commit 963059f
Show file tree
Hide file tree
Showing 32 changed files with 347 additions and 366 deletions.
4 changes: 2 additions & 2 deletions .github/actions/install-main-dependencies/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ runs:
- name: Get main last commit ids
run: |
echo "QISKIT_HASH=$(git ls-remote --heads https://github.com/Qiskit/qiskit.git refs/heads/main | awk '{print $1}')" >> $GITHUB_ENV
echo "OPTIMIZATION_HASH=$(git ls-remote --heads https://github.com/Qiskit/qiskit-optimization.git refs/heads/main | awk '{print $1}')" >> $GITHUB_ENV
echo "OPTIMIZATION_HASH=$(git ls-remote --heads https://github.com/qiskit-community/qiskit-optimization.git refs/heads/main | awk '{print $1}')" >> $GITHUB_ENV
shell: bash
- name: Qiskit Cache
env:
Expand Down Expand Up @@ -127,7 +127,7 @@ runs:
if [ "$build_from_main" == "true" ]; then
echo 'Create wheel file from main'
pip install -U wheel
git clone --depth 1 --branch main https://github.com/Qiskit/qiskit-optimization.git /tmp/qiskit-optimization
git clone --depth 1 --branch main https://github.com/qiskit-community/qiskit-optimization.git /tmp/qiskit-optimization
pushd /tmp/qiskit-optimization
python setup.py bdist_wheel
popd
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ evaluate a fixed income asset with uncertain interest rates.

```python
import numpy as np
from qiskit.algorithms import AmplitudeEstimation
from qiskit.primitives import Sampler
from qiskit_algorithms import AmplitudeEstimation
from qiskit_finance.circuit.library import NormalDistribution
from qiskit_finance.applications import FixedIncomePricing

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
"python": ("https://docs.python.org/3", None),
"numpy": ("https://numpy.org/doc/stable", None),
"scipy": ("https://docs.scipy.org/doc/scipy", None),
"qiskit_algorithms": ("https://qiskit.org/ecosystem/algorithms", None),
"qiskit_optimization": ("https://qiskit.org/ecosystem/optimization", None),
"qiskit": ("https://qiskit.org/documentation", None),
}
Expand Down
57 changes: 25 additions & 32 deletions docs/tutorials/00_amplitude_estimation.ipynb

Large diffs are not rendered by default.

95 changes: 43 additions & 52 deletions docs/tutorials/01_portfolio_optimization.ipynb

Large diffs are not rendered by default.

59 changes: 31 additions & 28 deletions docs/tutorials/02_portfolio_diversification.ipynb

Large diffs are not rendered by default.

46 changes: 25 additions & 21 deletions docs/tutorials/03_european_call_option_pricing.ipynb

Large diffs are not rendered by default.

38 changes: 21 additions & 17 deletions docs/tutorials/04_european_put_option_pricing.ipynb

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions docs/tutorials/05_bull_spread_pricing.ipynb

Large diffs are not rendered by default.

34 changes: 19 additions & 15 deletions docs/tutorials/06_basket_option_pricing.ipynb

Large diffs are not rendered by default.

44 changes: 26 additions & 18 deletions docs/tutorials/07_asian_barrier_spread_pricing.ipynb

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions docs/tutorials/08_fixed_income_pricing.ipynb

Large diffs are not rendered by default.

90 changes: 48 additions & 42 deletions docs/tutorials/09_credit_risk_analysis.ipynb

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions docs/tutorials/10_qgan_option_pricing.ipynb

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions qiskit_finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
===============================================
Qiskit's finance module (:mod:`qiskit_finance`)
===============================================
=============================================
Qiskit Finance module (:mod:`qiskit_finance`)
=============================================
.. currentmodule:: qiskit_finance
This is the Qiskit`s finance module. There is an initial set of function here that
will be built out over time. At present it has applications in the form of
Ising Hamiltonians and data providers which supply a source of financial data.
This is the Qiskit Finance module. It has applications based on
`Amplitude Estimation
<https://qiskit.org/ecosystem/algorithms/apidocs/qiskit_algorithms.html#amplitude-estimators>`__
and optimization using
`Qiskit Optimization <https://qiskit.org/ecosystem/optimization/>`__,
some library circuits useful for finance applications,
and data providers which supply a source of financial data.
.. autosummary::
:toctree: ../stubs/
:nosignatures:
QiskitFinanceError
In addition to standard Python errors Qiskit's finance module will raise this error if
circumstances are that it cannot proceed to completion.
In addition to standard Python errors the Qiskit Finance module will raise this error
if circumstances are that it cannot proceed to completion.
Submodules
==========
Expand Down
2 changes: 1 addition & 1 deletion qiskit_finance/applications/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
.. currentmodule:: qiskit_finance.applications
Applications for Qiskit's finance module.
Qiskit Finance ready-made applications.
Optimization Applications
=========================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""An abstract class for estimation application classes."""
from abc import ABC, abstractmethod

from qiskit.algorithms.amplitude_estimators import (
from qiskit_algorithms import (
EstimationProblem,
AmplitudeEstimatorResult,
)
Expand All @@ -27,14 +27,14 @@ class EstimationApplication(ABC):
@abstractmethod
def to_estimation_problem(self) -> EstimationProblem:
"""Convert a problem instance into a
:class:`qiskit.algorithms.EstimationProblem`
:class:`qiskit_algorithms.EstimationProblem`
"""
pass

@abstractmethod
def interpret(self, result: AmplitudeEstimatorResult) -> float:
"""Convert the calculation result of the problem
(:class:`qiskit.algorithms.AmplitudeEstimatorResult`)
(:class:`qiskit_algorithms.AmplitudeEstimatorResult`)
to the answer of the problem.
Args:
Expand Down
8 changes: 4 additions & 4 deletions qiskit_finance/applications/estimation/european_call_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Tuple

from qiskit.circuit import QuantumCircuit
from qiskit.algorithms.amplitude_estimators import (
from qiskit_algorithms import (
EstimationProblem,
AmplitudeEstimatorResult,
)
Expand Down Expand Up @@ -60,10 +60,10 @@ def __init__(

def to_estimation_problem(self) -> EstimationProblem:
"""Convert a problem instance into a
:class:`qiskit.algorithms.EstimationProblem`
:class:`qiskit_algorithms.EstimationProblem`
Returns:
The :class:`qiskit.algorithms.EstimationProblem` created
The :class:`qiskit_algorithms.EstimationProblem` created
from the European call delta problem instance.
"""
problem = EstimationProblem(
Expand All @@ -75,7 +75,7 @@ def to_estimation_problem(self) -> EstimationProblem:

def interpret(self, result: AmplitudeEstimatorResult) -> float:
"""Convert the calculation result of the problem
(:class:`qiskit.algorithms.AmplitudeEstimatorResult`)
(:class:`qiskit_algorithms.AmplitudeEstimatorResult`)
to the answer of the problem.
Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Tuple

from qiskit.circuit import QuantumCircuit
from qiskit.algorithms.amplitude_estimators import (
from qiskit_algorithms import (
EstimationProblem,
AmplitudeEstimatorResult,
)
Expand Down Expand Up @@ -65,10 +65,10 @@ def __init__(

def to_estimation_problem(self) -> EstimationProblem:
"""Convert a problem instance into a
:class:`qiskit.algorithms.EstimationProblem`
:class:`qiskit_algorithms.EstimationProblem`
Returns:
The :class:`qiskit.algorithms.EstimationProblem` created
The :class:`qiskit_algorithms.EstimationProblem` created
from the European call pricing problem instance.
"""
problem = EstimationProblem(
Expand All @@ -80,7 +80,7 @@ def to_estimation_problem(self) -> EstimationProblem:

def interpret(self, result: AmplitudeEstimatorResult) -> float:
"""Convert the calculation result of the problem
(:class:`qiskit.algorithms.AmplitudeEstimatorResult`)
(:class:`qiskit_algorithms.AmplitudeEstimatorResult`)
to the answer of the problem.
Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import numpy as np

from qiskit.circuit import QuantumCircuit
from qiskit.algorithms.amplitude_estimators import (
from qiskit_algorithms import (
EstimationProblem,
AmplitudeEstimatorResult,
)
Expand Down Expand Up @@ -83,10 +83,10 @@ def __init__(

def to_estimation_problem(self) -> EstimationProblem:
"""Convert a problem instance into a
:class:`qiskit.algorithms.EstimationProblem`
:class:`qiskit_algorithms.EstimationProblem`
Returns:
The :class:`qiskit.algorithms.EstimationProblem` created
The :class:`qiskit_algorithms.EstimationProblem` created
from the Fixed problem instance.
"""
problem = EstimationProblem(
Expand All @@ -98,7 +98,7 @@ def to_estimation_problem(self) -> EstimationProblem:

def interpret(self, result: AmplitudeEstimatorResult) -> float:
"""Convert the calculation result of the problem
(:class:`qiskit.algorithms.AmplitudeEstimatorResult`)
(:class:`qiskit_algorithms.AmplitudeEstimatorResult`)
to the answer of the problem.
Args:
Expand Down
4 changes: 2 additions & 2 deletions qiskit_finance/data_providers/_base_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import numpy as np
import fastdtw

from qiskit.utils import algorithm_globals
from qiskit_algorithms.utils import algorithm_globals
from ..exceptions import QiskitFinanceError

logger = logging.getLogger(__name__)
Expand All @@ -35,7 +35,7 @@ class StockMarket(Enum):


class BaseDataProvider(ABC):
"""The abstract base class for data_provider modules within Qiskit's finance module.
"""The abstract base class for data_provider modules within Qiskit Finance module.
To create add-on data_provider module subclass the BaseDataProvider class in this module.
Doing so requires that the required driver interface is implemented.
Expand Down
2 changes: 1 addition & 1 deletion qiskit_finance/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@


class QiskitFinanceError(QiskitError):
"""Class for errors returned by Qiskit's finance module."""
"""Class for errors returned by Qiskit Finance module."""

pass
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
qiskit>=0.44
qiskit-optimization>=0.2.0
qiskit-algorithms>=0.2.0
qiskit-optimization>=0.6.0
scipy>=1.4
numpy>=1.17
psutil>=5
Expand Down
6 changes: 0 additions & 6 deletions test/applications/test_european_call_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import unittest
from test import QiskitFinanceTestCase

from qiskit.utils import algorithm_globals
from qiskit.circuit.library import IntegerComparator
from qiskit.quantum_info import Operator
from qiskit_finance.applications.estimation import EuropeanCallDelta
Expand All @@ -25,11 +24,6 @@
class TestEuropeanCallDelta(QiskitFinanceTestCase):
"""Tests the EuropeanCallDelta application"""

def setUp(self):
super().setUp()
self.seed = 457
algorithm_globals.random_seed = self.seed

def test_to_estimation_problem(self):
"""Test the expected circuit."""
num_qubits = 3
Expand Down
6 changes: 0 additions & 6 deletions test/applications/test_european_call_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@

from qiskit.circuit.library import LinearAmplitudeFunction
from qiskit.quantum_info import Operator
from qiskit.utils import algorithm_globals
from qiskit_finance.applications.estimation import EuropeanCallPricing
from qiskit_finance.circuit.library.probability_distributions import UniformDistribution


class TestEuropeanCallPricing(QiskitFinanceTestCase):
"""Tests the EuropeanCallPricing application"""

def setUp(self):
super().setUp()
self.seed = 457
algorithm_globals.random_seed = self.seed

def test_to_estimation_problem(self):
"""Test the expected circuit."""
num_qubits = 3
Expand Down
6 changes: 0 additions & 6 deletions test/applications/test_fixed_income_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.utils import algorithm_globals
from qiskit_finance.applications.estimation import FixedIncomePricing
from qiskit_finance.circuit.library.probability_distributions import UniformDistribution
from qiskit_finance.circuit.library.payoff_functions import FixedIncomePricingObjective
Expand All @@ -28,11 +27,6 @@
class TestFixedIncomePricing(QiskitFinanceTestCase):
"""Tests the FixedIncomePricing application"""

def setUp(self):
super().setUp()
self.seed = 457
algorithm_globals.random_seed = self.seed

def test_to_estimation_problem(self):
"""Test the expected circuit."""
num_qubits = [2, 2]
Expand Down
2 changes: 0 additions & 2 deletions test/applications/test_portfolio_diversification.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from test import QiskitFinanceTestCase

import numpy as np
from qiskit.utils import algorithm_globals
from qiskit_optimization.problems import QuadraticProgram, VarType
from qiskit_finance.applications.optimization import PortfolioDiversification

Expand All @@ -30,7 +29,6 @@ class TestPortfolioDiversification(QiskitFinanceTestCase):
def setUp(self):
"""Set up for the tests"""
super().setUp()
algorithm_globals.random_seed = 100
self.n = 2
self.q = 1
self.similarity_matrix = np.ones((self.n, self.n))
Expand Down
2 changes: 0 additions & 2 deletions test/applications/test_portfolio_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from ddt import ddt, data, unpack
import numpy as np

from qiskit.utils import algorithm_globals
from qiskit_optimization.problems import QuadraticProgram
from qiskit_finance.applications.optimization import PortfolioOptimization
from qiskit_finance.exceptions import QiskitFinanceError
Expand All @@ -33,7 +32,6 @@ class TestPortfolioDiversification(QiskitFinanceTestCase):
def setUp(self):
"""Set up for the tests"""
super().setUp()
algorithm_globals.random_seed = 100
self.num_assets = 4
self.expected_returns = [0.01528439, -0.00078095, 0.00051792, 0.00087001]
self.covariances = [
Expand Down
19 changes: 5 additions & 14 deletions test/circuit/test_european_call_delta_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import numpy as np
from qiskit.circuit.library import IntegerComparator
from qiskit.quantum_info import Operator
from qiskit.utils import QuantumInstance, optionals
from qiskit.algorithms import IterativeAmplitudeEstimation, EstimationProblem
from qiskit.primitives import Sampler
from qiskit_algorithms import IterativeAmplitudeEstimation, EstimationProblem
from qiskit_finance.circuit.library import LogNormalDistribution

from qiskit_finance.circuit.library.payoff_functions import EuropeanCallDeltaObjective


Expand All @@ -46,7 +45,6 @@ def test_circuit(self):

self.assertTrue(Operator(ecd).equiv(comparator))

@unittest.skipUnless(optionals.HAS_AER, "qiskit-aer is required to run this test")
def test_application(self):
"""Test an end-to-end application."""

Expand Down Expand Up @@ -93,17 +91,10 @@ def test_application(self):
post_processing=european_call_delta.post_processing,
)

# run amplitude estimation
from qiskit_aer import Aer

q_i = QuantumInstance(
Aer.get_backend("aer_simulator"),
seed_simulator=125,
seed_transpiler=80,
)
iae = IterativeAmplitudeEstimation(epsilon_target=0.01, alpha=0.05, quantum_instance=q_i)
sampler = Sampler(options={"shots": 1024, "seed": 12})
iae = IterativeAmplitudeEstimation(epsilon_target=0.01, alpha=0.05, sampler=sampler)
result = iae.estimate(problem)
self.assertAlmostEqual(result.estimation_processed, 0.8088790606143996)
self.assertAlmostEqual(result.estimation_processed, 0.8088153392162598)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 963059f

Please sign in to comment.