Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to qiskit_algorithms #291

Merged
merged 10 commits into from
Sep 6, 2023
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.

44 changes: 24 additions & 20 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.

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
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
2 changes: 1 addition & 1 deletion 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 Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
qiskit-terra>=0.20.0
qiskit-algorithms>=0.2.0
qiskit-optimization>=0.2.0
woodsp-ibm marked this conversation as resolved.
Show resolved Hide resolved
scipy>=1.4
numpy>=1.17
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
22 changes: 5 additions & 17 deletions test/circuit/test_european_call_pricing_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@
from test import QiskitFinanceTestCase

import numpy as np
from qiskit.utils import algorithm_globals, QuantumInstance, optionals
from qiskit.algorithms import IterativeAmplitudeEstimation, EstimationProblem
from qiskit.circuit.library import LinearAmplitudeFunction, TwoLocal
from qiskit.quantum_info import Operator
from qiskit.primitives import Sampler
from qiskit_algorithms import IterativeAmplitudeEstimation, EstimationProblem
from qiskit_finance.circuit.library import EuropeanCallPricingObjective, NormalDistribution


class TestEuropeanCallExpectedValue(QiskitFinanceTestCase):
"""Tests EuropeanCallPricingObjective."""

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

def test_ecev_circuit(self):
"""Test the expected circuit.

Expand Down Expand Up @@ -59,7 +54,6 @@ def test_ecev_circuit(self):

self.assertTrue(Operator(ecev).equiv(linear_function))

@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 @@ -101,16 +95,10 @@ def test_application(self):
post_processing=european_call.post_processing,
)

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, 1.0364776997977694)
self.assertAlmostEqual(result.estimation_processed, 1.0341976859652098)


if __name__ == "__main__":
Expand Down
18 changes: 5 additions & 13 deletions test/circuit/test_fixed_income_pricing_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import numpy as np

from qiskit import QuantumCircuit
from qiskit.utils import QuantumInstance, optionals
from qiskit.algorithms import IterativeAmplitudeEstimation, EstimationProblem
from qiskit.quantum_info import Operator
from qiskit.primitives import Sampler
from qiskit_algorithms import IterativeAmplitudeEstimation, EstimationProblem
from qiskit_finance.circuit.library import NormalDistribution
from qiskit_finance.circuit.library.payoff_functions import FixedIncomePricingObjective

Expand Down Expand Up @@ -50,7 +50,6 @@ def test_circuit(self):

self.assertTrue(Operator(circuit).equiv(expected))

@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 @@ -87,19 +86,12 @@ def test_application(self):
post_processing=fixed_income.post_processing,
)

# run simulation
from qiskit_aer import Aer

q_i = QuantumInstance(
Aer.get_backend("aer_simulator"),
seed_simulator=2,
seed_transpiler=2,
)
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)

# compare to precomputed solution
self.assertAlmostEqual(result.estimation_processed, 2.3389012822103044)
self.assertAlmostEqual(result.estimation_processed, 2.329154511815111)


if __name__ == "__main__":
Expand Down