Skip to content

Commit ca174c1

Browse files
authored
Merge pull request #81 from OpenQuantumDesign/refactor_type_reflection
Refactor type reflection
2 parents aa0b455 + dc86ccb commit ca174c1

8 files changed

Lines changed: 121 additions & 72 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fixable = ["ALL"]
6464
oqd-compiler-infrastructure = { git = "https://github.com/openquantumdesign/oqd-compiler-infrastructure" }
6565

6666
[dependency-groups]
67-
dev = ["jupyter>=1.1.1", "pre-commit>=4.1.0"]
67+
dev = ["jupyter>=1.1.1", "pre-commit>=4.1.0", "ruff>=0.13.1"]
6868

6969

7070
[project.urls]

src/oqd_core/backend/metric.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pydantic.types import NonNegativeInt
1919

2020
########################################################################################
21-
from oqd_core.interface.analog.operator import OperatorSubtypes
21+
from oqd_core.interface.analog.operator import OperatorSubTypes
2222

2323
########################################################################################
2424

@@ -32,7 +32,7 @@
3232

3333

3434
class Expectation(VisitableBaseModel):
35-
operator: OperatorSubtypes
35+
operator: OperatorSubTypes
3636

3737

3838
class EntanglementEntropyVN(VisitableBaseModel):

src/oqd_core/interface/analog/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
OperatorMul,
3232
OperatorScalarMul,
3333
OperatorSub,
34-
OperatorSubtypes,
34+
OperatorSubTypes,
3535
OperatorTerminal,
3636
Pauli,
3737
PauliI,
@@ -43,7 +43,7 @@
4343
)
4444

4545
__all__ = [
46-
"OperatorSubtypes",
46+
"OperatorSubTypes",
4747
"Operator",
4848
"OperatorTerminal",
4949
"Pauli",

src/oqd_core/interface/analog/operation.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import List, Literal, Optional, Union
15+
from __future__ import annotations
1616

17-
# %%
18-
from oqd_compiler_infrastructure import TypeReflectBaseModel, VisitableBaseModel
17+
from typing import Annotated, List, Optional, Union
18+
19+
from oqd_compiler_infrastructure import TypeReflectBaseModel
20+
from pydantic import Discriminator
1921
from pydantic.types import NonNegativeInt
2022

21-
from oqd_core.interface.analog.operator import OperatorSubtypes
23+
from oqd_core.interface.analog.operator import OperatorSubTypes
2224

2325
__all__ = [
2426
"AnalogCircuit",
@@ -33,24 +35,26 @@
3335
########################################################################################
3436

3537

36-
class AnalogGate(TypeReflectBaseModel):
38+
class AnalogOperation(TypeReflectBaseModel):
3739
"""
38-
Class representing an analog gate composed of Hamiltonian terms and dissipation terms
39-
40-
Attributes:
41-
hamiltonian (Operator): Hamiltonian terms of the gate
40+
Class representing an analog operation applied to the quantum system
4241
"""
4342

44-
hamiltonian: OperatorSubtypes
43+
pass
44+
45+
46+
########################################################################################
4547

4648

47-
# %%
48-
class AnalogOperation(VisitableBaseModel):
49+
class AnalogGate(TypeReflectBaseModel):
4950
"""
50-
Class representing an analog operation applied to the quantum system
51+
Class representing an analog gate composed of Hamiltonian terms and dissipation terms
52+
53+
Attributes:
54+
hamiltonian (Operator): Hamiltonian terms of the gate
5155
"""
5256

53-
pass
57+
hamiltonian: OperatorSubTypes
5458

5559

5660
class Evolve(AnalogOperation):
@@ -62,17 +66,18 @@ class Evolve(AnalogOperation):
6266
gate (AnalogGate): Analog gate to evolve by
6367
"""
6468

65-
key: Literal["evolve"] = "evolve"
6669
duration: float
67-
gate: Union[AnalogGate, str]
70+
gate: AnalogGate
71+
72+
73+
########################################################################################
6874

6975

7076
class Measure(AnalogOperation):
7177
"""
7278
Class representing a measurement in the analog circuit
7379
"""
7480

75-
key: Literal["measure"] = "measure"
7681
targets: Optional[List[int]] = None
7782

7883

@@ -81,14 +86,20 @@ class Initialize(AnalogOperation):
8186
Class representing a initialization in the analog circuit
8287
"""
8388

84-
key: Literal["initialize"] = "initialize"
8589
targets: Optional[List[int]] = None
8690

8791

92+
########################################################################################
93+
8894
"""
8995
Union of classes
9096
"""
91-
Statement = Union[Measure, Evolve, Initialize]
97+
AnalogOperationSubTypes = Annotated[
98+
Union[Measure, Evolve, Initialize], Discriminator(discriminator="class_")
99+
]
100+
101+
102+
########################################################################################
92103

93104

94105
class AnalogCircuit(AnalogOperation):
@@ -100,7 +111,7 @@ class AnalogCircuit(AnalogOperation):
100111
101112
"""
102113

103-
sequence: List[Statement] = []
114+
sequence: List[AnalogOperationSubTypes] = []
104115

105116
n_qreg: Union[NonNegativeInt, None] = None
106117
n_qmode: Union[NonNegativeInt, None] = None

src/oqd_core/interface/analog/operator.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import Union
17+
from typing import Annotated, Union
1818

1919
from oqd_compiler_infrastructure import TypeReflectBaseModel
20+
from pydantic import Discriminator
2021

2122
########################################################################################
2223
from oqd_core.interface.math import (
2324
MathExpr,
24-
MathExprSubtypes,
25+
MathExprSubTypes,
2526
MathImag,
2627
MathMul,
2728
MathNum,
@@ -49,7 +50,7 @@
4950
"OperatorMul",
5051
"OperatorScalarMul",
5152
"OperatorKron",
52-
"OperatorSubtypes",
53+
"OperatorSubTypes",
5354
]
5455

5556

@@ -223,8 +224,8 @@ class OperatorScalarMul(Operator):
223224
expr (MathExpr): [`MathExpr`][oqd_core.interface.math.MathExpr] to multiply by
224225
"""
225226

226-
op: OperatorSubtypes
227-
expr: MathExprSubtypes
227+
op: OperatorSubTypes
228+
expr: MathExprSubTypes
228229

229230

230231
class OperatorBinaryOp(Operator):
@@ -244,8 +245,8 @@ class OperatorAdd(OperatorBinaryOp):
244245
op2 (Operator): Right hand side [`Operator`][oqd_core.interface.analog.operator.Operator]
245246
"""
246247

247-
op1: OperatorSubtypes
248-
op2: OperatorSubtypes
248+
op1: OperatorSubTypes
249+
op2: OperatorSubTypes
249250

250251

251252
class OperatorSub(OperatorBinaryOp):
@@ -257,8 +258,8 @@ class OperatorSub(OperatorBinaryOp):
257258
op2 (Operator): Right hand side [`Operator`][oqd_core.interface.analog.operator.Operator]
258259
"""
259260

260-
op1: OperatorSubtypes
261-
op2: OperatorSubtypes
261+
op1: OperatorSubTypes
262+
op2: OperatorSubTypes
262263

263264

264265
class OperatorMul(OperatorBinaryOp):
@@ -270,8 +271,8 @@ class OperatorMul(OperatorBinaryOp):
270271
op2 (Operator): Right hand side [`Operator`][oqd_core.interface.analog.operator.Operator]
271272
"""
272273

273-
op1: OperatorSubtypes
274-
op2: OperatorSubtypes
274+
op1: OperatorSubTypes
275+
op2: OperatorSubTypes
275276

276277

277278
class OperatorKron(OperatorBinaryOp):
@@ -283,25 +284,28 @@ class OperatorKron(OperatorBinaryOp):
283284
op2 (Operator): Right hand side [`Operator`][oqd_core.interface.analog.operator.Operator]
284285
"""
285286

286-
op1: OperatorSubtypes
287-
op2: OperatorSubtypes
287+
op1: OperatorSubTypes
288+
op2: OperatorSubTypes
288289

289290

290291
########################################################################################
291292

292-
OperatorSubtypes = Union[
293-
PauliI,
294-
PauliX,
295-
PauliY,
296-
PauliZ,
297-
Creation,
298-
Annihilation,
299-
Identity,
300-
OperatorAdd,
301-
OperatorSub,
302-
OperatorMul,
303-
OperatorScalarMul,
304-
OperatorKron,
293+
OperatorSubTypes = Annotated[
294+
Union[
295+
PauliI,
296+
PauliX,
297+
PauliY,
298+
PauliZ,
299+
Creation,
300+
Annihilation,
301+
Identity,
302+
OperatorAdd,
303+
OperatorSub,
304+
OperatorMul,
305+
OperatorScalarMul,
306+
OperatorKron,
307+
],
308+
Discriminator(discriminator="class_"),
305309
]
306310
"""
307311
Alias for the union of concrete Operator subtypes

src/oqd_core/interface/atomic/protocol.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import List, Tuple, Union
17+
from typing import Annotated, List, Tuple, Union
1818

1919
from oqd_compiler_infrastructure import TypeReflectBaseModel
20-
from pydantic import conlist
20+
from pydantic import Discriminator, conlist
2121

2222
from oqd_core.interface.atomic.system import Transition
2323
from oqd_core.interface.math import CastMathExpr, ConstantMathExpr
@@ -103,7 +103,7 @@ class ParallelProtocol(Protocol):
103103
sequence: List of pulses or subprotocols to compose together in a parallel fashion.
104104
"""
105105

106-
sequence: List[Union[PulseSubTypes, ProtocolSubTypes]]
106+
sequence: List[ProtocolPulseSubTypes]
107107

108108

109109
class SequentialProtocol(Protocol):
@@ -114,8 +114,16 @@ class SequentialProtocol(Protocol):
114114
sequence: List of pulses or subprotocols to compose together in a sequntial fashion.
115115
"""
116116

117-
sequence: List[Union[PulseSubTypes, ProtocolSubTypes]]
117+
sequence: List[ProtocolPulseSubTypes]
118118

119119

120-
ProtocolSubTypes = Union[SequentialProtocol, ParallelProtocol]
121-
PulseSubTypes = Union[Pulse, MeasurePulse]
120+
ProtocolSubTypes = Annotated[
121+
Union[SequentialProtocol, ParallelProtocol], Discriminator(discriminator="class_")
122+
]
123+
PulseSubTypes = Annotated[
124+
Union[Pulse, MeasurePulse], Discriminator(discriminator="class_")
125+
]
126+
ProtocolPulseSubTypes = Annotated[
127+
Union[SequentialProtocol, ParallelProtocol, Pulse, MeasurePulse],
128+
Discriminator(discriminator="class_"),
129+
]

src/oqd_core/interface/math.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"MathMul",
4949
"MathDiv",
5050
"MathPow",
51-
"MathExprSubtypes",
51+
"MathExprSubTypes",
5252
"ConstantMathExpr",
5353
"CastMathExpr",
5454
]
@@ -339,27 +339,25 @@ class MathPow(MathBinaryOp):
339339

340340
########################################################################################
341341

342-
MathExprSubtypes = Annotated[
342+
MathExprSubTypes = Annotated[
343343
Union[
344-
Annotated[MathNum, Tag("MathNum")],
345-
Annotated[MathVar, Tag("MathVar")],
346-
Annotated[MathImag, Tag("MathImag")],
347-
Annotated[MathFunc, Tag("MathFunc")],
348-
Annotated[MathAdd, Tag("MathAdd")],
349-
Annotated[MathSub, Tag("MathSub")],
350-
Annotated[MathMul, Tag("MathMul")],
351-
Annotated[MathDiv, Tag("MathDiv")],
352-
Annotated[MathPow, Tag("MathPow")],
344+
MathNum,
345+
MathVar,
346+
MathImag,
347+
MathFunc,
348+
MathAdd,
349+
MathSub,
350+
MathMul,
351+
MathDiv,
352+
MathPow,
353353
],
354-
Discriminator(
355-
lambda v: v["class_"] if isinstance(v, dict) else getattr(v, "class_")
356-
),
354+
Discriminator(discriminator="class_"),
357355
]
358356
"""
359357
Alias for the union of concrete MathExpr subtypes
360358
"""
361359

362-
CastMathExpr = Annotated[MathExprSubtypes, BeforeValidator(MathExpr.cast)]
360+
CastMathExpr = Annotated[MathExprSubTypes, BeforeValidator(MathExpr.cast)]
363361
"""
364362
Annotated type that cast typical numeric python types to MathExpr
365363
"""

0 commit comments

Comments
 (0)