你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

说明优化问题

若要说明要解决的问题,请创建 Problem 的实例,并将 problem_type 设置为 ProblemType.isingProblemType.pubo 或其分组模拟形式,请参阅 ProblemType

from azure.quantum.optimization import Problem, ProblemType, Term, ParallelTempering

problem = Problem(name="My First Problem", problem_type=ProblemType.ising)

下一步,创建一组术语,并添加到 problem

terms = [
    Term(c=-9, indices=[0]),
    Term(c=-3, indices=[1,0]),
    Term(c=5, indices=[2,0]),
    Term(c=9, indices=[2,1]),
    Term(c=2, indices=[3,0]),
    Term(c=-4, indices=[3,1]),
    Term(c=4, indices=[3,2])
]

problem.add_terms(terms=terms)

注意

如以下部分所述,有多种方法可为问题提供因子。

提供问题术语的方式

可通过三种方法为 Problem 提供因子:在构造函数中提供、单独提供或作为 Term 对象列表提供(或在支持时作为分组因子对象提供,例如:SlcTerm)。

通过构造函数

你可以在 Problem 构造函数中提供一组 Term 对象。

terms = [
    Term(c=-9, indices=[0]),
    Term(c=-3, indices=[1,0]),
    Term(c=5, indices=[2,0])
]

problem = Problem(name="My Difficult Problem", terms=terms)

分组因子对象可以包含在因子列表中,就像单项因子一样。

terms = [
    Term(c=-9, indices=[0]),
    Term(c=-3, indices=[1,0]),
    Term(c=5, indices=[2,0]),
    SlcTerm(
        terms=[
            Term(c=1, indices=[0]),
            Term(c=-1, indices=[1]),
        ],
        c=2
    )
]
problem = Problem(name="My Other Problem", terms=terms, problem_type=ProblemType.ising_grouped)

注意

目前只有一部分优化求解器支持分组因子。 有关详细信息,请参阅 SlcTerm 文档。

单独提供

可以通过调用 Problem 上的add_term 方法来单独提供每个 Term 对象。

problem = Problem(name="My Difficult Problem", problem_type=ProblemType.ising)
problem.add_term(c=-9, indices=[0])
problem.add_term(c=-3, indices=[1,0])
problem.add_term(c=5, indices=[2,0])

还可以通过根据特定分组因子类型来调用 Problem 上的特殊方法或通过重载 add_terms 方法来单独提供分组因子对象。

目前,平方线性组合是唯一启用的分组因子,可以通过 add_slc_term 进行添加。 有关详细信息,请参阅 Problem.add_slc_termSlcTerm

problem = Problem(name="My Other Difficult Problem", problem_type=ProblemType.ising)
subterms = [
    Term(c=1, indices=[0]),
    Term(c=-2, indices=[1]),
    Term(c=1, indices=[2]),
    Term(c=-1, indices=[])
]
problem.add_slc_term(terms=subterms, c=2)

提供平方线性组合 (slc) 分组因子的另一种方法是将其作为对列表提供,其中第一个条目是系数,第二个条目是变量 ID。常数作为 (c, None) 提供。

problem.add_slc_term(
    terms=[(1,0), (-2,1), (1,2), (-1,None)],
    c=2
)

添加术语列表

你还可以使用 Problem 上的 add-terms 方法提供 Term 对象列表。

problem = Problem(name="My Difficult Problem")
terms = [
    Term(c=-9, indices=[0]),
    Term(c=-3, indices=[1,0]),
    Term(c=5, indices=[2,0]),
    Term(c=9, indices=[2,1]),
    Term(c=2, indices=[3,0]),
    Term(c=-4, indices=[3,1]),
    Term(c=4, indices=[3,2])
]

problem.add_terms(terms=terms)

组合术语类型

可以在同一 Problem 对象中同时使用 TermSlcTerm 对象。

from azure.quantum.optimization import Problem, Term, SlcTerm

# create a problem object
problem = Problem(name="My Example Problem")

在这里,可使用提供问题术语的方式中所述的方式来添加 TermSlcTerm 对象。 如果成本函数中包含分组术语,SDK 将自动调整问题类型。 例如,你无需将问题类型从 ProblemType.pubo 更新为 ProblemType.pubo_grouped,因为此 SDK 将为你完成。

# add regular terms to the problem
problem.add_terms([
    Term(c=-9, indices=[0]),
    Term(c=-3, indices=[1,0]),
    Term(c=5, indices=[2,0])
])

# add a grouped term to the problem
subterms = [
    Term(c=1, indices=[0]),
    Term(c=-2, indices=[1]),
    Term(c=1, indices=[2]),
    Term(c=-1, indices=[])
]
problem.add_slc_term(terms=subterms, c=2)

你将能够使用 to_json() 在成本函数中查看这些术语类型的组合。

# convert to json to show "terms" and "terms_slc" array in cost function
problem.to_json()
> '{"metadata": {"name": "My Example Problem"}, "cost_function": {"version": "1.0", "type": "ising_grouped", "terms": [{"c": -9, "ids": [0]}, {"c": -3, "ids": [1, 0]}, {"c": 5, "ids": [2, 0]}], "terms_slc": [{"c": 2, "terms": [{"c": 1, "ids": [0]}, {"c": -2, "ids": [1]}, {"c": 1, "ids": [2]}, {"c": -1, "ids": []}]}]}}'

后续步骤

创建优化问题后,请了解如何通过应用求解器来解决此问题