mindquantum.framework.MQN2Ops

class mindquantum.framework.MQN2Ops(expectation_with_grad)[source]

MindQuantum operator.

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

\[O = \left|\left<\varphi\right| U^\dagger_l H U_r\left|\psi\right>\right|^2\]
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.

Inputs:
  • enc_data (Tensor) - Tensor of encoder data with shape \((N, M)\) that you want to encode into quantum state, where \(N\) means the batch size and \(M\) means the number of encoder parameters.

  • 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 MQN2Ops
>>> from mindquantum.simulator import Simulator
>>> ms.context.set_context(mode=ms.context.PYNATIVE_MODE, device_target="CPU")
>>> enc = Circuit().ry('a', 0).as_encoder()
>>> ans = Circuit().h(0).rx('b', 0)
>>> ham = Hamiltonian(QubitOperator('Z0'))
>>> sim = Simulator('projectq', 1)
>>> grad_ops = sim.get_expectation_with_grad(ham, enc + ans)
>>> enc_data = np.array([[0.1]])
>>> ans_data = np.array([0.2])
>>> f, g_enc, g_ans = grad_ops(enc_data, ans_data)
>>> np.abs(f) ** 2
array([[0.00957333]])
>>> net = MQN2Ops(grad_ops)
>>> f_ms = net(ms.Tensor(enc_data), ms.Tensor(ans_data))
>>> f_ms
Tensor(shape=[1, 1], dtype=Float32, value=
[[ 9.57333017e-03]])