예제 #1
0
    def test_lookahead_swap_hang_full_case(self):
        """Verify LookaheadSwap does not stall in reported case."""
        # ref: https://github.com/Qiskit/qiskit-terra/issues/2171

        qr = QuantumRegister(14, "q")
        qc = QuantumCircuit(qr)
        qc.cx(qr[0], qr[13])
        qc.cx(qr[1], qr[13])
        qc.cx(qr[1], qr[0])
        qc.cx(qr[13], qr[1])
        qc.cx(qr[6], qr[7])
        qc.cx(qr[8], qr[7])
        qc.cx(qr[8], qr[6])
        qc.cx(qr[7], qr[8])
        qc.cx(qr[0], qr[13])
        qc.cx(qr[1], qr[0])
        qc.cx(qr[13], qr[1])
        qc.cx(qr[0], qr[1])
        dag = circuit_to_dag(qc)

        cmap = CouplingMap(FakeMelbourne().configuration().coupling_map)

        out = LookaheadSwap(cmap, search_depth=4, search_width=4).run(dag)

        self.assertIsInstance(out, DAGCircuit)
    def test_from_backend_and_user(self):
        """Test from_backend() with a backend and user options.

        `FakeMelbourne` is used in this testcase. This backend does not have
        `defaults` attribute and thus not provide an instruction schedule map.
        """
        qr = QuantumRegister(4, "qr")
        initial_layout = [None, qr[0], qr[1], qr[2], None, qr[3]]

        backend = FakeMelbourne()
        config = PassManagerConfig.from_backend(backend,
                                                basis_gates=["user_gate"],
                                                initial_layout=initial_layout)
        self.assertEqual(config.basis_gates, ["user_gate"])
        self.assertNotEqual(config.basis_gates,
                            backend.configuration().basis_gates)
        self.assertIsNone(config.inst_map)
        self.assertEqual(
            str(config.coupling_map),
            str(CouplingMap(backend.configuration().coupling_map)))
        self.assertEqual(config.initial_layout, initial_layout)
예제 #3
0
    def test_layout_2532(self, level):
        """Test that a user-given initial layout is respected,
        in the transpiled circuit.

        See: https://github.com/Qiskit/qiskit-terra/issues/2532
        """
        # build a circuit which works as-is on the coupling map, using the initial layout
        qr = QuantumRegister(5, "q")
        cr = ClassicalRegister(2)
        ancilla = QuantumRegister(9, "ancilla")
        qc = QuantumCircuit(qr, cr)
        qc.cx(qr[2], qr[4])
        initial_layout = {
            qr[2]: 11,
            qr[4]: 3,  # map to [11, 3] connection
            qr[0]: 1,
            qr[1]: 5,
            qr[3]: 9,
        }
        final_layout = {
            0: ancilla[0],
            1: qr[0],
            2: ancilla[1],
            3: qr[4],
            4: ancilla[2],
            5: qr[1],
            6: ancilla[3],
            7: ancilla[4],
            8: ancilla[5],
            9: qr[3],
            10: ancilla[6],
            11: qr[2],
            12: ancilla[7],
            13: ancilla[8],
        }
        backend = FakeMelbourne()

        qc_b = transpile(qc,
                         backend,
                         initial_layout=initial_layout,
                         optimization_level=level)

        self.assertEqual(qc_b._layout._p2v, final_layout)

        output_qr = qc_b.qregs[0]
        for instruction in qc_b:
            if instruction.operation.name == "cx":
                for qubit in instruction.qubits:
                    self.assertIn(qubit, [output_qr[11], output_qr[3]])
예제 #4
0
    def test_backend(self, level):
        """With backend a layout and a swapper is run"""
        qr = QuantumRegister(5, "q")
        qc = QuantumCircuit(qr)
        qc.cx(qr[2], qr[4])
        backend = FakeMelbourne()

        _ = transpile(qc,
                      backend,
                      optimization_level=level,
                      callback=self.callback)

        self.assertIn("SetLayout", self.passes)
        self.assertIn("ApplyLayout", self.passes)
        self.assertIn("CheckGateDirection", self.passes)
예제 #5
0
    def test_5409(self, level):
        """The parameter layout_method='noise_adaptive' should be honored
        See: https://github.com/Qiskit/qiskit-terra/issues/5409
        """
        qr = QuantumRegister(5, "q")
        qc = QuantumCircuit(qr)
        qc.cx(qr[2], qr[4])
        backend = FakeMelbourne()

        _ = transpile(
            qc,
            backend,
            layout_method="noise_adaptive",
            optimization_level=level,
            callback=self.callback,
        )

        self.assertIn("SetLayout", self.passes)
        self.assertIn("ApplyLayout", self.passes)
        self.assertIn("NoiseAdaptiveLayout", self.passes)
예제 #6
0
class TestTranspileLevels(QiskitTestCase):
    """Test transpiler on fake backend"""
    @combine(
        circuit=[emptycircuit, circuit_2532],
        level=[0, 1, 2, 3],
        backend=[
            FakeTenerife(),
            FakeMelbourne(),
            FakeRueschlikon(),
            FakeTokyo(),
            FakePoughkeepsie(),
            None,
        ],
        dsc=
        "Transpiler {circuit.__name__} on {backend} backend at level {level}",
        name="{circuit.__name__}_{backend}_level{level}",
    )
    def test(self, circuit, level, backend):
        """All the levels with all the backends"""
        result = transpile(circuit(),
                           backend=backend,
                           optimization_level=level,
                           seed_transpiler=42)
        self.assertIsInstance(result, QuantumCircuit)
 def test_invalid_user_option(self):
     """Test from_backend() with an invalid user option."""
     with self.assertRaises(TypeError):
         PassManagerConfig.from_backend(FakeMelbourne(),
                                        invalid_option=None)
예제 #8
0
 def setup(self, _):
     self.rochester_coupling_map = [
         [0, 5],
         [0, 1],
         [1, 2],
         [1, 0],
         [2, 3],
         [2, 1],
         [3, 4],
         [3, 2],
         [4, 6],
         [4, 3],
         [5, 9],
         [5, 0],
         [6, 13],
         [6, 4],
         [7, 16],
         [7, 8],
         [8, 9],
         [8, 7],
         [9, 10],
         [9, 8],
         [9, 5],
         [10, 11],
         [10, 9],
         [11, 17],
         [11, 12],
         [11, 10],
         [12, 13],
         [12, 11],
         [13, 14],
         [13, 12],
         [13, 6],
         [14, 15],
         [14, 13],
         [15, 18],
         [15, 14],
         [16, 19],
         [16, 7],
         [17, 23],
         [17, 11],
         [18, 27],
         [18, 15],
         [19, 20],
         [19, 16],
         [20, 21],
         [20, 19],
         [21, 28],
         [21, 22],
         [21, 20],
         [22, 23],
         [22, 21],
         [23, 24],
         [23, 22],
         [23, 17],
         [24, 25],
         [24, 23],
         [25, 29],
         [25, 26],
         [25, 24],
         [26, 27],
         [26, 25],
         [27, 26],
         [27, 18],
         [28, 32],
         [28, 21],
         [29, 36],
         [29, 25],
         [30, 39],
         [30, 31],
         [31, 32],
         [31, 30],
         [32, 33],
         [32, 31],
         [32, 28],
         [33, 34],
         [33, 32],
         [34, 40],
         [34, 35],
         [34, 33],
         [35, 36],
         [35, 34],
         [36, 37],
         [36, 35],
         [36, 29],
         [37, 38],
         [37, 36],
         [38, 41],
         [38, 37],
         [39, 42],
         [39, 30],
         [40, 46],
         [40, 34],
         [41, 50],
         [41, 38],
         [42, 43],
         [42, 39],
         [43, 44],
         [43, 42],
         [44, 51],
         [44, 45],
         [44, 43],
         [45, 46],
         [45, 44],
         [46, 47],
         [46, 45],
         [46, 40],
         [47, 48],
         [47, 46],
         [48, 52],
         [48, 49],
         [48, 47],
         [49, 50],
         [49, 48],
         [50, 49],
         [50, 41],
         [51, 44],
         [52, 48]]
     self.basis_gates = ['u1', 'u2', 'u3', 'cx', 'id']
     self.qv_50_x_20 = build_qv_model_circuit(50, 20, 0)
     self.qv_14_x_14 = build_qv_model_circuit(14, 14, 0)
     self.qasm_path = os.path.abspath(
         os.path.join(os.path.dirname(__file__), 'qasm'))
     large_qasm_path = os.path.join(self.qasm_path, 'test_eoh_qasm.qasm')
     self.large_qasm = QuantumCircuit.from_qasm_file(large_qasm_path)
     self.melbourne = FakeMelbourne()
     self.durations = InstructionDurations([
         ("u1", None, 0),
         ("id", None, 160),
         ("u2", None, 160),
         ("u3", None, 320),
         ("cx", None, 800),
         ("measure", None, 3200),
     ], dt=1e-9)