Esempio n. 1
0
    def test_readme_sample(self):
        """ readme sample test """
        # pylint: disable=import-outside-toplevel

        # --- Exact copy of sample code ----------------------------------------

        import numpy as np
        from qiskit import BasicAer
        from qiskit.aqua.algorithms.single_sample.amplitude_estimation.ae import AmplitudeEstimation
        from qiskit.aqua.components.uncertainty_models import MultivariateNormalDistribution
        from qiskit.finance.components.uncertainty_problems import FixedIncomeExpectedValue

        # Create a suitable multivariate distribution
        mvnd = MultivariateNormalDistribution(num_qubits=[2, 2],
                                              low=[0, 0], high=[0.12, 0.24],
                                              mu=[0.12, 0.24], sigma=0.01 * np.eye(2))

        # Create fixed income component
        fixed_income = FixedIncomeExpectedValue(mvnd, np.eye(2), np.zeros(2),
                                                cash_flow=[1.0, 2.0], c_approx=0.125)

        # Set number of evaluation qubits (samples)
        num_eval_qubits = 5

        # Construct and run amplitude estimation
        algo = AmplitudeEstimation(num_eval_qubits, fixed_income)
        result = algo.run(BasicAer.get_backend('statevector_simulator'))

        print('Estimated value:\t%.4f' % result['estimation'])
        print('Probability:    \t%.4f' % result['max_probability'])

        # ----------------------------------------------------------------------

        self.assertAlmostEqual(result['estimation'], 2.46, places=4)
        self.assertAlmostEqual(result['max_probability'], 0.8487, places=4)
    def test_expected_value(self, simulator, a_e, expect):
        """ expected value test """
        # can be used in case a principal component analysis
        # has been done to derive the uncertainty model, ignored in this example.
        a_n = np.eye(2)
        b = np.zeros(2)

        # specify the number of qubits that are used to represent
        # the different dimensions of the uncertainty model
        num_qubits = [2, 2]

        # specify the lower and upper bounds for the different dimension
        low = [0, 0]
        high = [0.12, 0.24]
        m_u = [0.12, 0.24]
        sigma = 0.01 * np.eye(2)

        # construct corresponding distribution
        mund = MultivariateNormalDistribution(num_qubits, low, high, m_u,
                                              sigma)

        # specify cash flow
        c_f = [1.0, 2.0]

        # specify approximation factor
        c_approx = 0.125

        # get fixed income circuit appfactory
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=DeprecationWarning)
            fixed_income = FixedIncomeExpectedValue(mund, a_n, b, c_f,
                                                    c_approx)
            a_e.a_factory = fixed_income

        # run simulation
        result = a_e.run(self._qasm if simulator ==
                         'qasm' else self._statevector)

        # compare to precomputed solution
        for key, value in expect.items():
            self.assertAlmostEqual(getattr(result, key),
                                   value,
                                   places=4,
                                   msg="estimate `{}` failed".format(key))
Esempio n. 3
0
    def test_expected_value(self, simulator):

        # can be used in case a principal component analysis has been done to derive the uncertainty model, ignored in this example.
        A = np.eye(2)
        b = np.zeros(2)

        # specify the number of qubits that are used to represent the different dimenions of the uncertainty model
        num_qubits = [2, 2]

        # specify the lower and upper bounds for the different dimension
        low = [0, 0]
        high = [0.12, 0.24]
        mu = [0.12, 0.24]
        sigma = 0.01 * np.eye(2)

        # construct corresponding distribution
        u = MultivariateNormalDistribution(num_qubits, low, high, mu, sigma)

        # specify cash flow
        cf = [1.0, 2.0]

        # specify approximation factor
        c_approx = 0.125

        # get fixed income circuit appfactory
        fixed_income = FixedIncomeExpectedValue(u, A, b, cf, c_approx)

        # set number of evaluation qubits (samples)
        m = 5

        # construct amplitude estimation
        ae = AmplitudeEstimation(m, fixed_income)

        # run simulation
        quantum_instance = QuantumInstance(
            BasicAer.get_backend('statevector_simulator'),
            circuit_caching=False)
        result = ae.run(quantum_instance=quantum_instance)

        # compare to precomputed solution
        self.assertEqual(0.0,
                         np.round(result['estimation'] - 2.4600, decimals=4))