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

Primitives output schema #15

Merged
merged 7 commits into from
Jun 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
46 changes: 46 additions & 0 deletions schemas/estimator_result_v2_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://www.qiskit.org/schemas/estimator_result_v2_schema.json",
"title": "EstimatorV2 result",
"description": "The result for an EstimatorV2 API call",
"version": "1.0.0",
"type": "object",
"required": ["results", "metadata"],
"properties": {
"results": {
"type": "array",
"description": "Array of results, one for each PUB in the original job",
"items": {
"type": "object",
"description": "A single PUB result, containing result data and metadata",
"required": ["data", "metadata"],
"properties": {
"data": {
"type": "object",
"description": "PUB results for estimator include expectation values and standard errors, which can be either numbers or multidimensional arrays of numbers, depending on the shape of the observables and parameters in the original PUB",
"required": ["evs", "stds"],
"properties": {
"evs": {
"anyOf": [{"type": "number"},{"type": "array"}]
},
"stds": {
"anyOf": [{"type": "number"},{"type": "array"}]
},
"ensamble_standard_error": {
"anyOf": [{"type": "number"},{"type": "array"}]
}
}
},
"metadata": {
"type": "object",
"description": "PUB-specific metadata"
}
}
}
},
"metadata": {
"type": "object",
"description": "Metadata for the job"
}
}
}
49 changes: 49 additions & 0 deletions schemas/sampler_result_v2_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://www.qiskit.org/schemas/sampler_result_v2_schema.json",
"title": "EstimatorV2 result",
"description": "The result for an SamplerV2 API call",
"version": "1.0.0",
"type": "object",
"required": ["results", "metadata"],
"properties": {
"results": {
"type": "array",
"description": "Array of results, one for each PUB in the original job",
"items": {
"type": "object",
"description": "A single PUB result, containing result data and metadata",
"required": ["data", "metadata"],
"properties": {
"data": {
"type": "object",
"description": "PUB results for sampler are split according to the classical bits register names. For each register, an array of samples (in hexadecimal string format) is returned",
"additionalProperties": {
"type": "object",
"description": "The data corresponding to a specific measurement",
"properties": {
"samples": {
"type": "array",
"description": "An array of samples (given as hexadecimal strings)",
"items": {"type": "string"}
},
"num_bits": {
"type": "integer",
"description": "The number of measured bits"
}
}
}
},
"metadata": {
"type": "object",
"description": "PUB-specific metadata"
}
}
}
},
"metadata": {
"type": "object",
"description": "Metadata for the job"
}
}
}
63 changes: 63 additions & 0 deletions tests/test_primitive_result_schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

import json
import os
import unittest
import ddt
import jsonschema

SCHEMAS_PATH = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "schemas"
)


@ddt.ddt
class TestEstimatorV2ResultSchema(unittest.TestCase):
"""Tests the estimator result schema"""

def setUp(self) -> None:
with open(
os.path.join(SCHEMAS_PATH, "estimator_result_v2_schema.json"), "r"
) as fd:
self.estimator_schema = json.load(fd)
self.validator = jsonschema.Draft202012Validator(schema=self.estimator_schema)

def test_basic_result(self):
result_str = '{"results": [{"data": {"evs": 1.1142546245919478, "stds": 0.012101383035893986, ' \
'"ensemble_standard_error": 0.012101383035893986}, "metadata": ' \
'{"shots": 4096, "target_precision": 0.015625, "circuit_metadata": {}, "resilience": {}, ' \
'"num_randomizations": 32}}], "metadata": {"dynamical_decoupling": ' \
'{"enable": false, "sequence_type": "XX", "extra_slack_distribution": "middle", ' \
'"scheduling_method": "alap"}, "twirling": {"enable_gates": false, "enable_measure": true, ' \
'"num_randomizations": "auto", "shots_per_randomization": "auto", "interleave_randomizations": ' \
'true, "strategy": "active-accum"}, "resilience": {"measure_mitigation": true, ' \
'"zne_mitigation": false, "pec_mitigation": false}, "version": 2}}'
result = json.loads(result_str)
self.validator.validate(result)


class TestSamplerV2ResultSchema(unittest.TestCase):
"""Tests the sampler result schema"""

def setUp(self) -> None:
with open(
os.path.join(SCHEMAS_PATH, "sampler_result_v2_schema.json"), "r"
) as fd:
self.sampler_schema = json.load(fd)
self.validator = jsonschema.Draft202012Validator(schema=self.sampler_schema)

def test_basic_result(self):
result_str = '{"results": [{"data": {"meas": {"samples": ["0x3", "0x3", "0x3", "0x3", "0x3"],' \
' "num_bits": 2}}, "metadata": {"circuit_metadata": {}}}], "metadata": {"version": 2}}'
result = json.loads(result_str)
self.validator.validate(result)
Loading