Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify test transpiler #10943

Closed
wants to merge 10 commits into from
Closed
141 changes: 115 additions & 26 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@ def test_transpile_non_adjacent_layout(self):
└───┘

device:
0 - 1 - 2 - 3 - 4 - 5 - 6

| | | | | |

13 - 12 - 11 - 10 - 9 - 8 - 7
0 - 1 - 2 - 3 - 4 - 5
"""
qr = QuantumRegister(4, "qr")
circuit = QuantumCircuit(qr)
Expand All @@ -174,8 +170,8 @@ def test_transpile_non_adjacent_layout(self):
circuit.cx(qr[1], qr[2])
circuit.cx(qr[2], qr[3])

coupling_map = FakeMelbourne().configuration().coupling_map
basis_gates = FakeMelbourne().configuration().basis_gates
coupling_map = CouplingMap().from_line(6)
basis_gates = ['cx', 'id', 'rz', 'sx', 'x']
initial_layout = [None, qr[0], qr[1], qr[2], None, qr[3]]

new_circuit = transpile(
Expand All @@ -189,7 +185,7 @@ def test_transpile_non_adjacent_layout(self):

for instruction in new_circuit.data:
if isinstance(instruction.operation, CXGate):
self.assertIn([qubit_indices[x] for x in instruction.qubits], coupling_map)
self.assertIn(tuple([qubit_indices[x] for x in instruction.qubits]), coupling_map)

def test_transpile_qft_grid(self):
"""Transpile pipeline can handle 8-qubit QFT on 14-qubit grid."""
Expand All @@ -200,24 +196,48 @@ def test_transpile_qft_grid(self):
circuit.cp(math.pi / float(2 ** (i - j)), qr[i], qr[j])
circuit.h(qr[i])

coupling_map = FakeMelbourne().configuration().coupling_map
basis_gates = FakeMelbourne().configuration().basis_gates
# This makes a 14-qubit grid
coupling_map = CouplingMap().from_grid(num_rows=7, num_columns=2)
basis_gates = ['cx', 'id', 'rz', 'sx', 'x']
new_circuit = transpile(circuit, basis_gates=basis_gates, coupling_map=coupling_map)

qubit_indices = {bit: idx for idx, bit in enumerate(new_circuit.qubits)}

for instruction in new_circuit.data:
if isinstance(instruction.operation, CXGate):
self.assertIn([qubit_indices[x] for x in instruction.qubits], coupling_map)
self.assertIn(tuple([qubit_indices[x] for x in instruction.qubits]), coupling_map)

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
coupling_map = [
[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],
]

basis_gates = ["cx", "id", "rz", "sx", "x"]

qr = QuantumRegister(16, "qr")
cr = ClassicalRegister(16, "cr")
Expand Down Expand Up @@ -459,7 +479,6 @@ def test_transpile_singleton(self):

def test_mapping_correction(self):
"""Test mapping works in previous failed case."""
backend = FakeRueschlikon()
qr = QuantumRegister(name="qr", size=11)
cr = ClassicalRegister(name="qc", size=11)
circuit = QuantumCircuit(qr, cr)
Expand Down Expand Up @@ -516,7 +535,7 @@ def test_mapping_correction(self):
circuit.barrier(qr)
circuit.measure(qr, cr)

circuits = transpile(circuit, backend)
circuits = transpile(circuit, basis_gates=["cx", "id", "rz", "sx", "x"])

self.assertIsInstance(circuits, QuantumCircuit)

Expand Down Expand Up @@ -574,7 +593,6 @@ def test_transpiler_layout_from_intlist(self):

def test_mapping_multi_qreg(self):
"""Test mapping works for multiple qregs."""
backend = FakeRueschlikon()
qr = QuantumRegister(3, name="qr")
qr2 = QuantumRegister(1, name="qr2")
qr3 = QuantumRegister(4, name="qr3")
Expand All @@ -585,13 +603,12 @@ def test_mapping_multi_qreg(self):
qc.cx(qr[1], qr3[2])
qc.measure(qr, cr)

circuits = transpile(qc, backend)
circuits = transpile(qc, basis_gates=["cx", "id", "rz", "sx", "x"])

self.assertIsInstance(circuits, QuantumCircuit)

def test_transpile_circuits_diff_registers(self):
"""Transpile list of circuits with different qreg names."""
backend = FakeRueschlikon()
circuits = []
for _ in range(2):
qr = QuantumRegister(2)
Expand All @@ -602,12 +619,11 @@ def test_transpile_circuits_diff_registers(self):
circuit.measure(qr, cr)
circuits.append(circuit)

circuits = transpile(circuits, backend)
circuits = transpile(circuits, basis_gates=["cx", "id", "rz", "sx", "x"])
self.assertIsInstance(circuits[0], QuantumCircuit)

def test_wrong_initial_layout(self):
"""Test transpile with a bad initial layout."""
backend = FakeMelbourne()

qubit_reg = QuantumRegister(2, name="q")
clbit_reg = ClassicalRegister(2, name="c")
Expand All @@ -623,7 +639,7 @@ def test_wrong_initial_layout(self):
]

with self.assertRaises(TranspilerError) as cm:
transpile(qc, backend, initial_layout=bad_initial_layout)
transpile(qc, initial_layout=bad_initial_layout)

self.assertEqual(
"FullAncillaAllocation: The layout refers to a qubit that does not exist in circuit.",
Expand Down Expand Up @@ -703,10 +719,35 @@ def test_final_measurement_barrier_for_devices(self):
circ = QuantumCircuit.from_qasm_file(os.path.join(qasm_dir, "example.qasm"))
layout = Layout.generate_trivial_layout(*circ.qregs)
orig_pass = BarrierBeforeFinalMeasurements()
coupling_map = [
[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],
]

with patch.object(BarrierBeforeFinalMeasurements, "run", wraps=orig_pass.run) as mock_pass:
transpile(
circ,
coupling_map=FakeRueschlikon().configuration().coupling_map,
coupling_map=coupling_map,
initial_layout=layout,
)
self.assertTrue(mock_pass.called)
Expand All @@ -717,7 +758,32 @@ def test_do_not_run_gatedirection_with_symmetric_cm(self):
circ = QuantumCircuit.from_qasm_file(os.path.join(qasm_dir, "example.qasm"))
layout = Layout.generate_trivial_layout(*circ.qregs)
coupling_map = []
for node1, node2 in FakeRueschlikon().configuration().coupling_map:
c_map = [
[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],
]

for node1, node2 in c_map:
coupling_map.append([node1, node2])
coupling_map.append([node2, node1])

Expand Down Expand Up @@ -776,8 +842,31 @@ def test_pass_manager_empty(self):

def test_move_measurements(self):
"""Measurements applied AFTER swap mapping."""
backend = FakeRueschlikon()
cmap = backend.configuration().coupling_map
cmap = [
[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],
]

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"))

Expand Down