Skip to content

Commit acffe0f

Browse files
Merge pull request #1090 from qiboteam/fix_natives_0.2
Propagate #1077 to 0.2
2 parents 6d7f8a6 + 8328a81 commit acffe0f

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/qibocal/auto/transpile.py

+21-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from qibo.backends import Backend
55
from qibo.transpiler.pipeline import Passes
66
from qibo.transpiler.unroller import NativeGates, Unroller
7+
from qibolab import PulseSequence
78
from qibolab._core.compilers import Compiler
9+
from qibolab._core.native import NativeContainer
810

911
from qibocal.auto.operation import QubitId
1012

@@ -109,29 +111,35 @@ def execute_transpiled_circuit(
109111
)
110112

111113

112-
def natives(platform):
114+
def natives(platform) -> dict[str, NativeContainer]:
113115
"""
114-
Return the list of native gates defined in the `platform`.
115-
This function assumes the native gates to be the same for each
116+
Return the dict of native gates name with the associated native container
117+
defined in the `platform`. This function assumes the native gates to be the same for each
116118
qubit and pair.
117119
"""
118120
pair = next(iter(platform.pairs))
119121
qubit = next(iter(platform.qubits))
120-
two_qubit_natives = list(platform.natives.two_qubit[pair].model_fields)
121-
single_qubit_natives = list(platform.natives.single_qubit[qubit].model_fields)
122+
two_qubit_natives_container = platform.natives.two_qubit[pair]
123+
single_qubit_natives_container = platform.natives.single_qubit[qubit]
124+
single_qubit_natives = list(single_qubit_natives_container.model_fields)
125+
two_qubit_natives = list(two_qubit_natives_container.model_fields)
122126
# Solve Qibo-Qibolab mismatch
123127
single_qubit_natives.append("RZ")
124128
single_qubit_natives.append("Z")
125129
single_qubit_natives.remove("RX12")
126130
single_qubit_natives.remove("RX90")
127131
single_qubit_natives.remove("CP")
128-
new_single_natives = [REPLACEMENTS.get(i, i) for i in single_qubit_natives]
129-
return new_single_natives + two_qubit_natives
132+
single_qubit_natives = [REPLACEMENTS.get(x, x) for x in single_qubit_natives]
133+
return {i: platform.natives.single_qubit[qubit] for i in single_qubit_natives} | {
134+
i: platform.natives.two_qubit[pair] for i in two_qubit_natives
135+
}
130136

131137

132-
def create_rule(native):
133-
def rule(gate, native):
134-
return native.ensure(gate.__class__.__name__).create_sequence()
138+
def create_rule(name, natives):
139+
"""Create rule for gate name given container natives."""
140+
141+
def rule(gate: gates.Gate, natives: NativeContainer) -> PulseSequence:
142+
return natives.ensure(name).create_sequence()
135143

136144
return rule
137145

@@ -142,10 +150,10 @@ def set_compiler(backend, natives_):
142150
"""
143151
compiler = backend.compiler
144152
rules = {}
145-
for native in natives_:
146-
gate = getattr(gates, native)
153+
for name, natives_container in natives_.items():
154+
gate = getattr(gates, name)
147155
if gate not in compiler.rules:
148-
rules[gate] = create_rule(native)
156+
rules[gate] = create_rule(name, natives_container)
149157
else:
150158
rules[gate] = compiler.rules[gate]
151159
rules[gates.I] = compiler.rules[gates.I]

tests/test_transpile.py

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@
1010
)
1111

1212

13+
def test_natives():
14+
backend = construct_backend("qibolab", platform="dummy")
15+
transpiler = dummy_transpiler(backend)
16+
assert gates.iSWAP in backend.compiler.rules
17+
18+
circuit = Circuit(2)
19+
circuit.add(gates.iSWAP(0, 1))
20+
qubit_map = [1, 2]
21+
transpiled_circuit, _ = execute_transpiled_circuit(
22+
circuit, qubit_map, backend, transpiler=transpiler
23+
)
24+
sequence, _ = backend.compiler.compile(transpiled_circuit, backend.platform)
25+
assert len(sequence) == 4 # dummy compiles iSWAP in 4 pulses
26+
27+
1328
def test_padd_circuit():
1429
small_circuit = Circuit(2)
1530
small_circuit.add(gates.X(0))

0 commit comments

Comments
 (0)