简体   繁体   English

如何迭代字符串?

[英]How do I iterate over string?

I have code that looks like this我有看起来像这样的代码

''' '''

phi = (QubitOperator('X0') + 
       QubitOperator('Y0') +
       QubitOperator('Z0') + 
       QubitOperator('X1') +
       QubitOperator('Y1') +
       QubitOperator('Z1') )

''' '''

where QubitOperator is a command in a package I am using.其中 QubitOperator 是我正在使用的 package 中的命令。 How can I automate this to iterate over XY and Z and 0, 1, 2.... and create phi?我怎样才能自动化它来迭代 XY 和 Z 以及 0、1、2.... 并创建 phi?

your existing code may be cleaner, but supposing you had to do this for more than 8 elements:您现有的代码可能更干净,但假设您必须为超过 8 个元素执行此操作:

from functools import reduce

letters = ['X', 'Y', 'Z']
numbers = ['0', '1', '2']
pairs = [f'{l}{n}' for l in letters for n in numbers]
# this gets you ['X0', 'X1', 'X2', 'Y0', ..., 'Z2']
qubits = [QubitOperator(x) for x in pairs]
# not sure the best way to get this into a single phi. join seems logical, but probably doesn't work.
phi = reduce(lambda acc, cur: acc + cur, pairs)

Overview of the code:代码概述:

  1. The first three lines basically just define the data and merge them together.前三行基本上只是定义数据并将它们合并在一起。 the list comprehension is kind of 2-layered (unofficial term), so it'll iterate over letters, then iterate over numbers until all 9 elements are reached列表理解是一种 2 层(非官方术语),所以它会迭代字母,然后迭代数字,直到达到所有 9 个元素
  2. line 4 (beginning qubits = [... wraps each of the strings created in pairs inside a QubitOperator. Technically not needed, and I'd probably put it in line 3 if I were coding for myself.第 4 行(开始qubits = [...将每个成对创建的字符串包装在 QubitOperator 中。技术上不需要,如果我为自己编码,我可能会将其放在第 3 行。
  3. Line 5, as noted, is not necessarily the best way of doing this, but it was the best I could think of.如前所述,第 5 行不一定是最好的方法,但它是我能想到的最好的方法。 reduce basically allows you to compress a list of stuff into something else -- in this case, we're going to concatenate everything into phi. reduce基本上允许你将一个东西列表压缩成其他东西——在这种情况下,我们将把所有东西连接到 phi 中。 This will iterate over the list of qubits and add them together.这将遍历量子比特列表并将它们加在一起。 Expanding this out gets something close to what you've already done: QubitOperator('X0') + QubitOperator('X1')+... As a commenter pointed out, a for loop may work as well in this scenario.扩展此内容与您已经完成的工作接近: QubitOperator('X0') + QubitOperator('X1')+...正如评论者指出的那样,for 循环在这种情况下也可以正常工作。 That code looks like:该代码如下所示:
phi = qubits[0]
for i in range(1, len(qubits)):
  phi = phi + qubits[i]

There is a caveat here: I'm not 100 percent sure this is going to work.这里有一个警告:我不是 100% 确定这会奏效。 Every step prior to the last one should, and I think the last step should as well, but without any testing, it's hard to know.最后一步之前的每一步都应该,我认为最后一步也应该,但是没有任何测试,很难知道。

Also, as you can see, we effectively turned 8 lines of straight forward code into 4 lines of relatively complex code.此外,如您所见,我们有效地将 8 行直接代码转换为 4 行相对复杂的代码。 I'm not sure that tradeoff is worth it, unless you have a lot of data, or otherwise you need to repeat this process frequently with fresh data.我不确定权衡是否值得,除非您有大量数据,否则您需要使用新数据频繁重复此过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM