mindquantum.core.parameterresolver
Parameter resolver. Help to generate parameter in mindspore quantum.
- class mindquantum.core.parameterresolver.ParameterResolver(data=None, const=None)[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.
Examples
>>> from mindquantum.core.parameterresolver import ParameterResolver >>> pr = ParameterResolver({'a': 0.3}) >>> pr['b'] = 0.5 >>> pr.no_grad_part('a') {'a': 0.3, 'b': 0.5}, const: 0.0 >>> pr *= 2 >>> pr {'a': 0.6, 'b': 1.0}, const: 0.0 >>> pr.expression() '3/5*a + b' >>> pr.const = 0.5 >>> pr.expression() '3/5*a + b + 1/2' >>> pr.no_grad_parameters {'a'} >>> ParameterResolver(3) {}, const: 3.0 >>> ParameterResolver('a') {'a': 1.0}, const: 0.0
- property ansatz_parameters
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 {'a', 'b'}
- combination(other)[source]
Apply linear combination between this parameter resolver with input parameter resolver.
- Parameters
other (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) {}, const: 8.0
- 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 >>> import numpy as np >>> pr = PR({'a' : 1, 'b': 1j}, dtype=np.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
- 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}, const=3 + 4j, dtype=complex) >>> 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": "complex", "no_grad_parameters": [ "b", "a" ], "encoder_parameters": [] }
- property encoder_parameters
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 {'a', 'b'}
- 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') {'a': 1.0, 'b': 2.0}, const: 0.0 >>> 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
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 {'a': (1+1j)}, const: (3+4j) >>> pr.imag {'a': 1.0}, const: 4.0
- 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
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)[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': 1, 'b': 2, 'c': 3, 'd': 4}, const: 0 >>> 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.0, 'b': 2.0}, const: 0.0 >>> pr.requires_grad_parameters set()
- property no_grad_parameters
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 {'a', 'b'}
- 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.0, 'b': 2.0}, const: 0.0 >>> 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']
- pop(v)[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
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 {'a': (1+1j)}, const: (3+4j) >>> pr.real {'a': 1.0}, const: 3.0
- 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.0, 'b': 2.0}, const: 0.0 >>> pr.requires_grad() {'a': 1.0, 'b': 2.0}, const: 0.0 >>> pr.requires_grad_parameters {'a', 'b'}
- property requires_grad_parameters
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() {'a': 1.0, 'b': 2.0}, const: 0.0 >>> pr.requires_grad_part('a') {'a': 1.0, 'b': 2.0}, const: 0.0 >>> pr.requires_grad_parameters {'a'}
- update(other)[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() {'b': 2.0}, const: 0.0 >>> pr1.update(pr2) >>> pr1 {'a': 1.0, 'b': 2.0}, const: 0.0 >>> pr1.no_grad_parameters {'b'}