Skip to content

Commit

Permalink
Added dynamical decoupling tests and fixed some bugs in the framework
Browse files Browse the repository at this point in the history
  • Loading branch information
gadial committed Apr 4, 2024
1 parent 6913d3d commit 54ab15d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
50 changes: 50 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# 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.

"""With some utils"""

import itertools
from ddt import data, unpack

class Case(dict):
"""<no description>"""


def generate_cases(docstring, dsc=None, name=None, **kwargs):
"""Combines kwargs in Cartesian product and creates Case with them"""
ret = []
keys = kwargs.keys()
vals = kwargs.values()
for values in itertools.product(*vals):
case = Case(zip(keys, values))
if docstring is not None:
setattr(case, "__doc__", docstring.format(**case))
if dsc is not None:
setattr(case, "__doc__", dsc.format(**case))
if name is not None:
setattr(case, "__name__", name.format(**case))
ret.append(case)
return ret


def combine(**kwargs):
"""Decorator to create combinations and tests
@combine(level=[0, 1, 2, 3],
circuit=[a, b, c, d],
dsc='Test circuit {circuit.__name__} with level {level}',
name='{circuit.__name__}_level{level}')
"""

def deco(func):
return data(*generate_cases(docstring=func.__doc__, **kwargs))(unpack(func))

return deco
30 changes: 27 additions & 3 deletions tests/test_primitive_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from dataclasses import asdict
import jsonschema


from qiskit_ibm_runtime import SamplerV2, EstimatorV2
from tests import combine

SCHEMAS_PATH = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
Expand All @@ -42,20 +42,27 @@ def get_options_dict(estimator):
options_dict['pubs'] = []
return options_dict

def assert_valid_options(self, estimator, options_to_check, options_path=""):
def assert_valid_options(self, estimator, options_to_check, options_path="", options_object=None):
"""Verifies the schema validation gives the same result as attempting
to set the options in the estimator"""
options = estimator.options
if options_object is None:
options = estimator.options
else:
options = options_object
try:
for option_name, value in options_to_check.items():
setattr(options, option_name, value)
options_dict = TestEstimatorV2Schema.get_options_dict(estimator)
self.assertTrue(self.validator.is_valid(options_dict))
except AssertionError as err:
raise err
except Exception:
options_dict = TestEstimatorV2Schema.get_options_dict(estimator)
for option_name, value in options_to_check.items():
dict_to_change = options_dict
for key in options_path:
if key not in dict_to_change:
dict_to_change[key] = {}
dict_to_change = dict_to_change[key]
dict_to_change[option_name] = value
self.assertFalse(self.validator.is_valid(options_dict))
Expand Down Expand Up @@ -95,3 +102,20 @@ def test_default_shots(self, shots):
options_path = ['options']
options_to_set = {'default_shots': shots}
self.assert_valid_options(estimator, options_to_set, options_path)

@combine(enable=[True, False, 13, "False"],
sequence_type=["XX", "XpXm", "XY4", "ZZ", 13],
extra_slack_distribution=["middle", "edges", "end", 13],
scheduling_method=["alap", "asap", "lapsap", 13]
)
def test_dynamical_decoupling(self, enable, sequence_type, extra_slack_distribution, scheduling_method):
"""Testing various values of dynamical decoupling"""
estimator = EstimatorV2(backend=self.backend)
options_path = ['options', 'dynamical_decoupling']
options_to_set = {
'enable': enable,
'sequence_type': sequence_type,
'extra_slack_distribution': extra_slack_distribution,
'scheduling_method': scheduling_method
}
self.assert_valid_options(estimator, options_to_set, options_path, estimator.options.dynamical_decoupling)

0 comments on commit 54ab15d

Please sign in to comment.