-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce schema for Quantum Volume jobs (#67)
* Introduce schema for Quantum Volume jobs * type hinting in schema validator
- Loading branch information
Showing
9 changed files
with
382 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,50 @@ | ||
import json | ||
import os | ||
from jsonschema import validate | ||
|
||
|
||
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
DEFAULT_SCHEMA_DIR = os.path.join(CURRENT_DIR, "schemas") | ||
|
||
SCHEMA_MAPPING = { | ||
"Quantum Volume": "quantum_volume.schema.json", | ||
} | ||
|
||
|
||
def load_json_file(file_path: str) -> dict: | ||
""" | ||
Load and parse a JSON file. | ||
""" | ||
with open(file_path, "r") as file: | ||
return json.load(file) | ||
|
||
|
||
def load_schema(benchmark_name: str, schema_dir: str = DEFAULT_SCHEMA_DIR) -> dict: | ||
""" | ||
Load a JSON schema based on the benchmark name. | ||
""" | ||
schema_filename = SCHEMA_MAPPING.get(benchmark_name) | ||
if not schema_filename: | ||
raise ValueError(f"Unsupported benchmark: {benchmark_name}") | ||
|
||
schema_path = os.path.join(schema_dir, schema_filename) | ||
return load_json_file(schema_path) | ||
|
||
|
||
def validate_params(params: dict, schema_dir: str = DEFAULT_SCHEMA_DIR) -> None: | ||
""" | ||
Validate parameters against the corresponding JSON schema. | ||
Raises a ValidationError if the parameters do not match the schema. | ||
""" | ||
schema = load_schema(params.get("benchmark_name"), schema_dir) | ||
validate(instance=params, schema=schema) | ||
|
||
|
||
def load_and_validate(file_path: str, schema_dir: str = DEFAULT_SCHEMA_DIR) -> dict: | ||
""" | ||
Load parameters from a JSON file and validate them against the corresponding schema. | ||
Raises a ValidationError validation fails. | ||
""" | ||
params = load_json_file(file_path) | ||
validate_params(params, schema_dir) | ||
return params |
This file contains 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,37 @@ | ||
{ | ||
"$id": "metriq-gym/quantum_volume.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Quantum Volume", | ||
"type": "object", | ||
"properties": { | ||
"benchmark_name": { | ||
"type": "string", | ||
"const": "Quantum Volume" | ||
}, | ||
"num_qubits": { | ||
"type": "integer", | ||
"minimum": 1, | ||
"examples": [5] | ||
}, | ||
"shots": { | ||
"type": "integer", | ||
"default": 1000, | ||
"minimum": 1, | ||
"examples": [1000] | ||
}, | ||
"trials": { | ||
"type": "integer", | ||
"default": 100, | ||
"minimum": 1, | ||
"examples": [100] | ||
}, | ||
"confidence_level": { | ||
"type": "number", | ||
"minimum": 0, | ||
"maximum": 1, | ||
"default": 0.95, | ||
"examples": [0.95] | ||
} | ||
}, | ||
"required": ["benchmark_name", "num_qubits"] | ||
} |
Oops, something went wrong.