Skip to content

Variable

Bases: QuadraticProgramElement

Representation of a variable.

Source code in q3as/quadratic/problems/variable.py
class Variable(QuadraticProgramElement):
    """Representation of a variable."""

    Type = VarType

    def __init__(
        self,
        quadratic_program: Any,
        name: str,
        lowerbound: Union[float, int] = 0,
        upperbound: Union[float, int] = INFINITY,
        vartype: VarType = VarType.CONTINUOUS,
    ) -> None:
        """Creates a new Variable.

        The variables is exposed by the top-level `QuadraticProgram` class
        in `QuadraticProgram.variables`.  This constructor is not meant to be used
        externally.

        Args:
            quadratic_program: The parent QuadraticProgram.
            name: The variable name.
            lowerbound: The variable lowerbound.
            upperbound: The variable upperbound.
            vartype: The variable type.

        Raises:
            QiskitOptimizationError: if lowerbound is greater than upperbound.
        """
        if lowerbound > upperbound:
            raise QiskitOptimizationError("Lowerbound is greater than upperbound!")

        super().__init__(quadratic_program)
        self._name = name
        self._lowerbound = lowerbound
        self._upperbound = upperbound
        self._vartype = vartype

    @property
    def name(self) -> str:
        """Returns the name of the variable.

        Returns:
            The name of the variable.
        """
        return self._name

    @property
    def lowerbound(self) -> Union[float, int]:
        """Returns the lowerbound of the variable.

        Returns:
            The lower bound of the variable.
        """
        return self._lowerbound

    @lowerbound.setter
    def lowerbound(self, lowerbound: Union[float, int]) -> None:
        """Sets the lowerbound of the variable.

        Args:
            lowerbound: The lower bound of the variable.

        Raises:
            QiskitOptimizationError: if lowerbound is greater than upperbound.
        """
        if lowerbound > self.upperbound:
            raise QiskitOptimizationError("Lowerbound is greater than upperbound!")
        self._lowerbound = lowerbound

    @property
    def upperbound(self) -> Union[float, int]:
        """Returns the upperbound of the variable.

        Returns:
            The upperbound of the variable.
        """
        return self._upperbound

    @upperbound.setter
    def upperbound(self, upperbound: Union[float, int]) -> None:
        """Sets the upperbound of the variable.

        Args:
            upperbound: The upperbound of the variable.

        Raises:
            QiskitOptimizationError: if upperbound is smaller than lowerbound.
        """
        if self.lowerbound > upperbound:
            raise QiskitOptimizationError("Lowerbound is greater than upperbound!")
        self._upperbound = upperbound

    @property
    def vartype(self) -> VarType:
        """Returns the type of the variable.

        Returns:
            The variable type.

        """
        return self._vartype

    @vartype.setter
    def vartype(self, vartype: VarType) -> None:
        """Sets the type of the variable.

        Args:
            vartype: The variable type.
        """
        self._vartype = vartype

    def as_tuple(self) -> Tuple[str, Union[float, int], Union[float, int], VarType]:
        """Returns a tuple corresponding to this variable.

        Returns:
            A tuple corresponding to this variable consisting of name, lowerbound, upperbound and
            variable type.
        """
        return self.name, self.lowerbound, self.upperbound, self.vartype

    def __repr__(self):
        return f"<{self.__class__.__name__}: {str(self)}>"

    def __str__(self):
        if self._vartype == VarType.BINARY:
            return f"{self.name} ({self._vartype.name.lower()})"
        lowerbound = "" if self._lowerbound == -INFINITY else f"{self._lowerbound} <= "
        upperbound = "" if self._upperbound == INFINITY else f" <= {self._upperbound}"
        return f"{lowerbound}{self.name}{upperbound} ({self._vartype.name.lower()})"

lowerbound: Union[float, int] property writable

Returns the lowerbound of the variable.

Returns:

Type Description
Union[float, int]

The lower bound of the variable.

name: str property

Returns the name of the variable.

Returns:

Type Description
str

The name of the variable.

upperbound: Union[float, int] property writable

Returns the upperbound of the variable.

Returns:

Type Description
Union[float, int]

The upperbound of the variable.

vartype: VarType property writable

Returns the type of the variable.

Returns:

Type Description
VarType

The variable type.

__init__(quadratic_program, name, lowerbound=0, upperbound=INFINITY, vartype=VarType.CONTINUOUS)

Creates a new Variable.

The variables is exposed by the top-level QuadraticProgram class in QuadraticProgram.variables. This constructor is not meant to be used externally.

Parameters:

Name Type Description Default
quadratic_program Any

The parent QuadraticProgram.

required
name str

The variable name.

required
lowerbound Union[float, int]

The variable lowerbound.

0
upperbound Union[float, int]

The variable upperbound.

INFINITY
vartype VarType

The variable type.

CONTINUOUS

Raises:

Type Description
QiskitOptimizationError

if lowerbound is greater than upperbound.

Source code in q3as/quadratic/problems/variable.py
def __init__(
    self,
    quadratic_program: Any,
    name: str,
    lowerbound: Union[float, int] = 0,
    upperbound: Union[float, int] = INFINITY,
    vartype: VarType = VarType.CONTINUOUS,
) -> None:
    """Creates a new Variable.

    The variables is exposed by the top-level `QuadraticProgram` class
    in `QuadraticProgram.variables`.  This constructor is not meant to be used
    externally.

    Args:
        quadratic_program: The parent QuadraticProgram.
        name: The variable name.
        lowerbound: The variable lowerbound.
        upperbound: The variable upperbound.
        vartype: The variable type.

    Raises:
        QiskitOptimizationError: if lowerbound is greater than upperbound.
    """
    if lowerbound > upperbound:
        raise QiskitOptimizationError("Lowerbound is greater than upperbound!")

    super().__init__(quadratic_program)
    self._name = name
    self._lowerbound = lowerbound
    self._upperbound = upperbound
    self._vartype = vartype

as_tuple()

Returns a tuple corresponding to this variable.

Returns:

Type Description
str

A tuple corresponding to this variable consisting of name, lowerbound, upperbound and

Union[float, int]

variable type.

Source code in q3as/quadratic/problems/variable.py
def as_tuple(self) -> Tuple[str, Union[float, int], Union[float, int], VarType]:
    """Returns a tuple corresponding to this variable.

    Returns:
        A tuple corresponding to this variable consisting of name, lowerbound, upperbound and
        variable type.
    """
    return self.name, self.lowerbound, self.upperbound, self.vartype