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).
param.size() -> intReturns: 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
ParamExpression() # default constructor
ParamExpression(other: ParamExpression) # copy constructorMethods
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.
expr.calculate_expression_val() -> Noneget_expression_val
Returns the computed value of the expression. Must be called after calculate_expression_val.
expr.get_expression_val() -> floatReturns: 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.
expr.calculate_gradient_val(dg_div_df: float) -> None| Parameter | Type | Description |
|---|---|---|
| dg_div_df | float | The upstream derivative (chain rule multiplier). |
__str__
Returns a string representation of the expression tree.
str(expr) -> strArithmetic Operators
ParamExpression supports the following arithmetic operations, each producing a new ParamExpression:
| Operation | Signature | Description |
|---|---|---|
| Addition (expr + expr) | expr + other | Adds two expressions. |
| Addition (expr + scalar) | expr + scalar | Adds a float constant to an expression. |
| Reverse addition (scalar + expr) | scalar + expr | Adds an expression to a float constant. |
| Multiplication (expr * expr) | expr * other | Multiplies two expressions. |
| Multiplication (expr * scalar) | expr * scalar | Multiplies an expression by a float constant. |
| Reverse multiplication (scalar * expr) | scalar * expr | Multiplies a float constant by an expression. |
Returns: ParamExpression -- a new expression representing the operation.
Example
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 expressionSee Also
- VQCircuit -- the variational circuit class that manages parameters
- Gradient Results -- gradient computation result classes
- DiffMethod -- differentiation method selection