mindquantum.framework.QRamVecOps
- 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
]]) – AHamiltonian
or a list ofHamiltonian
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