Esempio n. 1
0
 def _setup_approximants(self):
     return [
         Chebyshev.interpolate(self.function,
                               self.degree,
                               domain=window,
                               *self.args) for window in self._windows
     ]
Esempio n. 2
0
    def polynomials(self) -> List[List[float]]:
        """The polynomials for the piecewise approximation.

                Returns:
                    The polynomials for the piecewise approximation.
        """
        if self.num_state_qubits is None:
            return [[]]

        # note this must be the private attribute since we handle missing breakpoints at
        # 0 and 2 ^ num_qubits here (e.g. if the function we approximate is not defined at 0
        # and the user takes that into account we just add an identity)
        num_intervals = len(self._breakpoints)

        # Calculate the polynomials
        polynomials = []
        for i in range(0, num_intervals - 1):
            # Calculate the polynomial approximating the function on the current interval
            poly = Chebyshev.interpolate(self._f_x, self._degree,
                                         domain=[self._breakpoints[i],
                                                 self._breakpoints[i + 1]])
            # Convert polynomial to the standard basis and rescale it for the rotation gates
            poly = 2 * poly.convert(kind=np.polynomial.Polynomial).coef
            # Convert to list and append
            polynomials.append(poly.tolist())

        # If the last breakpoint is < 2 ** num_qubits, add the identity polynomial
        if self._breakpoints[-1] < 2 ** self.num_state_qubits:
            polynomials = polynomials + [[2 * np.arcsin(1)]]

        # If the first breakpoint is > 0, add the identity polynomial
        if self._breakpoints[0] > 0:
            polynomials = [[2 * np.arcsin(1)]] + polynomials

        return polynomials
    def polynomials(self) -> List[List[float]]:
        """The polynomials for the piecewise approximation.

        Returns:
            The polynomials for the piecewise approximation.

        Raises:
            TypeError: If the input function is not in the correct format.
        """
        if self.num_state_qubits is None:
            return [[]]

        # note this must be the private attribute since we handle missing breakpoints at
        # 0 and 2 ^ num_qubits here (e.g. if the function we approximate is not defined at 0
        # and the user takes that into account we just add an identity)
        breakpoints = self._breakpoints
        # Need to take into account the case in which no breakpoints were provided in first place
        if breakpoints == [0]:
            breakpoints = [0, 2**self.num_state_qubits]

        num_intervals = len(breakpoints)

        # Calculate the polynomials
        polynomials = []
        for i in range(0, num_intervals - 1):
            # Calculate the polynomial approximating the function on the current interval
            try:
                # If the function is constant don't call Chebyshev (not necessary and gives errors)
                if isinstance(self.f_x, (float, int)):
                    # Append directly to list of polynomials
                    polynomials.append([self.f_x])
                else:
                    poly = Chebyshev.interpolate(
                        self.f_x, self.degree, domain=[breakpoints[i], breakpoints[i + 1]]
                    )
                    # Convert polynomial to the standard basis and rescale it for the rotation gates
                    poly = 2 * poly.convert(kind=np.polynomial.Polynomial).coef
                    # Convert to list and append
                    polynomials.append(poly.tolist())
            except ValueError as err:
                raise TypeError(
                    " <lambda>() missing 1 required positional argument: '"
                    + self.f_x.__code__.co_varnames[0]
                    + "'."
                    + " Constant functions should be specified as 'f_x = constant'."
                ) from err

        # If the last breakpoint is < 2 ** num_qubits, add the identity polynomial
        if breakpoints[-1] < 2**self.num_state_qubits:
            polynomials = polynomials + [[2 * np.arcsin(1)]]

        # If the first breakpoint is > 0, add the identity polynomial
        if breakpoints[0] > 0:
            polynomials = [[2 * np.arcsin(1)]] + polynomials

        return polynomials
def setup_numpy():
    return Chebyshev.interpolate(np.sin, deg=10, domain=(0, 1))
 def nextIter(self):
     VNext = T.interpolate(lambda W: -self.negValue(W),
                           deg=self.order,
                           domain=[self.WMin, self.WMax])
     return VNext
 def guessSln(self):
     self.V = T.interpolate(lambda W: self.u(W + self.y),
                            deg=self.order,
                            domain=[self.WMin, self.WMax])
 def findPolicy(self):
     self.policy = T.interpolate(lambda W: W + self.y -
                                 (self.findOptW1(W) / self.R),
                                 deg=self.order,
                                 domain=[self.WMin, self.WMax])