From 4765ebc0a8e1bc765ae19f41507e58ea2ba2d935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Ja=C5=82owiecki?= Date: Mon, 18 Mar 2024 13:17:51 +0100 Subject: [PATCH] Rename Operation to Routine --- src/hqar/__init__.py | 12 +-- src/hqar/{_operation_v1.py => _schema_v1.py} | 22 ++-- ...les.yaml => invalid_program_examples.yaml} | 102 +++++++++--------- ...mples.yaml => valid_program_examples.yaml} | 16 +-- tests/hqar/test_schema_validation.py | 8 +- 5 files changed, 80 insertions(+), 80 deletions(-) rename src/hqar/{_operation_v1.py => _schema_v1.py} (84%) rename tests/hqar/data/{invalid_operation_examples.yaml => invalid_program_examples.yaml} (76%) rename tests/hqar/data/{valid_operation_examples.yaml => valid_program_examples.yaml} (89%) diff --git a/src/hqar/__init__.py b/src/hqar/__init__.py index 4f690cd..07a3a24 100644 --- a/src/hqar/__init__.py +++ b/src/hqar/__init__.py @@ -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. @@ -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"] diff --git a/src/hqar/_operation_v1.py b/src/hqar/_schema_v1.py similarity index 84% rename from src/hqar/_operation_v1.py rename to src/hqar/_schema_v1.py index 2a9ce45..1f27001 100644 --- a/src/hqar/_operation_v1.py +++ b/src/hqar/_schema_v1.py @@ -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 @@ -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) @@ -76,23 +76,23 @@ 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 @@ -100,8 +100,8 @@ 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". diff --git a/tests/hqar/data/invalid_operation_examples.yaml b/tests/hqar/data/invalid_program_examples.yaml similarity index 76% rename from tests/hqar/data/invalid_operation_examples.yaml rename to tests/hqar/data/invalid_program_examples.yaml index 239e154..0da79e5 100644 --- a/tests/hqar/data/invalid_operation_examples.yaml +++ b/tests/hqar/data/invalid_program_examples.yaml @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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_]*'" diff --git a/tests/hqar/data/valid_operation_examples.yaml b/tests/hqar/data/valid_program_examples.yaml similarity index 89% rename from tests/hqar/data/valid_operation_examples.yaml rename to tests/hqar/data/valid_program_examples.yaml index fb96636..99b83d0 100644 --- a/tests/hqar/data/valid_operation_examples.yaml +++ b/tests/hqar/data/valid_program_examples.yaml @@ -1,11 +1,11 @@ - input: version: v1 - operation: + program: name: "root" - description: "Empty operation" + description: "Empty program" - input: version: v1 - operation: + program: name: root input_params: - "N" @@ -16,10 +16,10 @@ - name: out_0 size: 2 direction: output - description: "Operation with ports and input params" + description: "Program with ports and input params" - input: version: v1 - operation: + program: name: root input_params: - "N" @@ -66,10 +66,10 @@ target: out_0 - source: bar.out_0 target: out_1 - description: Fully-featured operation + description: Fully-featured program - input: version: v1 - operation: + program: name: root ports: - name: in_0 @@ -103,5 +103,5 @@ target: foo.in_0 - source: foo.out_0 target: out_1 - description: Operation with passthroughs + description: Program with passthroughs diff --git a/tests/hqar/test_schema_validation.py b/tests/hqar/test_schema_validation.py index 76cd330..55282a5 100644 --- a/tests/hqar/test_schema_validation.py +++ b/tests/hqar/test_schema_validation.py @@ -15,15 +15,15 @@ import yaml # type: ignore[import-untyped] from jsonschema import ValidationError, validate -from hqar import generate_operation_schema +from hqar import generate_program_schema def validate_with_v1(data): - validate(data, generate_operation_schema(version="v1")) + validate(data, generate_program_schema(version="v1")) def load_invalid_examples(): - with open(Path(__file__).parent / "data/invalid_operation_examples.yaml") as f: + with open(Path(__file__).parent / "data/invalid_program_examples.yaml") as f: data = yaml.safe_load(f) return [ @@ -38,7 +38,7 @@ def load_invalid_examples(): def load_valid_examples(): - with open(Path(__file__).parent / "data/valid_operation_examples.yaml") as f: + with open(Path(__file__).parent / "data/valid_program_examples.yaml") as f: data = yaml.safe_load(f) return [pytest.param(example["input"], id=example["description"]) for example in data]