Exemplo n.º 1
0
    def test_ecev(self):
        """ European Call Expected Value test """
        bounds = np.array([0., 7.])
        num_qubits = [3]
        entangler_map = []
        for i in range(sum(num_qubits)):
            entangler_map.append([i, int(np.mod(i + 1, sum(num_qubits)))])

        g_params = [
            0.29399714, 0.38853322, 0.9557694, 0.07245791, 6.02626428,
            0.13537225
        ]
        # Set an initial state for the generator circuit
        init_dist = NormalDistribution(int(sum(num_qubits)),
                                       mu=1.,
                                       sigma=1.,
                                       low=bounds[0],
                                       high=bounds[1])
        init_distribution = np.sqrt(init_dist.probabilities)
        init_distribution = Custom(num_qubits=sum(num_qubits),
                                   state_vector=init_distribution)
        var_form = TwoLocal(int(np.sum(num_qubits)),
                            'ry',
                            'cz',
                            reps=1,
                            initial_state=init_distribution,
                            entanglement=entangler_map)

        uncertainty_model = UnivariateVariationalDistribution(int(
            sum(num_qubits)),
                                                              var_form,
                                                              g_params,
                                                              low=bounds[0],
                                                              high=bounds[1])

        strike_price = 2
        c_approx = 0.25
        european_call = EuropeanCallExpectedValue(uncertainty_model,
                                                  strike_price=strike_price,
                                                  c_approx=c_approx)

        uncertainty_model.set_probabilities(
            QuantumInstance(BasicAer.get_backend('statevector_simulator')))

        algo = AmplitudeEstimation(5, european_call)
        result = algo.run(
            quantum_instance=BasicAer.get_backend('statevector_simulator'))
        self.assertAlmostEqual(result['estimation'], 1.2580, places=4)
        self.assertAlmostEqual(result['max_probability'], 0.8785, places=4)
Exemplo n.º 2
0
    def test_ecev(self, use_circuits):
        """ European Call Expected Value test """
        if not use_circuits:
            # ignore deprecation warnings from the deprecation of VariationalForm as input for
            # the univariate variational distribution
            warnings.filterwarnings("ignore", category=DeprecationWarning)

        bounds = np.array([0., 7.])
        num_qubits = [3]
        entangler_map = []
        for i in range(sum(num_qubits)):
            entangler_map.append([i, int(np.mod(i + 1, sum(num_qubits)))])

        g_params = [
            0.29399714, 0.38853322, 0.9557694, 0.07245791, 6.02626428,
            0.13537225
        ]
        # Set an initial state for the generator circuit
        init_dist = NormalDistribution(int(sum(num_qubits)),
                                       mu=1.,
                                       sigma=1.,
                                       low=bounds[0],
                                       high=bounds[1])
        init_distribution = np.sqrt(init_dist.probabilities)
        init_distribution = Custom(num_qubits=sum(num_qubits),
                                   state_vector=init_distribution)
        var_form = RY(int(np.sum(num_qubits)),
                      depth=1,
                      initial_state=init_distribution,
                      entangler_map=entangler_map,
                      entanglement_gate='cz')
        if use_circuits:
            theta = ParameterVector('θ', var_form.num_parameters)
            var_form = var_form.construct_circuit(theta)

        uncertainty_model = UnivariateVariationalDistribution(int(
            sum(num_qubits)),
                                                              var_form,
                                                              g_params,
                                                              low=bounds[0],
                                                              high=bounds[1])

        if use_circuits:
            uncertainty_model._var_form_params = theta

        strike_price = 2
        c_approx = 0.25
        european_call = EuropeanCallExpectedValue(uncertainty_model,
                                                  strike_price=strike_price,
                                                  c_approx=c_approx)

        uncertainty_model.set_probabilities(
            QuantumInstance(BasicAer.get_backend('statevector_simulator')))

        algo = AmplitudeEstimation(5, european_call)
        result = algo.run(
            quantum_instance=BasicAer.get_backend('statevector_simulator'))
        self.assertAlmostEqual(result['estimation'], 1.2580, places=4)
        self.assertAlmostEqual(result['max_probability'], 0.8785, places=4)

        if not use_circuits:
            warnings.filterwarnings(action="always",
                                    category=DeprecationWarning)