背景:
我有兴趣通过在DWave的绝热量子计算机上进行模拟来研究各种材料的量子相变。为了更容易地生成作为参数函数的相图,我正在编写实用程序来扫描参数,使用这些参数集运行模拟,并收集数据。你知道吗
输入条件背景:
在DWave上,我可以设置两组参数,h
偏差和J
耦合。这些输入如下:h = {qubit0: hvalue0, qubit1: hvalue1,...}
和J = {(qubit0, qubit1): J01, (qubit2, qubit3): J23, ...}
。到目前为止,我有一个工具可以在给定输入的情况下执行参数扫描:{qubit: [hz1, hz2,..., hzn]}
将量子位映射到h
要扫描的值,{coupler: [J1, J2,..., Jn]}
将耦合器映射到J
要扫描的值。在这两种情况下,输出都是[{trial1}, {trial2}, ... {trialn}]
形式的列表,它表示每个单独的量子位和耦合上h
和J
输入的笛卡尔积。你知道吗
我真正想要的和我到目前为止写的东西:
在上面,我遇到了一个严重的问题。假设我想浏览一系列参数,其中一些量子比特或耦合器在任何给定的运行中彼此之间有固定的关系。这一点很重要,因为逻辑问题必须以非常复杂的方式映射到DWave上。例如,假设我想运行一个问题,其中qubit0
在[0, 1, 2]
中有h
,qubit1
在[1, 2, 3]
中有h
,qubit3
在[5, 8]
中有h
,但关系qubit1_h = qubit0_h + 1
必须保留;也就是说,我希望值的乘积是[(0, 1, 5), (0, 1, 8), (1, 2, 5), (1, 2, 8), ...]
,而不是笛卡尔积给出的所有组合。你知道吗
下面的代码将对h参数执行此操作,但对J参数不起作用,因为字典键是元组。另外,如果我不想要这个功能,我必须运行我的原始代码来生成caretesian产品,所以它似乎生成了“3个案例”
def fixed_relationship_sweep(input_params, together):
"""
Inputs
------
input_params: {0:[x1, x2], 1:[x3, x4], 2:[y1, y2], 3:[y3, y4]]}
dictionary mapping qubits to parameter lists to iterate through
together: [[0, 1], [2, 3]]
list of qubit lists that specify which qubit parameters to sweep with a fixed relationship
Output
------
fixed_rel_sweep: [{trial1}, {trial2}, ...{trialn}] where qubits labelled as "together" are
swept with fixed 1-1 relationship, ie, above produces:
[{0:x1, 1:x3, 2:y1, 3:y3}, {0:x1, 1:x3, 2:y2, 3:y4}, {0:x2, 1:x4, 2:y1, 3:y3},
{0:x2, 1:x4, 2:y2, 3:y4}]
"""
qsorcs = []
params = []
#index representation of params, as cartesian product must respect fixed positions
#of arguments and not their values, ie [x1, x3] vary together in example
tempidxrep = []
for key, value in input_params.items():
qsorcs.append(key)
params.append(value)
tempidxrep.append([i for i in range(len(value))])
idxrep = []
#remove redundancy in index representation governed by fixed relationships in together
for fix_rel in together:
idxrep.append(tempidxrep[fix_rel[0]])
#sweep combinations via carteisan product
idxcombos = itertools.product(*idxrep)
#reconstruct actual parameter combinations
param_combos = []
for idxcombo in idxcombos:
trial = {qsorcs[j]: params[j][idxcombo[i]] for i in range(len(idxcombo)) for j in together[i]}
param_combos.append(trial)
return param_combos
有没有一种更简单更好的方法来实现这一点,像itertools这样的内置工具可以处理整数或元组键,而无需编写单独的复杂函数?换句话说,我只是从错误的方向来处理这个看似简单的问题吗?你知道吗
在发布了这个问题之后,我编写了一个原始代码的改进版本,它允许原始输入(形式为
{h_n: hval}
的字典,其中h是表示n个量子位的iteger)以及形式为{J_nm: Jval}
的附加输入,其中J是量子位n和m之间耦合强度的元组(qn,qm),当一些量子位/耦合器被排除在“在一起”的lit之外时,它不会像最初的那样中断。因此,这段新代码在功能上符合我的要求。不过,我怀疑有更好的办法。你知道吗相关问题 更多 >
编程相关推荐