mindquantum.core.parameterresolver
Parameter resolver.
- class mindquantum.core.parameterresolver.ParameterResolver(data=None)[source]
A ParameterRsolver can set the parameter of parameterized quantum gate or parameterized quantum circuit.
By specific which part of parameters needs to calculate gradient, the PQC operator can only calculate gradient of these parameters.
- Parameters
data (dict) – initial parameter names and its values. Default: None.
Examples
>>> from mindquantum.core import ParameterResolver >>> pr = ParameterResolver({'a': 0.3}) >>> pr['b'] = 0.5 >>> pr.no_grad_part('a') {'a': 0.3, 'b': 0.5} >>> pr *= 2 >>> pr {'a': 0.6, 'b': 1.0} >>> pr.no_grad_parameters {'a'}
- combination(pr)[source]
Apply linear combination between this parameter resolver with input pr.
- Parameters
pr (Union[dict, ParameterResolver]) – The parameter resolver you want to do linear combination.
- Returns
numbers.Number, the combination result.
Examples
>>> from mindquantum import ParameterResolver >>> pr1 = ParameterResolver({'a': 1, 'b': 2}) >>> pr2 = ParameterResolver({'a': 2, 'b': 3}) >>> pr1.combination(pr2) 8
- conjugate()[source]
Get the conjugate of the parameter resolver.
- Returns
ParameterResolver, the conjugate version of this parameter resolver.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a' : 1, 'b': 1j}) >>> pr.conjugate().expression() a - 1.0*I*b
- dumps(indent=4)[source]
Dump ParameterResolver into JSON(JavaScript Object Notation)
- Parameters
indent (int) – Then JSON array elements and object members will be pretty-printed with that indent level. Default: 4.
- Returns
string(JSON), the JSON of ParameterResolver
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2, 'c': 3, 'd': 4}) >>> pr.no_grad_part('a', 'b') >>> print(pr.dumps()) { "a": 1, "b": 2, "c": 3, "d": 4, "__class__": "ParameterResolver", "__module__": "parameterresolver", "no_grad_parameters": [ "a", "b" ] }
- expression()[source]
Get the expression of this parameter resolver.
- Returns
sympy.Expr, the symbol expression of this parameter resolver.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a' : 2, 'b' : 0.3}) >>> pr.expression() 2*a + 0.3*b
- property imag
Get the real part of this parameter resolver
- Returns
ParameterResolver, the image part of this parameter resolver.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a': 1.2 + 1.3j}) >>> pr.imag {'a': 1.3}
- static loads(strs)[source]
Load JSON(JavaScript Object Notation) into FermionOperator
- Parameters
strs (str) – The dumped parameter resolver string.
- Returns
FermionOperator, the FermionOperator load from strings
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver >>> strings = """ { "a": 1, "b": 2, "c": 3, "d": 4, "__class__": "ParameterResolver", "__module__": "parameterresolver", "no_grad_parameters": [ "a", "b" ] } """ >>> obj = ParameterResolver.loads(string) >>> print(obj) {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> print('requires_grad_parameters is:', obj.requires_grad_parameters) requires_grad_parameters is: {'c', 'd'} >>> print('no_grad_parameters is :', obj.no_grad_parameters) no_grad_parameters is : {'b', 'a'}
- no_grad()[source]
Set all parameters to not require gradient calculation. Inplace operation.
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.no_grad() {'a': 1, 'b': 2} >>> pr.requires_grad_parameters set()
- no_grad_part(*names)[source]
Set part of parameters that not requires grad.
- Parameters
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.no_grad_part('a') {'a': 1, 'b': 2} >>> pr.requires_grad_parameters {'b'}
- property para_value
Get the parameters value.
- Returns
list, a list of parameters value.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.para_value [1, 2]
- property params_name
Get the parameters name.
- Returns
list, a list of parameters name.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.params_name ['a', 'b']
- property real
Get the real part of this parameter resolver
- Returns
ParameterResolver, the real part of this parameter resolver.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a': 1.2 + 1.3j}) >>> pr.real {'a': 1.2}
- requires_grad()[source]
Set all parameters of this parameter resolver to require gradient calculation. Inplace operation.
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.no_grad_part('a') {'a': 1, 'b': 2} >>> pr.requires_grad() {'a': 1, 'b': 2} >>> pr.requires_grad_parameters {'a', 'b'}
- requires_grad_part(*names)[source]
Set part of parameters that requires grad. Inplace operation.
- Parameters
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.no_grad() {'a': 1, 'b': 2} >>> pr.requires_grad_part('a') {'a': 1, 'b': 2} >>> pr.requires_grad_parameters {'a'}
- update(others)[source]
Update this parameter resolver with other parameter resolver.
- Parameters
others (ParameterResolver) – other parameter resolver.
- Raises
ValueError – If some parameters require grad and not require grad in other parameter resolver and vice versa.
Examples
>>> from mindquantum import ParameterResolver >>> pr1 = ParameterResolver({'a': 1}) >>> pr2 = ParameterResolver({'b': 2}) >>> pr2.no_grad() {'b': 2} >>> pr1.update(pr2) >>> pr1 {'a': 1, 'b': 2} >>> pr1.no_grad_parameters {'b'}