跳转至

Parameter

QuICT.core.utils.Parameter

Parameter(symbol: str)

Used to represent trainable parameters of parameterized quantum gates or parameterized quantum circuits.

Note

Only supports number multiplication operation.

Parameters:

  • symbol (str) –

    The symbol.

Examples:

>>> from QuICT.core.circuit import Circuit
>>> from QuICT.core.gate import *
>>> cir = Circuit(3)
>>> H | cir
>>> Rx("x") | cir(0)
>>> Rx(Parameter("x") * 0.3) | cir(2)
>>> Rzz("y") | cir([0, 1])
>>> cir.draw("command")
        ┌───┐ ┌───────┐
q_0: |0>┤ h ├─┤ rx(x) ├───■──────
        ├───┤ └───────┘   │ZZ(y)
q_1: |0>┤ h ├─────────────■──────
        ├───┤┌──────────┐
q_2: |0>┤ h ├┤ rx(0.3x) ├────────
        └───┘└──────────┘

Initialize a Parameter instance.

Source code in QuICT/core/utils/parameter.py
def __init__(self, symbol: str):
    """Initialize a Parameter instance."""
    self._symbol = symbol
    self._expr = symbol
    self._multiplier = 1.0
    self._dx = 1.0

dx property

dx: float

The derivative of the parameter on the symbol.

Returns:

  • float ( float ) –

    The derivative.

expr property

expr: str

The expression of the parameter.

Returns:

  • str ( str ) –

    The expression of the parameter.

multiplier property

multiplier: float

The multiplier of the symbol.

Returns:

  • float ( float ) –

    The multiplier.

symbol property

symbol: str

The symbol in the parameter.

Returns:

  • str ( str ) –

    The symbol.

__lt__

__lt__(other)

Determine whether the current parameter is smaller than another parameter.

Parameters:

Returns:

  • bool

    If True, the current parameter is smaller than another parameter.

Source code in QuICT/core/utils/parameter.py
def __lt__(self, other):
    """Determine whether the current parameter is smaller than another parameter.

    Args:
        other (Parameter): Another parameter.

    Returns:
        bool: If True, the current parameter is smaller than another parameter.
    """
    assert isinstance(other, Parameter), TypeError(
        "Parameter.__lt__.other", "Parameter", type(other)
    )
    if self._symbol == other._symbol:
        if self._multiplier < other._multiplier:
            return True
    else:
        if self._expr < other._expr:
            return True
    return False

__mul__

__mul__(other)

Multiply a number.

Parameters:

  • other (Number) –

    The multiplier.

Returns:

  • Parameter

    The new parameter after number multiplication operation.

Source code in QuICT/core/utils/parameter.py
def __mul__(self, other):
    """Multiply a number.

    Args:
        other (numbers.Number): The multiplier.

    Returns:
        Parameter: The new parameter after number multiplication operation.
    """
    assert isinstance(other, numbers.Number), TypeError(
        "Parameter.__mul__.other", "numbers.Number", type(other)
    )
    new_param = Parameter(self._symbol)
    new_param._expr = str(other) + self._symbol
    new_param._multiplier = self._multiplier * other
    new_param._dx = self._dx * other
    return new_param

__rmul__

__rmul__(other)

Multiply a number.

Parameters:

  • other (Number) –

    The multiplier.

Returns:

  • Parameter

    The new parameter after number multiplication operation.

Source code in QuICT/core/utils/parameter.py
def __rmul__(self, other):
    """Multiply a number.

    Args:
        other (numbers.Number): The multiplier.

    Returns:
        Parameter: The new parameter after number multiplication operation.
    """
    return self.__mul__(other)

__str__

__str__() -> str

Return the expression of the parameter.

Source code in QuICT/core/utils/parameter.py
def __str__(self) -> str:
    """Return the expression of the parameter."""
    return self._expr