1 Star 0 Fork 3.6K

naziraa / mindquantum

forked from MindSpore / mindquantum 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 9.04 KB
一键复制 编辑 原始数据 按行查看 历史
lvmingfu 提交于 2022-09-26 17:00 . modify code formats for master

MindQuantum

API Tutorial Tutorial Tutorial Tutorial Release LICENSE PRs Welcome Documentation Status

查看中文

What is MindQuantum

MindQuantum is a general quantum computing framework developed by MindSpore and HiQ, that can be used to build and train different quantum neural networks. Thanks to the powerful algorithm of quantum software group of Huawei and High-performance automatic differentiation ability of MindSpore, MindQuantum can efficiently handle problems such as quantum machine learning, quantum chemistry simulation, and quantum optimization, which provides an efficient platform for researchers, teachers and students to quickly design and verify quantum machine learning algorithms. MindQuantum Architecture

First experience

Build parameterized quantum circuit

The below example shows how to build a parameterized quantum circuit.

from mindquantum import *
import numpy as np

encoder = Circuit().h(0).rx({'a0': 2}, 0).ry('a1', 1)
print(encoder)
print(encoder.get_qs(pr={'a0': np.pi / 2, 'a1': np.pi / 2}, ket=True))

Then you will get,

q0: ────H───────RX(2*a0)──

q1: ──RY(a1)──────────────

-1/2j¦00⟩
-1/2j¦01⟩
-1/2j¦10⟩
-1/2j¦11⟩

In jupyter notebook, we can just call svg() of any circuit to display the circuit in svg picture (dark and light mode are also supported).

circuit = (qft(range(3)) + BarrierGate(True)).measure_all()
circuit.svg()
Circuit SVG

Train quantum neural network

ansatz = CPN(encoder.hermitian(), {'a0': 'b0', 'a1': 'b1'})
sim = Simulator('projectq', 2)
ham = Hamiltonian(-QubitOperator('Z0 Z1'))
grad_ops = sim.get_expectation_with_grad(
    ham,
    encoder + ansatz,
    encoder_params_name=encoder.params_name,
    ansatz_params_name=ansatz.params_name,
)

import mindspore as ms

ms.set_context(mode=ms.PYNATIVE_MODE, device_target='CPU')
net = MQLayer(grad_ops)
encoder_data = ms.Tensor(np.array([[np.pi / 2, np.pi / 2]]))
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(encoder_data)
print(dict(zip(ansatz.params_name, net.trainable_params()[0].asnumpy())))

The trained parameters are,

{'b1': 1.5720831, 'b0': 0.006396801}

Tutorials

  1. Basic usage

  2. Variational quantum algorithm

  3. GENERAL QUANTUM ALGORITHM

API

For more API, please refer to MindQuantum API.

Installation

Confirming System Environment Information

  • The hardware platform should be CPU with avx2 supported.
  • Refer to MindQuantum Installation Guide, install MindSpore, version 1.4.0 or later is required.
  • See setup.py for the remaining dependencies.

Install by Source Code

1.Download Source Code from Gitee

cd ~
git clone https://gitee.com/mindspore/mindquantum.git

2.Compiling MindQuantum

cd ~/mindquantum
bash build.sh
cd output
pip install mindquantum-*.whl

Install by pip

Install MindSpore

Please refer to MindSpore installation guide to install MindSpore that at least 1.4.0 version.

Install MindQuantum

pip install mindquantum

Verifying Successful Installation

Successfully installed, if there is no error message such as No module named 'mindquantum' when execute the following command:

python -c 'import mindquantum'

Install with Docker

Mac or Windows users can install MindQuantum through Docker. Please refer to Docker installation guide

Note

Please set the parallel core number before running MindQuantum scripts. For example, if you want to set the parallel core number to 4, please run the command below:

export OMP_NUM_THREADS=4

For large servers, please set the number of parallel kernels appropriately according to the size of the model to achieve optimal results.

Building binary wheels

If you would like to build some binary wheels for redistribution, please have a look to our binary wheel building guide

Quick Start

For more details about how to build a parameterized quantum circuit and a quantum neural network and how to train these models, see the MindQuantum Tutorial.

Docs

More details about installation guide, tutorials and APIs, please see the User Documentation.

Community

Governance

Check out how MindSpore Open Governance works.

Contributing

Welcome contributions. See our Contributor Wiki for more details.

How to cite

When using MindQuantum for research, please cite:

@misc{mq_2021,
    author      = {MindQuantum Developer},
    title       = {MindQuantum, version 0.6.0},
    month       = {March},
    year        = {2021},
    url         = {https://gitee.com/mindspore/mindquantum}
}

License

Apache License 2.0

Python
1
https://gitee.com/naziraa/mindquantum.git
git@gitee.com:naziraa/mindquantum.git
naziraa
mindquantum
mindquantum
master

搜索帮助