Parameter
This class holds parameter values that can be used in circuits and in backends supporting symbolic computation.
>>> import perceval as pcvl
>>> p = pcvl.Parameter("phi") # Or equivalently pcvl.P("phi")
>>> p.spv
phi
>>> p.defined
False
>>> p.set_value(3.14)
>>> float(p)
3.14
>>> p.defined
True
When defining the parameter, you can also set its numerical value, max/min boundaries and periodicity:
>>> import perceval as pcvl
>>> import math
>>> alpha = pcvl.P("phi", min_v=0, max_v=2*math.pi, periodic=True)
>>> alpha.is_periodic
True
- class perceval.utils.parameter.Parameter(name, value=None, min_v=None, max_v=None, periodic=True)
A Parameter is a used as a variable in a circuit definition
Parameters are a simple way to introduce named variables in a circuit. They take floating number values. When non defined they are associated to sympy symbols and will be used to perform symbolic calculations.
- Parameters:
name (
str
) – name of the parametervalue (
Optional
[float
]) – optional value, when the value is provided at initialization, the parameter is considered as fixedmin_v (
Optional
[float
]) – minimal value that the parameter can take, is used in circuit optimizationmax_v (
Optional
[float
]) – maximal value that the parameter can take, is used in circuit optimizationperiodic (
bool
) – True if the parameter is periodic, False otherwise (default True)
- property bounds: tuple[float, float]
Minimal and maximal values for the parameter
- property defined: bool
Return True if the parameter has a value (fixed or non fixed)
- evalf(subs=None)
Convert the parameter to float, will fail if the parameter has no defined value
- Return type:
float
- fix_value(v)
Fix the value of a non-fixed Parameter. The Parameter is not variable afterwards.
- Parameters:
v (
float
) – the numerical value
- property fixed: bool
Return True if the parameter is fixed
- property is_periodic: bool
Return True if the parameter is defined as a period parameter
- property is_variable: bool
Returns True for a non-fixed parameter
- property max: float
The maximal value of the parameter
- property min
The minimal value of the parameter
- property pid
Unique identifier for the parameter
- reset()
Reset the value of a non-fixed parameter
- set_periodic(periodic)
set periodic flag
- set_value(v, force=False)
Define the numerical value of a Parameter
- Parameters:
v (
float
) – the numerical valueforce (
bool
) – enable to change the value of a fixed parameter
- Raise:
RuntimeError if the parameter is fixed
- property spv: Expr
The current value of the parameter defined as a sympy expression