Skip to content
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

Rename Operation to Routine #2

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions src/hqar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
"""
from typing import Any

from ._operation_v1 import generate_operation_schema_v1
from ._schema_v1 import generate_schema_v1

SCHEMA_GENERATORS = {"v1": generate_operation_schema_v1}
SCHEMA_GENERATORS = {"v1": generate_schema_v1}
LATEST_SCHEMA_VERSION = "v1"


def generate_operation_schema(version: str = LATEST_SCHEMA_VERSION) -> dict[str, Any]:
"""Generate Operation schema of given version.
def generate_program_schema(version: str = LATEST_SCHEMA_VERSION) -> dict[str, Any]:
"""Generate Program schema of given version.

Args:
version: version identifier of the schema.

Returns:
A dictionary with JSON schema describing operation.
A dictionary with JSON schema describing program.

Raises:
ValueError: if `version` does not match any known version schema.
Expand All @@ -35,4 +35,4 @@ def generate_operation_schema(version: str = LATEST_SCHEMA_VERSION) -> dict[str,
raise ValueError(f"Unknown schema version {version}")


__all__ = ["generate_operation_schema"]
__all__ = ["generate_program_schema"]
22 changes: 11 additions & 11 deletions src/hqar/_operation_v1.py → src/hqar/_schema_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
information is strictly prohibited without the express written permission of
PsiQuantum Corp.

Pydantic models used for defining V1 schema of Operation.
Pydantic models used for defining V1 schema of Routine.
"""
from __future__ import annotations

Expand Down Expand Up @@ -57,16 +57,16 @@ class _ParamLinkV1(BaseModel):
model_config = ConfigDict(title="ParamLink")


class OperationV1(BaseModel):
"""Description of Operation in V1 schema.
class RoutineV1(BaseModel):
"""Description of Routine in V1 schema.

Note:
This is NOT a top-level object in the schema. Instead, OperationV1 is wrapped in
This is NOT a top-level object in the schema. Instead, RoutineV1 is wrapped in
SchemaV1.
"""

name: Name
children: list[OperationV1] = Field(default_factory=list)
children: list[RoutineV1] = Field(default_factory=list)
type: Optional[str] = None
ports: list[_PortV1] = Field(default_factory=list)
resources: list[_ResourceV1] = Field(default_factory=list)
Expand All @@ -76,32 +76,32 @@ class OperationV1(BaseModel):
linked_params: list[_ParamLinkV1] = Field(default_factory=list)
meta: dict[str, Any] = Field(default_factory=dict)

model_config = ConfigDict(title="Operation")
model_config = ConfigDict(title="Routine")

def __init__(self, **data: Any):
super().__init__(**{k: v for k, v in data.items() if v != [] and v != {}})


class SchemaV1(BaseModel):
"""Root object in Operation schema V1."""
"""Root object in Program schema V1."""

version: Literal["v1"]
operation: OperationV1
program: RoutineV1


class _GenerateV1JsonSchema(GenerateJsonSchema):
def generate(self, schema, mode="validation"):
json_schema = super().generate(schema, mode=mode)
json_schema["title"] = "FTQC-ready quantum operation"
json_schema["title"] = "FTQC-ready quantum program"
json_schema["$schema"] = self.schema_dialect
return json_schema

def normalize_name(self, name):
return name.removeprefix("_").replace("V1", "")


def generate_operation_schema_v1() -> dict[str, Any]:
"""Generate Operation schema V1.
def generate_schema_v1() -> dict[str, Any]:
"""Generate Routine schema V1.

The schema is generated from DocumentRootV1 model, and then enriched with
additional fields "title" and "$schema".
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
- input:
operation:
program:
name: root
description: No version provided
error_path: "$"
error_message: "'version' is a required property"
- input:
version: v2
operation:
program:
name: root
description: Incorrect version provided
error_path: "$.version"
error_message: "'v1' was expected"
- input:
version: v1
operation:
name: "123my_operation"
program:
name: "123my_program"
description: "Root name starts with a number"
error_path: "$.operation.name"
error_message: "'123my_operation' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
error_path: "$.program.name"
error_message: "'123my_program' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
name: "*op"
program:
name: "*prog"
description: "Root name starts with a special char"
error_path: "$.operation.name"
error_message: "'*op' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
error_path: "$.program.name"
error_message: "'*prog' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
name: "op#eration"
program:
name: "pr#ogram"
description: "Root name contains special char"
error_path: "$.operation.name"
error_message: "'op#eration' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
error_path: "$.program.name"
error_message: "'pr#ogram' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
name: "my operation"
program:
name: "my program"
description: "Root name contains space"
error_path: "$.operation.name"
error_message: "'my operation' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
error_path: "$.program.name"
error_message: "'my program' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: "alias-sampling"
description: "Root name contains dash"
error_path: "$.operation.name"
error_path: "$.program.name"
error_message: "'alias-sampling' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: "root"
children:
- name: 123child
description: "Child name starts with a digit"
error_path: "$.operation.children[0].name"
error_path: "$.program.children[0].name"
error_message: "'123child' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: "root"
children:
- name: "*child"
description: "Child name starts with a special char"
error_path: "$.operation.children[0].name"
error_path: "$.program.children[0].name"
error_message: "'*child' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: "root"
children:
- name: "child#0"
description: "Child name contains special character"
error_path: "$.operation.children[0].name"
error_path: "$.program.children[0].name"
error_message: "'child#0' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: root
ports:
- name: 0in
Expand All @@ -85,11 +85,11 @@
direction: output
size: 1
description: "Root port name starts with a number"
error_path: "$.operation.ports[0].name"
error_path: "$.program.ports[0].name"
error_message: "'0in' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: root
ports:
- name: in0
Expand All @@ -99,11 +99,11 @@
direction: output
size: 1
description: "Root port name contains special char"
error_path: "$.operation.ports[1].name"
error_path: "$.program.ports[1].name"
error_message: "'out#0' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: root
children:
- name: foo
Expand All @@ -115,11 +115,11 @@
direction: output
size: 1
description: "Child port name contains special char"
error_path: "$.operation.children[0].ports[1].name"
error_path: "$.program.children[0].ports[1].name"
error_message: "'out#0' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: root
ports:
- name: in0
Expand All @@ -129,11 +129,11 @@
direction: out
size: 1
description: "Child port direction has unexpected value"
error_path: "$.operation.ports[1].direction"
error_path: "$.program.ports[1].direction"
error_message: "'out' is not one of ['input', 'output', 'through']"
- input:
version: v1
operation:
program:
name: root
resources:
- name: n-qubits
Expand All @@ -144,33 +144,33 @@
direction: input
size: 1
description: "Resource name contains dash"
error_path: "$.operation.resources[0].name"
error_path: "$.program.resources[0].name"
error_message: "'n-qubits' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: root
resources:
- name: n_qubits
value: "N"
type: "length"
description: "Resource has unexpected type"
error_path: "$.operation.resources[0].type"
error_path: "$.program.resources[0].type"
error_message: "'length' is not one of ['additive', 'multiplicative', 'qubits', 'other']"
- input:
version: v1
operation:
program:
name: root
resources:
- name: n_qubits
value: "N"
type: "length"
description: "Resource has unexpected type"
error_path: "$.operation.resources[0].type"
error_path: "$.program.resources[0].type"
error_message: "'length' is not one of ['additive', 'multiplicative', 'qubits', 'other']"
- input:
version: v1
operation:
program:
name: root
children:
- name: foo
Expand All @@ -193,28 +193,28 @@
- source: foo.foo.out_0
target: bar.in_0
description: "Connections have more than one namespace"
error_path: "$.operation.connections[0].source"
error_path: "$.program.connections[0].source"
error_message: "'foo.foo.out_0' does not match '^(([A-Za-z_][A-Za-z0-9_]*)|([A-Za-z_][A-Za-z0-9_]*\\\\.[A-Za-z_][A-Za-z0-9_]*))$'"
- input:
version: v1
operation:
program:
name: "root"
input_params:
- "lambda"
- "my-input-param"
description: "Input param has invalid name"
error_path: "$.operation.input_params[1]"
error_path: "$.program.input_params[1]"
error_message: "'my-input-param' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: ""
description: "Operation has an empty name"
error_path: "$.operation.name"
description: "Program has an empty name"
error_path: "$.program.name"
error_message: "'' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: "root"
input_params:
- N
Expand All @@ -226,11 +226,11 @@
input_params:
- N
description: Source of a paramater link is namespaced
error_path: "$.operation.linked_params[0].source"
error_path: "$.program.linked_params[0].source"
error_message: "'foo.N' does not match '^[A-Za-z_][A-Za-z0-9_]*$'"
- input:
version: v1
operation:
program:
name: "root"
input_params:
- N
Expand All @@ -242,5 +242,5 @@
input_params:
- N
description: "Target of a paramater link is not namespaced"
error_path: "$.operation.linked_params[0].targets[0]"
error_path: "$.program.linked_params[0].targets[0]"
error_message: "'N' does not match '^[A-Za-z_][A-Za-z0-9_]*\\\\.[A-Za-z_][A-Za-z0-9_]*'"
Loading
Loading