-
Notifications
You must be signed in to change notification settings - Fork 14
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
Kernel class #758
Kernel class #758
Changes from all commits
dabb583
c540535
ab5b64c
92e20eb
8f8f0f6
e3e0a8c
dac2474
cff8946
5980fe7
2cc1048
0c5a7ab
b383fa2
f7e73e1
5899914
f4ec7b4
a32876c
e216176
0f3ca6d
7f7bee9
e12a93e
b0b24f9
9f37fd8
1e6a7dc
3636038
4b8a505
55723ed
26e30d8
d33823c
fa13c5d
d869de0
c13a8a7
5e46e6f
a3fd68f
8f49f89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
ExecutionParameters, | ||
) | ||
from qibolab.platform import Platform | ||
from qibolab.serialize import PLATFORM | ||
|
||
__version__ = im.version(__package__) | ||
|
||
|
@@ -29,13 +30,14 @@ def get_platforms_path(): | |
return Path(profiles) | ||
|
||
|
||
def create_platform(name, runcard=None): | ||
def create_platform(name, path: Path = None) -> Platform: | ||
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. I have the impression that the However, this will become more clear once we try to define some actual platforms (eg in qibolab_platforms_qrc). |
||
"""A platform for executing quantum algorithms. | ||
|
||
It consists of a quantum processor QPU and a set of controlling instruments. | ||
|
||
Args: | ||
name (str): name of the platform. Options are 'tiiq', 'qili' and 'icarusq'. | ||
path (pathlib.Path): path with platform serialization | ||
Returns: | ||
The plaform class. | ||
""" | ||
|
@@ -44,17 +46,17 @@ def create_platform(name, runcard=None): | |
|
||
return create_dummy(with_couplers=name == "dummy_couplers") | ||
|
||
platform = get_platforms_path() / f"{name}.py" | ||
platform = get_platforms_path() / f"{name}" | ||
if not platform.exists(): | ||
raise_error(ValueError, f"Platform {name} does not exist.") | ||
|
||
spec = importlib.util.spec_from_file_location("platform", platform) | ||
spec = importlib.util.spec_from_file_location("platform", platform / PLATFORM) | ||
module = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(module) | ||
|
||
if runcard is None: | ||
if path is None: | ||
return module.create() | ||
return module.create(runcard) | ||
return module.create(path) | ||
|
||
|
||
def execute_qasm(circuit: str, platform, runcard=None, initial_state=None, nshots=1000): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .platform import create_dummy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
|
||
from qibolab.qubits import QubitId | ||
|
||
KERNELS = "kernels.npz" | ||
|
||
|
||
class Kernels(dict[QubitId, np.ndarray]): | ||
"""A dictionary subclass for handling Qubit Kernels. | ||
|
||
This class extends the built-in dict class and maps QubitId to numpy | ||
arrays. It provides methods to load and dump the kernels from and to | ||
a file. | ||
""" | ||
|
||
@classmethod | ||
def load(cls, path: Path): | ||
"""Class method to load kernels from a file. | ||
|
||
The file should contain a serialized dictionary where keys are | ||
serialized QubitId and values are numpy arrays. | ||
""" | ||
return cls( | ||
{json.loads(key): value for key, value in np.load(path / KERNELS).items()} | ||
) | ||
|
||
def dump(self, path: Path): | ||
"""Instance method to dump the kernels to a file. | ||
|
||
The keys (QubitId) are serialized to strings and the values | ||
(numpy arrays) are kept as is. | ||
""" | ||
np.savez( | ||
path / KERNELS, | ||
**{json.dumps(qubit_id): value for qubit_id, value in self.items()} | ||
) |
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 principle,
parameters.yml
is also optional, as someone can define the qubit/channel objects hardcoded in thecreate
. This is basically explained in thelab.rst
tutorial.However, dumping the platform after will automatically create parameters, so I am not sure whether we should advocate this kind of usage.