mindquantum.framework.MQEncoderOnlyOps

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

MindQuantum operator that get the expectation of a hamiltonian on a quantum state evaluated by a parameterized quantum circuit (PQC). This PQC should contains a encoder circuit only. 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 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.

Outputs:

Tensor, The expectation value of the hamiltonian.

Supported Platforms:

GPU, CPU

Examples

>>> import numpy as np
>>> from mindquantum import Circuit, Hamiltonian, QubitOperator
>>> from mindquantum import Simulator, MQEncoderOnlyOps
>>> import mindspore as ms
>>> ms.context.set_context(mode=ms.context.PYNATIVE_MODE, device_target="CPU")
>>> circ = Circuit().ry('a', 0).h(0).rx('b', 0)
>>> ham = Hamiltonian(QubitOperator('Z0'))
>>> sim = Simulator('projectq', 1)
>>> grad_ops = sim.get_expectation_with_grad(ham, circ, encoder_params_name=circ.params_name)
>>> 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]])