Esempio n. 1
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))
Esempio n. 2
0
    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
        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(result[key],
                                   value,
                                   places=4,
                                   msg="estimate `{}` failed".format(key))