你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
量子优化问题
问题
from azure.quantum.optimization import Problem
构造函数
要创建 Problem 对象,请指定以下信息:
name:问题的易记名称。 无唯一性约束。terms(可选):要添加到问题中的Term对象和分组因子对象的列表(如果支持)。problem_type(可选):问题类型。 必须是ProblemType.ising、ProblemType.pubo、ProblemType.ising_grouped或ProblemType.pubo_grouped。 默认为ProblemType.ising。 如果在问题表述中使用了分组因子,则系统会自动选取分组问题类型。init_config(可选):用户要为问题指定初始配置时值的变量 ID 的字典。content_type(可选):默认值为ContentType.json。 可设置为ContentType.protobuf。
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)
# with initial configuration set
config = {'0': 1, '1': 1, '2': 0}
problem2 = Problem(name="Problem with Initial Configuration", terms=terms, init_config=config)
Problem.add_term
向问题中添加一个单项因子。 它采用项的系数以及项中出现的变量的索引。
coefficient = 0.13
problem.add_term(c=coefficient, indices=[2,0])
Problem.add_slc_term
向问题中添加单个平方线性组合 (SLC) 因子。 它接受构成平方线性组合因子的单项因子列表以及前导系数。
subterms_Term = [
Term(c=1, indices=[0]),
Term(c=-2, indices=[1]),
Term(c=1, indices=[2]),
Term(c=-1, indices=[])
]
coefficient = 2
problem.add_slc_term(terms=subterms_Term, c=coefficient)
除了使用 Term 对象列表外,还可改为使用元组列表,其中每个元祖均包含一个单项因子系数,后跟该单项因子系数的变量索引(如果是常数则为 None)。
subterms_tuple = [
(1, 0),
(-2, 1),
(1, 2),
(-1, None)
]
coefficient = 2
problem.add_slc_term(terms=subterms_tuple, c=coefficient)
有关详细信息,请参阅 SlcTerm。
Problem.add_terms
使用一系列 Terms 在问题中添加多个项。
problem.add_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])
])
此函数还有一个重载,该重载充当分组因子的包装器,如以下示例所示。
可以通过指定 Term 对象列表以及分组因子类型来使用该重载。
problem.add_terms([
Term(c=1, indices=[0]),
Term(c=-2, indices=[1]),
Term(c=1, indices=[2]),
Term(c=-1, indices=[])
],
term_type = GroupType.squared_linear_combination,
c = 2
)
Problem.serialize
将问题串行化为 JSON 字符串或 protobuf。 有关 Protobuf 用法的详细信息,请参阅使用 Protobuf 处理大型输入。
problem = Problem("My Problem", [Term(c=1, indices=[0,1])])
problem.serialize()
> {"cost_function": {"version": "1.0", "type": "ising", "terms": [{"c": 1, "ids": [0, 1]}]}}
要串行化为 Protobuf,需要指定可选的 content_type 参数。
Protobuf 在部分优化求解器上受支持。
from azure.quantum.job.base_job import ContentType
problem = Problem(name = "protobuf_problem", terms = [Term(c=1, indices=[0,1])], content_type=ContentType.protobuf)
problem.serialize()
Problem.deserialize
将上传的输入数据中的问题反序列化为 Problem 实例。
deserialized_problem = Problem.deserialize(input_poblem = "your_problem")
Problem.upload
可使用 upload 方法将问题数据显式上传到 Azure 存储帐户,该方法以参数形式接收 Workspace 实例:
problem.upload(workspace=workspace)
显式上传问题后,该问题将不会在提交期间自动上传,除非其因子发生变化。
Problem.evaluate
一旦定义了问题,便可以评估提供的任何配置的问题。 应将配置作为变量 ID 的字典提供给值。
problem = Problem("My Problem", [Term(c=1, indices=[0,1])])
problem.evaluate({0:1, 1:1})
> 1
problem.evaluate({0:1, 1:0})
> 0
Problem.set_fixed_variables
试验期间,你可能想要将变量(或一组变量)设为特定值。 在修复所选变量后,调用 set_fixed_variables 将返回一个新的问题对象,该对象表示修改后的问题。
fixed_var = {'1': 1, '2': 1}
problem = Problem("My Problem", [Term(c=1, indices=[0,1]), Term(c=11, indices=[1,2]), Term(c=5, indices=[])])
new_problem = problem.set_fixed_variables(fixed_var)
new_problem.terms
> [{'c': 1, 'ids': [0]}, {'c': 16, 'ids': []}]
若要在减少的问题上将固定变量放回到解决方案上,请执行以下操作:
result = solver.optimize(new_problem)
result_config = json.loads(result)['solutions'][0]['configuration']
result_config.update(fixed_var) # join the fixed variables with the result
Problem.get_terms
可以使用此函数获取其中存在变量的因子。
problem = Problem("My problem" , [Term(c=1, indices=[0,1]), Term(c=11, indices=[1,2]), Term(c=5, indices=[1])])
terms = problem.get_terms(id = 1)
terms
> [{'c': 11, 'ids': [1,2]}, {'c': 5, 'ids': [1]}]
StreamingProblem
StreamingProblem 类可处理超过本地内存限制的大问题。 与问题类不同,StreamingProblem 中的术语会直接上传到 blob,而不会保留在内存中。
StreamingProblem 类使用与问题类相同的接口。 有关详细信息,请参阅 StreamingProblem 文档。 由于其流式处理性质,StreamingProblem 类尚不支持某些功能:
- Problem.set_fixed_variables()
- Problem.evaluate()
OnlineProblem
OnlineProblem 类从已上传优化问题的 blob 存储的 url 创建问题。 它允许重复使用已提交的问题。
它不支持客户端分析,例如 evaluate 和 set_fixed_variables 函数。 它允许将 blob 存储中的问题作为 Problem 类的实例下载,以执行任何客户端操作。
有关如何使用 OnlineProblem 类的示例,请参阅重复使用问题定义。
使用 Protobuf 处理大型输入
Protobuf 是 Google 的数据交换格式。 它是带有静态架构的二进制格式。 有关 Protobuf 的详细介绍,请参阅 Google 的协议缓冲区页。
在 Azure Quantum 中,我们添加了对 Protobuf 的支持,因为它对于编码较大的输入问题很有用。 使用二进制编码方法(而不是默认 JSON 格式)可减少有效负载大小、提高上传速度并降低处理速度。
使用此功能,可以完全按照 JSON 中当前支持的方式指定问题类型、术语、初始配置和问题元数据(例如问题名称)。
注意
此功能可用于 Microsoft QIO 提供程序中的部分求解器。 下面详细介绍了用法和可用性。
使用情况
要提交 Protobuf 序列化问题,请在 Problem 对象定义中指定可选参数 content_type,并将其设置为 Content.protobuf。
如果未显式设置此参数,则将其设置为 ContentType.json。
problem = Problem(name = "protobuf_problem", terms = [Term(c=1, indices=[0,1])], content_type=ContentType.protobuf)
若要详细了解成本函数以及因子与问题定义的关系,请参阅以下主题:
Protobuf 序列化可用性
Protobuf 序列化当前受以下 Microsoft QIO 求解器支持:
如果将问题以 Protobuf 形式提交到不支持它们的求解器中,SDK 中将显示客户端错误,并且提交将失败。
如果在使用 Protobuf 时发现任何 bug 或问题,请联系 Azure 支持人员。