def test_lookahead_swap_higher_depth_width_is_better(self): """Test that lookahead swap finds better circuit with increasing search space. Increasing the tree width and depth is expected to yield a better (or same) quality circuit, in the form of fewer SWAPs. """ qr = QuantumRegister(8, name='q') circuit = QuantumCircuit(qr) circuit.cx(qr[0], qr[1]) circuit.cx(qr[1], qr[2]) circuit.cx(qr[2], qr[3]) circuit.cx(qr[3], qr[4]) circuit.cx(qr[4], qr[5]) circuit.cx(qr[5], qr[6]) circuit.cx(qr[6], qr[7]) circuit.cx(qr[0], qr[3]) circuit.cx(qr[6], qr[4]) circuit.cx(qr[7], qr[1]) circuit.cx(qr[4], qr[2]) circuit.cx(qr[3], qr[7]) circuit.cx(qr[5], qr[3]) circuit.cx(qr[6], qr[2]) circuit.cx(qr[2], qr[7]) circuit.cx(qr[0], qr[6]) circuit.cx(qr[5], qr[7]) original_dag = circuit_to_dag(circuit) # Create a ring of 8 connected qubits coupling_map = CouplingMap.from_grid(num_rows=2, num_columns=4) mapped_dag_1 = LookaheadSwap(coupling_map, search_depth=3, search_width=3).run(original_dag) mapped_dag_2 = LookaheadSwap(coupling_map, search_depth=5, search_width=5).run(original_dag) num_swaps_1 = mapped_dag_1.count_ops().get('swap', 0) num_swaps_2 = mapped_dag_2.count_ops().get('swap', 0) self.assertLessEqual(num_swaps_2, num_swaps_1)
def test_grid_factory(self): coupling = CouplingMap.from_grid(2, 3) edges = coupling.get_edges() expected = [ (0, 3), (0, 1), (3, 0), (3, 4), (1, 0), (1, 4), (1, 2), (4, 1), (4, 3), (4, 5), (2, 1), (2, 5), (5, 2), (5, 4), ] self.assertEqual(set(edges), set(expected))
def test_grid_factory_unidirectional(self): coupling = CouplingMap.from_grid(2, 3, bidirectional=False) edges = coupling.get_edges() expected = [(0, 3), (0, 1), (3, 4), (1, 4), (1, 2), (4, 5), (2, 5)] self.assertEqual(set(edges), set(expected))
def setup(self, size, _): edge_len = int((2 * size + 2)**0.5) + 1 self.coupling_map = CouplingMap.from_grid(edge_len, edge_len) self.sim_backend = QasmSimulatorPy() self.circuit = build_ripple_adder_circuit(size)
def test_lookahead_swap_higher_depth_width_is_better(self): """Test that lookahead swap finds better circuit with increasing search space. Increasing the tree width and depth is expected to yield a better (or same) quality circuit, in the form of fewer SWAPs. """ # q_0: ──■───────────────────■───────────────────────────────────────────────» # ┌─┴─┐ │ ┌───┐ » # q_1: ┤ X ├──■──────────────┼─────────────────┤ X ├─────────────────────────» # └───┘┌─┴─┐ │ └─┬─┘┌───┐ ┌───┐ » # q_2: ─────┤ X ├──■─────────┼───────────────────┼──┤ X ├──────────┤ X ├──■──» # └───┘┌─┴─┐ ┌─┴─┐ │ └─┬─┘ ┌───┐└─┬─┘ │ » # q_3: ──────────┤ X ├──■──┤ X ├─────────────────┼────┼────■──┤ X ├──┼────┼──» # └───┘┌─┴─┐└───┘ ┌───┐ │ │ │ └─┬─┘ │ │ » # q_4: ───────────────┤ X ├──■────────────┤ X ├──┼────■────┼────┼────┼────┼──» # └───┘┌─┴─┐ └─┬─┘ │ │ │ │ │ » # q_5: ────────────────────┤ X ├──■─────────┼────┼─────────┼────■────┼────┼──» # └───┘┌─┴─┐ │ │ │ │ │ » # q_6: ─────────────────────────┤ X ├──■────■────┼─────────┼─────────■────┼──» # └───┘┌─┴─┐ │ ┌─┴─┐ ┌─┴─┐» # q_7: ──────────────────────────────┤ X ├───────■───────┤ X ├──────────┤ X ├» # └───┘ └───┘ └───┘» # «q_0: ──■─────── # « │ # «q_1: ──┼─────── # « │ # «q_2: ──┼─────── # « │ # «q_3: ──┼─────── # « │ # «q_4: ──┼─────── # « │ # «q_5: ──┼────■── # « ┌─┴─┐ │ # «q_6: ┤ X ├──┼── # « └───┘┌─┴─┐ # «q_7: ─────┤ X ├ # « └───┘ qr = QuantumRegister(8, name="q") circuit = QuantumCircuit(qr) circuit.cx(qr[0], qr[1]) circuit.cx(qr[1], qr[2]) circuit.cx(qr[2], qr[3]) circuit.cx(qr[3], qr[4]) circuit.cx(qr[4], qr[5]) circuit.cx(qr[5], qr[6]) circuit.cx(qr[6], qr[7]) circuit.cx(qr[0], qr[3]) circuit.cx(qr[6], qr[4]) circuit.cx(qr[7], qr[1]) circuit.cx(qr[4], qr[2]) circuit.cx(qr[3], qr[7]) circuit.cx(qr[5], qr[3]) circuit.cx(qr[6], qr[2]) circuit.cx(qr[2], qr[7]) circuit.cx(qr[0], qr[6]) circuit.cx(qr[5], qr[7]) original_dag = circuit_to_dag(circuit) # Create a ring of 8 connected qubits coupling_map = CouplingMap.from_grid(num_rows=2, num_columns=4) mapped_dag_1 = LookaheadSwap(coupling_map, search_depth=3, search_width=3).run(original_dag) mapped_dag_2 = LookaheadSwap(coupling_map, search_depth=5, search_width=5).run(original_dag) num_swaps_1 = mapped_dag_1.count_ops().get("swap", 0) num_swaps_2 = mapped_dag_2.count_ops().get("swap", 0) self.assertLessEqual(num_swaps_2, num_swaps_1)