mindquantum.framework.MQAnsatzOnlyLayer
- class mindquantum.framework.MQAnsatzOnlyLayer(expectation_with_grad, weight='normal')[source]
MindQuantum trainable layer.
Quantum neural network only include ansatz circuit. The ansatz circuit act as trainable circuit.
- Parameters
expectation_with_grad (GradOpsWrapper) – a grad ops that receive encoder data and ansatz data and return the expectation value and gradient value of parameters respect to expectation.
weight (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from 'TruncatedNormal',
'Normal'
,'Uniform'
,'HeUniform'
and'XavierUniform'
distributions as well as constant 'One' and 'Zero' distributions are possible. Alias'xavier_uniform'
,'he_uniform'
,'ones'
and'zeros'
are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default:'normal'
.
- Outputs:
Tensor, The expectation value of the hamiltonian.
- Raises
ValueError – If length of shape of weight is not equal to 1 and shape[0] of weight is not equal to weight_size.
- Supported Platforms:
GPU
,CPU
Examples
>>> import numpy as np >>> import mindspore as ms >>> from mindquantum.core.circuit import Circuit >>> from mindquantum.core.operators import Hamiltonian, QubitOperator >>> from mindquantum.framework import MQAnsatzOnlyLayer >>> from mindquantum.simulator import Simulator >>> ms.set_seed(42) >>> ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU") >>> circ = Circuit().ry('a', 0).h(0).rx('b', 0) >>> ham = Hamiltonian(QubitOperator('Z0')) >>> sim = Simulator('mqvector', 1) >>> grad_ops = sim.get_expectation_with_grad(ham, circ) >>> net = MQAnsatzOnlyLayer(grad_ops) >>> opti = ms.nn.Adam(net.trainable_params(), learning_rate=0.1) >>> train_net = ms.nn.TrainOneStepCell(net, opti) >>> for i in range(100): ... train_net() >>> net.weight.asnumpy() array([-1.5720805e+00, 1.7390326e-04], dtype=float32) >>> net() Tensor(shape=[1], dtype=Float32, value= [-9.99999166e-01])