Skip to content

Commit

Permalink
Remove uses of Any everywhere except metadata field
Browse files Browse the repository at this point in the history
  • Loading branch information
qartik committed Oct 17, 2023
1 parent 80f4f3e commit d32b8bb
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ clean:
build: clean
python -m build --sdist -n

updateschema:
phir -s > schema.json
updateschema: install
phir-cli -s > schema.json
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exclude = (?x)(
disallow_any_unimported = true
disallow_any_expr = true
disallow_any_decorated = true
; disallow_any_explicit = true
disallow_any_generics = true
disallow_subclassing_any = true

Expand Down
4 changes: 4 additions & 0 deletions phir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ def main() -> None:
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)


if __name__ == "__main__":
main()
23 changes: 12 additions & 11 deletions phir/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

from pydantic import BaseModel, ConfigDict, Field

Idx: TypeAlias = Annotated[int, Field(ge=0)]
Sym: TypeAlias = str
Bit: TypeAlias = Annotated[list[Sym | Idx], Field(max_length=2)]

# Data Management


Expand All @@ -33,7 +37,7 @@ class CVarDefine(Data):

data: Literal["cvar_define"]
data_type: str = "i64"
variable: str
variable: Sym
size: Annotated[int, Field(gt=0)] | None


Expand All @@ -42,16 +46,16 @@ class QVarDefine(Data):

data: Literal["qvar_define"]
data_type: str | None = "qubits"
variable: str
variable: Sym
size: int = Field(gt=0)


class ExportVar(Data):
"""Exporting Classical Variables."""

data: Literal["cvar_export"]
variables: list[str]
to: list[str] | None = None
variables: list[Sym]
to: list[Sym] | None = None


DataMgmt: TypeAlias = CVarDefine | QVarDefine | ExportVar
Expand All @@ -65,25 +69,22 @@ class Op(BaseModel, abc.ABC):
model_config = ConfigDict(extra="forbid")

metadata: dict[str, Any] | None = None
returns: list[Any] | None = None
args: list[Any] | None = None


class QOp(Op):
"""Quantum operation."""

qop: str
args: list[
list[str | Annotated[int, Field(ge=0)]]
| list[list[str | Annotated[int, Field(ge=0)]]]
]
returns: list[Bit] | None = None
args: list[Bit | list[Bit]]


class COp(Op):
"""Classical operation."""

cop: str
args: list[Any]
returns: list[Sym | Bit] | None = None
args: list[int | Sym | COp | Bit]


class FFCall(COp):
Expand Down
182 changes: 134 additions & 48 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,35 @@
"default": null,
"title": "Metadata"
},
"cop": {
"title": "Cop",
"type": "string"
},
"returns": {
"anyOf": [
{
"items": {},
"items": {
"anyOf": [
{
"type": "string"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"type": "array"
}
]
},
"type": "array"
},
{
Expand All @@ -30,18 +55,41 @@
"title": "Returns"
},
"args": {
"items": {},
"items": {
"anyOf": [
{
"type": "integer"
},
{
"type": "string"
},
{
"$ref": "#/$defs/COp"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"type": "array"
}
]
},
"title": "Args",
"type": "array"
},
"cop": {
"title": "Cop",
"type": "string"
}
},
"required": [
"args",
"cop"
"cop",
"args"
],
"title": "COp",
"type": "object"
Expand Down Expand Up @@ -178,10 +226,35 @@
"default": null,
"title": "Metadata"
},
"cop": {
"const": "ffcall",
"title": "Cop"
},
"returns": {
"anyOf": [
{
"items": {},
"items": {
"anyOf": [
{
"type": "string"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"type": "array"
}
]
},
"type": "array"
},
{
Expand All @@ -192,22 +265,45 @@
"title": "Returns"
},
"args": {
"items": {},
"items": {
"anyOf": [
{
"type": "integer"
},
{
"type": "string"
},
{
"$ref": "#/$defs/COp"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"type": "array"
}
]
},
"title": "Args",
"type": "array"
},
"cop": {
"const": "ffcall",
"title": "Cop"
},
"function": {
"title": "Function",
"type": "string"
}
},
"required": [
"args",
"cop",
"args",
"function"
],
"title": "FFCall",
Expand Down Expand Up @@ -309,32 +405,6 @@
"default": null,
"title": "Metadata"
},
"returns": {
"anyOf": [
{
"items": {},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"title": "Returns"
},
"args": {
"anyOf": [
{
"items": {},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"title": "Args"
},
"mop": {
"title": "Mop",
"type": "string"
Expand Down Expand Up @@ -362,10 +432,28 @@
"default": null,
"title": "Metadata"
},
"qop": {
"title": "Qop",
"type": "string"
},
"returns": {
"anyOf": [
{
"items": {},
"items": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"minimum": 0,
"type": "integer"
}
]
},
"maxItems": 2,
"type": "array"
},
"type": "array"
},
{
Expand All @@ -390,6 +478,7 @@
}
]
},
"maxItems": 2,
"type": "array"
},
{
Expand All @@ -405,6 +494,7 @@
}
]
},
"maxItems": 2,
"type": "array"
},
"type": "array"
Expand All @@ -413,15 +503,11 @@
},
"title": "Args",
"type": "array"
},
"qop": {
"title": "Qop",
"type": "string"
}
},
"required": [
"args",
"qop"
"qop",
"args"
],
"title": "QOp",
"type": "object"
Expand Down

0 comments on commit d32b8bb

Please sign in to comment.