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)
# Set an initial state for the generator circuit init_dist = NormalDistribution(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) # construct the variational form var_form = RealAmplitudes(sum(num_qubits), entanglement=entangler_map, reps=1, initial_state=init_distribution) var_form.entanglement_blocks = 'cz' theta = ParameterVector('θ', var_form.num_parameters) var_form = var_form.assign_parameters(theta) # Set generator circuit g_circuit = UnivariateVariationalDistribution(sum(num_qubits), var_form, g_params, low=bounds[0], high=bounds[1]) g_circuit._var_form_params = theta # construct circuit factory for uncertainty model uncertainty_model = g_circuit # set the strike price (should be within the low and the high value of the uncertainty) strike_price = 2 # set the approximation scaling for the payoff function c_approx = 0.25 # construct circuit factory for payoff function european_call = EuropeanCallExpectedValue( uncertainty_model, strike_price=strike_price,