mindspore-ai / mindspore

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.
https://gitee.com/mindspore/mindspore
Apache License 2.0
4.24k stars 700 forks source link

Not registered CPU kernel: op[Evolution]! #175

Closed marijascekic closed 2 years ago

marijascekic commented 2 years ago

I am trying to execute the next simple code:

from mindquantum.gate import X from mindquantum.circuit import Circuit from mindquantum.circuit import StateEvolution circuit = Circuit() circuit += X.on(0) StateEvolution(circuit).sampling(100)

I am using Windows 10 , mindspore 1.5.0, mindquantum 0.3.1 . Does anyone know how to fix the following error:

RuntimeError Traceback (most recent call last)

in 9 circuit += X.on(0) 10 ---> 11 StateEvolution(circuit).sampling(100) ~\Anaconda3\lib\site-packages\mindquantum\circuit\state_evolution.py in sampling(self, shots, param, show) 118 {'00': 29, '01': 24, '10': 23, '11': 24} 119 """ --> 120 final_state = self.final_state(param) 121 amps = normalize(np.abs(final_state)**2)**2 122 sampling = Counter(np.random.choice(self.index, p=amps, size=shots)) ~\Anaconda3\lib\site-packages\mindquantum\circuit\state_evolution.py in final_state(self, param, ket) 78 "Require a non parameterized quantum circuit, since not parameters specified." 79 ) ---> 80 return self.evol() if not ket else '\n'.join( 81 ket_string(self.evol())) 82 if isinstance(param, np.ndarray): ~\Anaconda3\lib\site-packages\mindquantum\nn\evolution.py in __call__(self, tmp) 83 "Parameterized circuit shuold have parameter input.") 84 tmp = Tensor(np.array([0]).astype(np.float32)) ---> 85 state = super().__call__(tmp) 86 state = state.asnumpy() 87 state = state[:, 0] + state[:, 1] * 1j ~\Anaconda3\lib\site-packages\mindspore\ops\primitive.py in __call__(self, *args) 245 if should_elim: 246 return output --> 247 return _run_op(self, self.name, args) 248 249 def __getstate__(self): ~\Anaconda3\lib\site-packages\mindspore\common\api.py in wrapper(*arg, **kwargs) 76 @wraps(fn) 77 def wrapper(*arg, **kwargs): ---> 78 results = fn(*arg, **kwargs) 79 80 def _convert_data(data): ~\Anaconda3\lib\site-packages\mindspore\ops\primitive.py in _run_op(obj, op_name, args) 680 def _run_op(obj, op_name, args): 681 """Single op execution function supported by ge in PyNative mode.""" --> 682 output = real_run_op(obj, op_name, args) 683 return output RuntimeError: mindspore\ccsrc\backend\kernel_compiler\cpu\cpu_kernel_factory.cc:169 GetSupportedKernelAttrList] Not registered CPU kernel: op[Evolution]!
donghufeng commented 2 years ago

Please use MindSpore1.4.0 or below.

marijascekic commented 2 years ago

Thank you for your replay. When I use version 1.4.0. or 1.3.0 also I get an error message "Not registered CPU kernel: op[Evolution]! ". Using the following versions 1.1.0 ,1.1.1, 1.2.0rc1, 1.2.0, 1.2.1 I got a similar error message "SetKernelInfo] Operator[Evolution] is not support". For the version 1.0.1 and below I got error message "AttributeError: type object 'Validator' has no attribute 'check_tensors_dtypes_same_and_valid'".

donghufeng commented 2 years ago

I really suggest to use the new developed master version. In the old version, the simulator was implement in a mindspore cpu operator, so that use graph mode of mindspore. But now, we move the simulator cpp code in MindQuantum its own, and use pynative mode to build classical quantum neural network. For the new version of mindquantum, you can download it from following link based on your system.

You can find the new API in https://www.mindspore.cn/mindquantum/api/zh-CN/master/mindquantum.core.html Here is the code to implement your task in new version of mindquantum, please take a look.

from mindquantum import *

circuit = Circuit()
circuit += H.on(0)
circuit += Measure('q0').on(0)
print("Circuit:")
print(circuit)

sim = Simulator('projectq', 1)
res = sim.sampling(circuit, shots=100)
print("sampling result:")
print(res)

And the nice outputs are:

Circuit:
q0: ──H────M(q0)──

sampling result:
shots: 100
Keys: q0│0.00    0.14        0.28        0.42        0.56         0.7
────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
       0│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
        │
       1│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
        │
{'0': 44, '1': 56}
marijascekic commented 2 years ago

After installing the master version, everything is fine and the output is nicer than in the older versions. Thank you very much!