背景:
我有兴趣研究各种材料的量子相变,在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参数不起作用,因为字典键是元组。另外,如果我不想要这个功能,我必须运行我的原始代码来生成谨慎的产品,所以它似乎生成了"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 )?换句话说,我是否只是从错误的方向来处理这个看似简单的问题呢?
发布于 2018-07-02 04:19:09
在发布了这个问题之后,我编写了一个改进版本的原始代码,它允许原始输入(表单{h_n: hval}的字典,其中h代表n qubit),以及表单{J_nm: Jval}的附加输入( J_nm a tuple,qn,qm),即量子位数n和m之间的耦合强度。此外,当一些量子位/耦合器被排除在“在一起”时,它也不会像原来那样中断。因此,这个新代码在功能上适用于我想要做的事情。不过,我怀疑有更好的出路。
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 https://stackoverflow.com/questions/51067109
复制相似问题