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]