qio 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
qio-0.1.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
qio-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,20 @@
1
+ Metadata-Version: 2.4
2
+ Name: qio
3
+ Version: 0.1.0
4
+ Summary: Base common quantum computing objects description library
5
+ License: Apache-2.0
6
+ Project-URL: Repository, https://github.com/QuantumCommons/qami
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Operating System :: OS Independent
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Topic :: Scientific/Engineering
12
+ Requires-Python: >=3.12
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: dataclasses
16
+ Requires-Dist: dataclasses-json
17
+ Dynamic: license-file
18
+
19
+ # Qami 'QuAntum Modeling Interfaces'
20
+ Base common quantum computing objects description
qio-0.1.0/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # Qami 'QuAntum Modeling Interfaces'
2
+ Base common quantum computing objects description
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "qio"
7
+ version = "0.1.0"
8
+ description = "Base common quantum computing objects description library"
9
+ readme = "README.md"
10
+ requires-python = ">=3.12"
11
+ license = { text = "Apache-2.0" }
12
+ classifiers = [
13
+ "Programming Language :: Python :: 3",
14
+ "Operating System :: OS Independent",
15
+ "Intended Audience :: Developers",
16
+ "Intended Audience :: Science/Research",
17
+ "Topic :: Scientific/Engineering",
18
+ ]
19
+
20
+ dependencies = [
21
+ "dataclasses",
22
+ "dataclasses-json",
23
+ ]
24
+
25
+ [project.urls]
26
+ Repository = "https://github.com/QuantumCommons/qami"
@@ -0,0 +1,23 @@
1
+ # Copyright 2025 Scaleway, Aqora
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from .quantum_program import QuantumProgram, QuantumProgramSerializationFormat
15
+ from .quantum_program_result import (
16
+ QuantumProgramResult,
17
+ QuantumProgramResultSerializationFormat,
18
+ )
19
+ from .quantum_computation_models import (
20
+ QuantumComputationModel,
21
+ QuantumComputationParameters,
22
+ )
23
+ from .quantum_noise_model import QuantumNoiseModelSerializationFormat, QuamtumNoiseModel
@@ -0,0 +1,80 @@
1
+ # Copyright 2025 Scaleway, Aqora, Quantum Commons
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import json
15
+
16
+ from typing import List, Optional, Dict, Union
17
+
18
+ from dataclasses import dataclass
19
+ from dataclasses_json import dataclass_json
20
+
21
+ from .quantum_program import QuantumProgram
22
+ from .quantum_noise_model import QuamtumNoiseModel
23
+
24
+
25
+ @dataclass_json
26
+ @dataclass
27
+ class ClientData:
28
+ user_agent: str
29
+
30
+
31
+ @dataclass_json
32
+ @dataclass
33
+ class BackendData:
34
+ name: str
35
+ version: str
36
+
37
+
38
+ @dataclass_json
39
+ @dataclass
40
+ class QuantumComputationModel:
41
+ programs: List[QuantumProgram]
42
+ noise_model: Optional[QuamtumNoiseModel] = None
43
+ client: Optional[ClientData] = None
44
+ backend: Optional[BackendData] = None
45
+
46
+ @classmethod
47
+ def from_dict(cls, data: Dict) -> "QuantumComputationModel":
48
+ return QuantumComputationModel.schema().load(data)
49
+
50
+ def to_dict(self) -> Dict:
51
+ return QuantumComputationModel.schema().dump(self)
52
+
53
+ @classmethod
54
+ def from_json(cls, str) -> "QuantumComputationModel":
55
+ data = json.loads(data) if isinstance(data, str) else data
56
+ return cls.from_dict(data)
57
+
58
+ def to_json(self) -> str:
59
+ return json.dumps(self.to_dict())
60
+
61
+
62
+ @dataclass_json
63
+ @dataclass
64
+ class QuantumComputationParameters:
65
+ shots: int
66
+
67
+ @classmethod
68
+ def from_dict(cls, data: Union[Dict, str]) -> "QuantumComputationParameters":
69
+ return QuantumComputationParameters.schema().load(data)
70
+
71
+ def to_dict(self) -> Dict:
72
+ return QuantumComputationParameters.schema().dump(self)
73
+
74
+ @classmethod
75
+ def from_json(cls, str) -> "QuantumComputationParameters":
76
+ data = json.loads(data) if isinstance(data, str) else data
77
+ return cls.from_dict(data)
78
+
79
+ def to_json(self) -> str:
80
+ return json.dumps(self.to_dict())
@@ -0,0 +1,160 @@
1
+ # Copyright 2025 Scaleway, Aqora, Quantum Commons
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import json
15
+ import zlib
16
+
17
+ from typing import Dict, Union
18
+ from enum import Enum
19
+
20
+ from dataclasses import dataclass
21
+ from dataclasses_json import dataclass_json
22
+
23
+
24
+ class QuantumNoiseModelSerializationFormat(Enum):
25
+ UNKOWN_SERIALIZATION_FORMAT = 0
26
+ QISKIT_AER_JSON_V1 = 1
27
+ QISKIT_AER_ZLIB_JSON_V1 = 2
28
+
29
+
30
+ @dataclass_json
31
+ @dataclass
32
+ class QuamtumNoiseModel:
33
+ serialization_format: QuantumNoiseModelSerializationFormat
34
+ serialization: bytes
35
+
36
+ @classmethod
37
+ def from_dict(cls, data: Union[Dict, str]) -> "QuamtumNoiseModel":
38
+ return QuamtumNoiseModel.schema().load(data)
39
+
40
+ def to_dict(self) -> Dict:
41
+ return QuamtumNoiseModel.schema().dump(self)
42
+
43
+ @classmethod
44
+ def from_json(cls, str) -> "QuamtumNoiseModel":
45
+ data = json.loads(data) if isinstance(data, str) else data
46
+ return cls.from_dict(data)
47
+
48
+ def to_json(self) -> str:
49
+ return json.dumps(self.to_dict())
50
+
51
+ @classmethod
52
+ def from_qiskit_noise_model(
53
+ noise_model: "qiskit_aer.NoiseModel",
54
+ ) -> "QuamtumNoiseModel":
55
+ try:
56
+ import numpy as np
57
+ except ImportError:
58
+ raise Exception("Numpy is not installed")
59
+
60
+ try:
61
+ from qiskit_aer.noise import NoiseModel
62
+ except ImportError:
63
+ raise Exception("Qiskit Aer is not installed")
64
+
65
+ def _encode_numpy_complex(obj):
66
+ """
67
+ Recursively traverses a structure and converts numpy arrays and
68
+ complex numbers into a JSON-serializable format.
69
+ """
70
+ if isinstance(obj, np.ndarray):
71
+ return {
72
+ "__ndarray__": True,
73
+ "data": _encode_numpy_complex(
74
+ obj.tolist()
75
+ ), # Recursively encode data
76
+ "dtype": obj.dtype.name,
77
+ "shape": obj.shape,
78
+ }
79
+ elif isinstance(obj, (complex, np.complex128)):
80
+ return {"__complex__": True, "real": obj.real, "imag": obj.imag}
81
+ elif isinstance(obj, dict):
82
+ return {key: _encode_numpy_complex(value) for key, value in obj.items()}
83
+ elif isinstance(obj, (list, tuple)):
84
+ return [_encode_numpy_complex(item) for item in obj]
85
+ else:
86
+ return obj
87
+
88
+ noise_model_dict = _encode_numpy_complex(noise_model.to_dict(False))
89
+ return QuamtumNoiseModel(
90
+ serialization_format=QuantumNoiseModelSerializationFormat.QISKIT_AER_ZLIB_JSON_V1,
91
+ serialization=zlib.compress(json.dumps(noise_model_dict).encode()),
92
+ frozenset=True,
93
+ )
94
+
95
+ def to_qiskit_noise_model(self) -> "qiskit_aer.NoiseModel":
96
+ try:
97
+ import numpy as np
98
+ except ImportError:
99
+ raise Exception("Numpy is not installed")
100
+
101
+ try:
102
+ from qiskit_aer.noise import NoiseModel
103
+ except ImportError:
104
+ raise Exception("Qiskit Aer is not installed")
105
+
106
+ def _custom_decode_numpy_and_complex(obj):
107
+ """
108
+ Recursively traverses a structure and converts dictionary representations
109
+ back into numpy arrays and complex numbers.
110
+ """
111
+ if isinstance(obj, dict):
112
+ if obj.get("__ndarray__", False):
113
+ # The data has been recursively decoded, so we can build the array
114
+ return np.array(
115
+ _custom_decode_numpy_and_complex(obj["data"]),
116
+ dtype=obj["dtype"],
117
+ ).reshape(obj["shape"])
118
+ elif obj.get("__complex__", False):
119
+ return complex(obj["real"], obj["imag"])
120
+ else:
121
+ return {
122
+ key: _custom_decode_numpy_and_complex(value)
123
+ for key, value in obj.items()
124
+ }
125
+ elif isinstance(obj, list):
126
+ return [_custom_decode_numpy_and_complex(item) for item in obj]
127
+ else:
128
+ return obj
129
+
130
+ def _json_deserialization_noise_model(noise_model_str: bytes):
131
+ noise_model_dict = json.loads(noise_model_str)
132
+ noise_model = NoiseModel.from_dict(noise_model_dict)
133
+ return noise_model
134
+
135
+ def _zlib_json_deserialization_noise_model(noise_model_raw: bytes):
136
+ """
137
+ Attempt to decode the noise model dictionary using the custom decoder, and if it fails, try to use the default decoder.
138
+ This is a workaround for a bug in the Qiskit NoiseModel.from_dict method.
139
+ """
140
+ noise_model_str = zlib.decompress(noise_model_raw)
141
+ noise_model_dict = json.loads(noise_model_str)
142
+ try:
143
+ noise_model_dict_custom_decoded = _custom_decode_numpy_and_complex(
144
+ noise_model_dict
145
+ )
146
+ except:
147
+ return NoiseModel.from_dict(noise_model_dict)
148
+ return NoiseModel.from_dict(noise_model_dict_custom_decoded)
149
+
150
+ match = {
151
+ QuantumNoiseModelSerializationFormat.QISKIT_AER_JSON_V1: _json_deserialization_noise_model,
152
+ QuantumNoiseModelSerializationFormat.QISKIT_AER_ZLIB_JSON_V1: _zlib_json_deserialization_noise_model,
153
+ }
154
+
155
+ try:
156
+ return match[self.serialization_format](self.serialization)
157
+ except:
158
+ raise Exception(
159
+ "unsupported serialization format:", self.serialization_format
160
+ )
@@ -0,0 +1,129 @@
1
+ # Copyright 2025 Scaleway, Aqora, Quantum Commons
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.from enum import Enum
14
+ import json
15
+
16
+ from enum import Enum
17
+ from typing import Dict, Union
18
+
19
+ from dataclasses import dataclass
20
+ from dataclasses_json import dataclass_json
21
+
22
+
23
+ class QuantumProgramSerializationFormat(Enum):
24
+ UNKOWN_SERIALIZATION_FORMAT = 0
25
+ QASM_V1 = 1
26
+ QASM_V2 = 2
27
+ QASM_V3 = 3
28
+ QIR_V1 = 4
29
+ CIRQ_CIRCUIT_JSON_V1 = 5
30
+ PERCEVAL_CIRCUIT_JSON_V1 = 6
31
+ PULSER_SEQUENCE_JSON_V1 = 7
32
+
33
+
34
+ @dataclass_json
35
+ @dataclass
36
+ class QuantumProgram:
37
+ serialization_format: QuantumProgramSerializationFormat
38
+ serialization: str
39
+
40
+ @classmethod
41
+ def from_dict(cls, data: Union[Dict, str]) -> "QuantumProgram":
42
+ return QuantumProgram.schema().load(data)
43
+
44
+ def to_dict(self) -> Dict:
45
+ return QuantumProgram.schema().dump(self)
46
+
47
+ @classmethod
48
+ def from_json(cls, str) -> "QuantumProgram":
49
+ data = json.loads(data) if isinstance(data, str) else data
50
+ return cls.from_dict(data)
51
+
52
+ def to_json(self) -> str:
53
+ return json.dumps(self.to_dict())
54
+
55
+ @classmethod
56
+ def from_qiskit_circuit(
57
+ cls, qiskit_circuit: "qiskit.QuantumCircuit"
58
+ ) -> "QuantumProgram":
59
+ try:
60
+ from qiskit import qasm3
61
+ except ImportError:
62
+ raise Exception("Qiskit is not installed")
63
+
64
+ serialization = qasm3.dumps(qiskit_circuit)
65
+
66
+ return cls(
67
+ serialization_format=QuantumProgramSerializationFormat.QASM_V3,
68
+ serialization=serialization,
69
+ frozenset=True,
70
+ )
71
+
72
+ def to_qiskit_circuit(self) -> "qiskit.QuantumCircuit":
73
+ try:
74
+ from qiskit import qasm3, qasm2, QuantumCircuit
75
+ except ImportError:
76
+ raise Exception("Qiskit is not installed")
77
+
78
+ match = {
79
+ QuantumProgramSerializationFormat.QASM_V1: QuantumCircuit.from_qasm_str,
80
+ QuantumProgramSerializationFormat.QASM_V2: qasm2.loads,
81
+ QuantumProgramSerializationFormat.QASM_V3: qasm3.loads,
82
+ }
83
+
84
+ try:
85
+ return match[self.serialization_format](self.serialization)
86
+ except:
87
+ raise Exception(
88
+ "unsupported serialization format:", self.serialization_format
89
+ )
90
+
91
+ @classmethod
92
+ def from_cirq_circuit(
93
+ cls, cirq_circuit: "cirq.AbstractCircuit"
94
+ ) -> "QuantumProgram":
95
+ try:
96
+ import cirq
97
+ except ImportError:
98
+ raise Exception("Cirq is not installed")
99
+
100
+ serialization = cirq.to_json(cirq_circuit)
101
+
102
+ return cls(
103
+ serialization_format=QuantumProgramSerializationFormat.CIRQ_CIRCUIT_JSON_V1,
104
+ serialization=serialization,
105
+ frozenset=True,
106
+ )
107
+
108
+ def to_cirq_circuit(self) -> "cirq.Circuit":
109
+ try:
110
+ import cirq
111
+ except ImportError:
112
+ raise Exception("Cirq is not installed")
113
+
114
+ if self.serialization_format in [
115
+ QuantumProgramSerializationFormat.QASM_V1,
116
+ QuantumProgramSerializationFormat.QASM_V2,
117
+ ]:
118
+ from cirq.contrib.qasm_import import circuit_from_qasm
119
+
120
+ return circuit_from_qasm(self.serialization)
121
+
122
+ if self.serialization_format in [
123
+ QuantumProgramSerializationFormat.CIRQ_CIRCUIT_JSON_V1,
124
+ ]:
125
+ from cirq import read_json
126
+
127
+ return read_json(json_text=self.serialization)
128
+
129
+ raise Exception("unsupported serialization format:", self.serialization_format)
@@ -0,0 +1,150 @@
1
+ # Copyright 2025 Scaleway, Aqora, Quantum Commons
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import json
15
+
16
+ from typing import Union, Dict
17
+ from enum import Enum
18
+
19
+ from dataclasses import dataclass
20
+ from dataclasses_json import dataclass_json
21
+
22
+
23
+ class QuantumProgramResultSerializationFormat(Enum):
24
+ UNKOWN_SERIALIZATION_FORMAT = 0
25
+ CIRQ_RESULT_JSON_V1 = 1
26
+ QISKIT_RESULT_JSON_V1 = 2
27
+
28
+
29
+ @dataclass_json
30
+ @dataclass
31
+ class QuantumProgramResult:
32
+ serialization_format: QuantumProgramResultSerializationFormat
33
+ serialization: str
34
+
35
+ @classmethod
36
+ def from_dict(cls, data: Union[Dict, str]) -> "QuantumProgramResult":
37
+ return QuantumProgramResult.schema().load(data)
38
+
39
+ def to_dict(self) -> Dict:
40
+ return QuantumProgramResult.schema().dump(self)
41
+
42
+ @classmethod
43
+ def from_json(cls, str) -> "QuantumProgramResult":
44
+ data = json.loads(data) if isinstance(data, str) else data
45
+ return cls.from_dict(data)
46
+
47
+ def to_json(self) -> str:
48
+ return json.dumps(self.to_dict())
49
+
50
+ @classmethod
51
+ def from_qiskit_result(
52
+ cls, qiskit_result: "qiskit.result.Result"
53
+ ) -> "QuantumProgramResult":
54
+ try:
55
+ from qiskit.result import Result
56
+ except ImportError:
57
+ raise Exception("Qiskit is not installed")
58
+
59
+ serialization = json.dumps(qiskit_result.to_dict())
60
+
61
+ return cls(
62
+ serialization_format=QuantumProgramResultSerializationFormat.QISKIT_RESULT_JSON_V1,
63
+ serialization=serialization,
64
+ frozenset=True,
65
+ )
66
+
67
+ @classmethod
68
+ def from_qiskit_result_dict(
69
+ cls, qiskit_result_dict: Union[str, Dict]
70
+ ) -> "QuantumProgramResult":
71
+ if isinstance(qiskit_result_dict, str):
72
+ qiskit_result_dict = json.loads(
73
+ qiskit_result_dict
74
+ ) # Ensure serialization is not ill-formatted
75
+
76
+ serialization = json.dumps(qiskit_result_dict)
77
+
78
+ return cls(
79
+ serialization_format=QuantumProgramResultSerializationFormat.QISKIT_RESULT_JSON_V1,
80
+ serialization=serialization,
81
+ frozenset=True,
82
+ )
83
+
84
+ def to_qiskit_result(self, **kwargs) -> "qiskit.result.Result":
85
+ try:
86
+ from qiskit.result import Result
87
+ except ImportError:
88
+ raise Exception("Qiskit is not installed")
89
+
90
+ result_dict = json.loads(self.serialization)
91
+
92
+ return Result.from_dict(
93
+ {
94
+ "results": result_dict["results"],
95
+ "success": result_dict["success"],
96
+ "header": result_dict.get("header"),
97
+ "metadata": result_dict.get("metadata"),
98
+ }
99
+ )
100
+
101
+ @classmethod
102
+ def from_cirq_result_dict(
103
+ cls, cirq_result_dict: Union[str, Dict]
104
+ ) -> "QuantumProgramResult":
105
+ if isinstance(cirq_result_dict, str):
106
+ cirq_result_dict = json.loads(
107
+ cirq_result_dict
108
+ ) # Ensure serialization is not ill-formatted
109
+
110
+ serialization = json.dumps(cirq_result_dict)
111
+
112
+ return cls(
113
+ serialization_format=QuantumProgramResultSerializationFormat.CIRQ_RESULT_JSON_V1,
114
+ serialization=serialization,
115
+ frozenset=True,
116
+ )
117
+
118
+ @classmethod
119
+ def from_cirq_result(cls, cirq_result: "cirq.Result") -> "QuantumProgramResult":
120
+ try:
121
+ import cirq
122
+ except ImportError:
123
+ raise Exception("Cirq is not installed")
124
+
125
+ serialization = json.dumps(cirq_result._json_dict_())
126
+
127
+ return cls(
128
+ serialization_format=QuantumProgramResultSerializationFormat.CIRQ_RESULT_JSON_V1,
129
+ serialization=serialization,
130
+ frozenset=True,
131
+ )
132
+
133
+ def to_cirq_result(self, **kwargs) -> "cirq.Result":
134
+ try:
135
+ from cirq import ResultDict
136
+ except ImportError:
137
+ raise Exception("Cirq is not installed")
138
+
139
+ if (
140
+ self.serialization_format
141
+ != QuantumProgramResultSerializationFormat.CIRQ_RESULT_JSON_V1
142
+ ):
143
+ raise Exception(
144
+ "unsupported serialization format:", self.serialization_format
145
+ )
146
+
147
+ result_dict = json.loads(self.serialization)
148
+ cirq_result = ResultDict._from_json_dict_(**result_dict)
149
+
150
+ return cirq_result
@@ -0,0 +1,20 @@
1
+ Metadata-Version: 2.4
2
+ Name: qio
3
+ Version: 0.1.0
4
+ Summary: Base common quantum computing objects description library
5
+ License: Apache-2.0
6
+ Project-URL: Repository, https://github.com/QuantumCommons/qami
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Operating System :: OS Independent
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Topic :: Scientific/Engineering
12
+ Requires-Python: >=3.12
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: dataclasses
16
+ Requires-Dist: dataclasses-json
17
+ Dynamic: license-file
18
+
19
+ # Qami 'QuAntum Modeling Interfaces'
20
+ Base common quantum computing objects description
@@ -0,0 +1,13 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ qio/__init__.py
5
+ qio/quantum_computation_models.py
6
+ qio/quantum_noise_model.py
7
+ qio/quantum_program.py
8
+ qio/quantum_program_result.py
9
+ qio.egg-info/PKG-INFO
10
+ qio.egg-info/SOURCES.txt
11
+ qio.egg-info/dependency_links.txt
12
+ qio.egg-info/requires.txt
13
+ qio.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ dataclasses
2
+ dataclasses-json
@@ -0,0 +1 @@
1
+ qio
qio-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+