Python中的固定关系笛卡尔积

3

背景:
我对用DWave的绝热量子计算机模拟各种材料的量子相变非常感兴趣。为了更容易地产生与参数相关的相图,我正在编写实用程序来扫描参数,使用这些参数集运行模拟,并收集数据。

输入条件背景:
在DWave上,我可以设置两组参数:h偏置和J耦合。其输入方式如下:h = {qubit0: hvalue0, qubit1: hvalue1,...}J = {(qubit0,qubit1): J01,(qubit2,qubit3): J23,...}。到目前为止,我有一个工具,可以对此类输入进行参数扫描:{qubit:[hz1,hz2,...,hzn]},将qubits映射到要扫描的h值上以及{coupler:[J1,J2,...,Jn]},将耦合器映射到要扫描的J值上。在这两种情况下,输出是一个形式为[{trial1},{trial2},... {trialn}]的列表,表示每个单独的qubit和耦合上hJ输入的笛卡尔积。

实际需要和已编写的内容:
在上述情况下,我遇到了一个严重的问题。假设我要扫描一系列参数,其中某些qubits或耦合器在任何给定运行时具有固定的关系。由于一些复杂性必须以非平凡的方式将逻辑问题映射到DWave上,因此这很重要。例如,假设我想运行一个问题,其中qubit0h分别为[0、1、2]qubit1h分别为[1、2、3],并且qubit3h分别为[5、8],但是必须保留关系qubit1_h = qubit0_h + 1。即,我希望值的乘积为[(0, 1, 5), (0, 1, 8), (1, 2, 5), (1, 2, 8), ...],而不是笛卡尔积所给出的所有组合。

以下代码将对h参数执行此操作,但由于字典键是元组,因此对于J参数无效。此外,如果我不想要此功能,则必须运行原始代码以生成笛卡尔积,因此似乎会生成“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来处理键是整数或元组而不需要编写单独的复杂函数?换句话说,我是否只是从错误的方向来解决这个看似简单的问题?

1个回答

0
在发布了这个问题之后,我编写了原始代码的改进版本,它允许使用原始输入(形式为{h_n: hval}的字典,其中h是表示第n个量子位的整数),以及形式为{J_nm: Jval}的附加输入,其中J_nm是耦合强度(qn, qm)的元组,表示量子位n和m之间的耦合强度。此外,当一些量子位/耦合器从“together”列表中排除时,它不会像原始代码那样崩溃。因此,这个新代码对于我想要做的事情在功能上是有效的。尽管如此,我怀疑还有更好的方法。
def fixed_relationship_sweep(input_params, together):
"""
Inputs
------
input_params: {qorc1:[x1, x2], qorc2:[x3, x4], qorc3:[y1, y2], qorc4:[y3, y4]]}
dictionary mapping qubits or couplers to parameter lists to iterate through
together: [[qorc1, qorc2], [qorc3, qorc4]]
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:
[{qorc1:x1, qorc2:x3, qorc3:y1, qorc4:y3}, {qorc1:x1, qorc2:x3, qorc3:y2, qorc4:y4},
{qorc1:x2, qorc2:x4, qorc3:y1, qorc4:y3},{qorc1:x2, qorc2:x4, qorc3:y2, qorc4:y4}] 
"""
#list of qubits or couplers
qsorcs = []
#index representation of params, as cartesian product must respect fixed positions
#of arguments and not their values, ie [x1, x3] vary together in example
idxrep = {}
for key, value in input_params.items():
    qsorcs.append(key)
    idxrep[key] = [i for i in range(len(value))]

#remove redundancy in index representation governed by fixed relationships in together
count = 0
for fix_rel in together:
    for j in range(len(fix_rel)):
        if j != 0:
            del idxrep[fix_rel[j]]

#sweep combinations via cartesian product
idxdict_combos = (list(dict(zip(idxrep, x)) for x in itertools.product(*idxrep.values())))

#reconstruct actual parameter combinations with "redundant" parameter values
dict_combos = []
for combo in idxdict_combos:
    #add back in "redundant" parameters
    for fix_rel in together:
        for qorc in fix_rel[1::]:
            combo[qorc] = combo[fix_rel[0]]

    #add back in true values corresponding to indices
    tempdict = {}
    for key, value in combo.items():
        tempdict[key] = input_params[key][value]

    dict_combos.append(tempdict)

return dict_combos 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接