This project named QudiTop is a numerically quantum simulator for qudit system based on MindSpore. QudiTop includes a variety of qudit gates, circuit and expectation etc. Especially for qudit gate, we can easily define the objective qudits, control qudits and corresponding control states, which allow you to simulate complex control in qudit circuit. Besides, QudiTop supports the difference of parameters, which allows you to run VQE (variational quantum eigen-solver) application well.
The core source code lies in quditop/
folder and examples in examples/
.
.
├── README.md
├── Math_for_Gates_and_Circuit.md # The matrix of gates and so on.
├── setup.py
├── ...
├── examples
│ ├── demo_basic.ipynb # Demonstrate basic functions.
│ └── demo_vqe.ipynb # Demonstrate the VQE application.
└── quditop
├── circuit.py # Define the Circuit class.
├── common.py # Common function.
├── evolution.py # Evolution of quantum state.
├── gates.py # Quantum gates.
├── global_var.py # Define some global configuration.
└── utils.py # Some tool functions.
To install this package:
git clone https://github.com/forcekeng/QudiTop.git
python setup.py install
Now this package is installed on your computer. To uninstall it, run pip uninstall quditop
at terminal.
These qudits gates are realized, the matrix definitions follows github repository GhostArtyom/QuditVQE.
1-qudit gates
Multi-qudits gates
For gates that contain parameters, such as $RX$, calculating the difference is supported to run VQE applications. A detailed introduction and mathematical matrix can be seen in document Math_for_Gates_and_Circuit. Comparing with qubit, the qudit system is more complex, not only more state level has to be solved, but also the control states may change. In qubit system we normally use to the state $1$ as the control state, while in qudit system, such for $d=3$, there're 3 states ($0,1$ and $2$), we can use $1$ or $2$ as control state, or even use state $0$. So a common format of gate is:
H(dim).on(obj_qudits, ctrl_qudits, ctrl_states)
X(dim, ind).on(obj_qudits, ctrl_qudits, ctrl_states)
RX(dim, ind, pr).on(obj_qudits, ctrl_qudits, ctrl_states)
The dim
is the dimension of the qudit, for qubit the dimension is 2, dimension is $3$ means there are 3 states for qudit.
The Quditop
framework allows the dim
is at most $10$, normally we use dim
$=3,4$ or $5$ in qudit simulation.
class Circuit
is implemented to simulate the quantum circuit. Interfaces of Circuit
include but not limit to:
append
, +
and '+=' for Circuit
and qudit gate(s).as_encoder
and as_ansatz
to allow or inhibit the difference of parameters.get_qs
and set_qs
to get or set the last state or set the initial state of circuit.We can build a qudit circuit simply by
dim = 2
n_qudits = 4
circ = Circuit(dim, n_qudits, gates=[
H(dim).on(0),
X(dim, [0,1]).on(0, [2,3], [1, 1])
])
We don't need a single class for hamiltonian. Because essentially we can treat hamiltonian as a series of gates that act on last quantum state. Specifically, we can write a hamiltonian as
# Assuming we act X and Y gates on qudit 1 and 2 respectively
xg = X(dim=3).on(1)
yg = Y(dim=3).on(2)
# a and b are the const coefficients of the hamiltonian
a = 1.0
b = 2.0
hams = [(a, xg), (b, yg)]
class Expectation
is used to get the measure expectation with given hamiltonian and circuit. Mathematically, expectation $h = \langle\psi |H| \psi\rangle$, where $O$ is the hamiltonian and $\psi$ is the last quantum state.
expect_fn = Expectation(hams) # Define expectation
last_qs = circ() # Get the last state of circuit
out = expect_fn(last_qs) # Get the expectation
This quantum simulator is realized based on PyTorch. Comparing with deep learning, you can just treat one qudit gate as a layer of network. In fact, the gate is subclass of torch.nn.Module
. So you can easily use the loss.backward()
to get the gradient of parameter, and optimizer.step()
to update parameter with specific optimizer. If you never or new to PyTorch, the documents of PyTorch are recommended. You can see how to train a hybrid quantum-classical VQE (variational quantum eigensolver) in file example/demo_vqe.ipynb
.
from quditop.gates import H, X, Y, Z, RX, RY, RZ, UMG
from quditop.circuit import Circuit
dim = 2
n_qudits = 4
circ = Circuit(dim, n_qudits, gates=[
H(dim).on(0),
X(dim, [0,1]).on(0, [2,3], [1, 1]),
Y(dim).on(1),
Z(dim).on(1, 2, 1),
RX(dim, pr='param').on(3),
RY(dim, pr=1.0).on(2, 3, 1),
X(dim).on(1, [0, 2, 3], 1),
RY(dim, pr=2.0).on(3, [0, 1, 2], 1)
])
print(circ)
print(f"quantum state:\n{circ.get_qs(ket=True)}")
The output should like this:
Circuit(
(gates): ModuleList(
(0): H(2|0)
(1): X(2 [0 1]|0 <-: 2 3 - 1 1)
(2): Y(2 [0 1]|1)
(3): Z(2 [0 1]|1 <-: 2 - 1)
(4): RX(2 [0 1] param|3)
(5): RY(2 [0 1] _param_|2 <-: 3)
(6): X(2 [0 1]|1 <-: 0 2 3 - 1 1 1)
(7): RY(2 [0 1] _param_|3 <-: 0 1 2)
)
)
quantum state:
0.6205j¦0010⟩
0.6205j¦0011⟩
0.2975¦1010⟩
0.2975¦1011⟩
0.1625¦1101⟩
0.1625¦1110⟩
More details in the folder of examples/
.
[2] pyquil.simulation._numpy — pyQuil 4.0.3 documentation (rigetti.com)
[3] 【开发者群英会】在MindQuantum中实现任意维度的通用qudit量子线路模拟 · Issue #I7Q1UV · MindSpore/community - Gitee.com
[4] QuditVQE/QuditSim at main · GhostArtyom/QuditVQE (github.com)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。