Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindquantum.framework.MQN2Ops

View Source On Gitee
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=|φ|UlHUr|ψ|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.set_context(mode=ms.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('mqvector', 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]])