mindquantum.core.parameterresolver.ParameterResolver
- class mindquantum.core.parameterresolver.ParameterResolver(data=None, const=None, dtype=None, internal=False)[source]
A ParameterResolver can set the parameter of parameterized quantum gate or parameterized quantum circuit.
- Parameters
data (Union[dict, numbers.Number, str, ParameterResolver]) – initial parameter names and its values. If data is a dict, the key will be the parameter name and the value will be the parameter value. If data is a number, this number will be the constant value of this parameter resolver. If data is a string, then this string will be the only parameter with coefficient be 1. Default:
None
.const (number.Number) – the constant part of this parameter resolver. Default:
None
.dtype (mindquantum.dtype) – the data type of this parameter resolver. Default:
None
.internal (bool) – whether the first argument is the c++ object of parameter resolver. Default:
False
.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver >>> pr = ParameterResolver({'a': 0.3}) >>> pr['b'] = 0.5 >>> pr.no_grad_part('a') ParameterResolver(dtype: float64, data: [ a: 0.300000, b: 0.500000 ], const: 0.000000, no grad parameters: {a, } ) >>> pr *= 2 >>> pr ParameterResolver(dtype: float64, data: [ a: 0.600000, b: 1.000000 ], const: 0.000000, no grad parameters: {a, } ) >>> pr.expression() '0.6*a + b' >>> pr.const = 0.5 >>> pr.expression() '0.6*a + b + 1/2' >>> pr.no_grad_parameters ['a'] >>> ParameterResolver(3) ParameterResolver(dtype: float64, const: 3.000000) >>> ParameterResolver('a') ParameterResolver(dtype: float64, data: [ a: 1.000000 ], const: 0.000000 )
- property ansatz_parameters: List[str]
Get parameters that is ansatz parameters.
- Returns
set, the set of ansatz parameters.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 1, 'b': 2}) >>> a.ansatz_parameters ['a', 'b']
- ansatz_part(*names)[source]
Set which part is ansatz parameters.
- Parameters
names (tuple[str]) – Parameters that will be serve as ansatz.
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.as_encoder() >>> pr.ansatz_part('a') >>> pr.ansatz_parameters ['a']
- as_ansatz()[source]
Set all the parameters as ansatz.
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a': 1, 'b': 2}) >>> pr.as_encoder() >>> pr.as_ansatz() >>> pr.ansatz_parameters ['a', 'b']
- as_encoder()[source]
Set all the parameters as encoder.
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a': 1, 'b': 2}) >>> pr.as_encoder() >>> pr.encoder_parameters ['b', 'a']
- astype(dtype)[source]
Convert ParameterResolver to different dtype.
- Parameters
dtype (mindquantum.dtype) – new data type of parameter resolver you want.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver >>> a = ParameterResolver('a') >>> a ParameterResolver(dtype: float64, data: [ a: 1.000000 ], const: 0.000000 ) >>> import mindquantum as mq >>> a.astype(mq.complex128) ParameterResolver(dtype: complex128, data: [ a: (1.000000, 0.000000) ], const: (0.000000, 0.000000) )
- combination(other: Union[Dict[str, numbers.Number], 'ParameterResolver'])[source]
Apply linear combination between this parameter resolver with input parameter resolver.
- Parameters
other (Union[Dict[str, numbers.Number], 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) ParameterResolver(dtype: float64, const: 8.000000)
- conjugate()[source]
Get the conjugate of the parameter resolver.
- Returns
ParameterResolver, the conjugate version of this parameter resolver.
Examples
>>> import mindquantum as mq >>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a' : 1, 'b': 1j}, dtype=mq.complex128) >>> pr.conjugate().expression() 'a + (-1j)*b'
- property const: numbers.Number
Get the constant part of this parameter resolver.
- Returns
numbers.Number, the constant part of this parameter resolver.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a': 1}, 2.5) >>> pr.const 2.5
- property dtype
Get ParameterResolver data type.
- dumps(indent=4)[source]
Dump ParameterResolver into JSON(JavaScript Object Notation).
Note
Since float32 type value is not serializable, so ParameterResolver with mindquantum.float32 and mindquantum.complex64 type is not serializable.
- 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}, const=3 + 4j) >>> pr.no_grad_part('a', 'b') >>> print(pr.dumps()) { "pr_data": { "a": [ 1.0, 0.0 ], "b": [ 2.0, 0.0 ] }, "const": [ 3.0, 4.0 ], "dtype": "mindquantum.complex128", "no_grad_parameters": [ "b", "a" ], "encoder_parameters": [] }
- property encoder_parameters: List[str]
Get parameters that is encoder parameters.
- Returns
set, the set of encoder parameters.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 1, 'b': 2}) >>> a.as_encoder() >>> a.encoder_parameters ['b', 'a']
- encoder_part(*names)[source]
Set which part is encoder parameters.
- Parameters
names (tuple[str]) – Parameters that will be serve as encoder.
- Returns
ParameterResolver, the parameter resolver itself.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.encoder_part('a') ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000, encoder parameters: {a, } ) >>> pr.encoder_parameters ['a']
- expression()[source]
Get the expression string of this parameter resolver.
- Returns
str, the string expression of this parameter resolver.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> import numpy as np >>> pr = PR({'a': np.pi}, np.sqrt(2)) >>> pr.expression() 'π*a + √2'
- property imag: mindquantum.core.parameterresolver.parameterresolver.ParameterResolver
Get the imaginary part of every parameter value.
- Returns
ParameterResolver, image part parameter value.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR('a', 3) + 1j * PR('a', 4) >>> pr ParameterResolver(dtype: complex128, data: [ a: (1.000000, 1.000000) ], const: (3.000000, 4.000000) ) >>> pr.imag ParameterResolver(dtype: float64, data: [ a: 1.000000 ], const: 4.000000 )
- is_anti_hermitian()[source]
To check whether the parameter value of this parameter resolver is anti hermitian or not.
- Returns
bool, whether the parameter resolver is anti hermitian or not.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a': 1}) >>> pr.is_anti_hermitian() False >>> (pr + 3).is_anti_hermitian() False >>> (pr*1j).is_anti_hermitian() True
- property is_complex: bool
Return whether the ParameterResolver instance is currently using complex coefficients.
- is_const()[source]
Check whether this parameter resolver represents a constant number.
This means that there is no parameter with non zero coefficient in this parameter resolver.
- Returns
bool, whether this parameter resolver represent a constant number.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR(1.0) >>> pr.is_const() True
- is_hermitian()[source]
To check whether the parameter value of this parameter resolver is hermitian or not.
- Returns
bool, whether the parameter resolver is hermitian or not.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR({'a': 1}) >>> pr.is_hermitian() True >>> (pr + 3).is_hermitian() True >>> (pr * 1j).is_hermitian() False
- items()[source]
Return an iterator that yields the name and value of all parameters.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 0, 'b': 1}) >>> list(a.items()) [('a', 0.0), ('b', 1.0)]
- keys()[source]
Return an iterator that yields the name and value of all parameters.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 0, 'b': 1}) >>> list(a.keys()) ['a', 'b']
- static loads(strs: str)[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 >>> ori = ParameterResolver({'a': 1, 'b': 2, 'c': 3, 'd': 4}) >>> ori.no_grad_part('a', 'b') >>> string = ori.dumps() >>> obj = ParameterResolver.loads(string) >>> print(obj) a + 2*b + 3*c + 4*d >>> 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() ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000, no grad parameters: {a, b} ) >>> pr.requires_grad_parameters []
- property no_grad_parameters: List[str]
Get parameters that do not require grad.
- Returns
set, the set of parameters that do not require grad.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 1, 'b': 2}) >>> a.no_grad() >>> a.no_grad_parameters ['b', 'a']
- 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') ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000, no grad parameters: {a, } ) >>> pr.requires_grad_parameters ['b']
- property params_name: List[str]
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 params_value: List[numbers.Number]
Get the parameters value.
- Returns
list, a list of parameters value.
Examples
>>> from mindquantum import ParameterResolver >>> pr = ParameterResolver({'a': 1, 'b': 2}) >>> pr.params_value [1.0, 2.0]
- pop(v: str)[source]
Pop out a parameter.
- Parameters
v (str) – The parameter you want to pop.
- Returns
numbers.Number, the popped out parameter value.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 1, 'b': 2}) >>> a.pop('a') 1.0
- property real: mindquantum.core.parameterresolver.parameterresolver.ParameterResolver
Get the real part of every parameter value.
- Returns
ParameterResolver, real part parameter value.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> pr = PR('a', 3) + 1j * PR('a', 4) >>> pr ParameterResolver(dtype: complex128, data: [ a: (1.000000, 1.000000) ], const: (3.000000, 4.000000) ) >>> pr.real ParameterResolver(dtype: float64, data: [ a: 1.000000 ], const: 3.000000 )
- 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') ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000, no grad parameters: {a, } ) >>> pr.requires_grad() ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000 ) >>> pr.requires_grad_parameters ['a', 'b']
- property requires_grad_parameters: List[str]
Get parameters that requires grad.
- Returns
set, the set of parameters that requires grad.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 1, 'b': 2}) >>> a.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() ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000, no grad parameters: {a, b} ) >>> pr.requires_grad_part('a') ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000, no grad parameters: {b, } ) >>> pr.requires_grad_parameters ['a']
- subs(other: Union['ParameterResolver', Dict[str, numbers.Number]])[source]
Substitute the variable value to ParameterResolver.
- Parameters
other (Union[ParameterResolver, Dict[str, numbers.Number]]) – the value of variables in parameter resolver.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver as PR >>> a = PR({'a': 1.2, 'b': 2}, 3.4) >>> a.expression() '1.2*a + 2*b + 3.4' >>> a.subs({'a': 0.3}) >>> a.expression() '2*b + 3.76'
- update(other: 'ParameterResolver')[source]
Update this parameter resolver with other parameter resolver.
- Parameters
other (ParameterResolver) – other parameter resolver.
- Raises
ValueError – If some parameters require grad and not require grad in other parameter resolver and vice versa and some parameters are encoder parameters and not encoder in other parameter resolver and vice versa.
Examples
>>> from mindquantum import ParameterResolver >>> pr1 = ParameterResolver({'a': 1}) >>> pr2 = ParameterResolver({'b': 2}) >>> pr2.no_grad() ParameterResolver(dtype: float64, data: [ b: 2.000000 ], const: 0.000000, no grad parameters: {b} ) >>> pr1.update(pr2) >>> pr1 ParameterResolver(dtype: float64, data: [ a: 1.000000, b: 2.000000 ], const: 0.000000, no grad parameters: {b, } ) >>> pr1.no_grad_parameters ['b']