# Buggy code
def run(self, dag):
    """Main run method for the noise adaptive layout."""
    self._initialize_backend_prop()
    num_qubits = self._create_program_graph(dag)
    if num_qubits > len(self.swap_graph):
        raise TranspilerError("Number of qubits greater than device.")
    for end1, end2, _ in sorted(
        self.prog_graph.edges(data=True), key=lambda x: x[2]["weight"], reverse=True
    ):
        self.pending_program_edges.append((end1, end2))
    while self.pending_program_edges:
        edge = self._select_next_edge()
        q1_mapped = edge[0] in self.prog2hw
        q2_mapped = edge[1] in self.prog2hw
        if (not q1_mapped) and (not q2_mapped):
            best_hw_edge = self._select_best_remaining_cx()
            self.prog2hw[edge[0]] = best_hw_edge[0]
            self.prog2hw[edge[1]] = best_hw_edge[1]
            self.available_hw_qubits.remove(best_hw_edge[0])
            self.available_hw_qubits.remove(best_hw_edge[1])
        elif not q1_mapped:
            best_hw_qubit = self._select_best_remaining_qubit(edge[0])
            self.prog2hw[edge[0]] = best_hw_qubit
            self.available_hw_qubits.remove(best_hw_qubit)
        else:
            best_hw_qubit = self._select_best_remaining_qubit(edge[1])
            self.prog2hw[edge[1]] = best_hw_qubit
            self.available_hw_qubits.remove(best_hw_qubit)
        new_edges = [
            x
            for x in self.pending_program_edges
            if not (x[0] in self.prog2hw and x[1] in self.prog2hw)
        ]
        self.pending_program_edges = new_edges
    for qid in self.qarg_to_id.values():
        if qid not in self.prog2hw:
            self.prog2hw[qid] = self.available_hw_qubits[0]
            self.available_hw_qubits.remove(self.prog2hw[qid])
    layout = Layout()
    for q in dag.qubits():
        pid = self._qarg_to_id(q)
        hwid = self.prog2hw[pid]
        layout[q] = hwid
    self.property_set["layout"] = layout


# Traceback (TypeError)
import qiskit as qk
from qiskit.test.mock import FakePoughkeepsie
qc = qk.QuantumCircuit.from_qasm_str('''
OPENQASM 2.0;
include "qelib1.inc";
qreg q7159[6];
qreg q7160[2];
creg c1212[1];
cx q7159[5],q7159[4];
cx q7159[3],q7159[2];
cx q7159[1],q7159[0];
cx q7160[1],q7160[0];
measure q7160[1] -> c1212[0];''')
qk.transpile(qc, backend=FakePoughkeepsie(), optimization_level=2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/compiler/transpile.py", line 200, in transpile
circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_configs)))
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/tools/parallel.py", line 100, in parallel_map
return [task(values[0], *task_args, **task_kwargs)]
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/compiler/transpile.py", line 221, in _transpile_circuit
return transpile_circuit(circuit, transpile_config)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/transpile_circuit.py", line 62, in transpile_circuit
out_circuit = pass_manager.run(circuit)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passmanager.py", line 172, in run
dag = self._do_pass(pass_, dag, passset.options)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passmanager.py", line 223, in _do_pass
dag = self._run_this_pass(pass_, dag)
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passmanager.py", line 255, in _run_this_pass
pass_.run(FencedDAGCircuit(dag))
File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/transpiler/passes/mapping/noise_adaptive_layout.py", line 229, in run
self.prog2hw[edge[0]] = best_hw_edge[0]
TypeError: 'NoneType' object is not subscriptable

# Fixed code
def run(self, dag):
    """Main run method for the noise adaptive layout."""
    self._initialize_backend_prop()
    num_qubits = self._create_program_graph(dag)
    if num_qubits > len(self.swap_graph):
        raise TranspilerError("Number of qubits greater than device.")
    for end1, end2, _ in sorted(
        self.prog_graph.edges(data=True), key=lambda x: x[2]["weight"], reverse=True
    ):
        self.pending_program_edges.append((end1, end2))
    while self.pending_program_edges:
        edge = self._select_next_edge()
        q1_mapped = edge[0] in self.prog2hw
        q2_mapped = edge[1] in self.prog2hw
        if (not q1_mapped) and (not q2_mapped):
            best_hw_edge = self._select_best_remaining_cx()
            if best_hw_edge is None:
                raise TranspilerError(
                    "CNOT({}, {}) could not be placed in selected device.".format(
                        edge[0], edge[1]
                    )
                )
            self.prog2hw[edge[0]] = best_hw_edge[0]
            self.prog2hw[edge[1]] = best_hw_edge[1]
            self.available_hw_qubits.remove(best_hw_edge[0])
            self.available_hw_qubits.remove(best_hw_edge[1])
        elif not q1_mapped:
            best_hw_qubit = self._select_best_remaining_qubit(edge[0])
            if best_hw_qubit is None:
                raise TranspilerError(
                    "CNOT({}, {}) could not be placed in selected device. "
                    "No qubit near qr[{}] available".format(edge[0], edge[1], edge[0])
                )
            self.prog2hw[edge[0]] = best_hw_qubit
            self.available_hw_qubits.remove(best_hw_qubit)
        else:
            best_hw_qubit = self._select_best_remaining_qubit(edge[1])
            if best_hw_qubit is None:
                raise TranspilerError(
                    "CNOT({}, {}) could not be placed in selected device. "
                    "No qubit near qr[{}] available".format(edge[0], edge[1], edge[1])
                )
            self.prog2hw[edge[1]] = best_hw_qubit
            self.available_hw_qubits.remove(best_hw_qubit)
        new_edges = [
            x
            for x in self.pending_program_edges
            if not (x[0] in self.prog2hw and x[1] in self.prog2hw)
        ]
        self.pending_program_edges = new_edges
    for qid in self.qarg_to_id.values():
        if qid not in self.prog2hw:
            self.prog2hw[qid] = self.available_hw_qubits[0]
            self.available_hw_qubits.remove(self.prog2hw[qid])
    layout = Layout()
    for q in dag.qubits():
        pid = self._qarg_to_id(q)
        hwid = self.prog2hw[pid]
        layout[q] = hwid
    self.property_set["layout"] = layout