qsl-quantum 0.1.0__py3-none-any.whl
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/__init__.py +59 -0
- qsl/__main__.py +134 -0
- qsl/backends/__init__.py +24 -0
- qsl/backends/base.py +105 -0
- qsl/backends/ibm.py +574 -0
- qsl/backends/registry.py +93 -0
- qsl/backends/simulator.py +138 -0
- qsl/compiler/__init__.py +15 -0
- qsl/compiler/compiler.py +289 -0
- qsl/compiler/dsl.py +231 -0
- qsl/compiler/program.py +143 -0
- qsl/core/__init__.py +16 -0
- qsl/core/grover.py +404 -0
- qsl/core/parser.py +345 -0
- qsl/core/state.py +611 -0
- qsl/utils/__init__.py +1 -0
- qsl/utils/exceptions.py +180 -0
- qsl/utils/validation.py +178 -0
- qsl_quantum-0.1.0.dist-info/METADATA +207 -0
- qsl_quantum-0.1.0.dist-info/RECORD +24 -0
- qsl_quantum-0.1.0.dist-info/WHEEL +5 -0
- qsl_quantum-0.1.0.dist-info/entry_points.txt +2 -0
- qsl_quantum-0.1.0.dist-info/licenses/LICENSE +21 -0
- qsl_quantum-0.1.0.dist-info/top_level.txt +1 -0
qsl/__init__.py
ADDED
|
@@ -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
|
+
]
|
qsl/__main__.py
ADDED
|
@@ -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()
|
qsl/backends/__init__.py
ADDED
|
@@ -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
|
+
]
|
qsl/backends/base.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""
|
|
2
|
+
量子后端抽象基类。
|
|
3
|
+
|
|
4
|
+
所有后端必须实现的统一接口。该接口使得上层编译器无需关心
|
|
5
|
+
具体是在模拟器还是真实量子计算机上运行。
|
|
6
|
+
|
|
7
|
+
设计原则:
|
|
8
|
+
- 所有后端实现相同的 run_grover_search() 方法
|
|
9
|
+
- 每个后端负责自己的初始化、验证和错误处理
|
|
10
|
+
- 结果统一返回 GroverResult (由 core.grover 定义)
|
|
11
|
+
|
|
12
|
+
失败模式分析 (接口层):
|
|
13
|
+
1. n_qubits 超出后端能力: 每个后端有独立的量子比特上限
|
|
14
|
+
2. Oracle 函数无法直接传输到真实硬件: 需要电路编译
|
|
15
|
+
3. shots 超出后端限制: 某些后端有最大测量次数限制
|
|
16
|
+
4. 连接超时: 真实硬件可能有队列等待
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from abc import ABC, abstractmethod
|
|
20
|
+
from typing import Callable, Optional
|
|
21
|
+
|
|
22
|
+
from ..core.grover import GroverResult
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class AbstractBackend(ABC):
|
|
26
|
+
"""
|
|
27
|
+
量子计算后端抽象基类。
|
|
28
|
+
|
|
29
|
+
子类必须实现:
|
|
30
|
+
- run_grover_search()
|
|
31
|
+
- max_qubits 属性
|
|
32
|
+
- backend_name 属性
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, name: str = "abstract", **options):
|
|
36
|
+
"""
|
|
37
|
+
初始化后端。
|
|
38
|
+
|
|
39
|
+
参数:
|
|
40
|
+
name: 后端名称标识
|
|
41
|
+
**options: 后端特定选项
|
|
42
|
+
"""
|
|
43
|
+
self._name = name
|
|
44
|
+
self._options = options
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def name(self) -> str:
|
|
48
|
+
"""后端名称。"""
|
|
49
|
+
return self._name
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
@abstractmethod
|
|
53
|
+
def max_qubits(self) -> int:
|
|
54
|
+
"""该后端支持的最大量子比特数。"""
|
|
55
|
+
...
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
def run_grover_search(self,
|
|
59
|
+
n_qubits: int,
|
|
60
|
+
oracle: Callable[[int], bool],
|
|
61
|
+
num_solutions: int,
|
|
62
|
+
shots: int,
|
|
63
|
+
verbose: bool = False,
|
|
64
|
+
**run_options) -> GroverResult:
|
|
65
|
+
"""
|
|
66
|
+
在后端执行 Grover 搜索。
|
|
67
|
+
|
|
68
|
+
参数:
|
|
69
|
+
n_qubits: 量子比特数
|
|
70
|
+
oracle: Boolean oracle 函数 f(x) -> bool
|
|
71
|
+
num_solutions: 解的数量 M
|
|
72
|
+
shots: 测量次数
|
|
73
|
+
verbose: 是否输出过程信息
|
|
74
|
+
**run_options: 后端特定选项
|
|
75
|
+
|
|
76
|
+
返回:
|
|
77
|
+
GroverResult 包含搜索完整结果
|
|
78
|
+
|
|
79
|
+
失败模式 (由子类具体处理):
|
|
80
|
+
- 量子比特数超出上限
|
|
81
|
+
- Oracle 无法编译到目标硬件
|
|
82
|
+
- 连接 / 认证失败
|
|
83
|
+
- 作业超时
|
|
84
|
+
- 测量结果解析失败
|
|
85
|
+
"""
|
|
86
|
+
...
|
|
87
|
+
|
|
88
|
+
def validate_request(self, n_qubits: int, shots: int):
|
|
89
|
+
"""
|
|
90
|
+
验证搜索请求的合法性。
|
|
91
|
+
|
|
92
|
+
子类应调用此方法作为 run_grover_search 的第一步。
|
|
93
|
+
|
|
94
|
+
失败模式:
|
|
95
|
+
- n_qubits > max_qubits: 抛出异常
|
|
96
|
+
- n_qubits < 1: 抛出异常
|
|
97
|
+
- shots < 1: 抛出异常
|
|
98
|
+
"""
|
|
99
|
+
from ..utils.validation import validate_n_qubits, validate_shots
|
|
100
|
+
|
|
101
|
+
validate_n_qubits(n_qubits, self.max_qubits)
|
|
102
|
+
validate_shots(shots)
|
|
103
|
+
|
|
104
|
+
def __repr__(self) -> str:
|
|
105
|
+
return f"<{self.__class__.__name__}: {self._name}>"
|