mindquantum.framework.MQN2EncoderOnlyOps

View Source On Gitee
class mindquantum.framework.MQN2EncoderOnlyOps(expectation_with_grad)[source]

MindQuantum operator.

A quantum circuit evolution operator that only include encoder circuit, who return the square of absolute value of given hamiltonian w.r.t final state of parameterized quantum circuit (PQC). This ops is PYNATIVE_MODE supported only.

Parameters

expectation_with_grad (GradOpsWrapper) – a grad ops that receive encoder data and ansatz data and return the square of absolute value of expectation value and gradient value of parameters respect to expectation.

Inputs:
  • ans_data (Tensor) - Tensor with shape \(N\) for ansatz circuit, where \(N\) means the number of ansatz parameters.

Outputs:

Tensor, The square of absolute value of expectation value of the hamiltonian.

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 MQN2EncoderOnlyOps
>>> 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)
>>> np.abs(f) ** 2
array([[0.00957333],
       [0.07408856]])
>>> net = MQN2EncoderOnlyOps(grad_ops)
>>> f_ms = net(ms.Tensor(data))
>>> f_ms
Tensor(shape=[2, 1], dtype=Float32, value=
[[ 9.57333017e-03],
 [ 7.40885586e-02]])