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.QRamVecOps

View Source On Gitee
class mindquantum.framework.QRamVecOps(hams, circ, sim, n_thread=None)[source]

MindQuantum vector state qram operator.

A QRam operator with can directly encode classical data into quantum state vector. This ops is PYNATIVE_MODE supported only.

Note

  • For MindSpore with version less than 2.0.0, complex tensor as neural network cell input is not supported, so we should split quantum state to real and image part, and use them as input tensor. This may change when MindSpore upgrade.

  • Currently, we can not compute the gradient of the measurement result with respect to each quantum amplitude.

Parameters
  • hams (Union[Hamiltonian, List[Hamiltonian]]) – A Hamiltonian or a list of Hamiltonian that need to get expectation.

  • circ (Circuit) – The parameterized quantum circuit.

  • sim (Simulator) – The simulator to do simulation.

  • n_thread (int) – The parallel thread for evaluate a batch of initial state. If None, evolution will run in single thread. Default: None.

Inputs:
  • qs_r (Tensor) - The real part of quantum state with shape (N,M), where N is batch size and M is the length of quantum state vector.

  • qs_i (Tensor) - The image part of quantum state with shape (N,M), where N is batch size and M is the length of quantum state vector.

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

Outputs:

Tensor, The 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 QRamVecOps
>>> from mindquantum.simulator import Simulator
>>> from mindquantum.utils import random_state
>>> ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU")
>>> circ = Circuit().ry('a', 0).h(0).rx('b', 0).as_ansatz()
>>> ham = Hamiltonian(QubitOperator('Z0'))
>>> sim = Simulator('mqvector', 1)
>>> grad_ops = sim.get_expectation_with_grad(ham, circ)
>>> qs = random_state((3, 2), norm_axis=1, seed=42)
>>> qs_r, qs_i = ms.Tensor(qs.real), ms.Tensor(qs.imag)
>>> ansatz_data = np.array([1.0, 2.0])
>>> net = QRamVecOps(ham, circ, sim)
>>> f_ms = net(qs_r, qs_i, ms.Tensor(ansatz_data))
>>> f_ms
Tensor(shape=[3, 1], dtype=Float32, value=
[[-7.97555372e-02],
 [-3.92564088e-01],
 [ 4.03987877e-02]])
>>> for i in qs:
...     sim.set_qs(i)
...     f, g = grad_ops(ansatz_data)
...     print(f.real[0, 0])
-0.07975553553458492
-0.39256407750502403
0.04039878594782581