def test_layout_add_register(self): """add_register() method""" layout = Layout() layout.add_register(QuantumRegister(2, 'q0')) layout.add_register(QuantumRegister(1, 'q1')) self.assertEqual(layout[(QuantumRegister(2, 'q0'), 0)], 0) self.assertEqual(layout[(QuantumRegister(2, 'q0'), 1)], 1) self.assertEqual(layout[(QuantumRegister(1, 'q1'), 0)], 2)
class CheckMap(AnalysisPass): """ Checks if a DAGCircuit is mapped to `coupling_map`. It checks that all 2-qubit interactions are laid out to be physically close. """ def __init__(self, coupling_map, initial_layout=None): """ Checks if a DAGCircuit is mapped to `coupling_map`. Args: coupling_map (CouplingMap): Directed graph represented a coupling map. initial_layout (Layout): The initial layout of the DAG to analyze. """ super().__init__() self.layout = initial_layout self.coupling_map = coupling_map self.results = {'is_swap_mapped': [], 'is_direction_mapped': []} def run(self, dag): """ If `dag` is mapped to `coupling_map`, the property `is_swap_mapped` is set to True (or to False otherwise). If `dag` is mapped and the direction is correct the property `is_direction_mapped` is set to True (or to False otherwise). Args: dag (DAGCircuit): DAG to map. """ if self.layout is None: self.layout = Layout() for qreg in dag.qregs.values(): self.layout.add_register(qreg) self.property_set['is_swap_mapped'] = True self.property_set['is_direction_mapped'] = True for gate in dag.get_2q_nodes(): physical_q0 = self.layout[gate['qargs'][0]] physical_q1 = self.layout[gate['qargs'][1]] if self.coupling_map.distance(physical_q0, physical_q1) != 1: self.property_set['is_swap_mapped'] = False self.property_set['is_direction_mapped'] = False return else: if (physical_q0, physical_q1) not in self.coupling_map.get_edges(): self.property_set['is_direction_mapped'] = False if isinstance(gate['op'], SwapGate): if (physical_q1, physical_q0) not in self.coupling_map.get_edges(): self.property_set['is_direction_mapped'] = False
class CheckMap(AnalysisPass): """ Checks if a DAGCircuit is mapped to `coupling_map`. """ def __init__(self, coupling_map, initial_layout=None): """ Checks if a DAGCircuit is mapped to `coupling_map`. Args: coupling_map (CouplingMap): Directed graph represented a coupling map. initial_layout (Layout): The initial layout of the DAG to analyze. """ super().__init__() self.layout = initial_layout self.coupling_map = coupling_map def run(self, dag): """ If `dag` is mapped to `coupling_map`, the property `is_mapped` is set to True (or to False otherwise). If `dag` is mapped and the direction is correct the property `is_direction_mapped` is set to True (or to False otherwise). Args: dag (DAGCircuit): DAG to map. """ if self.layout is None: self.layout = Layout() for qreg in dag.qregs.values(): self.layout.add_register(qreg) self.property_set['is_mapped'] = True self.property_set['is_direction_mapped'] = True for layer in dag.serial_layers(): subdag = layer['graph'] for cnot in subdag.get_cnot_nodes(): physical_q0 = self.layout[cnot['qargs'][0]] physical_q1 = self.layout[cnot['qargs'][1]] if self.coupling_map.distance(physical_q0, physical_q1) != 1: self.property_set['is_mapped'] = False self.property_set['is_direction_mapped'] = False return else: if (physical_q0, physical_q1) not in self.coupling_map.get_edges(): self.property_set['is_direction_mapped'] = False
def run(self, dag): """ Pick a convenient layout depending on the best matching qubit connectivity, and set the property `layout`. Args: dag (DAGCircuit): DAG to find layout for. Raises: TranspilerError: if dag wider than self.coupling_map """ num_dag_qubits = sum([qreg.size for qreg in dag.qregs.values()]) if num_dag_qubits > self.coupling_map.size(): raise TranspilerError('Number of qubits greater than device.') layout = Layout() for qreg in dag.qregs.values(): layout.add_register(qreg) self.property_set['layout'] = layout
class CheckMap(AnalysisPass): """ Checks if a DAGCircuit is mapped to `coupling_map`. """ def __init__(self, coupling_map, initial_layout=None): """ Checks if a DAGCircuit is mapped to `coupling_map`. Args: coupling_map (Coupling): Directed graph represented a coupling map. initial_layout (Layout): The initial layout of the DAG to analyze. """ super().__init__() self.layout = initial_layout self.coupling_map = coupling_map def run(self, dag): """ If `dag` is mapped to coupling_map, the property `is_mapped` is set to True (or to False otherwise). Args: dag (DAGCircuit): DAG to map. """ if self.layout is None: self.layout = Layout() for qreg in dag.qregs.values(): self.layout.add_register(qreg) self.property_set['is_mapped'] = None for layer in dag.serial_layers(): subdag = layer['graph'] for a_cx in subdag.get_cnot_nodes(): q = QuantumRegister(self.coupling_map.node_counter, 'q') physical_q0 = (q, self.layout[a_cx['op'].qargs[0]]) physical_q1 = (q, self.layout[a_cx['op'].qargs[1]]) if self.coupling_map.distance(physical_q0, physical_q1) != 1: self.property_set['is_mapped'] = False return self.property_set['is_mapped'] = True