Skip to content

Commit

Permalink
remove comments
Browse files Browse the repository at this point in the history
  • Loading branch information
changsookim committed Sep 9, 2024
1 parent 98fd9f1 commit 8641a0c
Showing 1 changed file with 4 additions and 46 deletions.
50 changes: 4 additions & 46 deletions src/qibo/transpiler/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ def __init__(
initial_layout: Optional[dict] = None,
circuit: Optional[Circuit] = None,
blocks: Optional[CircuitBlocks] = None,
temp: Optional[bool] = False, # 2# for temporary circuit
temp: Optional[bool] = False,
):
self._p2l, self._l2p = [], []

self._temporary = temp
if self._temporary: # 2# if temporary circuit, no need to store the blocks
if self._temporary:
return
elif circuit is None:
raise_error(ValueError, "Circuit must be provided.")
Expand All @@ -212,14 +212,13 @@ def __init__(
else:
self.circuit_blocks = CircuitBlocks(circuit, index_names=True)

self._nqubits = circuit.nqubits # 1# number of qubits
self._nqubits = circuit.nqubits
self._routed_blocks = CircuitBlocks(Circuit(circuit.nqubits))
self._swaps = 0

if initial_layout is None:
return

# 1# initialize physical to logical mapping
self.wire_names = list(initial_layout.keys())
self.physical_to_logical = list(initial_layout.values())

Expand All @@ -240,8 +239,6 @@ def physical_to_logical(self, p2l_map: list):
Args:
p2l_map (list): physical to logical mapping.
"""
# 1# update bidirectional mapping
# 4# use shallow copy
self._p2l = p2l_map.copy()
self._l2p = [0] * len(self._p2l)
for i, l in enumerate(self._p2l):
Expand All @@ -254,8 +251,6 @@ def logical_to_physical(self, l2p_map: list):
Args:
l2p_map (list): logical to physical mapping.
"""
# 1# update bidirectional mapping
# 4# use shallow copy
self._l2p = l2p_map.copy()
self._p2l = [0] * len(self._l2p)
for i, p in enumerate(self._l2p):
Expand Down Expand Up @@ -307,7 +302,6 @@ def routed_circuit(self, circuit_kwargs: Optional[dict] = None):
def final_layout(self):
"""Returns the final physical-logical qubits mapping."""

# 1# return {"A": lq_num0, "B": lq_num1, ...}
return {self.wire_names[i]: self._p2l[i] for i in range(self._nqubits)}

def update(self, logical_swap: tuple):
Expand All @@ -321,15 +315,12 @@ def update(self, logical_swap: tuple):
"""

physical_swap = self.logical_pair_to_physical(logical_swap)

# 2# add the real SWAP gate, not a temporary circuit
if not self._temporary:
self._routed_blocks.add_block(
Block(qubits=physical_swap, gates=[gates.SWAP(*physical_swap)])
)
self._swaps += 1

# 1# update the bidirectional mapping
self._update_mappings_swap(logical_swap, physical_swap)

def undo(self):
Expand All @@ -340,7 +331,6 @@ def undo(self):
self._routed_blocks.remove_block(last_swap_block)
self._swaps -= 1

# 1# update the bidirectional mapping
self._update_mappings_swap(logical_swap, physical_swap)

def get_physical_qubits(self, block: Union[int, Block]):
Expand All @@ -357,7 +347,6 @@ def get_physical_qubits(self, block: Union[int, Block]):

return tuple(self._l2p[q] for q in block.qubits)

# 1# logical_to_physical -> logical_pair_to_physical
def logical_pair_to_physical(self, logical_qubits: tuple):
"""Returns the physical qubits associated to the logical qubit pair.
Expand All @@ -367,11 +356,8 @@ def logical_pair_to_physical(self, logical_qubits: tuple):
Returns:
tuple: physical qubit numbers associated to the logical qubit pair.
"""
# 1# return physical qubit numbers corresponding to the logical qubit pair
return self._l2p[logical_qubits[0]], self._l2p[logical_qubits[1]]

# 1# circuit_to_logical(), circuit_to_physical() removed


class ShortestPaths(Router):
"""A class to perform initial qubit mapping and connectivity matching.
Expand Down Expand Up @@ -475,9 +461,8 @@ def _add_swaps(candidate: tuple, circuitmap: CircuitMap):
"""
path = candidate[0]
meeting_point = candidate[1]
forward = path[0 : meeting_point + 1] # 1# physical qubits
forward = path[0 : meeting_point + 1]
backward = list(reversed(path[meeting_point + 1 :]))
# 1# apply logical swaps
for f in forward[1:]:
circuitmap.update(
(
Expand All @@ -504,13 +489,11 @@ def _compute_cost(self, candidate: tuple):
Returns:
(list, int): best path to move qubits and qubit meeting point in the path.
"""
# 2# CircuitMap might be used
temporary_circuit = CircuitMap(
circuit=Circuit(self.circuit_map._nqubits),
blocks=deepcopy(self.circuit_map.circuit_blocks),
)

# 1# copy the current physical to logical mapping
temporary_circuit.physical_to_logical = self.circuit_map.physical_to_logical
self._add_swaps(candidate, temporary_circuit)
temporary_dag = deepcopy(self._dag)
Expand All @@ -525,7 +508,6 @@ def _compute_cost(self, candidate: tuple):
all_executed = True
for block in temporary_front_layer:
if (
# 3# might be changed to use _get_dag_layer(qubits=True) to avoid using get_physical_qubits(block_num)
temporary_circuit.get_physical_qubits(block)
in self.connectivity.edges
or not temporary_circuit.circuit_blocks.search_by_index(
Expand Down Expand Up @@ -553,7 +535,6 @@ def _check_execution(self):
executable_blocks = []
for block in self._front_layer:
if (
# 3# might be changed to use _get_dag_layer(qubits=True) to avoid using get_physical_qubits(block_num)
self.circuit_map.get_physical_qubits(block) in self.connectivity.edges
or not self.circuit_map.circuit_blocks.search_by_index(block).entangled
):
Expand Down Expand Up @@ -603,7 +584,6 @@ def _preprocessing(self, circuit: Circuit, initial_layout: dict):
initial_layout (dict): initial physical-to-logical qubit mapping.
"""

# 1# Relabel the nodes of the connectivity graph
self.connectivity = _relabel_connectivity(self.connectivity, initial_layout)

copied_circuit = circuit.copy(deep=True)
Expand Down Expand Up @@ -639,7 +619,6 @@ def _append_final_measurements(self, routed_circuit: Circuit):
for measurement in self._final_measurements:
original_qubits = measurement.qubits
routed_qubits = list(
# 1# use l2p to get physical qubit numbers
self.circuit_map.logical_to_physical[qubit]
for qubit in original_qubits
)
Expand Down Expand Up @@ -763,7 +742,6 @@ def _preprocessing(self, circuit: Circuit, initial_layout: dict):
initial_layout (dict): initial physical-to-logical qubit mapping.
"""

# 1# Relabel the nodes of the connectivity graph
self.connectivity = _relabel_connectivity(self.connectivity, initial_layout)

copied_circuit = circuit.copy(deep=True)
Expand Down Expand Up @@ -801,7 +779,6 @@ def _append_final_measurements(self, routed_circuit: Circuit):
for measurement in self._final_measurements:
original_qubits = measurement.qubits
routed_qubits = list(
# 1# use l2p to get physical qubit numbers
self.circuit_map.logical_to_physical[qubit]
for qubit in original_qubits
)
Expand Down Expand Up @@ -838,8 +815,6 @@ def _get_dag_layer(self, n_layer, qubits=False):
(list): list of block numbers or target qubits.
"""

# 3# depend on the 'qubits' flag, return the block number or target qubits
# 3# return target qubits -> to avoid using get_physical_qubits(block_num)
if qubits:
return [
node[1]["qubits"]
Expand All @@ -853,7 +828,6 @@ def _find_new_mapping(self):
"""Find the new best mapping by adding one swap."""
candidates_evaluation = {}

# 4# use shallow copy
self._memory_map.append(self.circuit_map.physical_to_logical.copy())
for candidate in self._swap_candidates():
candidates_evaluation[candidate] = self._compute_cost(candidate)
Expand All @@ -872,28 +846,19 @@ def _find_new_mapping(self):
def _compute_cost(self, candidate: int):
"""Compute the cost associated to a possible SWAP candidate."""

# 2# use CircuitMap for temporary circuit to save time
# 2# no gates, no block decomposition, no Circuit object
# 2# just logical-physical mapping
temporary_circuit = CircuitMap(temp=True)

# 1# copy the current physical to logical mapping
temporary_circuit.physical_to_logical = self.circuit_map.physical_to_logical
temporary_circuit.update(candidate)

# 1# use p2l to check if the mapping is already in the memory
if temporary_circuit.physical_to_logical in self._memory_map:
return float("inf")

tot_distance = 0.0
weight = 1.0
for layer in range(self.lookahead + 1):
# 3# return gates' target qubit pairs in the layer
# 3# to avoid using get_physical_qubits(block_num)
layer_gates = self._get_dag_layer(layer, qubits=True)
avg_layer_distance = 0.0
for lq_pair in layer_gates:
# 3# logical qubit pairs to node numbers (physical qubit pairs) in the connectivity graph
qubits = temporary_circuit.logical_pair_to_physical(lq_pair)
avg_layer_distance += (
max(self._delta_register[i] for i in qubits)
Expand All @@ -915,7 +880,6 @@ def _swap_candidates(self):
(list): list of candidates.
"""
candidates = []
# 3# might be changed to use _get_dag_layer(qubits=True) to avoid using get_physical_qubits(block_num)
for block in self._front_layer:
for qubit in self.circuit_map.get_physical_qubits(block):
for connected in self.connectivity.neighbors(qubit):
Expand All @@ -941,7 +905,6 @@ def _check_execution(self):
executable_blocks = []
for block in self._front_layer:
if (
# 3# might be changed to use _get_dag_layer(qubits=True) to avoid using get_physical_qubits(block_num)
self.circuit_map.get_physical_qubits(block) in self.connectivity.edges
or not self.circuit_map.circuit_blocks.search_by_index(block).entangled
):
Expand Down Expand Up @@ -984,8 +947,6 @@ def _shortest_path_routing(self):
shortest_path_qubits = None

for block in self._front_layer:
# 3# return node numbers (physical qubits) in the connectivity graph
# 3# might be changed to use _get_dag_layer(qubits=True) to avoid using get_physical_qubits(block_num)
q1, q2 = self.circuit_map.get_physical_qubits(block)
distance = self._dist_matrix[q1, q2]

Expand All @@ -998,7 +959,6 @@ def _shortest_path_routing(self):
)

# move q1
# 1# qubit moving algorithm is changed
q1 = self.circuit_map.physical_to_logical[shortest_path[0]]
for q2 in shortest_path[1:-1]:
self.circuit_map.update((q1, self.circuit_map.physical_to_logical[q2]))
Expand All @@ -1019,7 +979,6 @@ def _create_dag(gates_qubits_pairs: list):
dag = nx.DiGraph()
dag.add_nodes_from(range(len(gates_qubits_pairs)))

# 3# additionally store target qubits of the gates
for i in range(len(gates_qubits_pairs)):
dag.nodes[i]["qubits"] = gates_qubits_pairs[i]

Expand Down Expand Up @@ -1051,7 +1010,6 @@ def _remove_redundant_connections(dag: nx.DiGraph):
(:class:`networkx.DiGraph`): reduced dag.
"""
new_dag = nx.DiGraph()
# 3# add nodes with attributes
new_dag.add_nodes_from(dag.nodes(data=True))
transitive_reduction = nx.transitive_reduction(dag)
new_dag.add_edges_from(transitive_reduction.edges)
Expand Down

0 comments on commit 8641a0c

Please sign in to comment.