qsl-quantum 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qsl_quantum-0.1.0/LICENSE +21 -0
- qsl_quantum-0.1.0/PKG-INFO +207 -0
- qsl_quantum-0.1.0/README.md +173 -0
- qsl_quantum-0.1.0/pyproject.toml +50 -0
- qsl_quantum-0.1.0/qsl/__init__.py +59 -0
- qsl_quantum-0.1.0/qsl/__main__.py +134 -0
- qsl_quantum-0.1.0/qsl/backends/__init__.py +24 -0
- qsl_quantum-0.1.0/qsl/backends/base.py +105 -0
- qsl_quantum-0.1.0/qsl/backends/ibm.py +574 -0
- qsl_quantum-0.1.0/qsl/backends/registry.py +93 -0
- qsl_quantum-0.1.0/qsl/backends/simulator.py +138 -0
- qsl_quantum-0.1.0/qsl/compiler/__init__.py +15 -0
- qsl_quantum-0.1.0/qsl/compiler/compiler.py +289 -0
- qsl_quantum-0.1.0/qsl/compiler/dsl.py +231 -0
- qsl_quantum-0.1.0/qsl/compiler/program.py +143 -0
- qsl_quantum-0.1.0/qsl/core/__init__.py +16 -0
- qsl_quantum-0.1.0/qsl/core/grover.py +404 -0
- qsl_quantum-0.1.0/qsl/core/parser.py +345 -0
- qsl_quantum-0.1.0/qsl/core/state.py +611 -0
- qsl_quantum-0.1.0/qsl/utils/__init__.py +1 -0
- qsl_quantum-0.1.0/qsl/utils/exceptions.py +180 -0
- qsl_quantum-0.1.0/qsl/utils/validation.py +178 -0
- qsl_quantum-0.1.0/qsl_quantum.egg-info/PKG-INFO +207 -0
- qsl_quantum-0.1.0/qsl_quantum.egg-info/SOURCES.txt +34 -0
- qsl_quantum-0.1.0/qsl_quantum.egg-info/dependency_links.txt +1 -0
- qsl_quantum-0.1.0/qsl_quantum.egg-info/entry_points.txt +2 -0
- qsl_quantum-0.1.0/qsl_quantum.egg-info/requires.txt +9 -0
- qsl_quantum-0.1.0/qsl_quantum.egg-info/top_level.txt +2 -0
- qsl_quantum-0.1.0/setup.cfg +4 -0
- qsl_quantum-0.1.0/setup.py +68 -0
- qsl_quantum-0.1.0/tests/test_backends.py +98 -0
- qsl_quantum-0.1.0/tests/test_compiler.py +274 -0
- qsl_quantum-0.1.0/tests/test_grover.py +217 -0
- qsl_quantum-0.1.0/tests/test_integration.py +154 -0
- qsl_quantum-0.1.0/tests/test_parser.py +219 -0
- qsl_quantum-0.1.0/tests/test_state.py +398 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Song Ziming
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: qsl-quantum
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Quantum Search Language - DSL for Grover search with simulator and IBM Quantum backends
|
|
5
|
+
Home-page: https://gitee.com/song_jack/qsl
|
|
6
|
+
Author: Song Ziming
|
|
7
|
+
Author-email: Song Ziming <15011462616@163.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Intended Audience :: Education
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Provides-Extra: ibm
|
|
24
|
+
Requires-Dist: qiskit>=1.0.0; extra == "ibm"
|
|
25
|
+
Requires-Dist: qiskit-aer>=0.14.0; extra == "ibm"
|
|
26
|
+
Requires-Dist: qiskit-ibm-runtime>=0.20.0; extra == "ibm"
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
30
|
+
Dynamic: author
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
Dynamic: requires-python
|
|
34
|
+
|
|
35
|
+
<h1 align="center">QSL — 量子搜索语言</h1>
|
|
36
|
+
|
|
37
|
+
<p align="center">
|
|
38
|
+
<b>用一句话描述你想找什么,剩下的交给量子计算。</b>
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
<p align="center">
|
|
42
|
+
<img src="https://img.shields.io/badge/Python-3.9+-blue?logo=python&logoColor=white" alt="Python">
|
|
43
|
+
<img src="https://img.shields.io/badge/License-MIT-green" alt="License">
|
|
44
|
+
<img src="https://img.shields.io/badge/tests-123%20passed-brightgreen" alt="Tests">
|
|
45
|
+
<img src="https://img.shields.io/badge/dependencies-zero-success" alt="Dependencies">
|
|
46
|
+
</p>
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 这是什么?
|
|
51
|
+
|
|
52
|
+
一个 **Python 库**,让你用日常的布尔表达式描述搜索条件,自动在量子计算机(或本地模拟器)上找到答案。
|
|
53
|
+
|
|
54
|
+
> 传统做法:手写量子电路,理解 H 门、CNOT 门、相位翻转……
|
|
55
|
+
> QSL 的做法:写 `x0 & ~x1`,然后等结果。
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## 5 秒看懂
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from qsl import QSLProgram, compile_and_run
|
|
63
|
+
|
|
64
|
+
# 我想找:3 个变量中,哪些赋值同时满足这三个条件?
|
|
65
|
+
program = QSLProgram(
|
|
66
|
+
name="3-SAT",
|
|
67
|
+
n_qubits=3,
|
|
68
|
+
premises=[
|
|
69
|
+
"x0 | ~x1", # x0 为真,或者 x1 为假
|
|
70
|
+
"x1 | x2", # x1 为真,或者 x2 为真
|
|
71
|
+
"~x0 | ~x2", # x0 为假,或者 x2 为假
|
|
72
|
+
],
|
|
73
|
+
shots=10
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
result = compile_and_run(program)
|
|
77
|
+
print(result.get_solutions())
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**输出:**
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
[3, 4] # 即二进制 011 和 100 —— 这两个就是答案
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
就这么简单。不需要懂量子力学。
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## 两种运行模式
|
|
91
|
+
|
|
92
|
+
| 模式 | 说明 | 需要什么 |
|
|
93
|
+
|------|------|----------|
|
|
94
|
+
| **本地模拟** | 在你的电脑上模拟量子计算 | 零依赖,纯 Python |
|
|
95
|
+
| **真实硬件** | 连接 IBM 量子计算机跑真任务 | `pip install qsl-quantum[ibm]` + IBM 账号 |
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
# 模式 1:本地模拟(默认)
|
|
99
|
+
compiler = QSLCompiler(backend="simulator")
|
|
100
|
+
result = compiler.compile_and_run(program)
|
|
101
|
+
|
|
102
|
+
# 模式 2:IBM 真实量子芯片
|
|
103
|
+
compiler = QSLCompiler(backend="ibm", backend_options={"token": "your_token"})
|
|
104
|
+
result = compiler.compile_and_run(program)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 还能更简单 —— DSL 文本语法
|
|
110
|
+
|
|
111
|
+
不想写 Python?用配置文件式的文本语法:
|
|
112
|
+
|
|
113
|
+
```text
|
|
114
|
+
program "图着色" {
|
|
115
|
+
qubits: 3
|
|
116
|
+
|
|
117
|
+
premise {
|
|
118
|
+
x0 ^ x1 # 相邻顶点不能同色
|
|
119
|
+
x1 ^ x2
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
main {
|
|
123
|
+
algorithm: grover
|
|
124
|
+
shots: 10
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from qsl import parse_qsl, compile_and_run
|
|
131
|
+
|
|
132
|
+
source = open("problem.qsl").read()
|
|
133
|
+
program = parse_qsl(source)
|
|
134
|
+
result = compile_and_run(program)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## 它能做什么?
|
|
140
|
+
|
|
141
|
+
用布尔表达式描述**任意搜索约束**,QSL 自动找到所有满足条件的解:
|
|
142
|
+
|
|
143
|
+
| 实际问题 | 布尔表达式 | 说明 |
|
|
144
|
+
|----------|-----------|------|
|
|
145
|
+
| SAT 求解 | `x0 \| ~x1` | 逻辑可满足性 |
|
|
146
|
+
| 图着色 | `x0 ^ x1` | 相邻顶点不同色 |
|
|
147
|
+
| 约束满足 | `x0 & ~x1 & x2` | 精确约束条件 |
|
|
148
|
+
| 组合优化 | `(x0&x1) \| (x2&x3)` | 任意布尔函数 |
|
|
149
|
+
|
|
150
|
+
**支持全部逻辑运算符:** `&`(与) `|`(或) `^`(异或) `~`(非) `()`(括号)
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## 安装
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
pip install qsl-quantum
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
> 核心部分零外部依赖。只有在使用 IBM 真实硬件时才需要 `qiskit`。
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## 为什么用 Grover 算法?
|
|
165
|
+
|
|
166
|
+
| | 经典搜索 | QSL(Grover) |
|
|
167
|
+
|------|----------|---------------|
|
|
168
|
+
| 搜索 N 个可能 | 查 N 次 | 查约 √N 次 |
|
|
169
|
+
| 256 个状态 | 最多查 256 次 | 约 16 次 |
|
|
170
|
+
| 1024 个状态 | 最多查 1024 次 | 约 32 次 |
|
|
171
|
+
| 百万级 | 不可行 | 约 1000 次 |
|
|
172
|
+
|
|
173
|
+
> 搜索空间越大,优势越明显。这是量子计算为数不多已被**数学证明**比经典算法快的场景。
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## 项目结构
|
|
178
|
+
|
|
179
|
+
```
|
|
180
|
+
qsl/
|
|
181
|
+
├── core/ 量子态、布尔解析、Grover 算法
|
|
182
|
+
├── compiler/ 编译器 + DSL 解析器
|
|
183
|
+
├── backends/ 模拟器后端 + IBM 后端
|
|
184
|
+
└── utils/ 异常体系、输入验证
|
|
185
|
+
tests/ 123 个测试用例
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## 运行测试
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
pip install -e ".[dev]"
|
|
194
|
+
pytest tests/ -v # 123 passed
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## 作者
|
|
200
|
+
|
|
201
|
+
宋梓铭 · [Gitee](https://gitee.com/song-jack/qsl) · 15011462616@163.com
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## 许可证
|
|
206
|
+
|
|
207
|
+
MIT — 随意使用、修改、分发。
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
<h1 align="center">QSL — 量子搜索语言</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<b>用一句话描述你想找什么,剩下的交给量子计算。</b>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<img src="https://img.shields.io/badge/Python-3.9+-blue?logo=python&logoColor=white" alt="Python">
|
|
9
|
+
<img src="https://img.shields.io/badge/License-MIT-green" alt="License">
|
|
10
|
+
<img src="https://img.shields.io/badge/tests-123%20passed-brightgreen" alt="Tests">
|
|
11
|
+
<img src="https://img.shields.io/badge/dependencies-zero-success" alt="Dependencies">
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 这是什么?
|
|
17
|
+
|
|
18
|
+
一个 **Python 库**,让你用日常的布尔表达式描述搜索条件,自动在量子计算机(或本地模拟器)上找到答案。
|
|
19
|
+
|
|
20
|
+
> 传统做法:手写量子电路,理解 H 门、CNOT 门、相位翻转……
|
|
21
|
+
> QSL 的做法:写 `x0 & ~x1`,然后等结果。
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## 5 秒看懂
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from qsl import QSLProgram, compile_and_run
|
|
29
|
+
|
|
30
|
+
# 我想找:3 个变量中,哪些赋值同时满足这三个条件?
|
|
31
|
+
program = QSLProgram(
|
|
32
|
+
name="3-SAT",
|
|
33
|
+
n_qubits=3,
|
|
34
|
+
premises=[
|
|
35
|
+
"x0 | ~x1", # x0 为真,或者 x1 为假
|
|
36
|
+
"x1 | x2", # x1 为真,或者 x2 为真
|
|
37
|
+
"~x0 | ~x2", # x0 为假,或者 x2 为假
|
|
38
|
+
],
|
|
39
|
+
shots=10
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
result = compile_and_run(program)
|
|
43
|
+
print(result.get_solutions())
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**输出:**
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
[3, 4] # 即二进制 011 和 100 —— 这两个就是答案
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
就这么简单。不需要懂量子力学。
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## 两种运行模式
|
|
57
|
+
|
|
58
|
+
| 模式 | 说明 | 需要什么 |
|
|
59
|
+
|------|------|----------|
|
|
60
|
+
| **本地模拟** | 在你的电脑上模拟量子计算 | 零依赖,纯 Python |
|
|
61
|
+
| **真实硬件** | 连接 IBM 量子计算机跑真任务 | `pip install qsl-quantum[ibm]` + IBM 账号 |
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
# 模式 1:本地模拟(默认)
|
|
65
|
+
compiler = QSLCompiler(backend="simulator")
|
|
66
|
+
result = compiler.compile_and_run(program)
|
|
67
|
+
|
|
68
|
+
# 模式 2:IBM 真实量子芯片
|
|
69
|
+
compiler = QSLCompiler(backend="ibm", backend_options={"token": "your_token"})
|
|
70
|
+
result = compiler.compile_and_run(program)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## 还能更简单 —— DSL 文本语法
|
|
76
|
+
|
|
77
|
+
不想写 Python?用配置文件式的文本语法:
|
|
78
|
+
|
|
79
|
+
```text
|
|
80
|
+
program "图着色" {
|
|
81
|
+
qubits: 3
|
|
82
|
+
|
|
83
|
+
premise {
|
|
84
|
+
x0 ^ x1 # 相邻顶点不能同色
|
|
85
|
+
x1 ^ x2
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
main {
|
|
89
|
+
algorithm: grover
|
|
90
|
+
shots: 10
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from qsl import parse_qsl, compile_and_run
|
|
97
|
+
|
|
98
|
+
source = open("problem.qsl").read()
|
|
99
|
+
program = parse_qsl(source)
|
|
100
|
+
result = compile_and_run(program)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## 它能做什么?
|
|
106
|
+
|
|
107
|
+
用布尔表达式描述**任意搜索约束**,QSL 自动找到所有满足条件的解:
|
|
108
|
+
|
|
109
|
+
| 实际问题 | 布尔表达式 | 说明 |
|
|
110
|
+
|----------|-----------|------|
|
|
111
|
+
| SAT 求解 | `x0 \| ~x1` | 逻辑可满足性 |
|
|
112
|
+
| 图着色 | `x0 ^ x1` | 相邻顶点不同色 |
|
|
113
|
+
| 约束满足 | `x0 & ~x1 & x2` | 精确约束条件 |
|
|
114
|
+
| 组合优化 | `(x0&x1) \| (x2&x3)` | 任意布尔函数 |
|
|
115
|
+
|
|
116
|
+
**支持全部逻辑运算符:** `&`(与) `|`(或) `^`(异或) `~`(非) `()`(括号)
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## 安装
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pip install qsl-quantum
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
> 核心部分零外部依赖。只有在使用 IBM 真实硬件时才需要 `qiskit`。
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 为什么用 Grover 算法?
|
|
131
|
+
|
|
132
|
+
| | 经典搜索 | QSL(Grover) |
|
|
133
|
+
|------|----------|---------------|
|
|
134
|
+
| 搜索 N 个可能 | 查 N 次 | 查约 √N 次 |
|
|
135
|
+
| 256 个状态 | 最多查 256 次 | 约 16 次 |
|
|
136
|
+
| 1024 个状态 | 最多查 1024 次 | 约 32 次 |
|
|
137
|
+
| 百万级 | 不可行 | 约 1000 次 |
|
|
138
|
+
|
|
139
|
+
> 搜索空间越大,优势越明显。这是量子计算为数不多已被**数学证明**比经典算法快的场景。
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## 项目结构
|
|
144
|
+
|
|
145
|
+
```
|
|
146
|
+
qsl/
|
|
147
|
+
├── core/ 量子态、布尔解析、Grover 算法
|
|
148
|
+
├── compiler/ 编译器 + DSL 解析器
|
|
149
|
+
├── backends/ 模拟器后端 + IBM 后端
|
|
150
|
+
└── utils/ 异常体系、输入验证
|
|
151
|
+
tests/ 123 个测试用例
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## 运行测试
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
pip install -e ".[dev]"
|
|
160
|
+
pytest tests/ -v # 123 passed
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## 作者
|
|
166
|
+
|
|
167
|
+
宋梓铭 · [Gitee](https://gitee.com/song-jack/qsl) · 15011462616@163.com
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## 许可证
|
|
172
|
+
|
|
173
|
+
MIT — 随意使用、修改、分发。
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=64.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "qsl-quantum"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Quantum Search Language - DSL for Grover search with simulator and IBM Quantum backends"
|
|
9
|
+
authors = [
|
|
10
|
+
{name = "Song Ziming", email = "15011462616@163.com"}
|
|
11
|
+
]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = {text = "MIT"}
|
|
14
|
+
requires-python = ">=3.9"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"Intended Audience :: Education",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Physics",
|
|
20
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.9",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
ibm = [
|
|
31
|
+
"qiskit>=1.0.0",
|
|
32
|
+
"qiskit-aer>=0.14.0",
|
|
33
|
+
"qiskit-ibm-runtime>=0.20.0",
|
|
34
|
+
]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest>=7.0.0",
|
|
37
|
+
"pytest-cov>=4.0.0",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.scripts]
|
|
41
|
+
qsl = "qsl.__main__:main"
|
|
42
|
+
|
|
43
|
+
[tool.pytest.ini_options]
|
|
44
|
+
testpaths = ["tests"]
|
|
45
|
+
python_files = ["test_*.py"]
|
|
46
|
+
python_classes = ["Test*"]
|
|
47
|
+
python_functions = ["test_*"]
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.packages.find]
|
|
50
|
+
exclude = ["tests", "tests.*"]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""
|
|
2
|
+
QSL - Quantum Search Language
|
|
3
|
+
=============================
|
|
4
|
+
|
|
5
|
+
基于"前提-工具-问题-主函数"框架的量子搜索领域专用语言。
|
|
6
|
+
|
|
7
|
+
两种运行模式:
|
|
8
|
+
1. 经典模拟器 (默认) - 零外部依赖,本地运行
|
|
9
|
+
2. IBM 量子计算机 - 需要 qiskit,在真实量子硬件上执行
|
|
10
|
+
|
|
11
|
+
快速开始:
|
|
12
|
+
>>> from qsl import QSLProgram, QSLCompiler
|
|
13
|
+
>>> program = QSLProgram(
|
|
14
|
+
... name="我的搜索问题",
|
|
15
|
+
... n_qubits=3,
|
|
16
|
+
... premises=["x0 & x1", "~x2 | x0"],
|
|
17
|
+
... shots=10
|
|
18
|
+
... )
|
|
19
|
+
>>> compiler = QSLCompiler() # 默认模拟器后端
|
|
20
|
+
>>> result = compiler.compile_and_run(program)
|
|
21
|
+
|
|
22
|
+
数学基础:
|
|
23
|
+
Grover 搜索: O(sqrt(N)) vs 经典 O(N)
|
|
24
|
+
布尔表达式 -> 量子 Oracle 自动编译
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
__version__ = "1.0.0"
|
|
28
|
+
__author__ = "Song Ziming"
|
|
29
|
+
__email__ = "15011462616@163.com"
|
|
30
|
+
|
|
31
|
+
# 公共 API
|
|
32
|
+
from .compiler.program import QSLProgram
|
|
33
|
+
from .compiler.compiler import QSLCompiler, compile_and_run, analyze
|
|
34
|
+
from .compiler.dsl import parse_qsl
|
|
35
|
+
from .core.parser import parse_bool
|
|
36
|
+
from .core.state import QuantumState
|
|
37
|
+
from .core.grover import GroverSearch, GroverResult
|
|
38
|
+
from .backends.registry import list_backends, get_backend
|
|
39
|
+
from .backends.simulator import SimulatorBackend
|
|
40
|
+
|
|
41
|
+
__all__ = [
|
|
42
|
+
# 核心 API
|
|
43
|
+
"QSLProgram",
|
|
44
|
+
"QSLCompiler",
|
|
45
|
+
"compile_and_run",
|
|
46
|
+
"analyze",
|
|
47
|
+
"parse_qsl",
|
|
48
|
+
"parse_bool",
|
|
49
|
+
# 量子底层
|
|
50
|
+
"QuantumState",
|
|
51
|
+
"GroverSearch",
|
|
52
|
+
"GroverResult",
|
|
53
|
+
# 后端
|
|
54
|
+
"SimulatorBackend",
|
|
55
|
+
"list_backends",
|
|
56
|
+
"get_backend",
|
|
57
|
+
# 元数据
|
|
58
|
+
"__version__",
|
|
59
|
+
]
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""
|
|
2
|
+
QSL 命令行入口。
|
|
3
|
+
|
|
4
|
+
使用:
|
|
5
|
+
python -m qsl # 交互式选择示例
|
|
6
|
+
python -m qsl --demo 1 # 运行指定示例
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def main():
|
|
13
|
+
"""命令行主入口。"""
|
|
14
|
+
from qsl import QSLProgram, QSLCompiler, parse_qsl
|
|
15
|
+
|
|
16
|
+
if len(sys.argv) > 1 and sys.argv[1] == "--demo":
|
|
17
|
+
run_demo()
|
|
18
|
+
elif len(sys.argv) > 1 and sys.argv[1] == "--help":
|
|
19
|
+
print_help()
|
|
20
|
+
else:
|
|
21
|
+
run_interactive()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def print_help():
|
|
25
|
+
print("""
|
|
26
|
+
QSL - Quantum Search Language
|
|
27
|
+
=============================
|
|
28
|
+
|
|
29
|
+
用法:
|
|
30
|
+
python -m qsl 交互式模式
|
|
31
|
+
python -m qsl --demo 1 运行指定示例
|
|
32
|
+
python -m qsl --help 显示此帮助
|
|
33
|
+
""")
|
|
34
|
+
print("""
|
|
35
|
+
Python API:
|
|
36
|
+
>>> from qsl import QSLProgram, QSLCompiler
|
|
37
|
+
>>> program = QSLProgram(
|
|
38
|
+
... name="我的搜索问题",
|
|
39
|
+
... n_qubits=4,
|
|
40
|
+
... premises=["x0 & x1", "~x2 | x3"],
|
|
41
|
+
... shots=10
|
|
42
|
+
... )
|
|
43
|
+
>>> compiler = QSLCompiler(backend="simulator")
|
|
44
|
+
>>> result = compiler.compile_and_run(program)
|
|
45
|
+
""")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def run_demo():
|
|
49
|
+
print("\n" + "=" * 60)
|
|
50
|
+
print(" QSL - Quantum Search Language v1.0.0")
|
|
51
|
+
print(" Grover 量子搜索演示")
|
|
52
|
+
print("=" * 60)
|
|
53
|
+
|
|
54
|
+
from qsl import QSLProgram, QSLCompiler
|
|
55
|
+
|
|
56
|
+
# 内嵌示例
|
|
57
|
+
demos = {
|
|
58
|
+
"1": ("SAT 求解 (n=3)", QSLProgram(
|
|
59
|
+
name="3-SAT 求解",
|
|
60
|
+
n_qubits=3,
|
|
61
|
+
premises=["x0 | ~x1", "x1 | x2", "~x0 | ~x2"],
|
|
62
|
+
shots=5,
|
|
63
|
+
)),
|
|
64
|
+
"2": ("图着色 (3顶点2色)", QSLProgram(
|
|
65
|
+
name="图着色",
|
|
66
|
+
n_qubits=3,
|
|
67
|
+
premises=["x0 ^ x1", "x1 ^ x2"],
|
|
68
|
+
shots=5,
|
|
69
|
+
)),
|
|
70
|
+
"3": ("恰好一个1 (n=3)", QSLProgram(
|
|
71
|
+
name="恰好一个1",
|
|
72
|
+
n_qubits=3,
|
|
73
|
+
premises=["x0 | x1 | x2", "~x0 | ~x1", "~x0 | ~x2", "~x1 | ~x2"],
|
|
74
|
+
shots=5,
|
|
75
|
+
)),
|
|
76
|
+
"4": ("大空间搜索 (n=6)", QSLProgram(
|
|
77
|
+
name="大空间搜索",
|
|
78
|
+
n_qubits=6,
|
|
79
|
+
premises=["~(x0 ^ x1 ^ x2)", "x3 | x4", "x5"],
|
|
80
|
+
shots=3,
|
|
81
|
+
)),
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
print("\n可用示例:")
|
|
85
|
+
for key, (desc, _) in demos.items():
|
|
86
|
+
print(f" {key}. {desc}")
|
|
87
|
+
print(f" 0. 运行所有示例")
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
choice = input("\n请选择 [0-4]: ").strip()
|
|
91
|
+
if not choice:
|
|
92
|
+
choice = "0"
|
|
93
|
+
except (EOFError, KeyboardInterrupt):
|
|
94
|
+
print()
|
|
95
|
+
return
|
|
96
|
+
|
|
97
|
+
compiler = QSLCompiler(verbose=True)
|
|
98
|
+
|
|
99
|
+
if choice == "0":
|
|
100
|
+
for key, (desc, program) in demos.items():
|
|
101
|
+
print(f"\n{'─'*60}")
|
|
102
|
+
print(f" 示例 {key}: {desc}")
|
|
103
|
+
print(f"{'─'*60}")
|
|
104
|
+
try:
|
|
105
|
+
result = compiler.compile_and_run(program)
|
|
106
|
+
print(f" 结果: {result.summary()}")
|
|
107
|
+
except Exception as e:
|
|
108
|
+
print(f" 错误: {e}")
|
|
109
|
+
elif choice in demos:
|
|
110
|
+
desc, program = demos[choice]
|
|
111
|
+
print(f"\n{'─'*60}")
|
|
112
|
+
print(f" 示例 {choice}: {desc}")
|
|
113
|
+
print(f"{'─'*60}")
|
|
114
|
+
try:
|
|
115
|
+
result = compiler.compile_and_run(program)
|
|
116
|
+
print(f" 结果: {result.summary()}")
|
|
117
|
+
except Exception as e:
|
|
118
|
+
print(f" 错误: {e}")
|
|
119
|
+
else:
|
|
120
|
+
print(f"无效选择: {choice}")
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def run_interactive():
|
|
124
|
+
print("\n" + "=" * 60)
|
|
125
|
+
print(" QSL - Quantum Search Language v1.0.0")
|
|
126
|
+
print(" Quantum Search Language")
|
|
127
|
+
print("=" * 60)
|
|
128
|
+
print("\n用法: python -m qsl --demo 运行演示")
|
|
129
|
+
print(" python -m qsl --help 查看帮助")
|
|
130
|
+
print()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
if __name__ == "__main__":
|
|
134
|
+
main()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
QSL 后端模块 - 经典模拟器 和 IBM 量子计算机。
|
|
3
|
+
|
|
4
|
+
使用:
|
|
5
|
+
>>> from qsl.backends import get_backend
|
|
6
|
+
>>> backend = get_backend("simulator") # 经典模拟
|
|
7
|
+
>>> backend = get_backend("ibm") # IBM 量子计算机
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from .base import AbstractBackend
|
|
11
|
+
from .simulator import SimulatorBackend
|
|
12
|
+
from .registry import get_backend, list_backends, register_backend
|
|
13
|
+
|
|
14
|
+
# 预注册内置后端
|
|
15
|
+
from .registry import _register_builtins
|
|
16
|
+
_register_builtins()
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"AbstractBackend",
|
|
20
|
+
"SimulatorBackend",
|
|
21
|
+
"get_backend",
|
|
22
|
+
"list_backends",
|
|
23
|
+
"register_backend",
|
|
24
|
+
]
|