将常见问题转换为qubo/ising形式的包
qubovert的Python项目详细描述
请看Repository和Docs。
将常见问题转换为Qubo形式。
到目前为止,我们刚刚实现了[Lucas]中的一些公式。qubovert的目标是成为一个映射到qubo和ising形式的大量问题集合,以帮助最近由于量子优化算法而增加对这些问题的研究。我希望有很多人参与,这样我们就可以汇编所有这些问题!
若要参与,请分叉存储库,添加您的贡献,然后提交请求。为您添加的任何功能添加测试。确保在提交任何内容之前运行python -m pytest,python -m pytest --codestyle--ignore=docs(是的,即使是测试也需要通过代码样式检查)和python -m pydocstyle convention=numpy qubovert,以确保生成通过。当您将更改推送到主分支时,travis ci将自动检查是否所有测试都通过。注意,所有问题都应该从qubovert.utils.Problem类派生!确保所有docstring都遵循numpydoc标准格式。
使用python的help函数!我对所有函数和类都有非常描述性的doc字符串。
安装
pip install qubovert
从源安装:
git clone https://github.com/jiosue/qubovert.git
cd QUBOVert
pip install -e .
然后您可以在python中使用它
importqubovert# get infohelp(qubovert)# see all the problems specifiedprint(qubovert.__all__)# or equivalentlyprint(qubovert.problems.__all__)# see the utilities definedhelp(qubovert.utils)print(qubovert.utils.__all__)# to see specifically the np problems:help(qubovert.problems.np)print(qubovert.problems.np.__all__)# to see specifically the benchmarking problems:help(qubovert.problems.benchmarking)print(qubovert.problems.benchmarking.__all__)# etc ...
请参阅下面的封面示例。所有其他问题都可以以类似的方式使用。
fromqubovertimportSetCoverfromany_moduleimportqubo_solver# or you can use my bruteforce solver...# from qubovert.utils import solve_qubo_bruteforce as qubo_solverU={"a","b","c","d"}V=[{"a","b"},{"a","c"},{"c","d"}]problem=SetCover(U,V)Q,offset=problem.to_qubo()obj,sol=qubo_solver(Q)obj+=offsetsolution=problem.convert_solution(sol)print(solution)# will print {0, 2}print(problem.is_solution_valid(solution))# will print True, since V[0] + V[2] covers all of Uprint(obj==len(solution))# will print True
改用伊辛公式:
fromqubovertimportSetCoverfromany_moduleimportising_solver# or you can use my bruteforce solver...# from qubovert.utils import solve_ising_bruteforce as ising_solverU={"a","b","c","d"}V=[{"a","b"},{"a","c"},{"c","d"}]problem=SetCover(U,V)h,J,offset=problem.to_ising()obj,sol=ising_solver(h,J)obj+=offsetsolution=problem.convert_solution(sol)print(solution)# will print {0, 2}print(problem.is_solution_valid(solution))# will print True, since V[0] + V[2] covers all of Uprint(obj==len(solution))# will print True
要查看问题的详细信息,请运行
help(qubovert.SetCover)help(qubovert.VertexCover)# etc
我有非常描述性的doc字符串,可以解释使用每个问题类需要知道的所有内容。
转换的技术细节
对于他提到的日志技巧,我们通常需要像sum(x) >= 1这样的约束。为了实施这个约束,我们在形式1 - sum(x) + sum(x[i] x[j] for i in range(len(x)) for j in range(i+1, len(x)))的qubo中添加了一个惩罚(这个想法来自[Glover])。
参考文献
[Lucas] | Andrew Lucas. Ising formulations of many np problems. Frontiers in Physics, 2:5, 2014. |
[Glover] | Fred Glover, Gary Kochenberger, and Yu Du. A tutorial on formulating and using qubo models. arXiv:1811.11538v5, 2019. |