|
51 | 51 | FakeRueschlikon,
|
52 | 52 | FakeBoeblingen,
|
53 | 53 | FakeMumbaiV2,
|
| 54 | + FakeNairobiV2, |
54 | 55 | )
|
55 | 56 | from qiskit.transpiler import Layout, CouplingMap
|
56 | 57 | from qiskit.transpiler import PassManager, TransformationPass
|
|
61 | 62 | from qiskit.transpiler.passmanager_config import PassManagerConfig
|
62 | 63 | from qiskit.transpiler.preset_passmanagers import level_0_pass_manager
|
63 | 64 | from qiskit.tools import parallel
|
| 65 | +from qiskit.pulse import InstructionScheduleMap |
64 | 66 |
|
65 | 67 |
|
66 | 68 | class CustomCX(Gate):
|
@@ -1899,3 +1901,73 @@ def run(self, dag):
|
1899 | 1901 | for qc_test in qcs_cal_added:
|
1900 | 1902 | added_cal = qc_test.calibrations["sx"][((0,), ())]
|
1901 | 1903 | self.assertEqual(added_cal, ref_cal)
|
| 1904 | + |
| 1905 | + @data(0, 1, 2, 3) |
| 1906 | + def test_backendv2_and_basis_gates(self, opt_level): |
| 1907 | + """Test transpile() with BackendV2 and basis_gates set.""" |
| 1908 | + backend = FakeNairobiV2() |
| 1909 | + qc = QuantumCircuit(5) |
| 1910 | + qc.h(0) |
| 1911 | + qc.cz(0, 1) |
| 1912 | + qc.cz(0, 2) |
| 1913 | + qc.cz(0, 3) |
| 1914 | + qc.cz(0, 4) |
| 1915 | + qc.measure_all() |
| 1916 | + tqc = transpile( |
| 1917 | + qc, |
| 1918 | + backend=backend, |
| 1919 | + basis_gates=["u", "cz"], |
| 1920 | + optimization_level=opt_level, |
| 1921 | + seed_transpiler=12345678942, |
| 1922 | + ) |
| 1923 | + op_count = set(tqc.count_ops()) |
| 1924 | + self.assertEqual({"u", "cz", "measure", "barrier"}, op_count) |
| 1925 | + for inst in tqc.data: |
| 1926 | + if inst.operation.name not in {"u", "cz"}: |
| 1927 | + continue |
| 1928 | + qubits = tuple(tqc.find_bit(x).index for x in inst.qubits) |
| 1929 | + self.assertIn(qubits, backend.target.qargs) |
| 1930 | + |
| 1931 | + @data(0, 1, 2, 3) |
| 1932 | + def test_backendv2_and_coupling_map(self, opt_level): |
| 1933 | + """Test transpile() with custom coupling map.""" |
| 1934 | + backend = FakeNairobiV2() |
| 1935 | + qc = QuantumCircuit(5) |
| 1936 | + qc.h(0) |
| 1937 | + qc.cz(0, 1) |
| 1938 | + qc.cz(0, 2) |
| 1939 | + qc.cz(0, 3) |
| 1940 | + qc.cz(0, 4) |
| 1941 | + qc.measure_all() |
| 1942 | + cmap = CouplingMap.from_line(5, bidirectional=False) |
| 1943 | + tqc = transpile( |
| 1944 | + qc, |
| 1945 | + backend=backend, |
| 1946 | + coupling_map=cmap, |
| 1947 | + optimization_level=opt_level, |
| 1948 | + seed_transpiler=12345678942, |
| 1949 | + ) |
| 1950 | + op_count = set(tqc.count_ops()) |
| 1951 | + self.assertTrue({"rz", "sx", "x", "cx", "measure", "barrier"}.issuperset(op_count)) |
| 1952 | + for inst in tqc.data: |
| 1953 | + if len(inst.qubits) == 2: |
| 1954 | + qubit_0 = tqc.find_bit(inst.qubits[0]).index |
| 1955 | + qubit_1 = tqc.find_bit(inst.qubits[1]).index |
| 1956 | + self.assertEqual(qubit_1, qubit_0 + 1) |
| 1957 | + |
| 1958 | + @data(0, 1, 2, 3) |
| 1959 | + def test_backend_and_custom_gate(self, opt_level): |
| 1960 | + """Test transpile() with BackendV2, custom basis pulse gate.""" |
| 1961 | + backend = FakeNairobiV2() |
| 1962 | + inst_map = InstructionScheduleMap() |
| 1963 | + inst_map.add("newgate", [0, 1], pulse.ScheduleBlock()) |
| 1964 | + newgate = Gate("newgate", 2, []) |
| 1965 | + circ = QuantumCircuit(2) |
| 1966 | + circ.append(newgate, [0, 1]) |
| 1967 | + tqc = transpile( |
| 1968 | + circ, backend, inst_map=inst_map, basis_gates=["newgate"], optimization_level=opt_level |
| 1969 | + ) |
| 1970 | + self.assertEqual(len(tqc.data), 1) |
| 1971 | + self.assertEqual(tqc.data[0].operation, newgate) |
| 1972 | + qubits = tuple(tqc.find_bit(x).index for x in tqc.data[0].qubits) |
| 1973 | + self.assertIn(qubits, backend.target.qargs) |
0 commit comments