A hybrid quantum-classic framework for quantum computing
Project description
MindQuantum
- MindQuantum
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.
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()
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.context.set_context(mode=ms.context.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
-
Basic usage
-
Variational quantum algorithm
-
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
- Linux
pip install https://hiq.huaweicloud.com/download/mindquantum/newest/linux/mindquantum-master-cp37-cp37m-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
- Windows
pip install https://hiq.huaweicloud.com/download/mindquantum/newest/windows/mindquantum-master-cp37-cp37m-win_amd64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
- MacOSX
pip install https://hiq.huaweicloud.com/download/mindquantum/newest/macosx/mindquantum-master-cp37-cp37m-macosx_10_13_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
- Change
cp37-cp37m
tocp38-cp38
orcp39-cp39
according to your python version.- When the network is connected, dependency items are automatically downloaded during .whl package installation. (For details about other dependency items, see setup.py). In other cases, you need to manually install dependency items.
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for mindquantum-0.7.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a25ca4dfec55ec6fd11007619ce74adc58ec451f9ec47631fd9612e1d1606dd9 |
|
MD5 | 0269a66fcb21e0e7ebeaaaac356c13af |
|
BLAKE2b-256 | 4921bc086c5592d407d044d34522fcb6a2d995ad4ec2c804e3b88d69d92b3dee |
Hashes for mindquantum-0.7.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e5f19d024762bace873eb5e55cd71daf10799205438d3a9fb0fc34391ff1cd8 |
|
MD5 | 2873bc70e8d12debf7430d043e7a93ab |
|
BLAKE2b-256 | 89f54bdb3a103b6cb743aef0e474b742000de299a87222021fdf955d003d8fb1 |
Hashes for mindquantum-0.7.0-cp39-cp39-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4c55d1e53bc2713baea4b32f65ece9d83a9f63808bbb1d5b9c7f33bcbd78f12 |
|
MD5 | 3cc054dac86e1bfba0ba90d432be7f26 |
|
BLAKE2b-256 | 8c8b18c8a9f066e7d11139ac7f6680ffad1292410836f218ff23271a968fa3bb |
Hashes for mindquantum-0.7.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de043d45e6535d24da0fd6395f7795441314002a4bed5460bdb03662871d121d |
|
MD5 | bdc832125cf7127412240b9deb8daf36 |
|
BLAKE2b-256 | 0eca4f5800639d11b47ee5980e3d6f3d2cd84d8c69a4ea57dd3edda40d5341a1 |
Hashes for mindquantum-0.7.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4b9d38117f16ef3a3d24cd1d6404ba50dc4b128ddf0360247f7c1bee0c57ccf |
|
MD5 | 6b0c4e2a568bca82cc9361d618a3945f |
|
BLAKE2b-256 | 68d1db59cd5d00aad22f74c1729335483f0f4e3d7e60ecf5f1b56e4c65dff3ca |
Hashes for mindquantum-0.7.0-cp38-cp38-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cab323b91c10a2c280468c7cef5121446e330851ff47a89fa0677a4a0f9eb0d8 |
|
MD5 | 5e0d7aba6e8051c7ad2be90958dc6dc4 |
|
BLAKE2b-256 | 0660708783c1ad7da6c23904bc83cab73b2a0f581f52931f56a0e2e7845b81bf |
Hashes for mindquantum-0.7.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3403802cfd9c3b0dee662f9c73781a6a7e2465101ec7517e25f5f1083eb3036d |
|
MD5 | 047f62954232e5f61b2c627d2a7fe82b |
|
BLAKE2b-256 | 12a288a609f8fc253987beb134c1102e444b18d2c51121e2596a4d97e9b2d9aa |
Hashes for mindquantum-0.7.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e37a089ff0f3ba82213c532c19e63a613a34989543213921b612e683c6e839cf |
|
MD5 | ee5a034bfcba0e188f4603983155518a |
|
BLAKE2b-256 | fe0bcc3adca302a2dc7b1f1329f481303ecf42f6e53c515ad4caf869b2c3d6b9 |
Hashes for mindquantum-0.7.0-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9115cfd8c1765d02133e6b9c545cab90d130d724ec436364e5b06a33ef30a8c1 |
|
MD5 | 4094d6863d128b5a6fdc04441dd5a191 |
|
BLAKE2b-256 | 3b2bc1553bf63efaece927cb86bdfff188066b17b48c8d66bc0cf795c2001fab |