- MindQuantum
MindQuantum is a new-generation quantum computing framework based on MindSpore, focusing on the implementation of NISQ algorithms. It combines a high-performance quantum computing simulator with the parallel automatic differentiation capability of MindSpore. MindQuantum is easy-to-use with ultra-high performance. It can efficiently handle problems like quantum machine learning, quantum chemistry simulation, and quantum optimization. MindQuantum provides an efficient platform for researchers, teachers and students to quickly design and verify quantum algorithms, making quantum computing at your fingertips.
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('light')ansatz = CPN(encoder.hermitian(), {"a0": "b0", "a1": "b1"})
sim = Simulator("mqvector", 2)
ham = Hamiltonian(-QubitOperator("Z0 Z1"))
grad_ops = sim.get_expectation_with_grad(
ham,
encoder.as_encoder() + ansatz.as_ansatz(),
)
import mindspore as ms
ms.set_device("CPU")
ms.set_context(mode=ms.PYNATIVE_MODE)
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}-
Beginner Tutorial
-
Middle Level Tutorial
-
Advanced Tutorial
-
General Quantum Algorithms
-
NISQ Algorithms
For more API, please refer to MindQuantum API.
- 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.
1.Download Source Code from AtomGit
cd ~
git clone https://atomgit.com/mindspore/mindquantum.git2.Compiling MindQuantum
cd ~/mindquantum
bash build.sh
cd output
pip install mindquantum-*.whlPlease refer to the MindSpore installation guide to install an appropriate version of MindSpore.
pip install mindquantum-
Clone source.
cd ~ git clone https://atomgit.com/mindspore/mindquantum.git
-
Build MindQuantum
For linux system, please make sure your cmake version >= 3.18.3, and then run code:
cd ~/mindquantum bash build.sh --gitee
Here
--giteeis telling the script to download third party from gitee. If you want to download from github, you can ignore this flag. If you want to build under GPU, please make sure you have install CUDA 11.x and the GPU driver, and then run code:cd ~/mindquantum bash build.sh --gitee --gpu
For windows system, please make sure you have install MinGW-W64 and CMake >= 3.18.3, and then run:
cd ~/mindquantum ./build.bat /Gitee
For Mac system, please make sure you have install openmp and CMake >= 3.18.3, and then run:
cd ~/mindquantum bash build.sh --gitee
-
Install whl
Please go to output, and install mindquantum wheel package by
pip.
Successfully installed, if there is no error message such as No module named 'mindquantum' when execute the following command:
python -c 'import mindquantum'Mac or Windows users can install MindQuantum through Docker. Please refer to Docker installation guide
The CPU simulators parallelize with OpenMP. The number of threads used for an operation grows with the size of the simulated state (one thread per 8192 amplitudes), up to the OpenMP thread limit, which defaults to the number of CPU cores. Small circuits therefore never fan out across a whole many-core server, and large ones use every core by default.
To lower the limit, for example to 4 threads on a shared host, set OMP_NUM_THREADS before starting Python:
export OMP_NUM_THREADS=4On servers with more than a few dozen cores, capping the limit around the number of cores of one CPU socket usually gives the best throughput unless you simulate more than about 24 qubits.
If you would like to build some binary wheels for redistribution, please have a look to our binary wheel building guide
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.
More details about installation guide, tutorials and APIs, please see the User Documentation.
Check out how MindSpore Open Governance works.
Welcome contributions. See our Contributor Guide for more details.
Welcome to access the white paper of MindQuantum. When using MindQuantum for research, please cite:
@misc{xu2024mindspore,
title={MindSpore Quantum: A User-Friendly, High-Performance, and AI-Compatible Quantum Computing Framework},
author={Xusheng Xu and Jiangyu Cui and Zidong Cui and Runhong He and Qingyu Li and Xiaowei Li and Yanling Lin and Jiale Liu and Wuxin Liu and Jiale Lu and others},
year={2024},
eprint={2406.17248},
archivePrefix={arXiv},
primaryClass={quant-ph},
url={https://arxiv.org/abs/2406.17248},
}
