示例#1
0
    def test_only_output_cx_and_swaps_in_coupling_map(self):
        """Test that output DAG contains only 2q gates from the the coupling map."""

        coupling = CouplingMap([[0, 1], [1, 2], [2, 3]])
        qr = QuantumRegister(4, 'q')
        cr = ClassicalRegister(4, 'c')
        circuit = QuantumCircuit(qr, cr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])
        circuit.cx(qr[0], qr[2])
        circuit.cx(qr[0], qr[3])
        circuit.measure(qr, cr)
        dag = circuit_to_dag(circuit)

        layout = Layout([(QuantumRegister(4, 'q'), 0),
                         (QuantumRegister(4, 'q'), 1),
                         (QuantumRegister(4, 'q'), 2),
                         (QuantumRegister(4, 'q'), 3)])

        pass_ = StochasticSwap(coupling, layout, 20, 5)
        after = pass_.run(dag)

        valid_couplings = [
            set([layout[a], layout[b]]) for (a, b) in coupling.get_edges()
        ]

        for _2q_node in after.twoQ_nodes():
            self.assertIn(set(_2q_node.qargs), valid_couplings)
 def test_empty_coupling_class(self):
     coupling = CouplingMap()
     self.assertEqual(0, coupling.size())
     self.assertEqual([], coupling.physical_qubits)
     self.assertEqual([], coupling.get_edges())
     self.assertFalse(coupling.is_connected())
     self.assertEqual("", str(coupling))
 def test_coupling_distance(self):
     coupling_list = [(0, 1), (0, 2), (1, 2)]
     coupling = CouplingMap(couplinglist=coupling_list)
     self.assertTrue(coupling.is_connected())
     physical_qubits = coupling.physical_qubits
     result = coupling.distance(physical_qubits[0], physical_qubits[1])
     self.assertEqual(1, result)
示例#4
0
def _transpilation(circuit,
                   basis_gates=None,
                   coupling_map=None,
                   initial_layout=None,
                   seed_mapper=None,
                   pass_manager=None):
    """Perform transpilation of a single circuit.

    Args:
        circuit (QuantumCircuit): A circuit to transpile.
        basis_gates (str): comma-separated basis gate set to compile to
        coupling_map (list): coupling map (perhaps custom) to target in mapping
        initial_layout (list): initial layout of qubits in mapping
        seed_mapper (int): random seed for the swap_mapper
        pass_manager (PassManager): a pass_manager for the transpiler stage

    Returns:
        QuantumCircuit: A transpiled circuit.

    Raises:
        TranspilerError: if args are not complete for transpiler to function.
    """
    if pass_manager and not pass_manager.working_list:
        return circuit

    dag = circuit_to_dag(circuit)

    # pick a trivial layout if the circuit already satisfies the coupling constraints
    # else layout on the most densely connected physical qubit subset
    # FIXME: this should be simplified once it is ported to a PassManager
    if coupling_map:
        check_map = CheckMap(CouplingMap(coupling_map))
        check_map.run(dag)
        if check_map.property_set['is_direction_mapped']:
            trivial_layout = TrivialLayout(CouplingMap(coupling_map))
            trivial_layout.run(dag)
            initial_layout = trivial_layout.property_set['layout']
        else:
            dense_layout = DenseLayout(CouplingMap(coupling_map))
            dense_layout.run(dag)
            initial_layout = dense_layout.property_set['layout']
        # temporarily build old-style layout dict
        # (FIXME: remove after transition to StochasticSwap pass)
        layout = initial_layout.copy()
        virtual_qubits = layout.get_virtual_bits()
        initial_layout = {(v[0].name, v[1]): ('q', layout[v])
                          for v in virtual_qubits}

    final_dag = transpile_dag(dag,
                              basis_gates=basis_gates,
                              coupling_map=coupling_map,
                              initial_layout=initial_layout,
                              format='dag',
                              seed_mapper=seed_mapper,
                              pass_manager=pass_manager)

    out_circuit = dag_to_circuit(final_dag)

    return out_circuit
示例#5
0
    def setUp(self):
        self.cmap5 = CouplingMap([[1, 0], [2, 0], [2, 1], [3, 2], [3, 4],
                                  [4, 2]])

        self.cmap16 = CouplingMap([[1, 0], [1, 2], [2, 3], [3, 4], [3, 14],
                                   [5, 4], [6, 5], [6, 7], [6, 11], [7, 10],
                                   [8, 7], [9, 8], [9, 10], [11, 10], [12, 5],
                                   [12, 11], [12, 13], [13, 4], [13, 14],
                                   [15, 0], [15, 2], [15, 14]])
    def test_init_with_couplinglist(self):
        coupling_list = [[0, 1], [1, 2]]
        coupling = CouplingMap(couplinglist=coupling_list)

        qubits_expected = [0, 1, 2]
        edges_expected = [(0, 1), (1, 2)]

        self.assertEqual(coupling.physical_qubits, qubits_expected)
        self.assertEqual(coupling.get_edges(), edges_expected)
        self.assertEqual(2, coupling.distance(0, 2))
    def test_lookahead_swap_maps_measurements(self):
        """Verify measurement nodes are updated to map correct cregs to re-mapped qregs.

        Create a circuit with measures on q0 and q2, following a swap between q0 and q2.
        Since that swap is not in the coupling, one of the two will be required to move.
        Verify that the mapped measure corresponds to one of the two possible layouts following
        the swap.

        """

        qr = QuantumRegister(3)
        cr = ClassicalRegister(2)
        circuit = QuantumCircuit(qr, cr)

        circuit.cx(qr[0], qr[2])
        circuit.measure(qr[0], cr[0])
        circuit.measure(qr[2], cr[1])

        dag_circuit = circuit_to_dag(circuit)

        coupling_map = CouplingMap(couplinglist=[(0, 1), (1, 2)])

        pass_manager = PassManager()
        pass_manager.append([LookaheadSwap(coupling_map)])
        mapped_dag = transpile_dag(dag_circuit, pass_manager=pass_manager)

        mapped_measure_qargs = set(mapped_dag.multi_graph.nodes(data=True)[op]['qargs'][0]
                                   for op in mapped_dag.get_named_nodes('measure'))

        self.assertIn(mapped_measure_qargs,
                      [set(((QuantumRegister(3, 'q'), 0), (QuantumRegister(3, 'q'), 1))),
                       set(((QuantumRegister(3, 'q'), 1), (QuantumRegister(3, 'q'), 2)))])
    def test_lookahead_swap_finds_minimal_swap_solution(self):
        """Of many valid SWAPs, test that LookaheadSwap finds the cheapest path.

        For a two CNOT circuit: cx q[0],q[2]; cx q[0],q[1]
        on the initial layout: qN -> qN
        (At least) two solutions exist:
        - SWAP q[0],[1], cx q[0],q[2], cx q[0],q[1]
        - SWAP q[1],[2], cx q[0],q[2], SWAP q[1],q[2], cx q[0],q[1]

        Verify that we find the first solution, as it requires fewer SWAPs.
        """

        qr = QuantumRegister(3)
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[0], qr[2])
        circuit.cx(qr[0], qr[1])

        dag_circuit = circuit_to_dag(circuit)

        coupling_map = CouplingMap(couplinglist=[(0, 1), (1, 2)])

        pass_manager = PassManager()
        pass_manager.append([LookaheadSwap(coupling_map)])
        mapped_dag = transpile_dag(dag_circuit, pass_manager=pass_manager)

        self.assertEqual(mapped_dag.count_ops().get('swap', 0),
                         dag_circuit.count_ops().get('swap', 0) + 1)
    def test_lookahead_swap_doesnt_modify_mapped_circuit(self):
        """Test that lookahead mapper is idempotent.

        It should not modify a circuit which is already compatible with the
        coupling map, and can be applied repeatedly without modifying the circuit.
        """

        qr = QuantumRegister(3, name='q')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[0], qr[2])
        circuit.cx(qr[0], qr[1])
        original_dag = circuit_to_dag(circuit)

        # Create coupling map which contains all two-qubit gates in the circuit.
        coupling_map = CouplingMap(couplinglist=[(0, 1), (0, 2)])

        pass_manager = PassManager()
        pass_manager.append(LookaheadSwap(coupling_map))
        mapped_dag = transpile_dag(original_dag, pass_manager=pass_manager)

        self.assertEqual(original_dag, mapped_dag)

        second_pass_manager = PassManager()
        second_pass_manager.append(LookaheadSwap(coupling_map))
        remapped_dag = transpile_dag(mapped_dag, pass_manager=second_pass_manager)

        self.assertEqual(mapped_dag, remapped_dag)
示例#10
0
    def test_lookahead_swap_maps_measurements(self):
        """Verify measurement nodes are updated to map correct cregs to re-mapped qregs.

        Create a circuit with measures on q0 and q2, following a swap between q0 and q2.
        Since that swap is not in the coupling, one of the two will be required to move.
        Verify that the mapped measure corresponds to one of the two possible layouts following
        the swap.

        """

        qr = QuantumRegister(3)
        cr = ClassicalRegister(2)
        circuit = QuantumCircuit(qr, cr)

        circuit.cx(qr[0], qr[2])
        circuit.measure(qr[0], cr[0])
        circuit.measure(qr[2], cr[1])

        dag_circuit = circuit_to_dag(circuit)

        coupling_map = CouplingMap([[0, 1], [1, 2]])

        mapped_dag = LookaheadSwap(coupling_map).run(dag_circuit)

        mapped_measure_qargs = set(op.qargs[0]
                                   for op in mapped_dag.named_nodes('measure'))

        self.assertIn(mapped_measure_qargs, [
            set(((QuantumRegister(3, 'q'), 0), (QuantumRegister(3, 'q'), 1))),
            set(((QuantumRegister(3, 'q'), 1), (QuantumRegister(3, 'q'), 2)))
        ])
示例#11
0
    def test_lookahead_swap_maps_barriers(self):
        """Verify barrier nodes are updated to re-mapped qregs.

        Create a circuit with a barrier on q0 and q2, following a swap between q0 and q2.
        Since that swap is not in the coupling, one of the two will be required to move.
        Verify that the mapped barrier corresponds to one of the two possible layouts following
        the swap.

        """

        qr = QuantumRegister(3)
        cr = ClassicalRegister(2)
        circuit = QuantumCircuit(qr, cr)

        circuit.cx(qr[0], qr[2])
        circuit.barrier(qr[0], qr[2])

        dag_circuit = circuit_to_dag(circuit)

        coupling_map = CouplingMap([[0, 1], [1, 2]])

        mapped_dag = LookaheadSwap(coupling_map).run(dag_circuit)

        mapped_barrier_qargs = [
            set(mapped_dag.node(op)['qargs'])
            for op in mapped_dag.named_nodes('barrier')
        ][0]

        self.assertIn(mapped_barrier_qargs, [
            set(((QuantumRegister(3, 'q'), 0), (QuantumRegister(3, 'q'), 1))),
            set(((QuantumRegister(3, 'q'), 1), (QuantumRegister(3, 'q'), 2)))
        ])
示例#12
0
    def test_multiple_registers_with_default_layout(self):
        """
        Test two registers + measurements using no layout.
        The default layout will be adjusted to all gates
        become nearest neighbor. The pass has the layout
        in pass_.initial_layout.
        """
        coupling = CouplingMap(couplinglist=[[0, 1], [1, 2]])

        qr_q = QuantumRegister(2, 'q')
        qr_a = QuantumRegister(1, 'a')
        cr_c = ClassicalRegister(3, 'c')
        circ = QuantumCircuit(qr_q, qr_a, cr_c)
        circ.cx(qr_q[0], qr_a[0])
        circ.cx(qr_q[1], qr_a[0])
        circ.measure(qr_q[0], cr_c[0])
        circ.measure(qr_q[1], cr_c[1])
        circ.measure(qr_a[0], cr_c[2])
        dag = circuit_to_dag(circ)

        layout = None

        pass_ = StochasticSwap(coupling, layout, 20, 13)
        after = pass_.run(dag)

        self.assertEqual(dag, after)
示例#13
0
    def test_permute_wires_6(self):
        """
         qr0:--(+)-------.--
                |        |
         qr1:---|--------|--
                |
         qr2:---|--------|--
                |        |
         qr3:---.--[H]--(+)-

         Coupling map: [0]--[1]--[2]--[3]
        """
        coupling = CouplingMap(couplinglist=[[0, 1], [1, 2], [2, 3]])

        qr = QuantumRegister(4, 'q')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[3], qr[0])
        circuit.h(qr[3])
        circuit.cx(qr[0], qr[3])
        dag = circuit_to_dag(circuit)

        pass_ = StochasticSwap(coupling, None, 20, 13)
        after = pass_.run(dag)

        self.assertEqual(dag, after)
示例#14
0
    def test_already_mapped(self):
        """Circuit not remapped if matches topology.

        See: https://github.com/Qiskit/qiskit-terra/issues/342
        """
        coupling = CouplingMap(
            couplinglist=[[1, 0], [1, 2], [2, 3], [3, 4], [3, 14], [5, 4], [6, 5],
                          [6, 7], [6, 11], [7, 10], [8, 7], [9, 8], [9, 10],
                          [11, 10], [12, 5], [12, 11], [12, 13], [13, 4], [13, 14],
                          [15, 0], [15, 0], [15, 2], [15, 14]])
        qr = QuantumRegister(16, 'q')
        cr = ClassicalRegister(16, 'c')
        circ = QuantumCircuit(qr, cr)
        circ.cx(qr[3], qr[14])
        circ.cx(qr[5], qr[4])
        circ.h(qr[9])
        circ.cx(qr[9], qr[8])
        circ.x(qr[11])
        circ.cx(qr[3], qr[4])
        circ.cx(qr[12], qr[11])
        circ.cx(qr[13], qr[4])
        for j in range(16):
            circ.measure(qr[j], cr[j])

        dag = circuit_to_dag(circ)

        pass_ = StochasticSwap(coupling, None, 20, 13)
        after = pass_.run(dag)
        self.assertEqual(circuit_to_dag(circ), after)
示例#15
0
    def test_multiple_registers_with_layout_adjust(self):
        """
        Test two registers + measurements using a layout.
        The mapper will adjust the initial layout so that
        all of the gates can be done without swaps.
        """
        coupling = CouplingMap(couplinglist=[[0, 1], [1, 2]])

        qr_q = QuantumRegister(2, 'q')
        qr_a = QuantumRegister(1, 'a')
        cr_c = ClassicalRegister(3, 'c')
        circ = QuantumCircuit(qr_q, qr_a, cr_c)
        circ.cx(qr_q[0], qr_a[0])
        circ.cx(qr_q[1], qr_a[0])
        circ.measure(qr_q[0], cr_c[0])
        circ.measure(qr_q[1], cr_c[1])
        circ.measure(qr_a[0], cr_c[2])
        dag = circuit_to_dag(circ)

        layout = Layout([(QuantumRegister(2, 'q'), 0),
                         (QuantumRegister(2, 'q'), 1),
                         (QuantumRegister(1, 'a'), 0)])

        pass_ = StochasticSwap(coupling, layout, 20, 13)
        after = pass_.run(dag)

        self.assertEqual(dag, after)
示例#16
0
    def test_permute_wires_5(self):
        """This is the same case as permute_wires_4
        except the single qubit gate is after the two-qubit
        gate, so the layout is adjusted.
         qr0:--(+)------
                |
         qr1:---|-------
                |
         qr2:---|-------
                |
         qr3:---.--[H]--

         Coupling map: [0]--[1]--[2]--[3]
        """
        coupling = CouplingMap(couplinglist=[[0, 1], [1, 2], [2, 3]])

        qr = QuantumRegister(4, 'q')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[3], qr[0])
        circuit.h(qr[3])
        dag = circuit_to_dag(circuit)

        pass_ = StochasticSwap(coupling, None, 20, 13)
        after = pass_.run(dag)

        self.assertEqual(dag, after)
示例#17
0
    def test_keep_layout(self):
        """After a swap, the following gates also change the wires.
         qr0:---.---[H]--
                |
         qr1:---|--------
                |
         qr2:--(+)-------

         CouplingMap map: [0]--[1]--[2]

         qr0:--X-----------
               |
         qr1:--X---.--[H]--
                   |
         qr2:-----(+)------
        """
        coupling = CouplingMap([[1, 0], [1, 2]])

        qr = QuantumRegister(3, 'q')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[0], qr[2])
        circuit.h(qr[0])
        dag = circuit_to_dag(circuit)

        expected = QuantumCircuit(qr)
        expected.swap(qr[0], qr[1])
        expected.cx(qr[1], qr[2])
        expected.h(qr[1])

        pass_ = BasicSwap(coupling)
        after = pass_.run(dag)

        self.assertEqual(circuit_to_dag(expected), after)
示例#18
0
    def test_permute_wires_1(self):
        """All of the test_permute_wires tests are derived
        from the basic mapper tests. In this case, the
        stochastic mapper handles a single
        layer by qubit label permutations so as not to
        introduce additional swap gates. The new
        initial layout is found in pass_.initial_layout.
         q0:-------

         q1:--(+)--
               |
         q2:---.---

         Coupling map: [1]--[0]--[2]
        """
        coupling = CouplingMap(couplinglist=[[0, 1], [0, 2]])

        qr = QuantumRegister(3, 'q')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[1], qr[2])
        dag = circuit_to_dag(circuit)

        pass_ = StochasticSwap(coupling, None, 20, 13)
        after = pass_.run(dag)

        self.assertEqual(dag, after)
示例#19
0
def optimize_circuit(circuit, coupling_list=None):
    """Use the qiskit transpiler module to perform a suite of optimizations on
    the given circuit, including imposing swap coupling.
    Args:
    circuit :: qiskit.QuantumCircuit - the circuit to optimize
    coupling_list :: [(int, int)] - the list of connected qubit pairs
                     if None is passed in, will not perform mapping

    Returns:
    optimized_circuit :: qiskit.QuantumCircuit - the optimized circuit
    """
    # TODO: optimize until gates count stays stagnant.
    # TODO: implement rotation merge for clifford gates as pass.
    merge_rotation_gates(circuit)

    coupling_map = None if coupling_list is None else CouplingMap(
        coupling_list)

    pass_manager = PassManager()
    pass_manager.append(HCancellation())
    pass_manager.append(CXCancellation())
    # Some CNOT identities are interleaved between others,
    # for this reason a second pass is required. More passes
    # may be required for other circuits.
    pass_manager.append(CXCancellation())
    if coupling_map is not None:
        pass_manager.append(BasicSwap(coupling_map))

    optimized_circuit = transpile(circuit,
                                  backend=state_backend,
                                  coupling_map=coupling_list,
                                  pass_manager=pass_manager)

    return optimized_circuit
示例#20
0
    def test_direction_flip(self):
        """ Flip a CX
         qr0:----.----
                 |
         qr1:---(+)---

         CouplingMap map: [0] -> [1]

         qr0:-[H]-(+)-[H]--
                   |
         qr1:-[H]--.--[H]--
        """
        qr = QuantumRegister(2, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[1], qr[0])
        coupling = CouplingMap([(0, 1)])
        dag = circuit_to_dag(circuit)

        expected = QuantumCircuit(qr)
        expected.h(qr[0])
        expected.h(qr[1])
        expected.cx(qr[0], qr[1])
        expected.h(qr[0])
        expected.h(qr[1])

        pass_ = CXDirection(coupling)
        after = pass_.run(dag)

        self.assertEqual(circuit_to_dag(expected), after)
示例#21
0
    def test_a_single_swap(self):
        """ Adding a swap
         q0:-------

         q1:--(+)--
               |
         q2:---.---

         CouplingMap map: [1]--[0]--[2]

         q0:--X---.---
              |   |
         q1:--X---|---
                  |
         q2:-----(+)--

        """
        coupling = CouplingMap([[0, 1], [0, 2]])

        qr = QuantumRegister(3, 'q')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[1], qr[2])
        dag = circuit_to_dag(circuit)

        expected = QuantumCircuit(qr)
        expected.swap(qr[1], qr[0])
        expected.cx(qr[0], qr[2])

        pass_ = BasicSwap(coupling)
        after = pass_.run(dag)

        self.assertEqual(circuit_to_dag(expected), after)
示例#22
0
    def test_all_single_qubit(self):
        """Test all trivial layers."""
        coupling = CouplingMap(couplinglist=[[0, 1], [1, 2], [1, 3]])
        qr = QuantumRegister(2, 'q')
        ar = QuantumRegister(2, 'a')
        cr = ClassicalRegister(4, 'c')
        circ = QuantumCircuit(qr, ar, cr)
        circ.h(qr)
        circ.h(ar)
        circ.s(qr)
        circ.s(ar)
        circ.t(qr)
        circ.t(ar)
        circ.measure(qr[0], cr[0])  # intentional duplicate
        circ.measure(qr[0], cr[0])
        circ.measure(qr[1], cr[1])
        circ.measure(ar[0], cr[2])
        circ.measure(ar[1], cr[3])
        dag = circuit_to_dag(circ)

        layout = Layout([(QuantumRegister(2, 'q'), 0),
                         (QuantumRegister(2, 'q'), 1),
                         (QuantumRegister(2, 'a'), 0),
                         (QuantumRegister(2, 'a'), 1)])

        pass_ = StochasticSwap(coupling, layout, 20, 13)
        after = pass_.run(dag)
        self.assertEqual(dag, after)
示例#23
0
    def test_initial_layout(self):
        """ Using an initial_layout
         0:q1:--(+)--
                 |
         1:q0:---|---
                 |
         2:q2:---.---

         CouplingMap map: [0]--[1]--[2]

         0:q1:--X-------
                |
         1:q0:--X---.---
                    |
         2:q2:-----(+)--

        """
        coupling = CouplingMap([[0, 1], [1, 2]])

        qr = QuantumRegister(3, 'q')
        circuit = QuantumCircuit(qr)
        circuit.cx(qr[1], qr[2])
        dag = circuit_to_dag(circuit)
        layout = Layout({qr[1]: 0, qr[0]: 1, qr[2]: 2})

        expected = QuantumCircuit(qr)
        expected.swap(qr[1], qr[0])
        expected.cx(qr[0], qr[2])

        pass_ = BasicSwap(coupling, initial_layout=layout)
        after = pass_.run(dag)

        self.assertEqual(circuit_to_dag(expected), after)
示例#24
0
    def test_multiple_registers_with_good_layout(self):
        """
        Test two registers + measurements using a layout.
        The layout makes all gates nearest neighbor.
        """
        coupling = CouplingMap(couplinglist=[[0, 1], [1, 2]])

        qr_q = QuantumRegister(2, 'q')
        qr_a = QuantumRegister(1, 'a')
        cr_c = ClassicalRegister(3, 'c')
        circ = QuantumCircuit(qr_q, qr_a, cr_c)
        circ.cx(qr_q[0], qr_a[0])
        circ.cx(qr_q[1], qr_a[0])
        circ.measure(qr_q[0], cr_c[0])
        circ.measure(qr_q[1], cr_c[1])
        circ.measure(qr_a[0], cr_c[2])
        dag = circuit_to_dag(circ)

        layout = Layout([(QuantumRegister(2, 'q'), 0),
                         (QuantumRegister(1, 'a'), 0),
                         (QuantumRegister(2, 'q'), 1)])

        pass_ = StochasticSwap(coupling, layout, 20, 13)
        after = pass_.run(dag)

        self.assertEqual(dag, after)
示例#25
0
    def test_initial_layout_in_different_qregs(self):
        """ Using an initial_layout, and with several qregs
         0:q1_0:--(+)--
                   |
         1:q0_0:---|---
                   |
         2:q2_0:---.---

         CouplingMap map: [0]--[1]--[2]

         0:q1_0:--X-------
                  |
         1:q0_0:--X---.---
                      |
         2:q2_0:-----(+)--
        """
        coupling = CouplingMap([[0, 1], [1, 2]])

        qr0 = QuantumRegister(1, 'q0')
        qr1 = QuantumRegister(1, 'q1')
        qr2 = QuantumRegister(1, 'q2')
        circuit = QuantumCircuit(qr0, qr1, qr2)
        circuit.cx(qr1[0], qr2[0])
        dag = circuit_to_dag(circuit)
        layout = Layout({qr1[0]: 0, qr0[0]: 1, qr2[0]: 2})

        expected = QuantumCircuit(qr0, qr1, qr2)
        expected.swap(qr1[0], qr0[0])
        expected.cx(qr0[0], qr2[0])

        pass_ = BasicSwap(coupling, initial_layout=layout)
        after = pass_.run(dag)

        self.assertEqual(circuit_to_dag(expected), after)
示例#26
0
 def create_passmanager(self, coupling_map, initial_layout=None):
     """Returns a PassManager using self.pass_class(coupling_map, initial_layout)"""
     passmanager = PassManager(
         self.pass_class(CouplingMap(coupling_map),  # pylint: disable=not-callable
                         **self.additional_args))
     if initial_layout:
         passmanager.property_set['layout'] = Layout(initial_layout)
     return passmanager
示例#27
0
    def create_passmanager(self, coupling_map, initial_layout=None):
        """Returns a PassManager using self.pass_class(coupling_map, initial_layout)"""
        passmanager = PassManager()
        if initial_layout:
            passmanager.append(SetLayout(Layout(initial_layout)))

        # pylint: disable=not-callable
        passmanager.append(self.pass_class(CouplingMap(coupling_map), **self.additional_args))
        return passmanager
示例#28
0
def _transpilation(circuit,
                   basis_gates=None,
                   coupling_map=None,
                   initial_layout=None,
                   seed_mapper=None,
                   pass_manager=None):
    """Perform transpilation of a single circuit.

    Args:
        circuit (QuantumCircuit): A circuit to transpile.
        basis_gates (list[str]): list of basis gate names supported by the
            target. Default: ['u1','u2','u3','cx','id']
        coupling_map (list): coupling map (perhaps custom) to target in mapping
        initial_layout (list): initial layout of qubits in mapping
        seed_mapper (int): random seed for the swap_mapper
        pass_manager (PassManager): a pass_manager for the transpiler stage

    Returns:
        QuantumCircuit: A transpiled circuit.

    Raises:
        TranspilerError: if args are not complete for transpiler to function.
    """
    if pass_manager and not pass_manager.working_list:
        return circuit

    dag = circuit_to_dag(circuit)
    del circuit

    # pick a trivial layout if the circuit already satisfies the coupling constraints
    # else layout on the most densely connected physical qubit subset
    # FIXME: this should be simplified once it is ported to a PassManager
    if coupling_map and initial_layout is None:
        cm_object = CouplingMap(coupling_map)
        check_cnot_direction = CheckCnotDirection(cm_object)
        check_cnot_direction.run(dag)
        if check_cnot_direction.property_set['is_direction_mapped']:
            trivial_layout = TrivialLayout(cm_object)
            trivial_layout.run(dag)
            initial_layout = trivial_layout.property_set['layout']
        else:
            dense_layout = DenseLayout(cm_object)
            dense_layout.run(dag)
            initial_layout = dense_layout.property_set['layout']

    final_dag = transpile_dag(dag,
                              basis_gates=basis_gates,
                              coupling_map=coupling_map,
                              initial_layout=initial_layout,
                              seed_mapper=seed_mapper,
                              pass_manager=pass_manager)

    out_circuit = dag_to_circuit(final_dag)

    return out_circuit
    def test_raises_wider_circuit(self):
        """Test error is raised if the circuit is wider than coupling map.
        """
        qr0 = QuantumRegister(3, 'q0')
        qr1 = QuantumRegister(3, 'q1')
        circuit = QuantumCircuit(qr0, qr1)
        circuit.cx(qr0, qr1)

        dag = circuit_to_dag(circuit)
        with self.assertRaises(TranspilerError):
            pass_ = TrivialLayout(CouplingMap(self.cmap5))
            pass_.run(dag)
示例#30
0
def impose_swap_coupling(circuit, coupling_list):
    """Impose a qubit topology on the given circuit using swap gates.
    Args:
    circuit :: qiskit.QuantumCircuit - the circuit to impose a topology upon.
    coupling_list :: [(int, int)] - the list of connected qubit pairs

    Returns:
    coupled_circuit :: qiskit.QuantumCircuit - the circuit equivalent to the
    original that abides by the qubit mapping via swap gates
    """
    dag = circuit_to_dag(circuit)
    coupling_map = CouplingMap(coupling_list)
    coupled_dag = swap_mapper(dag, coupling_map)[0]
    coupled_circuit = dag_to_circuit(coupled_dag)
    return coupled_circuit