-
Notifications
You must be signed in to change notification settings - Fork 0
[QI2-1523] HWCS validator #26
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
24cf3b4
Move old tests
Mythir 8e1711f
Test requirements
Mythir bd22601
Add model generation
Mythir 24ef5dc
Add validation step
Mythir e53b57c
Disable timestamp
Mythir 7cd1ae4
Test validation
Mythir dc81629
Success
Mythir e6752d9
Remove useless comments
Mythir 472a8ff
First test
Mythir c0cf6d8
Add readme
Mythir 532f815
Happy flow
Mythir 373c4b5
Add two init flow
Mythir 646591b
Refactor
Mythir 9007283
Add readme
Mythir 0f4bb37
Add MyPy
Mythir e06c599
Oops
Mythir a363eba
Add dry run mode
Mythir d400d06
Add dry run to actions
Mythir 9b7e34e
Oops
Mythir 530f764
Fix workflow
Mythir ef535ec
Remove poison test
Mythir 337134d
Complete readme
Mythir 4aa9ccd
Expand readme
Mythir e266116
Add timeouts
Mythir 74bf217
Small clean up
Mythir e299870
Remove two init test
Mythir 8516f05
Add test for failure response
Mythir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,8 @@ | |
"editor.rulers": [ | ||
120 | ||
] | ||
} | ||
} | ||
}, | ||
"python.analysis.extraPaths": [ | ||
"tests/control-software-validator" | ||
], | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ | |
], | ||
"title": "Hardware data failure", | ||
"type": "object" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Control Software Validator | ||
|
||
## Introduction | ||
|
||
This tool can be used to verify that the control software respects the Compute Runtime Schema. | ||
It will send correctly formatted requests to the control software and verify that responses are correctly formatted as well. | ||
|
||
## Usage | ||
|
||
After installing the dependencies in requirements.txt, | ||
simply run the following command in the control-software-validator directory: | ||
|
||
```bash | ||
pytest . | ||
``` | ||
|
||
The tool assumes you have the control software running on the same host with its reply channel | ||
available on port 4203 (`tcp://localhost:4203`), and its publish channel available on port 4204 (`tcp://localhost:4204`). | ||
These addresses are configurable using environment variables `CSV_HWCS_REQ_ADDRESS` and `CSV_HWCS_SUB_ADDRESS` | ||
respectively (or directly in the test file). | ||
|
||
Setting the environment variable `CSV_MODE` to `dry_run` triggers a dry run that does not connect to the control software. | ||
This is useful for verifying the validator itself, as it will fail if any of the request messages are badly formatted. | ||
|
||
## Model generation | ||
|
||
The models used in the validator are automatically generated based on the jsonschemas. The generate.sh script can be run | ||
in the same Python environment as the tool. | ||
|
||
## Limitations | ||
|
||
There is no way in the protocol to reliably trigger a failure, so the tool can only verify the | ||
correct formatting of successful responses. |
Empty file.
27 changes: 27 additions & 0 deletions
27
tests/control-software-validator/channels/request_channel.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import Any | ||
|
||
from pydantic import BaseModel | ||
from typing_extensions import override | ||
from zmq.asyncio import Socket | ||
|
||
|
||
class RequestChannel: | ||
def __init__(self, stream: Socket) -> None: | ||
self.stream = stream | ||
|
||
def connect(self, address: str) -> None: | ||
self.stream.connect(address) | ||
|
||
async def request(self, request: BaseModel, return_type: type[BaseModel]) -> Any: | ||
# Serializes request and tests if response is correctly formatted | ||
self.stream.send_string(request.model_dump_json()) | ||
reply = await self.stream.recv_string() | ||
return return_type.model_validate_json(reply) | ||
|
||
|
||
class MockRequestChannel(RequestChannel): | ||
@override | ||
async def request(self, request: BaseModel, return_type: type[BaseModel]) -> Any: | ||
# Only serializes request but does not actually send it | ||
_ = request.model_dump_json() | ||
return None |
24 changes: 24 additions & 0 deletions
24
tests/control-software-validator/channels/subscribe_channel.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Any | ||
|
||
from typing_extensions import override | ||
from zmq.asyncio import Socket | ||
|
||
|
||
class SubscribeChannel: | ||
def __init__(self, stream: Socket) -> None: | ||
self.stream = stream | ||
|
||
def connect(self, address: str) -> None: | ||
self.stream.connect(address) | ||
self.stream.subscribe("") | ||
|
||
async def receive(self, return_type: Any) -> Any: | ||
reply = await self.stream.recv_string() | ||
return return_type.model_validate_json(reply) | ||
|
||
|
||
class MockSubscribeChannel(SubscribeChannel): | ||
@override | ||
async def receive(self, return_type: Any) -> Any: | ||
# Does not actually receive anything | ||
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
|
||
# Directory to search | ||
schemas="../../schemas" | ||
models="./models" | ||
|
||
# Loop through all files in the schemas directory and subdirectories | ||
find "$schemas" -type f -name "*.json" | while read -r file; do | ||
filename=$(basename "$file" | sed 's/\..*//') | ||
parentdir=$(basename "$(dirname "$file")") | ||
classname="${parentdir}_${filename}" | ||
|
||
datamodel-codegen \ | ||
--input "$file" \ | ||
--class-name $classname \ | ||
--disable-timestamp \ | ||
--input-file-type jsonschema \ | ||
--output "./models/$classname.py" \ | ||
--field-constraints \ | ||
--output-model-type pydantic_v2.BaseModel | ||
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Models | ||
|
||
This directory contains generated models based on the json schemas. Do not edit manually, use the generate.sh script instead. |
Empty file.
28 changes: 28 additions & 0 deletions
28
tests/control-software-validator/models/execute_reply_failure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# generated by datamodel-codegen: | ||
# filename: reply_failure.schema.json | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class QuantumHardwareFailureData(BaseModel): | ||
error_msg: str = Field( | ||
..., | ||
description='A descriptive error message to be passed on to the user.', | ||
title='Error Msg', | ||
) | ||
|
||
|
||
class ExecuteReplyFailure(BaseModel): | ||
version: str = Field(..., pattern='^\\d+\\.\\d+\\.\\d$', title='Version') | ||
session_id: UUID = Field( | ||
..., | ||
description='An arbitrary string, filled in in the request, which is copied into the reply object.', | ||
title='Session Id', | ||
) | ||
status: Literal['failure'] = Field(..., title='Status') | ||
payload: QuantumHardwareFailureData |
36 changes: 36 additions & 0 deletions
36
tests/control-software-validator/models/execute_reply_success.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# generated by datamodel-codegen: | ||
# filename: reply_success.schema.json | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Dict, List, Literal, Optional | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class QuantumHardwareRunCircuitResult(BaseModel): | ||
job_id: int = Field( | ||
..., description='Client defined identifier for the execution.', title='Job Id' | ||
) | ||
results: Dict[str, int] = Field( | ||
..., | ||
description='Mapping of measured bitstring (little endian notation; q[n]...q[0]) to count of occurrences.', | ||
title='Results', | ||
) | ||
raw_data: Optional[List[str]] = Field( | ||
None, | ||
description='A list of bitstrings (little endian notation; `q[n]...q[0]`) ordered by the shot in which it was measured.', | ||
title='Raw Data', | ||
) | ||
|
||
|
||
class ExecuteReplySuccess(BaseModel): | ||
version: str = Field(..., pattern='^\\d+\\.\\d+\\.\\d$', title='Version') | ||
session_id: UUID = Field( | ||
..., | ||
description='An arbitrary string, filled in in the request, which is copied into the reply object.', | ||
title='Session Id', | ||
) | ||
status: Literal['success'] = Field(..., title='Status') | ||
payload: QuantumHardwareRunCircuitResult |
40 changes: 40 additions & 0 deletions
40
tests/control-software-validator/models/execute_request.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# generated by datamodel-codegen: | ||
# filename: request.schema.json | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Literal, Optional | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class QuantumHardwareRunCircuitPayload(BaseModel): | ||
job_id: int = Field( | ||
..., description='Client identifier for the execution', title='Job Id' | ||
) | ||
circuit: str = Field( | ||
..., description='Circuit description in cQASM language', title='Circuit' | ||
) | ||
number_of_shots: int = Field( | ||
..., | ||
description='Number of shots to be executed for the circuit.', | ||
gt=0, | ||
title='Number Of Shots', | ||
) | ||
include_raw_data: Optional[bool] = Field( | ||
False, | ||
description='Whether or not to return all bitstrings in the order in which they were measured.', | ||
title='Include Raw Data', | ||
) | ||
|
||
|
||
class ExecuteRequest(BaseModel): | ||
version: str = Field(..., pattern='^\\d+\\.\\d+\\.\\d$', title='Version') | ||
session_id: UUID = Field( | ||
..., | ||
description='An arbitrary string, filled in in the request, which is copied into the reply object.', | ||
title='Session Id', | ||
) | ||
command: Literal['execute'] = Field(..., title='Command') | ||
payload: QuantumHardwareRunCircuitPayload |
13 changes: 13 additions & 0 deletions
13
tests/control-software-validator/models/get_dynamic_reply_failure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# generated by datamodel-codegen: | ||
# filename: reply_failure.schema.json | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class GetDynamicReplyFailure(BaseModel): | ||
version: str = Field(..., pattern='^\\d+\\.\\d+\\.\\d$', title='Version') | ||
status: Literal['failure'] = Field(..., title='Status') |
20 changes: 20 additions & 0 deletions
20
tests/control-software-validator/models/get_dynamic_reply_success.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# generated by datamodel-codegen: | ||
# filename: reply_success.schema.json | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class QuantumHardwareDynamicData(BaseModel): | ||
pass | ||
|
||
|
||
class GetDynamicReplySuccess(BaseModel): | ||
version: str = Field(..., pattern='^\\d+\\.\\d+\\.\\d$', title='Version') | ||
status: Literal['success'] = Field(..., title='Status') | ||
payload: QuantumHardwareDynamicData = Field( | ||
..., description='The return value(s) of the executed command.' | ||
) |
13 changes: 13 additions & 0 deletions
13
tests/control-software-validator/models/get_dynamic_request.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# generated by datamodel-codegen: | ||
# filename: request.schema.json | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class GetDynamicRequest(BaseModel): | ||
version: str = Field(..., pattern='^\\d+\\.\\d+\\.\\d$', title='Version') | ||
command: Literal['get_dynamic'] = Field(..., title='Command') |
13 changes: 13 additions & 0 deletions
13
tests/control-software-validator/models/get_static_reply_failure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# generated by datamodel-codegen: | ||
# filename: reply_failure.schema.json | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class GetStaticReplyFailure(BaseModel): | ||
version: str = Field(..., pattern='^\\d+\\.\\d+\\.\\d$', title='Version') | ||
status: Literal['failure'] = Field(..., title='Status') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.