Skip to main content

Quantum Search Language - DSL for Grover search with simulator and IBM Quantum backends

Project description

QSL — 量子搜索语言

用一句话描述你想找什么,剩下的交给量子计算。

Python License Tests Dependencies


这是什么?

一个 Python 库,让你用日常的布尔表达式描述搜索条件,自动在量子计算机(或本地模拟器)上找到答案。

传统做法:手写量子电路,理解 H 门、CNOT 门、相位翻转……
QSL 的做法:写 x0 & ~x1,然后等结果。


5 秒看懂

from qsl import QSLProgram, compile_and_run

# 我想找:3 个变量中,哪些赋值同时满足这三个条件?
program = QSLProgram(
    name="3-SAT",
    n_qubits=3,
    premises=[
        "x0 | ~x1",    # x0 为真,或者 x1 为假
        "x1 | x2",     # x1 为真,或者 x2 为真
        "~x0 | ~x2",   # x0 为假,或者 x2 为假
    ],
    shots=10
)

result = compile_and_run(program)
print(result.get_solutions())

输出:

[3, 4]   # 即二进制 011 和 100 —— 这两个就是答案

就这么简单。不需要懂量子力学。


两种运行模式

模式 说明 需要什么
本地模拟 在你的电脑上模拟量子计算 零依赖,纯 Python
真实硬件 连接 IBM 量子计算机跑真任务 pip install qsl-quantum[ibm] + IBM 账号
# 模式 1:本地模拟(默认)
compiler = QSLCompiler(backend="simulator")
result = compiler.compile_and_run(program)

# 模式 2:IBM 真实量子芯片
compiler = QSLCompiler(backend="ibm", backend_options={"token": "your_token"})
result = compiler.compile_and_run(program)

还能更简单 —— DSL 文本语法

不想写 Python?用配置文件式的文本语法:

program "图着色" {
    qubits: 3

    premise {
        x0 ^ x1       # 相邻顶点不能同色
        x1 ^ x2
    }

    main {
        algorithm: grover
        shots: 10
    }
}
from qsl import parse_qsl, compile_and_run

source = open("problem.qsl").read()
program = parse_qsl(source)
result = compile_and_run(program)

它能做什么?

用布尔表达式描述任意搜索约束,QSL 自动找到所有满足条件的解:

实际问题 布尔表达式 说明
SAT 求解 x0 | ~x1 逻辑可满足性
图着色 x0 ^ x1 相邻顶点不同色
约束满足 x0 & ~x1 & x2 精确约束条件
组合优化 (x0&x1) | (x2&x3) 任意布尔函数

支持全部逻辑运算符: &(与) |(或) ^(异或) ~(非) ()(括号)


安装

pip install qsl-quantum

核心部分零外部依赖。只有在使用 IBM 真实硬件时才需要 qiskit


为什么用 Grover 算法?

经典搜索 QSL(Grover)
搜索 N 个可能 查 N 次 查约 √N 次
256 个状态 最多查 256 次 约 16 次
1024 个状态 最多查 1024 次 约 32 次
百万级 不可行 约 1000 次

搜索空间越大,优势越明显。这是量子计算为数不多已被数学证明比经典算法快的场景。


项目结构

qsl/
├── core/           量子态、布尔解析、Grover 算法
├── compiler/       编译器 + DSL 解析器
├── backends/       模拟器后端 + IBM 后端
└── utils/          异常体系、输入验证
tests/              123 个测试用例

运行测试

pip install -e ".[dev]"
pytest tests/ -v        # 123 passed

作者

宋梓铭 · Gitee · 15011462616@163.com


许可证

MIT — 随意使用、修改、分发。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qsl_quantum-0.1.0.tar.gz (45.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qsl_quantum-0.1.0-py3-none-any.whl (42.6 kB view details)

Uploaded Python 3

File details

Details for the file qsl_quantum-0.1.0.tar.gz.

File metadata

  • Download URL: qsl_quantum-0.1.0.tar.gz
  • Upload date:
  • Size: 45.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for qsl_quantum-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e930edc87e34470d39db76cf79d7d5e12735e7ae88978123923a9496d55c3ad6
MD5 a32d497c03257218c33f9cb6cfa0907c
BLAKE2b-256 83c723460fd3a8e2f0bf4cf1695c08b804974b3cec2163e828cc6f6f07ab634a

See more details on using hashes here.

File details

Details for the file qsl_quantum-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: qsl_quantum-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for qsl_quantum-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28b5610712b241f0e86d94e82ecd63c951a0df7c3d05823289323b51b6dde4f8
MD5 e8053815667a6d44b8e9477ec3e4d12e
BLAKE2b-256 a7f0a8d2de7e269accb81232a5331a5a52a4f26a1daa5cf15c459bd369f8dbff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page