def collect_runs(self, namelist):
"""Return a set of runs of "op" nodes with the given names.
For example, "... h q[0]; cx q[0],q[1]; cx q[0],q[1]; h q[1]; .."
would produce the tuple of cx nodes as an element of the set returned
from a call to collect_runs(["cx"]). If instead the cx nodes were
"cx q[0],q[1]; cx q[1],q[0];", the method would still return the
pair in a tuple. The namelist can contain names that are not
in the circuit's basis.
Nodes must have only one successor to continue the run.
"""
group_list = []
# Iterate through the nodes of self in topological order
# and form tuples containing sequences of gates
# on the same qubit(s).
ts = nx.topological_sort(self.multi_graph)
nodes_seen = dict(zip(ts, [False] * len(ts)))
for node in ts:
nd = self.multi_graph.node[node]
if nd["type"] == "op" and nd["name"] in namelist \
and not nodes_seen[node]:
group = [node]
nodes_seen[node] = True
s = self.multi_graph.successors(node)
while len(s) == 1 and \
self.multi_graph.node[s[0]]["type"] == "op" and \
self.multi_graph.node[s[0]]["name"] in namelist:
group.append(s[0])
nodes_seen[s[0]] = True
s = self.multi_graph.successors(s[0])
if len(group) > 1:
group_list.append(tuple(group))
return set(group_list)
评论列表
文章目录