qiskit_optimization/problems/substitute_variables.py
def substitute_variables(
    quadratic_program: QuadraticProgram,
    constants: Optional[Dict[Union[str, int], float]] = None,
    variables: Optional[Dict[Union[str, int], Tuple[Union[str, int], float]]] = None,
) -> QuadraticProgram:
    """Substitutes variables with constants or other variables.

    Args:
        quadratic_program: a quadratic program whose variables are substituted.

        constants: replace variable by constant
            e.g., {'x': 2} means 'x' is substituted with 2

        variables: replace variables by weighted other variable
            need to copy everything using name reference to make sure that indices are matched
            correctly. The lower and upper bounds are updated accordingly.
            e.g., {'x': ('y', 2)} means 'x' is substituted with 'y' * 2

    Returns:
        An optimization problem by substituting variables with constants or other variables.
        If the substitution is valid, `QuadraticProgram.status` is still
        `QuadraticProgram.Status.VALID`.
        Otherwise, it gets `QuadraticProgram.Status.INFEASIBLE`.

    Raises:
        QiskitOptimizationError: if the substitution is invalid as follows.
            - Same variable is substituted multiple times.
            - Coefficient of variable substitution is zero.
    """
    # guarantee that there is no overlap between variables to be replaced and combine input
    subs = {}
    if constants:
        for i, v in constants.items():
            # substitute i <- v
            i_2 = quadratic_program.get_variable(i).name
            if i_2 in subs:
                raise QiskitOptimizationError(
                    f"Cannot substitute the same variable twice: {i} <- {v}"
                )
            subs[i_2] = SubstitutionExpression(const=v)

    if variables:
        for i, (j, v) in variables.items():
            if v == 0:
                raise QiskitOptimizationError(f"coefficient must be non-zero: {i} {j} {v}")
            # substitute i <- j * v
            i_2 = quadratic_program.get_variable(i).name
            j_2 = quadratic_program.get_variable(j).name
            if i_2 == j_2:
                raise QiskitOptimizationError(
                    f"Cannot substitute the same variable: {i} <- {j} {v}"
                )
            if i_2 in subs:
                raise QiskitOptimizationError(
                    f"Cannot substitute the same variable twice: {i} <- {j} {v}"
                )
            if j_2 in subs:
                raise QiskitOptimizationError(
                    "Cannot substitute by variable that gets substituted itself: " f"{i} <- {j} {v}"
                )
            subs[i_2] = SubstitutionExpression(variable=j_2, coeff=v)

    return _SubstituteVariables().substitute_variables(quadratic_program, subs)
Substitutes variables with constants or other variables.

    Args:
        quadratic_program: a quadratic program whose variables are substituted.

        constants: replace variable by constant
            e.g., {'x': 2} means 'x' is substituted with 2

        variables: replace variables by weighted other variable
            need to copy everything using name reference to make sure that indices are matched
            correctly. The lower and upper bounds are updated accordingly.
            e.g., {'x': ('y', 2)} means 'x' is substituted with 'y' * 2

    Returns:
        An optimization problem by substituting variables with constants or other variables.
        If the substitution is valid, `QuadraticProgram.status` is still
        `QuadraticProgram.Status.VALID`.
        Otherwise, it gets `QuadraticProgram.Status.INFEASIBLE`.

    Raises:
        QiskitOptimizationError: if the substitution is invalid as follows.
            - Same variable is substituted multiple times.
            - Coefficient of variable substitution is zero.
    
Substitutes variables with constants or other variables.
Substitutes variables with constants or other variables .
def substitute_variables(
    quadratic_program: QuadraticProgram,
    constants: Optional[Dict[Union[str, int], float]] = None,
    variables: Optional[Dict[Union[str, int], Tuple[Union[str, int], float]]] = None,
) -> QuadraticProgram:
    subs = {}
    if constants:
        for i, v in constants.items():
            i_2 = quadratic_program.get_variable(i).name
            if i_2 in subs:
                raise QiskitOptimizationError(
                    f"Cannot substitute the same variable twice: {i} <- {v}"
                )
            subs[i_2] = SubstitutionExpression(const=v)
    if variables:
        for i, (j, v) in variables.items():
            if v == 0:
                raise QiskitOptimizationError(f"coefficient must be non-zero: {i} {j} {v}")
            i_2 = quadratic_program.get_variable(i).name
            j_2 = quadratic_program.get_variable(j).name
            if i_2 == j_2:
                raise QiskitOptimizationError(
                    f"Cannot substitute the same variable: {i} <- {j} {v}"
                )
            if i_2 in subs:
                raise QiskitOptimizationError(
                    f"Cannot substitute the same variable twice: {i} <- {j} {v}"
                )
            if j_2 in subs:
                raise QiskitOptimizationError(
                    "Cannot substitute by variable that gets substituted itself: " f"{i} <- {j} {v}"
                )
            subs[i_2] = SubstitutionExpression(variable=j_2, coeff=v)
    return _SubstituteVariables().substitute_variables(quadratic_program, subs)
def substitute_variables ( quadratic_program : QuadraticProgram , constants : Optional [ Dict [ Union [ str , int ] , float ] ] = None , variables : Optional [ Dict [ Union [ str , int ] , Tuple [ Union [ str , int ] , float ] ] ] = None , ) -> QuadraticProgram : subs = { } if constants : for i , v in constants . items ( ) : i_2 = quadratic_program . get_variable ( i ) . name if i_2 in subs : raise QiskitOptimizationError ( f"Cannot substitute the same variable twice: {i} <- {v}" ) subs [ i_2 ] = SubstitutionExpression ( const = v ) if variables : for i , ( j , v ) in variables . items ( ) : if v == 0 : raise QiskitOptimizationError ( f"coefficient must be non-zero: {i} {j} {v}" ) i_2 = quadratic_program . get_variable ( i ) . name j_2 = quadratic_program . get_variable ( j ) . name if i_2 == j_2 : raise QiskitOptimizationError ( f"Cannot substitute the same variable: {i} <- {j} {v}" ) if i_2 in subs : raise QiskitOptimizationError ( f"Cannot substitute the same variable twice: {i} <- {j} {v}" ) if j_2 in subs : raise QiskitOptimizationError ( "Cannot substitute by variable that gets substituted itself: " f"{i} <- {j} {v}" ) subs [ i_2 ] = SubstitutionExpression ( variable = j_2 , coeff = v ) return _SubstituteVariables ( ) . substitute_variables ( quadratic_program , subs )
Substitutes variables with constants or other variables.
Substitutes variables with constants or other variables .
"""Substitutes variables with constants or other variables.

    Args:
        quadratic_program: a quadratic program whose variables are substituted.

        constants: replace variable by constant
            e.g., {'x': 2} means 'x' is substituted with 2

        variables: replace variables by weighted other variable
            need to copy everything using name reference to make sure that indices are matched
            correctly. The lower and upper bounds are updated accordingly.
            e.g., {'x': ('y', 2)} means 'x' is substituted with 'y' * 2

    Returns:
        An optimization problem by substituting variables with constants or other variables.
        If the substitution is valid, `QuadraticProgram.status` is still
        `QuadraticProgram.Status.VALID`.
        Otherwise, it gets `QuadraticProgram.Status.INFEASIBLE`.

    Raises:
        QiskitOptimizationError: if the substitution is invalid as follows.
            - Same variable is substituted multiple times.
            - Coefficient of variable substitution is zero.
    """ # guarantee that there is no overlap between variables to be replaced and combine input # substitute i <- v # substitute i <- j * v
An optimization problem by substituting variables with constants or other variables.
If the substitution is valid, `QuadraticProgram.status` is still
`QuadraticProgram.Status.VALID`.
Otherwise, it gets `QuadraticProgram.Status.INFEASIBLE`.
An optimization problem by substituting variables with constants or other variables . If the substitution is valid ` QuadraticProgram . status ` is still ` QuadraticProgram . Status . VALID ` . Otherwise it gets ` QuadraticProgram . Status . INFEASIBLE ` .
if the substitution is invalid as follows.
Same variable is substituted multiple times.
Coefficient of variable substitution is zero.
if the substitution is invalid as follows . Same variable is substituted multiple times . Coefficient of variable substitution is zero .
QiskitOptimizationError
quadratic_program
QuadraticProgram
a quadratic program whose variables are substituted.
a quadratic program whose variables are substituted .
constants
Optional[Dict[Union[str, int], float]]
replace variable by constant
e.g., {'x': 2} means 'x' is substituted with 2
replace variable by constant e . g . { ' x ' : 2 } means ' x ' is substituted with 2
variables
Optional[Dict[Union[str, int], Tuple[Union[str, int], float]]]
replace variables by weighted other variable
need to copy everything using name reference to make sure that indices are matched
correctly. The lower and upper bounds are updated accordingly.
replace variables by weighted other variable need to copy everything using name reference to make sure that indices are matched correctly . The lower and upper bounds are updated accordingly .