Skip to content

Parameter

Parameter represents a multi-dimensional array of variational parameters managed by a VQCircuit. Each element in the array is a placeholder that can be referenced by variational quantum gates or used to build arithmetic expressions via ParamExpression.

Parameters are not constructed directly by users. Instead, they are configured on a VQCircuit through set_Param and accessed through the VQCircuit.Param method.

Overview

A Parameter object stores:

  • Dimension sizes -- the shape of the parameter array (e.g., [4] for 4 parameters, [2, 3] for a 2x3 grid).
  • Dimension labels -- optional human-readable names for each dimension.
  • Element labels -- optional names for individual parameter elements.
  • Values and gradients -- bound parameter values and their computed gradients.

Methods

size

Returns the total number of parameter elements (product of all dimension sizes).

python
param.size() -> int

Returns: int -- the total number of parameter elements.


ParamExpression

ParamExpression represents an arithmetic expression built from parameter placeholders and constants. It supports addition and multiplication, enabling users to compose parameterized values for variational quantum gates beyond simple single-placeholder references.

For example, if p0 and p1 are parameter placeholders, an expression like p0 + 0.5 * p1 can be constructed and passed as the parameter of a variational gate.

Signature

python
ParamExpression()                  # default constructor
ParamExpression(other: ParamExpression)  # copy constructor

Methods

calculate_expression_val

Evaluates the expression numerically. After calling this method, retrieve the result with get_expression_val. The parameter values must be bound (via VQCircuit.__call__) before evaluation.

python
expr.calculate_expression_val() -> None

get_expression_val

Returns the computed value of the expression. Must be called after calculate_expression_val.

python
expr.get_expression_val() -> float

Returns: float -- the evaluated numeric value of the expression.


calculate_gradient_val

Computes the gradient of the expression with respect to its constituent placeholders, given the upstream gradient dg_div_df.

python
expr.calculate_gradient_val(dg_div_df: float) -> None
ParameterTypeDescription
dg_div_dffloatThe upstream derivative (chain rule multiplier).

__str__

Returns a string representation of the expression tree.

python
str(expr) -> str

Arithmetic Operators

ParamExpression supports the following arithmetic operations, each producing a new ParamExpression:

OperationSignatureDescription
Addition (expr + expr)expr + otherAdds two expressions.
Addition (expr + scalar)expr + scalarAdds a float constant to an expression.
Reverse addition (scalar + expr)scalar + exprAdds an expression to a float constant.
Multiplication (expr * expr)expr * otherMultiplies two expressions.
Multiplication (expr * scalar)expr * scalarMultiplies an expression by a float constant.
Reverse multiplication (scalar * expr)scalar * exprMultiplies a float constant by an expression.

Returns: ParamExpression -- a new expression representing the operation.

Example

python
from pyqpanda3.vqcircuit import VQCircuit
from pyqpanda3.core import RX

vqc = VQCircuit()
vqc.set_Param([4])

# Get individual placeholders
p0 = vqc.Param([0])
p1 = vqc.Param([1])

# Build an expression: 0.5 * p0 + p1
# Note: p0 and p1 are ParamExpression objects returned by VQCircuit.Param
expr = 0.5 * p0 + p1

# Use the expression in a variational gate
vqc << RX(0, expr)

# The total parameter count is still 4 (p0, p1, p2, p3)
# but the gate parameter is a derived expression

See Also

Released under the MIT License.