-
Notifications
You must be signed in to change notification settings - Fork 7
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
Native gates directly defined by the platform #1077
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8423633
Merge branch 'fix_backend' into native_gates
Edoardo-Pedicillo 0886c0d
fix: use platform native gates in unrolling
Edoardo-Pedicillo 0674c61
refactor: Modify execute_circuits accordingly
andrea-pasquale ed0f495
fix: platform and backend mismatch
Edoardo-Pedicillo 5190fe0
Merge branch 'fix_backend' into native_gates
Edoardo-Pedicillo ee44f8d
fix: define new compiler according to pulses in the platform and
Edoardo-Pedicillo ec80474
feat: add possibility to add custom single qubit gates
Edoardo-Pedicillo efd70aa
doc: add documentation
Edoardo-Pedicillo b433c46
Merge branch 'fix_backend' into native_gates
Edoardo-Pedicillo 266e3a7
Update src/qibocal/auto/transpile.py
Edoardo-Pedicillo 3331404
restore empty line
Edoardo-Pedicillo aa92440
Merge branch 'native_gates' of github.com:qiboteam/qibocal into nativ…
Edoardo-Pedicillo f8f69de
Apply suggestions from code review
Edoardo-Pedicillo 68e2cd7
ref: minor fixes
Edoardo-Pedicillo 2d6d8ce
Merge branch 'native_gates' of github.com:qiboteam/qibocal into nativ…
Edoardo-Pedicillo 2ae7fde
ref: change pairs name
Edoardo-Pedicillo e1c0047
ref: change get_natives name
Edoardo-Pedicillo a04745a
remove print
Edoardo-Pedicillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
from typing import Optional | ||
|
||
from qibo import Circuit | ||
from qibo import Circuit, gates | ||
from qibo.backends import Backend | ||
from qibo.transpiler.pipeline import Passes | ||
from qibo.transpiler.unroller import NativeGates, Unroller | ||
from qibolab.compilers.compiler import Compiler | ||
from qibolab.pulses import PulseSequence | ||
from qibolab.qubits import QubitId | ||
|
||
REPLACEMENTS = { | ||
"RX": "GPI2", | ||
"MZ": "M", | ||
} | ||
|
||
|
||
def transpile_circuits( | ||
circuits: list[Circuit], | ||
|
@@ -97,19 +104,71 @@ def execute_transpiled_circuit( | |
backend, | ||
transpiler, | ||
)[0] | ||
|
||
return transpiled_circ, backend.execute_circuit( | ||
transpiled_circ, initial_state=initial_state, nshots=nshots | ||
) | ||
|
||
|
||
def natives(platform): | ||
""" | ||
Return the list of native gates defined in the `platform`. | ||
This function assumes the native gates to be the same for each | ||
qubit and pair. | ||
""" | ||
pair = next(iter(platform.pairs.values())) | ||
qubit = next(iter(platform.qubits.values())) | ||
two_qubit_natives = list(pair.native_gates.raw) | ||
single_qubit_natives = list(qubit.native_gates.raw) | ||
# Solve Qibo-Qibolab mismatch | ||
single_qubit_natives.append("RZ") | ||
single_qubit_natives.append("Z") | ||
single_qubit_natives.remove("RX12") | ||
new_single_natives = [REPLACEMENTS.get(i, i) for i in single_qubit_natives] | ||
return new_single_natives + two_qubit_natives | ||
|
||
|
||
def create_rule(native): | ||
def rule(qubits_ids, platform, parameters=None): | ||
if len(qubits_ids[1]) == 1: | ||
native_gate = platform.qubits[tuple(qubits_ids[1])].native_gates | ||
else: | ||
native_gate = platform.pairs[tuple(qubits_ids[1])].native_gates | ||
pulses = getattr(native_gate, native).pulses | ||
return PulseSequence(pulses), {} | ||
|
||
return rule | ||
|
||
|
||
def set_compiler(backend, natives_): | ||
""" | ||
Set the compiler to execute the native gates defined by the platform. | ||
""" | ||
compiler = backend.compiler | ||
print("EEEEEEE", backend.natives) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems I forgot this in the previous review: I guess this is just a leftover. |
||
rules = {} | ||
for native in natives_: | ||
gate = getattr(gates, native) | ||
if gate not in compiler.rules: | ||
rules[gate] = create_rule(native) | ||
else: | ||
rules[gate] = compiler.rules[gate] | ||
rules[gates.I] = compiler.rules[gates.I] | ||
backend.compiler = Compiler(rules=rules) | ||
|
||
|
||
def dummy_transpiler(backend: Backend) -> Passes: | ||
""" | ||
If the backend is `qibolab`, a transpiler with just an unroller is returned, | ||
otherwise None. | ||
otherwise `None`. This function overwrites the compiler defined in the | ||
backend, taking into account the native gates defined in the`platform` (see | ||
:func:`set_compiler`). | ||
""" | ||
unroller = Unroller(NativeGates.default()) | ||
return Passes(connectivity=backend.platform.topology, passes=[unroller]) | ||
platform = backend.platform | ||
native_gates = natives(platform) | ||
set_compiler(backend, native_gates) | ||
native_gates = [getattr(gates, x) for x in native_gates] | ||
unroller = Unroller(NativeGates.from_gatelist(native_gates)) | ||
return Passes(connectivity=platform.topology, passes=[unroller]) | ||
|
||
|
||
def pad_circuit(nqubits, circuit: Circuit, qubit_map: list[int]) -> Circuit: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if
Z
should be somehow directly inferred fromRZ
by the transpiler itself...(implying that is not platform's/Qibocal's business)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory yes, I am using Qibolab compiler and NativeGates convention to differentiate between the two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine, but is there a reason to differentiate?
I.e. with the current compiler, the
RZ
will always be implemented as virtual phase, and that will be the same for theZ
as well...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, maybe @andrea-pasquale could have an explanation.