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

Add properties in QibolabBackend #1076

Merged
merged 17 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/qibolab/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from qibolab.execution_parameters import ExecutionParameters
from qibolab.platform import Platform, create_platform
from qibolab.platform.load import available_platforms
from qibolab.qubits import QubitId, QubitPairId
from qibolab.version import __version__ as qibolab_version


Expand Down Expand Up @@ -50,6 +51,33 @@ def __init__(self, platform):
}
self.compiler = Compiler.default()

@property
def qubits(self) -> list[QubitId]:
"""Returns the qubits in the platform."""
return list(self.platform.qubits)

@property
def connectivity(self) -> list[QubitPairId]:
"""Returns the list of connected qubits."""
return list(self.platform.pairs)

@property
def natives(self) -> list[str]:
"""Returns the list of native gates supported by the platform."""
compiler = Compiler.default()
natives = [g.__name__ for g in list(compiler.rules)]
calibrated = self.platform.pairs

check_2q = ["CZ", "CNOT"]
for gate in check_2q:
if gate in natives and all(
getattr(calibrated[p].native_gates, gate) is None
for p in self.connectivity
):
natives.remove(gate)

return natives

def apply_gate(self, gate, state, nqubits): # pragma: no cover
raise_error(NotImplementedError, "Qibolab cannot apply gates directly.")

Expand Down
58 changes: 58 additions & 0 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,64 @@ def connected_backend(connected_platform):
yield QibolabBackend(connected_platform)


def test_qubits():
backend = QibolabBackend("dummy")
assert isinstance(backend.qubits, list)
assert set(backend.qubits) == {0, 1, 2, 3, 4}


def test_connectivity():
backend = QibolabBackend("dummy")
assert isinstance(backend.connectivity, list)
assert set(backend.connectivity) == {
(0, 2),
(2, 0),
(1, 2),
(2, 1),
(2, 3),
(3, 2),
(2, 4),
(4, 2),
}


def test_natives():
backend = QibolabBackend("dummy")
assert isinstance(backend.natives, list)
assert set(backend.natives) == {
"I",
"Z",
"RZ",
"U3",
"CZ",
"CNOT",
"GPI2",
"GPI",
"M",
}


def test_natives_no_cz_cnot():
platform = create_platform("dummy")
backend = QibolabBackend(platform)
assert set(backend.natives) == {
"I",
"Z",
"RZ",
"U3",
"GPI2",
"GPI",
"M",
"CZ",
"CNOT",
}

for gate in ["CZ", "CNOT"]:
for p in platform.pairs:
setattr(platform.pairs[p].native_gates, gate, None)
assert gate not in set(backend.natives)


def test_execute_circuit_initial_state():
backend = QibolabBackend("dummy")
circuit = Circuit(1)
Expand Down
Loading