Parameters
Parameters
ParamMap(**kwargs)
Connects UUIDs of parameters to their expressions and names.
This class is not user-facing and only needed for more complex block definitions. It provides convenient access to expressions/UUIDs/names needed in different backends.
PARAMETER | DESCRIPTION |
---|---|
kwargs |
Parameters.
TYPE:
|
Example:
import sympy
from qadence.parameters import ParamMap
(x,y) = sympy.symbols("x y")
ps = ParamMap(omega=2.0, duration=x+y)
print(f"{ps.names() = }")
print(f"{ps.expressions() = }")
print(f"{ps.uuids() = }")
Source code in qadence/parameters.py
Parameter
Bases:
A wrapper on top of sympy.Symbol
.
Includes two additional keywords: trainable
and value
.
This class is to define both feature parameter and variational parameters.
trainable: bool
instance-attribute
Trainable parameters are variational parameters.
Non-trainable parameters are feature parameters.
value: TNumber
instance-attribute
(Initial) value of the parameter.
__new__(name, **assumptions)
Arguments:
name: When given a string only, the class
constructs a trainable Parameter with a a randomly initialized value.
**assumptions: are passed on to the parent class `sympy.Symbol`. Two new assumption
kwargs are supported by this constructor: `trainable: bool`, and `value: TNumber`.
Example:
from qadence.parameters import Parameter, VariationalParameter
theta = Parameter("theta")
print(f"{theta}: trainable={theta.trainable} value={theta.value}")
assert not theta.is_number
# you can specify both trainable/value in the constructor
theta = Parameter("theta", trainable=True, value=2.0)
print(f"{theta}: trainable={theta.trainable} value={theta.value}")
# VariationalParameter/FeatureParameter are constructing
# trainable/untrainable Parameters
theta = VariationalParameter("theta", value=2.0)
assert theta == Parameter("theta", trainable=True, value=2.0)
# When provided with a numeric type, Parameter constructs a sympy numeric type":
constant_zero = Parameter(0)
assert constant_zero.is_number
# When passed a Parameter or a sympy expression, it just returns it.
expr = Parameter("x") * Parameter("y")
print(f"{expr=} : {expr.free_symbols}")
Source code in qadence/parameters.py
FeatureParameter(name, **kwargs)
TimeParameter(name)
VariationalParameter(name, **kwargs)
evaluate(expr, values=None, as_torch=False)
Arguments:
expr: An expression consisting of Parameters.
values: values dict which contains values for the Parameters,
if empty, Parameter.value will be used.
as_torch: Whether to retrieve a torch-differentiable expression result.
Example:
from qadence.parameters import Parameter, evaluate
expr = Parameter("x") * Parameter("y")
# Unless specified, Parameter initialized random values
# Lets evaluate this expression and see what the result is
res = evaluate(expr)
print(res)
# We can also evaluate the expr using a custom dict
d = {"x": 1, "y":2}
res = evaluate(expr, d)
print(res)
# Lastly, if we want a differentiable result, lets put the as_torch flag
res = evaluate(expr, d, as_torch=True)
print(res)
Source code in qadence/parameters.py
extract_original_param_entry(param)
Given an Expression, what was the original "param" given by the user? It is either.
going to be a numeric value, or a sympy Expression (in case a string was given, it was converted via Parameter("string").
Source code in qadence/parameters.py
Parameter embedding
embedding(block, to_gate_params=False, engine=Engine.TORCH)
Construct embedding function which maps user-facing parameters to either expression-level.
parameters or gate-level parameters. The constructed embedding function has the signature:
embedding_fn(params: ParamDictType, inputs: ParamDictType) -> ParamDictType:
which means that it maps the variational parameter dict params
and the feature parameter
dict inputs
to one new parameter dict embedded_dict
which holds all parameters that are
needed to execute a circuit on a given backend. There are two different modes for this
mapping:
- Expression-level parameters: For AD-based optimization. For every unique expression we end
up with one entry in the embedded dict:
len(embedded_dict) == len(unique_parameter_expressions)
. - Gate-level parameters: For PSR-based optimization or real devices. One parameter for each
gate parameter, regardless if they are based on the same expression.
len(embedded_dict) == len(parametric_gates)
. This is needed because PSR requires to shift the angles of every gate where the same parameter appears.
PARAMETER | DESCRIPTION |
---|---|
block |
parametrized block into which we want to embed parameters.
TYPE:
|
to_gate_params |
A boolean flag whether to generate gate-level parameters or expression-level parameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
|
A tuple with variational parameter dict and the embedding function. |
Source code in qadence/blocks/embedding.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|