Skip to content
New issue

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

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

Already on GitHub? # to your account

[QILI-48] Implement platform, backend and settings classes #4

Merged
merged 94 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
7d1c723
chore: :memo: Update requirements
amitjansc Mar 23, 2022
61c8385
chore: :loud_sound: Add raise_error function in config.py file
amitjansc Mar 23, 2022
61b427f
feat: :sparkles: Add AbstractPlatform class.
amitjansc Mar 23, 2022
f223c48
style: :art: Remove long line
amitjansc Mar 23, 2022
183c9b5
feat: :sparkles: Add HardwareCircuit class
amitjansc Mar 23, 2022
ecfe7a9
feat: :sparkles: Add QililabBackend class
amitjansc Mar 23, 2022
affdb1d
feat: :sparkles: Add gates.py file
amitjansc Mar 23, 2022
ba94393
Merge branch 'main' into platform
amitjansc Mar 24, 2022
151fbf4
refactor: :recycle: Remove useless raise_error
amitjansc Mar 24, 2022
5df6be4
docs: :memo: Update docs of backend and platform classes
amitjansc Mar 24, 2022
d3d9b74
feat: :sparkles: Add I, X, Y, Z gates.
amitjansc Mar 24, 2022
62f8d55
test: :test_tube: Add backend tests
amitjansc Mar 24, 2022
61e6180
refactor: :recycle: Remove dummy files and examples
amitjansc Mar 24, 2022
3766fde
feat: :sparkles: Add QiliPlatform class.
amitjansc Mar 24, 2022
1e462c3
feat: :sparkles: Add Settings class.
amitjansc Mar 24, 2022
911cbe0
feat: :sparkles: Add SettingsLoader class.
amitjansc Mar 25, 2022
530fd2f
chore: :wrench: Add types-PyYAML to dev-requirements.txt
amitjansc Mar 25, 2022
788d1f1
ci: configure codecov
iamtxena Mar 25, 2022
cca0d13
refactor: :recycle: Create one python file for each gate
amitjansc Mar 25, 2022
fc70bf3
fix: :bug: Remove dummy import
amitjansc Mar 25, 2022
632d043
refactor: :recycle: Change file name
amitjansc Mar 25, 2022
19c9482
test: :test_tube: Add settings_manager tests. Fix backend tests.
amitjansc Mar 25, 2022
614c76c
refactor: :recycle: Change class name
amitjansc Mar 25, 2022
05b4a3e
Merge remote-tracking branch 'origin/PR-QIBO-114-fix-codecov' into pl…
amitjansc Mar 25, 2022
af5b0db
feat: :sparkles: Add platform and instrument setting files
amitjansc Mar 25, 2022
a1ea361
refactor: :recycle: Change gates file names
amitjansc Mar 28, 2022
b78b06c
build: :wrench: Add typeguard to requirements
amitjansc Mar 28, 2022
1b8574c
refactor: :recycle: Delete useless methods.
amitjansc Mar 28, 2022
707effe
refactor: :recycle: Change gate name in __init__.
amitjansc Mar 28, 2022
47c3edc
refactor: :recycle: Remove useless methods
amitjansc Mar 28, 2022
c118303
refactor: :recycle: Remove raise_error
amitjansc Mar 28, 2022
288bef6
feat: :sparkles: Change SettingsManager to singleton
amitjansc Mar 28, 2022
3f0df38
feat: :sparkles: Add PlatformSettings class
amitjansc Mar 28, 2022
4194c73
feat: :sparkles: Add AbstractSettings and SettingsLoader classes
amitjansc Mar 28, 2022
a662714
feat: :sparkles: Add QubitCalibrationSettings class
amitjansc Mar 28, 2022
b037d92
refactor: :recycle: Remove Settings class
amitjansc Mar 28, 2022
8f60cf2
refactor: :recycle: Move instrument settings file to single yaml file
amitjansc Mar 28, 2022
73119d1
test: :test_tube: Update backend and settings_manager tests.
amitjansc Mar 28, 2022
f2828e4
Merge branch 'main' into platform
amitjansc Mar 28, 2022
32b73b7
fix: :bug: Add init file
amitjansc Mar 28, 2022
a4c6f69
style: :art: Remove unused import
amitjansc Mar 28, 2022
29eddac
refactor: :recycle: Change classes to dataclass
amitjansc Mar 29, 2022
c825b2f
style: :art: Remove unused import
amitjansc Mar 29, 2022
ff929fc
Merge branch 'main' into platform
amitjansc Mar 29, 2022
d005665
ci: :construction_worker: Install requirements.txt before code qualit…
amitjansc Mar 29, 2022
7c924b1
ci: :construction_worker: Add MANIFEST.in file to install yaml files
amitjansc Mar 29, 2022
8c4e048
ci: :construction_worker: Modify MANIFEST.in file
amitjansc Mar 29, 2022
f67c013
fix: :bug: Call __post_init__() of AbstractPlatform
amitjansc Mar 29, 2022
43db8a6
refactor: :recycle: Remove SettingsLoader class
amitjansc Mar 29, 2022
1453a11
docs: :memo: Remove unused argument docs
amitjansc Mar 29, 2022
83929c7
fix: :bug: Add ClassVar to class variables inside a dataclass
amitjansc Mar 29, 2022
38e7c03
style: :art: Disable sort when dumping yaml file
amitjansc Mar 29, 2022
46758f1
feat: :sparkles: Add buses in PlatformSettings
amitjansc Mar 29, 2022
1f83f60
style: :art: Declare platform attribute instead of setting it to None
amitjansc Mar 29, 2022
f699cf6
refactor: :recycle: Change settings files structure
amitjansc Mar 29, 2022
b193b1c
refactor: :recycle: Change platform class name
amitjansc Mar 29, 2022
096375b
refactor: :recycle: Remove raise error
amitjansc Mar 29, 2022
50f0bf2
feat: :sparkles: Add PlatformBuilder. Add relative imports to __init_…
amitjansc Mar 29, 2022
5fa48d7
refactor: :recycle: Move settings loading to PlatformBuilder
amitjansc Mar 29, 2022
c48fb1a
style: :art: Log message when instantiating SettingsManager class
amitjansc Mar 29, 2022
93b5b01
feat: :sparkles: Make PlatformBuilder a singleton
amitjansc Mar 29, 2022
53adfee
docs: :memo: Add docs
amitjansc Mar 29, 2022
6f73d79
style: :art: Delete unused imports.
amitjansc Mar 29, 2022
a1869ee
style: :art: Delete unused imports.
amitjansc Mar 29, 2022
0188730
style: :art: Use match case statement.
amitjansc Mar 29, 2022
8170d7f
refactor: :recycle: Add SettingsHashTable
amitjansc Mar 30, 2022
0512397
Remove imports from __init__ to avoid circular imports
amitjansc Mar 30, 2022
600f256
refactor: :recycle: Remove typing of current class
amitjansc Mar 30, 2022
1371020
refactor: :recycle: Move all instrument files inside instrument/ folder
amitjansc Mar 30, 2022
ab4855a
feat: :sparkles: Add Singleton metaclass
amitjansc Mar 30, 2022
4b994c7
ci: :construction_worker: Check with pylint only staged files
amitjansc Mar 30, 2022
b5b3c4d
refactor: :recycle: Use platform-specific settings
amitjansc Apr 4, 2022
8d93673
style: :art: Solve pylint issues.
amitjansc Apr 4, 2022
5a3777d
refactor: :recycle: Move settings category inside yaml file
amitjansc Apr 4, 2022
aae549e
refactor: :recycle: Add category attribute to AbstractSettings
amitjansc Apr 4, 2022
9ad2153
refactor: :recycle: Remove 'abstract' from any file or class.
amitjansc Apr 4, 2022
5732303
refactor: :recycle: Change name PB to PLATFORM_BUILDER
amitjansc Apr 4, 2022
50deadf
refactor: :recycle: Remove dataclass from HardwareCircuit
amitjansc Apr 4, 2022
b9c3938
refactor: :recycle: Change name SM to SETTINGS_MANAGER
amitjansc Apr 4, 2022
93c7c3c
refactor: :recycle: Rename 'platform' to 'platform_name'
amitjansc Apr 4, 2022
5eda895
refactor: :recycle: Add constants.py file
amitjansc Apr 4, 2022
995ee0f
refactor: :recycle: Add DEFAULT_SETTINGS_FOLDERNAME to constants.py file
amitjansc Apr 4, 2022
19352db
refactor: :recycle: Use full name for variables
amitjansc Apr 4, 2022
7aca372
docs: :memo: Delete TODO
amitjansc Apr 4, 2022
c1b0a99
style: :art: Change settings type
amitjansc Apr 4, 2022
dd81b3d
style: :art: Remove typing lsit
amitjansc Apr 4, 2022
c84ca17
refactor: :recycle: Check that dict has key before deleting it
amitjansc Apr 4, 2022
9040226
test: :test_tube: Split tests
amitjansc Apr 4, 2022
9d9e5d8
fix: :bug: Change backend attributes after parent init.
amitjansc Apr 4, 2022
60c62f7
chore: :memo: Add load_platform example
amitjansc Apr 4, 2022
f85db18
refactor: :recycle: Remove dependency from qibo
amitjansc Apr 5, 2022
3193a7c
style: :art: Add entry points
amitjansc Apr 5, 2022
ceab8e2
chore: :memo: Add load_platform example using PLATFORM_BUILDER
amitjansc Apr 5, 2022
1d2fe82
docs: :memo: Fix typo in docs
amitjansc Apr 5, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/pull_request_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
run: |
python -m pip install -U pip
python -m pip install -r dev-requirements.txt
python -m pip install -r requirements.txt

- name: Code Quality
run: |
Expand Down
6 changes: 2 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ repos:
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
additional_dependencies: [types-PyYAML==6.0.5]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
Expand Down Expand Up @@ -59,10 +60,7 @@ repos:
entry: pylint
language: system
types: [python]
args: [
"src",
"-E", # Only check for errors
]
args: ["-E"] # Only check for errors

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.14 # Use the ref you want to point at
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include src *.yml
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ tomli==2.0.1
tomlkit==0.10.0
tornado==6.1
traitlets==5.1.1
types-PyYAML==6.0.5
typing-extensions==4.1.1
uc-micro-py==1.0.1
urllib3==1.26.9
Expand Down
61 changes: 0 additions & 61 deletions examples/example.ipynb

This file was deleted.

10 changes: 0 additions & 10 deletions examples/example.py

This file was deleted.

27 changes: 27 additions & 0 deletions examples/load_platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import qibo

from qililab import PLATFORM_BUILDER

# FIXME: Need to add backend in qibo's profiles.yml file
backend = {
"name": "qililab",
"driver": "qililab.backend.QililabBackend",
"minimum_version": "0.0.1.dev0",
"is_hardware": True,
}
qibo.K.profile["backends"].append(backend)
# ------------------------------------------------------


def load_platform() -> None:
"""Load the platform 'platform_0' from the settings folder."""
# Using qibo (needed when using qibo circuits)
qibo.set_backend(backend="qililab", platform="platform_0")
print(f"Platform name: {qibo.K.platform}")
# Using PLATFORM_BUILDER
platform = PLATFORM_BUILDER.build(name="platform_0")
print(f"Platform name: {platform}")


if __name__ == "__main__":
load_platform()
7 changes: 5 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# here goes your library dependencies to be installed
# with the exact version
# example:
# scipy==1.8.0
# example:
# scipy==1.8.0
qibo==0.1.7
pyyaml==6.0
typeguard==2.13.3
4 changes: 3 additions & 1 deletion src/qililab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__version__ = "0.0.0"

from .qililab import Qililab
from .circuit import HardwareCircuit
from .gates import I, X, Y, Z
from .platforms import PLATFORM_BUILDER
62 changes: 62 additions & 0 deletions src/qililab/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from dataclasses import dataclass
from typing import ClassVar, Type

from qibo.backends.numpy import NumpyBackend

from qililab import gates
from qililab.circuit import HardwareCircuit
from qililab.platforms import PLATFORM_BUILDER
from qililab.platforms.platform import Platform


@dataclass
class QililabBackend(NumpyBackend):
"""Hardware backend used to execute circuits on specified lab platforms

Attributes:
name (str): Name of the backend.
is_hardware (bool): Flag used by Qibo to identify a hardware backend.
platform (object): Platform object describing the lab setup.

"""

name: str
is_hardware: bool

def __init__(self) -> None:
super().__init__()
self.platform: Platform
self.name = "qililab"
self.is_hardware = True

def set_platform(self, platform: str) -> None:
"""Set platform for controlling quantum devices.

Args:
name (str): Name of the platform.
"""
self.platform = PLATFORM_BUILDER.build(name=platform)

def get_platform(self) -> str:
"""
Returns:
str: Platform name.
"""
return str(self.platform)

def circuit_class(self, accelerators: dict = None, density_matrix: bool = False) -> Type[HardwareCircuit]:
"""
Returns:
Type[HardwareCircuit]: Circuit class used to create circuit model.
"""
if accelerators is not None:
raise NotImplementedError("Hardware backend does not support multi-GPU configuration.")
if density_matrix:
raise NotImplementedError("Hardware backend does not support density matrix simulation.")

return HardwareCircuit

def create_gate(self, cls, *args, **kwargs) -> object:
"""Create gate object"""

return getattr(gates, cls.__name__)(*args, **kwargs)
18 changes: 18 additions & 0 deletions src/qililab/circuit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from dataclasses import dataclass

from numpy import ndarray


@dataclass
class HardwareCircuit:
"""Class used to create circuit model.

Args:
nqubits (int): Number of qubits.
"""

nqubits: int

def execute(self, initial_state: ndarray = None, nshots: int = None):
"""Executes a pulse sequence"""
raise NotImplementedError
2 changes: 2 additions & 0 deletions src/qililab/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DEFAULT_PLATFORM_FILENAME = "platform"
DEFAULT_SETTINGS_FOLDERNAME = "qili"
4 changes: 4 additions & 0 deletions src/qililab/gates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .i import I
from .x import X
from .y import Y
from .z import Z
17 changes: 17 additions & 0 deletions src/qililab/gates/hardware_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
from abc import ABC, abstractmethod


class HardwareGate(ABC):
"""Abstract Base Class of a hardware gate."""

module = sys.modules[__name__] # used to avoid maximum recursion depth with qibo gates

# TODO: Replace 'object' with PulseSequence class
@abstractmethod
def to_sequence(self, sequence: object) -> None:
"""Translates the gate to pulses and adds them to the given PulseSequence.

Args:
sequence (PulseSequence): Class containing the sequence of pulses to be applied.
"""
22 changes: 22 additions & 0 deletions src/qililab/gates/i.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass

from qililab.gates.hardware_gate import HardwareGate


@dataclass
class I(HardwareGate): # noqa: E742
"""Identity gate

Args:
q (int): Index of the qubit to which the gate is applied.
"""

q: int

def to_sequence(self, sequence: object) -> None:
"""Translates the gate to pulses and adds them to the given PulseSequence.

Args:
sequence (PulseSequence): Class containing the sequence of pulses to be applied.
"""
raise NotImplementedError
22 changes: 22 additions & 0 deletions src/qililab/gates/x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass

from qililab.gates.hardware_gate import HardwareGate


@dataclass
class X(HardwareGate):
"""X gate

Args:
q (int): Index of the qubit to which the gate is applied.
"""

q: int

def to_sequence(self, sequence: object) -> None:
"""Translates the gate to pulses and adds them to the given PulseSequence.

Args:
sequence (PulseSequence): Class containing the sequence of pulses to be applied.
"""
raise NotImplementedError
22 changes: 22 additions & 0 deletions src/qililab/gates/y.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass

from qililab.gates.hardware_gate import HardwareGate


@dataclass
class Y(HardwareGate):
"""Y gate

Args:
q (int): Index of the qubit to which the gate is applied.
"""

q: int

def to_sequence(self, sequence: object) -> None:
"""Translates the gate to pulses and adds them to the given PulseSequence.

Args:
sequence (PulseSequence): Class containing the sequence of pulses to be applied.
"""
raise NotImplementedError
22 changes: 22 additions & 0 deletions src/qililab/gates/z.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass

from qililab.gates.hardware_gate import HardwareGate


@dataclass
class Z(HardwareGate):
"""Z gate

Args:
q (int): Index of the qubit to which the gate is applied.
"""

q: int

def to_sequence(self, sequence: object) -> None:
"""Translates the gate to pulses and adds them to the given PulseSequence.

Args:
sequence (PulseSequence): Class containing the sequence of pulses to be applied.
"""
raise NotImplementedError
4 changes: 4 additions & 0 deletions src/qililab/platforms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .platform import Platform
from .platform_builder import PlatformBuilder

PLATFORM_BUILDER = PlatformBuilder()
24 changes: 24 additions & 0 deletions src/qililab/platforms/platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from dataclasses import dataclass

from qililab.settings.settings import Settings


@dataclass
class Platform:
"""Platform object that describes setup used to control quantum devices.

Args:
name (str): Name of the platform.
settings (Settings): Dataclass containing the settings of the platform.
"""

name: str
settings: Settings

def __str__(self) -> str:
"""String representation of the platform

Returns:
str: Name of the platform
"""
return self.name
Loading