mindquantum.framework.MQEncoderOnlyOps
- class mindquantum.framework.MQEncoderOnlyOps(expectation_with_grad)[源代码]
仅包含encoder线路的量子线路演化算子。通过参数化量子线路(PQC)获得对量子态的哈密顿期望。此算子只能在 PYNATIVE_MODE 下执行。
- 参数:
expectation_with_grad (GradOpsWrapper) - 接收encoder数据和ansatz数据,并返回期望值和参数相对于期望的梯度值。
- 输入:
enc_data (Tensor) - 希望编码为量子态的Tensor,其shape为 \((N, M)\) ,其中 \(N\) 表示batch大小, \(M\) 表示encoder数量。
- 输出:
Tensor,hamiltonian的期望值。
- 支持平台:
GPU
,CPU
样例:
>>> 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 MQEncoderOnlyOps >>> from mindquantum.simulator import Simulator >>> ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU") >>> circ = Circuit().ry('a', 0).h(0).rx('b', 0).as_encoder() >>> ham = Hamiltonian(QubitOperator('Z0')) >>> sim = Simulator('mqvector', 1) >>> grad_ops = sim.get_expectation_with_grad(ham, circ) >>> data = np.array([[0.1, 0.2], [0.3, 0.4]]) >>> f, g = grad_ops(data) >>> f array([[0.0978434 +0.j], [0.27219214+0.j]]) >>> net = MQEncoderOnlyOps(grad_ops) >>> f_ms = net(ms.Tensor(data)) >>> f_ms Tensor(shape=[2, 1], dtype=Float32, value= [[ 9.78433937e-02], [ 2.72192121e-01]])