Ejemplo n.º 1
0
def _read_parameter_expression(file_obj):
    param_expr_raw = struct.unpack(PARAMETER_EXPR_PACK,
                                   file_obj.read(PARAMETER_EXPR_SIZE))
    map_elements = param_expr_raw[0]
    from sympy.parsing.sympy_parser import parse_expr

    if HAS_SYMENGINE:
        expr = symengine.sympify(
            parse_expr(file_obj.read(param_expr_raw[1]).decode("utf8")))
    else:
        expr = parse_expr(file_obj.read(param_expr_raw[1]).decode("utf8"))
    symbol_map = {}
    for _ in range(map_elements):
        elem_raw = file_obj.read(PARAM_EXPR_MAP_ELEM_SIZE)
        elem = struct.unpack(PARAM_EXPR_MAP_ELEM_PACK, elem_raw)
        param = _read_parameter(file_obj)
        elem_type = elem[0].decode("utf8")
        elem_data = file_obj.read(elem[1])
        if elem_type == "f":
            value = struct.unpack("!d", elem_data)
        elif elem_type == "i":
            value = struct.unpack("!q", elem_data)
        elif elem_type == "c":
            value = complex(*struct.unpack(COMPLEX_PACK, elem_data))
        elif elem_type == "p":
            value = param._symbol_expr
        elif elem_type == "e":
            value = _read_parameter_expression(io.BytesIO(elem_data))
        else:
            raise TypeError("Invalid parameter expression map type: %s" %
                            elem_type)
        symbol_map[param] = value
    return ParameterExpression(symbol_map, expr)
Ejemplo n.º 2
0
def _read_parameter_expression_v3(file_obj, vectors):
    data = formats.PARAMETER_EXPR(
        *struct.unpack(formats.PARAMETER_EXPR_PACK,
                       file_obj.read(formats.PARAMETER_EXPR_SIZE)))
    from sympy.parsing.sympy_parser import parse_expr

    if _optional.HAS_SYMENGINE:
        import symengine

        expr = symengine.sympify(
            parse_expr(file_obj.read(data.expr_size).decode(common.ENCODE)))
    else:
        expr = parse_expr(file_obj.read(data.expr_size).decode(common.ENCODE))
    symbol_map = {}
    for _ in range(data.map_elements):
        elem_data = formats.PARAM_EXPR_MAP_ELEM_V3(*struct.unpack(
            formats.PARAM_EXPR_MAP_ELEM_V3_PACK,
            file_obj.read(formats.PARAM_EXPR_MAP_ELEM_V3_SIZE),
        ))
        symbol_key = type_keys.Value(elem_data.symbol_type)

        if symbol_key == type_keys.Value.PARAMETER:
            symbol = _read_parameter(file_obj)
        elif symbol_key == type_keys.Value.PARAMETER_VECTOR:
            symbol = _read_parameter_vec(file_obj, vectors)
        else:
            raise exceptions.QpyError(
                "Invalid parameter expression map type: %s" % symbol_key)

        elem_key = type_keys.Value(elem_data.type)
        binary_data = file_obj.read(elem_data.size)
        if elem_key == type_keys.Value.INTEGER:
            value = struct.unpack("!q", binary_data)
        elif elem_key == type_keys.Value.FLOAT:
            value = struct.unpack("!d", binary_data)
        elif elem_key == type_keys.Value.COMPLEX:
            value = complex(*struct.unpack(formats.COMPLEX_PACK, binary_data))
        elif elem_key in (type_keys.Value.PARAMETER,
                          type_keys.Value.PARAMETER_VECTOR):
            value = symbol._symbol_expr
        elif elem_key == type_keys.Value.PARAMETER_EXPRESSION:
            value = common.data_from_binary(binary_data,
                                            _read_parameter_expression_v3,
                                            vectors=vectors)
        else:
            raise exceptions.QpyError(
                "Invalid parameter expression map type: %s" % elem_key)
        symbol_map[symbol] = value

    return ParameterExpression(symbol_map, expr)
Ejemplo n.º 3
0
 def test_func(dur: ParameterExpression, t_val: int):
     dur_bound = dur.bind({t_param: t_val})
     sched = Schedule()
     sched += Play(library.constant(int(float(dur_bound)), amp),
                   DriveChannel(0))
     return sched