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

View Source On Gitee
class mindquantum.framework.QRamVecLayer(ham, circ, sim, n_thread=None, weight='normal')[source]

Quantum neural network include a qram as quantum state encoder and ansatz circuit.

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
  • ham (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.

  • weight (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from 'TruncatedNormal', 'Normal', 'Uniform', 'HeUniform' and 'XavierUniform' distributions as well as constant 'One' and 'Zero' distributions are possible. Alias 'xavier_uniform', 'he_uniform', 'ones' and 'zeros' are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: 'normal'.

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.

Outputs:

Tensor, The expectation value of the hamiltonian.

Raises

ValueError – If length of shape of weight is not equal to 1 or shape[0] of weight is not equal to weight_size.

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 QRamVecLayer
>>> from mindquantum.simulator import Simulator
>>> ms.set_seed(42)
>>> ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU")
>>> ans = Circuit().ry('a', 0).rx('b', 0).as_ansatz()
>>> ham = Hamiltonian(QubitOperator('Z0'))
>>> sim = Simulator('mqvector', 1)
>>> grad_ops = sim.get_expectation_with_grad(ham, ans)
>>> qs = np.array([[1.0, 2.0]])/np.sqrt(5)
>>> qs_r, qs_i = ms.Tensor(qs.real), ms.Tensor(qs.imag)
>>> net =  QRamVecLayer(ham, ans, sim)
>>> opti = ms.nn.Adam(net.trainable_params(), learning_rate=0.1)
>>> train_net = ms.nn.TrainOneStepCell(net, opti)
>>> for i in range(100):
...     train_net(qs_r, qs_i)
>>> net.weight.asnumpy()
array([ 9.2439342e-01, -3.3963533e-04], dtype=float32)
>>> net(qs_r, qs_i)
Tensor(shape=[1, 1], dtype=Float32, value=
[[-9.99995708e-01]])
>>> sim.set_qs(qs[0])
>>> sim.apply_circuit(ans, pr=net.weight.asnumpy())
>>> print(sim.get_qs())
[0.0014509 +1.69817484e-04j 0.99999893+2.46388577e-07j]