def get_cdf_operator_factory(x_eval): # comparator as objective cdf_objective = Comparator(agg.num_sum_qubits, x_eval + 1, geq=False) # define overall uncertainty problem multivariate_cdf = MultivariateProblem(u, agg, cdf_objective) return multivariate_cdf
def test_fixed_value_comparator(self, num_state_qubits, value, geq): """ fixed value comparator test """ # initialize weighted sum operator factory comp = Comparator(num_state_qubits, value, geq) # initialize circuit q = QuantumRegister(num_state_qubits + 1) if comp.required_ancillas() > 0: q_a = QuantumRegister(comp.required_ancillas()) qc = QuantumCircuit(q, q_a) else: q_a = None qc = QuantumCircuit(q) # set equal superposition state qc.h(q[:num_state_qubits]) # build circuit comp.build(qc, q, q_a) # run simulation job = execute(qc, BasicAer.get_backend('statevector_simulator'), shots=1) for i, s_a in enumerate(job.result().get_statevector()): prob = np.abs(s_a)**2 if prob > 1e-6: # equal superposition self.assertEqual(True, np.isclose(1.0, prob * 2.0**num_state_qubits)) b_value = '{0:b}'.format(i).rjust(qc.width(), '0') x = int(b_value[(-num_state_qubits):], 2) comp_result = int(b_value[-num_state_qubits - 1], 2) if geq: self.assertEqual(x >= value, comp_result == 1) else: self.assertEqual(x < value, comp_result == 1)
def test_asian_barrier_spread(self): """Test the asian barrier spread model.""" try: from qiskit.aqua.circuits import WeightedSumOperator, FixedValueComparator as Comparator from qiskit.aqua.components.uncertainty_problems import ( UnivariatePiecewiseLinearObjective as PwlObjective, MultivariateProblem) from qiskit.aqua.components.uncertainty_models import MultivariateLogNormalDistribution except ImportError: import warnings warnings.warn( 'Qiskit Aqua is not installed, skipping the application test.') return # number of qubits per dimension to represent the uncertainty num_uncertainty_qubits = 2 # parameters for considered random distribution spot_price = 2.0 # initial spot price volatility = 0.4 # volatility of 40% interest_rate = 0.05 # annual interest rate of 5% time_to_maturity = 40 / 365 # 40 days to maturity # resulting parameters for log-normal distribution # pylint: disable=invalid-name mu = ((interest_rate - 0.5 * volatility**2) * time_to_maturity + np.log(spot_price)) sigma = volatility * np.sqrt(time_to_maturity) mean = np.exp(mu + sigma**2 / 2) variance = (np.exp(sigma**2) - 1) * np.exp(2 * mu + sigma**2) stddev = np.sqrt(variance) # lowest and highest value considered for the spot price; in between, # an equidistant discretization is considered. low = np.maximum(0, mean - 3 * stddev) high = mean + 3 * stddev # map to higher dimensional distribution # for simplicity assuming dimensions are independent and identically distributed) dimension = 2 num_qubits = [num_uncertainty_qubits] * dimension low = low * np.ones(dimension) high = high * np.ones(dimension) mu = mu * np.ones(dimension) cov = sigma**2 * np.eye(dimension) # construct circuit factory distribution = MultivariateLogNormalDistribution(num_qubits=num_qubits, low=low, high=high, mu=mu, cov=cov) # determine number of qubits required to represent total loss weights = [] for n in num_qubits: for i in range(n): weights += [2**i] num_sum_qubits = WeightedSumOperator.get_required_sum_qubits(weights) # create circuit factoy agg = WeightedSumOperator(sum(num_qubits), weights) # set the strike price (should be within the low and the high value of the uncertainty) strike_price_1 = 3 strike_price_2 = 4 # set the barrier threshold barrier = 2.5 # map strike prices and barrier threshold from [low, high] to {0, ..., 2^n-1} max_value = 2**num_sum_qubits - 1 low_ = low[0] high_ = high[0] mapped_strike_price_1 = (strike_price_1 - dimension*low_) / \ (high_ - low_) * (2**num_uncertainty_qubits - 1) mapped_strike_price_2 = (strike_price_2 - dimension*low_) / \ (high_ - low_) * (2**num_uncertainty_qubits - 1) mapped_barrier = (barrier - low) / (high - low) * (2**num_uncertainty_qubits - 1) conditions = [] for i in range(dimension): # target dimension of random distribution and corresponding condition conditions += [(i, Comparator(num_qubits[i], mapped_barrier[i] + 1, geq=False))] # set the approximation scaling for the payoff function c_approx = 0.25 # setup piecewise linear objective fcuntion breakpoints = [0, mapped_strike_price_1, mapped_strike_price_2] slopes = [0, 1, 0] offsets = [0, 0, mapped_strike_price_2 - mapped_strike_price_1] f_min = 0 f_max = mapped_strike_price_2 - mapped_strike_price_1 bull_spread_objective = PwlObjective(num_sum_qubits, 0, max_value, breakpoints, slopes, offsets, f_min, f_max, c_approx) # define overall multivariate problem asian_barrier_spread = MultivariateProblem(distribution, agg, bull_spread_objective, conditions=conditions) num_req_qubits = asian_barrier_spread.num_target_qubits num_req_ancillas = asian_barrier_spread.required_ancillas() qr = QuantumRegister(num_req_qubits, name='q') qr_ancilla = QuantumRegister(num_req_ancillas, name='q_a') qc = QuantumCircuit(qr, qr_ancilla) asian_barrier_spread.build(qc, qr, qr_ancilla) job = execute(qc, backend=BasicAer.get_backend('statevector_simulator')) # evaluate resulting statevector value = 0 for i, amplitude in enumerate(job.result().get_statevector()): b = ('{0:0%sb}' % asian_barrier_spread.num_target_qubits ).format(i)[-asian_barrier_spread.num_target_qubits:] prob = np.abs(amplitude)**2 if prob > 1e-4 and b[0] == '1': value += prob # all other states should have zero probability due to ancilla qubits if i > 2**num_req_qubits: break # map value to original range mapped_value = asian_barrier_spread.value_to_estimation(value) / ( 2**num_uncertainty_qubits - 1) * (high_ - low_) expected = 0.83188 self.assertAlmostEqual(mapped_value, expected, places=5)