def __init__(self,
                 uncertainty_model,
                 strike_price,
                 c_approx,
                 i_state=None,
                 i_compare=None,
                 i_objective=None):
        """
        Constructor.

        Args:
            uncertainty_model (UnivariateDistribution): uncertainty model for spot price
            strike_price (float): strike price of the European option
            c_approx (float): approximation factor for linear payoff
            i_state (Optional(Union(list, numpy.ndarray))): indices of qubits
                                                            representing the uncertainty
            i_compare (Optional(int)): index of qubit for comparing spot price to strike price
                            (enabling payoff or not)
            i_objective (Optional(int)): index of qubit for objective function
        """
        super().__init__(uncertainty_model.num_target_qubits + 2)

        self._uncertainty_model = uncertainty_model
        self._strike_price = strike_price
        self._c_approx = c_approx

        if i_state is None:
            i_state = list(range(uncertainty_model.num_target_qubits))
        self.i_state = i_state
        if i_compare is None:
            i_compare = uncertainty_model.num_target_qubits
        self.i_compare = i_compare
        if i_objective is None:
            i_objective = uncertainty_model.num_target_qubits + 1
        self.i_objective = i_objective

        super().validate(locals())

        # map strike price to {0, ..., 2^n-1}
        lb = uncertainty_model.low
        ub = uncertainty_model.high
        self._mapped_strike_price = int(
            np.round((strike_price - lb) / (ub - lb) *
                     (uncertainty_model.num_values - 1)))

        # create comparator
        self._comparator = FixedValueComparator(
            uncertainty_model.num_target_qubits, self._mapped_strike_price)

        self.offset_angle_zero = np.pi / 4 * (1 - self._c_approx)
        if self._mapped_strike_price < uncertainty_model.num_values - 1:
            self.offset_angle = -1 * np.pi / 2 * self._c_approx * self._mapped_strike_price / \
                        (uncertainty_model.num_values - self._mapped_strike_price - 1)
            self.slope_angle = np.pi / 2 * self._c_approx / \
                (uncertainty_model.num_values - self._mapped_strike_price - 1)
        else:
            self.offset_angle = 0
            self.slope_angle = 0
Exemple #2
0
    def __init__(self,
                 uncertainty_model,
                 strike_price,
                 i_state=None,
                 i_objective=None):
        """
        Constructor.

        Args:
            uncertainty_model (UnivariateDistribution): uncertainty model for spot price
            strike_price (float): strike price of the European option
            c_approx (float): approximation factor for linear payoff
            i_state (list or array): indices of qubits representing the uncertainty
            i_objective (int): index of qubit for objective function
        """
        super().__init__(uncertainty_model.num_target_qubits + 1)

        self._uncertainty_model = uncertainty_model
        self._strike_price = strike_price

        if i_state is None:
            i_state = list(range(uncertainty_model.num_target_qubits))
        self.i_state = i_state
        if i_objective is None:
            i_objective = uncertainty_model.num_target_qubits
        self.i_objective = i_objective

        super().validate(locals())

        # map strike price to {0, ..., 2^n-1}
        lb = uncertainty_model.low
        ub = uncertainty_model.high
        self._mapped_strike_price = int(
            np.ceil((strike_price - lb) / (ub - lb) *
                    (uncertainty_model.num_values - 1)))

        # create comparator
        self._comparator = FixedValueComparator(
            uncertainty_model.num_target_qubits, self._mapped_strike_price)
    def __init__(self,
                 uncertainty_model: UnivariateDistribution,
                 strike_price: float,
                 i_state: Optional[Union[List[int], np.ndarray]] = None,
                 i_objective: Optional[int] = None) -> None:
        """
        Constructor.

        Args:
            uncertainty_model: uncertainty model for spot price
            strike_price: strike price of the European option
            i_state: indices of qubits representing the uncertainty
            i_objective: index of qubit for objective function
        """
        super().__init__(uncertainty_model.num_target_qubits + 1)

        self._uncertainty_model = uncertainty_model
        self._strike_price = strike_price

        if i_state is None:
            i_state = list(range(uncertainty_model.num_target_qubits))
        self.i_state = i_state
        if i_objective is None:
            i_objective = uncertainty_model.num_target_qubits
        self.i_objective = i_objective

        # map strike price to {0, ..., 2^n-1}
        lb = uncertainty_model.low
        ub = uncertainty_model.high
        self._mapped_strike_price = int(
            np.ceil((strike_price - lb) / (ub - lb) *
                    (uncertainty_model.num_values - 1)))

        # create comparator
        self._comparator = FixedValueComparator(
            uncertainty_model.num_target_qubits, self._mapped_strike_price)