def test_already_mapped_1(self):
        """Circuit not remapped if matches topology.

        See: https://github.com/Qiskit/qiskit-terra/issues/342
        """
        backend = FakeRueschlikon()
        coupling_map = backend.configuration().coupling_map
        basis_gates = backend.configuration().basis_gates

        qr = QuantumRegister(16, 'qr')
        cr = ClassicalRegister(16, 'cr')
        qc = QuantumCircuit(qr, cr)
        qc.cx(qr[3], qr[14])
        qc.cx(qr[5], qr[4])
        qc.h(qr[9])
        qc.cx(qr[9], qr[8])
        qc.x(qr[11])
        qc.cx(qr[3], qr[4])
        qc.cx(qr[12], qr[11])
        qc.cx(qr[13], qr[4])
        qc.measure(qr, cr)

        new_qc = transpile(qc, coupling_map=coupling_map, basis_gates=basis_gates)
        cx_qubits = [qargs for (gate, qargs, _) in new_qc.data
                     if gate.name == "cx"]
        cx_qubits_physical = [[ctrl[1], tgt[1]] for [ctrl, tgt] in cx_qubits]
        self.assertEqual(sorted(cx_qubits_physical),
                         [[3, 4], [3, 14], [5, 4], [9, 8], [12, 11], [13, 4]])
 def test_failed_reduced_map(self):
     """Generate a bad disconnected reduced map"""
     fake = FakeRueschlikon()
     cmap = fake.configuration().coupling_map
     coupling_map = CouplingMap(cmap)
     with self.assertRaises(CouplingError):
         coupling_map.reduce([12, 11, 10, 3])
    def test_move_measurements(self):
        """Measurements applied AFTER swap mapping.
        """
        backend = FakeRueschlikon()
        cmap = backend.configuration().coupling_map
        circ = QuantumCircuit.from_qasm_file(
            self._get_resource_path('move_measurements.qasm', Path.QASMS))

        lay = Layout({
            ('qa', 0): ('q', 0),
            ('qa', 1): ('q', 1),
            ('qb', 0): ('q', 15),
            ('qb', 1): ('q', 2),
            ('qb', 2): ('q', 14),
            ('qN', 0): ('q', 3),
            ('qN', 1): ('q', 13),
            ('qN', 2): ('q', 4),
            ('qc', 0): ('q', 12),
            ('qNt', 0): ('q', 5),
            ('qNt', 1): ('q', 11),
            ('qt', 0): ('q', 6)
        })
        out = transpile(circ, initial_layout=lay, coupling_map=cmap)
        out_dag = circuit_to_dag(out)
        meas_nodes = out_dag.named_nodes('measure')
        for meas_node in meas_nodes:
            is_last_measure = all([
                after_measure.type == 'out'
                for after_measure in out_dag.quantum_successors(meas_node)
            ])
            self.assertTrue(is_last_measure)
Exemple #4
0
    def test_layout_1711(self, level):
        """Test that a user-given initial layout is respected,
        in the qobj.

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

        backend = FakeRueschlikon()

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

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

        compiled_ops = qobj.experiments[0].instructions
        for operation in compiled_ops:
            if operation.name == 'cx':
                self.assertIn(operation.qubits, backend.configuration().coupling_map)
                self.assertIn(operation.qubits, [[15, 0], [15, 2]])
    def test_initial_layout_1(self):
        """Test that a user-given initial layout is respected,
        in the qobj.

        See: https://github.com/Qiskit/qiskit-terra/issues/1711
        """
        # build a circuit which works as-is on the coupling map, using the initial layout
        qr = QuantumRegister(3)
        cr = ClassicalRegister(3)
        qc = QuantumCircuit(qr, cr)
        qc.cx(qr[2], qr[1])
        qc.cx(qr[2], qr[0])
        initial_layout = {0: qr[1], 2: qr[0], 15: qr[2]}
        backend = FakeRueschlikon()

        for optimization_level in range(4):
            qc_b = transpile(qc,
                             backend,
                             initial_layout=initial_layout,
                             optimization_level=optimization_level)
            qobj = assemble(qc_b)

            compiled_ops = qobj.experiments[0].instructions
            for operation in compiled_ops:
                if operation.name == 'cx':
                    self.assertIn(operation.qubits,
                                  backend.configuration().coupling_map)
                    self.assertIn(operation.qubits, [[15, 0], [15, 2]])
 def test_successful_reduced_map(self):
     """Generate a reduced map"""
     fake = FakeRueschlikon()
     cmap = fake.configuration().coupling_map
     coupling_map = CouplingMap(cmap)
     out = coupling_map.reduce([12, 11, 10, 9]).get_edges()
     ans = [(1, 2), (3, 2), (0, 1)]
     self.assertEqual(set(out), set(ans))
    def test_delay_converts_to_dt(self):
        """Test that a delay instruction is converted to units of dt given a backend."""
        qc = QuantumCircuit(2)
        qc.delay(1000, [0], unit='us')

        backend = FakeRueschlikon()
        backend.configuration().dt = 0.5e-6
        out = transpile([qc, qc], backend)
        self.assertEqual(out[0].data[0][0].unit, 'dt')
        self.assertEqual(out[1].data[0][0].unit, 'dt')

        out = transpile(qc, dt=1e-9)
        self.assertEqual(out.data[0][0].unit, 'dt')
    def test_move_measurements(self):
        """Measurements applied AFTER swap mapping.
        """
        backend = FakeRueschlikon()
        cmap = backend.configuration().coupling_map
        circ = QuantumCircuit.from_qasm_file(
            self._get_resource_path('move_measurements.qasm', Path.QASMS))

        lay = [0, 1, 15, 2, 14, 3, 13, 4, 12, 5, 11, 6]
        out = transpile(circ, initial_layout=lay, coupling_map=cmap)
        out_dag = circuit_to_dag(out)
        meas_nodes = out_dag.named_nodes('measure')
        for meas_node in meas_nodes:
            is_last_measure = all([after_measure.type == 'out'
                                   for after_measure in out_dag.quantum_successors(meas_node)])
            self.assertTrue(is_last_measure)
Exemple #9
0
    def test_compile_with_initial_layout(self):
        """Test compile with an initial layout.
        Regression test for #1711
        """
        qr = QuantumRegister(3)
        cr = ClassicalRegister(3)
        qc = QuantumCircuit(qr, cr)
        qc.cx(qr[2], qr[1])
        qc.cx(qr[2], qr[0])
        initial_layout = {0: (qr, 1), 2: (qr, 0), 15: (qr, 2)}
        backend = FakeRueschlikon()

        qobj = compile(qc, backend, seed=42, initial_layout=initial_layout)

        compiled_ops = qobj.experiments[0].instructions
        for operation in compiled_ops:
            if operation.name == 'cx':
                self.assertIn(operation.qubits, backend.configuration().coupling_map)
                self.assertIn(operation.qubits, [[15, 0], [15, 2]])
    def test_move_measurements(self):
        """Measurements applied AFTER swap mapping.
        """
        backend = FakeRueschlikon()
        cmap = backend.configuration().coupling_map
        qasm_dir = os.path.join(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
            'qasm')
        circ = QuantumCircuit.from_qasm_file(
            os.path.join(qasm_dir, 'move_measurements.qasm'))

        lay = [0, 1, 15, 2, 14, 3, 13, 4, 12, 5, 11, 6]
        out = transpile(circ, initial_layout=lay, coupling_map=cmap)
        out_dag = circuit_to_dag(out)
        meas_nodes = out_dag.named_nodes('measure')
        for meas_node in meas_nodes:
            is_last_measure = all(after_measure.type == 'out'
                                  for after_measure in out_dag.quantum_successors(meas_node))
            self.assertTrue(is_last_measure)
Exemple #11
0
 def test_mapping_already_satisfied(self):
     """Test compiler doesn't change circuit already matching backend coupling
     """
     backend = FakeRueschlikon()
     qr = QuantumRegister(16)
     cr = ClassicalRegister(16)
     qc = QuantumCircuit(qr, cr)
     qc.h(qr[1])
     qc.x(qr[2])
     qc.x(qr[3])
     qc.x(qr[4])
     qc.cx(qr[1], qr[2])
     qc.cx(qr[2], qr[3])
     qc.cx(qr[3], qr[4])
     qc.cx(qr[3], qr[14])
     qc.measure(qr, cr)
     qobj = compile(qc, backend)
     compiled_ops = qobj.experiments[0].instructions
     original_cx_qubits = [[1, 2], [2, 3], [3, 4], [3, 14]]
     for operation in compiled_ops:
         if operation.name == 'cx':
             self.assertIn(operation.qubits, backend.configuration().coupling_map)
             self.assertIn(operation.qubits, original_cx_qubits)