Skip to content

Commit

Permalink
feat: create NativeGates from list[str]
Browse files Browse the repository at this point in the history
  • Loading branch information
changsookim committed Oct 14, 2024
1 parent b44c13a commit 11548da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/qibo/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def set_transpiler(cls, transpiler):
# TODO: check if transpiler is valid on the backend

@classmethod
def _default_transpiler(cls):
def _default_transpiler(cls): # pragma: no cover
import networkx as nx

from qibo.transpiler.optimizer import Preprocessing
Expand Down
31 changes: 29 additions & 2 deletions src/qibo/transpiler/unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,44 @@ def from_gatelist(cls, gatelist: list):
return natives

@classmethod
def from_gate(cls, gate: gates.Gate):
def from_gate(cls, gate):
"""Create a :class:`qibo.transpiler.unroller.NativeGates`
object from a :class:`qibo.gates.gates.Gate`."""
object from a :class:`qibo.gates.gates.Gate`, a gate class, or a gate name as a string.
"""
if isinstance(gate, gates.Gate):
return cls.from_gate(gate.__class__)
elif isinstance(gate, str):
return getattr(cls, cls.from_str(gate).__name__)

try:
return getattr(cls, gate.__name__)
except AttributeError:
raise_error(ValueError, f"Gate {gate} cannot be used as native.")

@classmethod
def from_str(cls, gate: str):
"""Return the gate class corresponding to the input gate name."""
gate_format = gate.lower()
if gate_format == "i":
return gates.I
elif gate_format == "z":
return gates.Z
elif gate_format == "rz":
return gates.RZ
elif gate_format == "m":
return gates.M
elif gate_format == "gpi2":
return gates.GPI2
elif gate_format == "u3":
return gates.U3
elif gate_format == "cz":
return gates.CZ
elif gate_format == "iswap":
return gates.iSWAP
elif gate_format == "cnot" or gate_format == "cx":
return gates.CNOT
raise_error(ValueError, f"Gate name {gate} not found.")


# TODO: Make setting single-qubit native gates more flexible
class Unroller:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_transpiler_unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ def test_native_gates_from_gatelist_fail():
NativeGates.from_gatelist([gates.RZ, gates.X(0)])


def test_native_gates_str_from_gatelist():
natives = NativeGates.from_gatelist(
["I", "Z", "RZ", "M", "GPI2", "U3", "CZ", "iSWAP", "CX"]
)
assert (
natives
== NativeGates.I
| NativeGates.Z
| NativeGates.RZ
| NativeGates.M
| NativeGates.GPI2
| NativeGates.U3
| NativeGates.CZ
| NativeGates.iSWAP
| NativeGates.CNOT
)


def test_native_gate_str_from_gatelist_fail():
with pytest.raises(ValueError):
NativeGates.from_gatelist(["qibo"])


def test_translate_gate_error_1q():
natives = NativeGates(0)
with pytest.raises(DecompositionError):
Expand Down

0 comments on commit 11548da

Please sign in to comment.