Пример #1
0
    def __init__(self, *args, **kwargs):
        """Positional and keyword arguments cannot be mixed.

        Args:
            *args: Interpreted as ``(start, )`` or ``(start, stop[, step])``
            **kwargs: Expected to contain ``start``, ``stop`` and ``step``
        Raises:
            TypeError: If positional and keyword arguments are mixed
            KeyError: If keyword arguments but one of ``start``, ``stop`` or ``step`` is missing
        """
        if args and kwargs:
            raise TypeError(
                'ParametrizedRange only takes either positional or keyword arguments'
            )
        elif kwargs:
            start = kwargs['start']
            stop = kwargs['stop']
            step = kwargs['step']
        elif len(args) in (1, 2, 3):
            if len(args) == 3:
                start, stop, step = args
            elif len(args) == 2:
                (start, stop), step = args, 1
            elif len(args) == 1:
                start, (stop, ), step = 0, args, 1
        else:
            raise TypeError(
                'ParametrizedRange expected 1 to 3 arguments, got {}'.format(
                    len(args)))

        self.start = ExpressionScalar.make(start)
        self.stop = ExpressionScalar.make(stop)
        self.step = ExpressionScalar.make(step)
Пример #2
0
    def test_make(self):
        self.assertTrue(Expression.make('a') == 'a')
        self.assertTrue(Expression.make('a + b') == 'a + b')
        self.assertTrue(Expression.make(9) == 9)

        self.assertIsInstance(Expression.make([1, 'a']), ExpressionVector)

        self.assertIsInstance(ExpressionScalar.make('a'), ExpressionScalar)
        self.assertIsInstance(ExpressionVector.make(['a']), ExpressionVector)
Пример #3
0
    def __new__(cls, t: ValueInInit, v: ValueInInit, interp: Optional[Union[str, InterpolationStrategy]]='default'):
        if interp in TablePulseTemplate.interpolation_strategies:
            interp = TablePulseTemplate.interpolation_strategies[interp]
        if interp is not None and not isinstance(interp, InterpolationStrategy):
            raise KeyError(interp, 'is not a valid interpolation strategy')

        return super().__new__(cls, ExpressionScalar.make(t),
                                    Expression.make(v),
                                    interp)
Пример #4
0
    def __init__(self,
                 expression: Union[str, ExpressionScalar],
                 duration_expression: Union[str, ExpressionScalar],
                 channel: ChannelID = 'default',
                 identifier: Optional[str] = None,
                 *,
                 measurements: Optional[List[MeasurementDeclaration]] = None,
                 parameter_constraints: Optional[List[Union[
                     str, ParameterConstraint]]] = None,
                 registry: PulseRegistryType = None) -> None:
        """Creates a new FunctionPulseTemplate object.

        Args:
            expression: The function represented by this FunctionPulseTemplate
                as a mathematical expression where 't' denotes the time variable and other variables
                will be parameters of the pulse.
            duration_expression: A mathematical expression which reliably
                computes the duration of an instantiation of this FunctionPulseTemplate from
                provided parameter values.
            channel: The channel this pulse template is defined on.
            identifier: A unique identifier for use in serialization.
            measurements: A list of measurement declarations forwarded to the
                :class:`~qupulse.pulses.measurement.MeasurementDefiner` superclass
            parameter_constraints: A list of parameter constraints forwarded to the
                :class:`~qupulse.pulses.measurement.ParameterConstrainer` superclass
        """
        AtomicPulseTemplate.__init__(self,
                                     identifier=identifier,
                                     measurements=measurements)
        ParameterConstrainer.__init__(
            self, parameter_constraints=parameter_constraints)

        self.__expression = ExpressionScalar.make(expression)
        self.__duration_expression = ExpressionScalar.make(duration_expression)
        self.__parameter_names = {
            *self.__duration_expression.variables, *self.__expression.variables
        } - {'t'}
        self.__channel = channel

        self._register(registry=registry)
Пример #5
0
    def __init__(self,
                 body: PulseTemplate,
                 repetition_count: Union[int, str, ExpressionScalar],
                 identifier: Optional[str] = None,
                 *args,
                 parameter_constraints: Optional[List] = None,
                 measurements: Optional[List[MeasurementDeclaration]] = None,
                 registry: PulseRegistryType = None) -> None:
        """Create a new RepetitionPulseTemplate instance.

        Args:
            body (PulseTemplate): The PulseTemplate which will be repeated.
            repetition_count (int or ParameterDeclaration): The number of repetitions either as a
                constant integer value or as a parameter declaration.
            identifier (str): A unique identifier for use in serialization. (optional)
        """
        if len(args) == 1 and parameter_constraints is None:
            warn(
                'You used parameter_constraints as a positional argument. It will be keyword only in a future version.',
                DeprecationWarning)
        elif args:
            TypeError(
                'RepetitionPulseTemplate expects 3 positional arguments, got '
                + str(3 + len(args)))

        LoopPulseTemplate.__init__(self, identifier=identifier, body=body)
        ParameterConstrainer.__init__(
            self, parameter_constraints=parameter_constraints)
        MeasurementDefiner.__init__(self, measurements=measurements)

        repetition_count = ExpressionScalar.make(repetition_count)

        if repetition_count < 0:
            raise ValueError('Repetition count may not be negative')

        if repetition_count == 0:
            warn(
                "Repetition pulse template with 0 repetitions on construction."
            )

        self._repetition_count = repetition_count

        self._register(registry=registry)
Пример #6
0
 def _as_expression(self) -> Dict[ChannelID, ExpressionScalar]:
     expr = ExpressionScalar.make(
         self.__expression.underlying_expression.subs(
             {'t': self._AS_EXPRESSION_TIME}))
     return {self.__channel: expr}