Skip to content

Commit

Permalink
Stabilizing changes (#1856)
Browse files Browse the repository at this point in the history
* pydantic deprecation related changes

* updated cloudpickle version upper limit

* fixed executor_data propagation from lattice to default electrons

* fixing metadata propagation to electron from lattice

* updated changelog

* fixed ui backend test

* added fastapi min limit to be 0.100

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* running precommit hooks

* added typing-extensions

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
kessler-frost and pre-commit-ci[bot] authored Nov 23, 2023
1 parent 3bcebff commit 0e67dfa
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 76 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed usage of deprecated pydantic validation methods
- Fixed qelectron_db retrieval in result object
- Fixed editability of Qelectron on settings page - UI changes
- Certain pydantic v2 related updates
- Fixed lattice's metadata propagation to electron's metadata in case no metadata was provided to the electron

### Operations

Expand All @@ -45,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Significant Changes] Migrated core server-side code to new data access layer.
- Changed the way UI was accessing the qelectron database to access it directly from the mdb file in object store
- Update version of browserverify-sign
- Limiting cloudpickle version to less than 3.0 for now

### Added

Expand Down
2 changes: 1 addition & 1 deletion covalent/_serialize/transport_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _deserialize_edge(e: EdgeSchema) -> dict:
return {
"source": e.source,
"target": e.target,
"attrs": e.metadata.dict(),
"attrs": e.metadata.model_dump(),
}


Expand Down
8 changes: 4 additions & 4 deletions covalent/_shared_files/qinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class QNodeSpecs(BaseModel):
num_trainable_params: int = None
num_device_wires: int
device_name: str
diff_method: Optional[str]
diff_method: Optional[str] = None
expansion_strategy: str
gradient_options: Dict[str, int]
interface: Optional[str]
gradient_fn: Any # can be string or `qml.gradients.gradient_transform`
interface: Optional[str] = None
gradient_fn: Any = None # can be string or `qml.gradients.gradient_transform`
num_gradient_executions: Any = 0
num_parameter_shift_executions: int = None

Expand All @@ -56,7 +56,7 @@ class QElectronInfo(BaseModel):
device_import_path: str # used to inherit type converters and other methods
device_shots: Union[
None, int, Sequence[int], Sequence[Union[int, Sequence[int]]]
] # optional default for execution devices
] = None # optional default for execution devices
device_shots_type: Any = None
device_wires: int # this can not be reliably inferred from tapes alone
pennylane_active_return: bool # client-side status of `pennylane.active_return()`
4 changes: 2 additions & 2 deletions covalent/_shared_files/schemas/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

class EdgeMetadata(BaseModel):
edge_name: str
param_type: Optional[str]
arg_index: Optional[int]
param_type: Optional[str] = None
arg_index: Optional[int] = None


class EdgeSchema(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions covalent/_shared_files/schemas/electron.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from datetime import datetime
from typing import Dict, Optional

from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator

from .asset import AssetSchema
from .common import StatusEnum
Expand Down Expand Up @@ -122,7 +122,7 @@ class ElectronSchema(BaseModel):
assets: ElectronAssets
custom_assets: Optional[Dict[str, AssetSchema]] = None

@validator("custom_assets")
@field_validator("custom_assets")
def check_custom_asset_keys(cls, v):
if v is not None:
for key in v:
Expand Down
4 changes: 2 additions & 2 deletions covalent/_shared_files/schemas/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from typing import Dict, Optional

from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator

from .asset import AssetSchema
from .transport_graph import TransportGraphSchema
Expand Down Expand Up @@ -115,7 +115,7 @@ class LatticeSchema(BaseModel):

transport_graph: TransportGraphSchema

@validator("custom_assets")
@field_validator("custom_assets")
def check_custom_asset_keys(cls, v):
if v is not None:
for key in v:
Expand Down
1 change: 1 addition & 0 deletions covalent/_workflow/electron.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"""Class corresponding to computation nodes."""


import inspect
import json
import operator
Expand Down
8 changes: 4 additions & 4 deletions covalent/_workflow/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def build_graph(self, *args, **kwargs) -> None:

named_args, named_kwargs = get_named_params(workflow_function, args, kwargs)
new_args = [v for _, v in named_args.items()]
new_kwargs = {k: v for k, v in named_kwargs.items()}
new_kwargs = dict(named_kwargs.items())

self.inputs = TransportableObject({"args": args, "kwargs": kwargs})
self.named_args = TransportableObject(named_args)
Expand All @@ -215,7 +215,7 @@ def build_graph(self, *args, **kwargs) -> None:
new_metadata = {
name: DEFAULT_METADATA_VALUES[name]
for name in constraint_names
if not self.metadata[name]
if self.metadata[name] is None
}
new_metadata = encode_metadata(new_metadata)

Expand Down Expand Up @@ -330,8 +330,8 @@ def lattice(
# Add custom metadata fields here
deps_bash: Union[DepsBash, list, str] = None,
deps_pip: Union[DepsPip, list] = None,
call_before: Union[List[DepsCall], DepsCall] = [],
call_after: Union[List[DepsCall], DepsCall] = [],
call_before: Union[List[DepsCall], DepsCall] = None,
call_after: Union[List[DepsCall], DepsCall] = None,
triggers: Union["BaseTrigger", List["BaseTrigger"]] = None,
# e.g. schedule: True, whether to use a custom scheduling logic or not
) -> Lattice:
Expand Down
6 changes: 4 additions & 2 deletions covalent/_workflow/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ def encode_metadata(metadata: dict) -> dict:
encoded_metadata = deepcopy(metadata)
if "executor" in metadata:
if "executor_data" not in metadata:
encoded_metadata["executor_data"] = {}
encoded_metadata["executor_data"] = None if metadata["executor"] is None else {}
if metadata["executor"] is not None and not isinstance(metadata["executor"], str):
encoded_executor = metadata["executor"].to_dict()
encoded_metadata["executor"] = encoded_executor["short_name"]
encoded_metadata["executor_data"] = encoded_executor

if "workflow_executor" in metadata:
if "workflow_executor_data" not in metadata:
encoded_metadata["workflow_executor_data"] = {}
encoded_metadata["workflow_executor_data"] = (
None if metadata["workflow_executor"] is None else {}
)
if metadata["workflow_executor"] is not None and not isinstance(
metadata["workflow_executor"], str
):
Expand Down
15 changes: 10 additions & 5 deletions covalent/executor/qbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
import orjson
import pennylane as qml
from mpire import WorkerPool
from pydantic import BaseModel, Extra, Field, root_validator # pylint: disable=no-name-in-module
from pydantic import ( # pylint: disable=no-name-in-module
BaseModel,
ConfigDict,
Field,
model_validator,
)

from .._shared_files.qinfo import QElectronInfo, QNodeSpecs

Expand Down Expand Up @@ -109,10 +114,10 @@ def override_shots(self) -> Union[int, None]:
# User has specified `shots` as an int.
return self.shots

class Config:
extra = Extra.allow
model_config = ConfigDict(extra="allow")

@root_validator(pre=True)
@model_validator(mode="before")
@classmethod
def set_name(cls, values):
# pylint: disable=no-self-argument
# Set the `name` attribute to the class name
Expand All @@ -138,7 +143,7 @@ def run_circuit(self, qscript, device, result_obj: "QCResult") -> "QCResult":
return result_obj

def dict(self, *args, **kwargs):
dict_ = super().dict(*args, **kwargs)
dict_ = super().model_dump(*args, **kwargs)

# Ensure shots is a hashable value.
shots = dict_.get("shots")
Expand Down
4 changes: 2 additions & 2 deletions covalent/executor/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from typing import Dict, List

from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator

from covalent._shared_files.schemas.asset import AssetUpdate
from covalent._shared_files.util_classes import RESULT_STATUS, Status
Expand All @@ -41,7 +41,7 @@ class TaskUpdate(BaseModel):
status: Status
assets: Dict[str, AssetUpdate]

@validator("status")
@field_validator("status")
def validate_status(cls, v):
if RESULT_STATUS.is_terminal(v):
return v
Expand Down
6 changes: 2 additions & 4 deletions covalent/quantum/qcluster/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing import Callable, List, Sequence, Union

from mpire.async_result import AsyncResult
from pydantic import BaseModel, Extra
from pydantic import BaseModel, ConfigDict

from ...executor.qbase import AsyncBaseQExecutor, BaseQExecutor, QCResult

Expand Down Expand Up @@ -96,6 +96,4 @@ def selector_function(self, qscript, executors):
"""
raise NotImplementedError

class Config:
# Allows defining extra state fields in subclasses.
extra = Extra.allow
model_config = ConfigDict(extra="allow")
4 changes: 2 additions & 2 deletions covalent/quantum/qcluster/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from typing import Union

from pydantic import validator
from pydantic import field_validator

from ...executor.qbase import (
BaseProcessPoolQExecutor,
Expand Down Expand Up @@ -61,7 +61,7 @@ class Simulator(BaseQExecutor):
parallel: Union[bool, str] = "thread"
workers: int = 10

@validator("device")
@field_validator("device")
def validate_device(cls, device): # pylint: disable=no-self-argument
"""
Check that the `device` attribute is NOT a provider or hardware device.
Expand Down
2 changes: 1 addition & 1 deletion covalent_dispatcher/_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ElectronAssetKey(str, Enum):
class ExportResponseSchema(BaseModel):
id: str
status: str
result_export: Optional[ResultSchema]
result_export: Optional[ResultSchema] = None


class AssetRepresentation(str, Enum):
Expand Down
39 changes: 17 additions & 22 deletions covalent_ui/api/v1/models/dispatch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from typing import List, Optional, Union
from uuid import UUID

from pydantic import BaseModel, conint
from pydantic import BaseModel, ConfigDict, Field
from typing_extensions import Annotated

from covalent_ui.api.v1.utils.models_helper import SortBy, SortDirection
from covalent_ui.api.v1.utils.status import Status
Expand All @@ -30,8 +31,8 @@
class DispatchSummaryRequest(BaseModel):
"""Dispatch Summary Request model"""

count: conint(gt=0, lt=100)
offset: Optional[conint(gt=-1)] = 0
count: Annotated[int, Field(gt=0, lt=100)]
offset: Optional[Annotated[int, Field(gt=-1)]] = 0
sort_by: Optional[SortBy] = SortBy.STARTED
search: Optional[str] = ""
direction: Optional[SortDirection] = SortDirection.DESCENDING
Expand All @@ -43,28 +44,23 @@ class DispatchModule(BaseModel):

dispatch_id: str
lattice_name: str
runtime: Optional[Union[int, float, None]]
total_electrons: Optional[Union[int, None]]
total_electrons_completed: Optional[Union[int, None]]
started_at: Optional[Union[datetime, None]]
ended_at: Optional[Union[datetime, None]]
runtime: Optional[Union[int, float, None]] = None
total_electrons: Optional[Union[int, None]] = None
total_electrons_completed: Optional[Union[int, None]] = None
started_at: Optional[Union[datetime, None]] = None
ended_at: Optional[Union[datetime, None]] = None
status: Status
updated_at: Optional[Union[datetime, None]]

class Config:
from_attributes = True
updated_at: Optional[Union[datetime, None]] = None
model_config = ConfigDict(from_attributes=True)


class DispatchResponse(BaseModel):
"""Dispatch Response Model"""

items: List[DispatchModule]
total_count: int

class Config:
"""Configure example for openAPI"""

json_schema_extra = {
model_config = ConfigDict(
json_schema_extra={
"example": {
"dispatches": [
{
Expand All @@ -79,6 +75,7 @@ class Config:
"total_count": 10,
}
}
)


class DeleteDispatchesRequest(BaseModel):
Expand Down Expand Up @@ -113,11 +110,8 @@ class DispatchDashBoardResponse(BaseModel):
total_jobs_new_object: Union[int, None] = None
latest_running_task_status: Union[Status, None] = None
total_dispatcher_duration: Union[int, None] = None

class Config:
"""Configure example for openAPI"""

json_schema_extra = {
model_config = ConfigDict(
json_schema_extra={
"example": {
"total_jobs": 5,
"total_jobs_running": 5,
Expand All @@ -129,3 +123,4 @@ class Config:
"total_dispatcher_duration": 90,
}
}
)
14 changes: 7 additions & 7 deletions covalent_ui/api/v1/models/electrons_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@


class Job(BaseModel):
job_id: Union[str, None]
start_time: Union[datetime, None]
executor: Union[str, None]
status: Union[str, None]
job_id: Union[str, None] = None
start_time: Union[datetime, None] = None
executor: Union[str, None] = None
status: Union[str, None] = None


class JobsResponse(BaseModel):
Expand All @@ -38,9 +38,9 @@ class JobsResponse(BaseModel):


class JobDetails(BaseModel):
overview: Union[dict, None]
circuit: Union[dict, None]
executor: Union[dict, None]
overview: Union[dict, None] = None
circuit: Union[dict, None] = None
executor: Union[dict, None] = None


class JobDetailsResponse(BaseModel):
Expand Down
Loading

0 comments on commit 0e67dfa

Please sign in to comment.