qcodes.utils.validators

Provides validators for different types of values. Validator validates if the value belongs to the given type and is in the provided range.

class qcodes.utils.validators.Anything[source]

Bases: qcodes.utils.validators.Validator[Any]

Allow any value to pass.

validate(value: Any, context: str = '') None[source]
is_numeric = True
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Arrays(min_value: Optional[Union[float, int, numpy.floating, numpy.integer]] = None, max_value: Optional[Union[float, int, numpy.floating, numpy.integer]] = None, shape: Optional[Sequence[Union[int, Callable[[], int]]]] = None, valid_types: Optional[Sequence[type]] = None)[source]

Bases: qcodes.utils.validators.Validator[numpy.ndarray]

Validator for numerical numpy arrays of numeric types (int, float, complex). By default it validates int and float arrays.

Min and max validation is not supported for complex numbers.

Parameters
  • min_value – Min value allowed, default None for which min value check is not performed

  • max_value – Max value allowed, default None for which max value check is not performed

  • shape – The shape of the array, tuple of either ints or Callables taking no arguments that return the size along that dim as an int.

  • valid_types – Sequence of types that the validator should support. Should be a subset of the supported types, or None. If None, all real datatypes will validate.

Raises

TypeError – If value of arrays are not supported.

property valid_values: Tuple[numpy.ndarray]
property shape_unevaluated: Optional[Tuple[Union[int, Callable[[], int]], ...]]
property shape: Optional[Tuple[int, ...]]
validate(value: numpy.ndarray, context: str = '') None[source]
is_numeric = True
class qcodes.utils.validators.Bool[source]

Bases: qcodes.utils.validators.Validator[bool]

Requires a boolean.

validate(value: bool, context: str = '') None[source]

Validates if bool else raises error.

Parameters
  • value – Bool

  • context – Context for validation.

Raises

TypeError – IF not a boolean.

is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Callable[source]

Bases: qcodes.utils.validators.Validator[Callable[[…], Any]]

Validator for callables such as functions.

validate(value: Callable[[...], Any], context: str = '') None[source]

Validates if callable else raise typeerror.

Parameters
  • value – Value to validate.

  • context – Context for validation.

Raises

TypeError – If not a callable.

is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.ComplexNumbers[source]

Bases: qcodes.utils.validators.Validator[Union[complex, np.complexfloating[Any,Any]]]

A validator for complex numbers.

validtypes = (<class 'numpy.complex64'>, <class 'numpy.complex128'>, <class 'numpy.complex128'>, <class 'complex'>)
validate(value: Union[complex, np.complexfloating[Any, Any]], context: str = '') None[source]

Validates if complex number else raises error.

Parameters
  • value – A complex number.

  • context – Context for validation.

Raises

TypeError – If not a complex number.

is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Dict(allowed_keys: Optional[Sequence[Hashable]] = None)[source]

Bases: qcodes.utils.validators.Validator[Dict[Hashable, Any]]

Validator for dictionaries.

Validator for dictionary keys

Parameters

allowed_keys – if set, all keys must be in allowed_keys

is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
validate(value: Dict[Hashable, Any], context: str = '') None[source]

Validates dictionary keys else raise typeerror.

Parameters
  • value – Dictionary.

  • context – Context for validation.

Raises
class qcodes.utils.validators.Enum(*values: Optional[Hashable])[source]

Bases: qcodes.utils.validators.Validator[Hashable]

Requires one of a provided set of values. eg. Enum(val1, val2, val3)

Raises

TypeError – If no value provided

validate(value: Hashable, context: str = '') None[source]
is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Ints(min_value: Union[int, numpy.integer] = - 1000000000000000000, max_value: Union[int, numpy.integer] = 1000000000000000000)[source]

Bases: qcodes.utils.validators.Validator[Union[int, np.integer[Any]]]

Requires an integer. Optional parameters min_value and max_value, enforce min_value <= value <= max_value.

Parameters
  • max_value – value must be <= max_value

  • min_value – value must be >= min_value

Raises

TypeError – If min_value and max_value is not an integer. Or min_value is larger than the min_value.

validtypes = (<class 'int'>, <class 'numpy.integer'>)
inttypes

alias of Union[int, numpy.integer]

validate(value: Union[int, numpy.integer], context: str = '') None[source]

Validates if int else raises error.

Parameters
  • value – An integer.

  • context – Context for validation.

Raises
is_numeric = True
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Lists(elt_validator: qcodes.utils.validators.Validator[Any] = <Anything>)[source]

Bases: qcodes.utils.validators.Validator[List[Any]]

Validator for lists

Parameters

elt_validator – Used to validate the individual elements of the list.

validate(value: List[qcodes.utils.validators.Anything], context: str = '') None[source]

Validate if list else raises error.

Parameters
  • value – A list.

  • context – Context for validation.

Raises

TypeError – If not list.

is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.MultiType(*validators: qcodes.utils.validators.Validator[Any])[source]

Bases: qcodes.utils.validators.Validator[Any]

Allow the union of several different validators. For example, to allow numbers as well as “off”: MultiType(Numbers(), Enum(“off”)) The resulting validator acts as a logical OR between the different validators.

Raises

TypeError – If no validators provided. Or if any of the provided argument is not a valid validator.

is_numeric = False
validate(value: Any, context: str = '') None[source]
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Multiples(divisor: int = 1, **kwargs: Any)[source]

Bases: qcodes.utils.validators.Validator[Union[int, np.integer[Any]]]

A validator that checks if a value is an integer multiple of a fixed divisor. This class extends validators.Ints such that the value is also checked for being integer between an optional min_value and max_value. Furthermore this validator checks that the value is an integer multiple of an fixed, integer divisor. (i.e. value % divisor == 0)

Parameters
  • divisor – the value need the be a multiple of this divisor

  • max_value – value must be <= max_value

  • min_value – value must be >= min_value

validate(value: Union[int, np.integer[Any]], context: str = '') None[source]

Validates if the value is a integer multiple of divisor else raises error.

Parameters
  • value – An integer.

  • context – Context for validation.

Raises

ValueError – If not a multiple of a divisor.

is_numeric = True
inttypes

alias of Union[int, numpy.integer]

property valid_values: Tuple[qcodes.utils.validators.T, ...]
validtypes = (<class 'int'>, <class 'numpy.integer'>)
class qcodes.utils.validators.Nothing(reason: str)[source]

Bases: qcodes.utils.validators.Validator[Any]

Allow no value to pass.

validate(value: Any, context: str = '') None[source]
is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Numbers(min_value: Union[float, int, numpy.floating, numpy.integer] = - inf, max_value: Union[float, int, numpy.floating, numpy.integer] = inf)[source]

Bases: qcodes.utils.validators.Validator[Union[float, int, numpy.floating, numpy.integer]]

Requires a number of type int, float, numpy.integer or numpy.floating.

Parameters
  • min_value – Minimal value allowed, default -inf.

  • max_value – Maximal value allowed, default inf.

Raises

TypeError – If min or max value not a number. Or if min_value is larger than the max_value.

validtypes = (<class 'float'>, <class 'int'>, <class 'numpy.integer'>, <class 'numpy.floating'>)
validate(value: Union[float, int, numpy.floating, numpy.integer], context: str = '') None[source]

Validate if number else raises error.

Parameters
  • value – A number.

  • context – Context for validation.

Raises
  • TypeError – If not int or float.

  • ValueError – If number is not between the min and the max value.

is_numeric = True
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.OnOff[source]

Bases: qcodes.utils.validators.Validator[str]

Requires either the string ‘on’ or ‘off’.

validate(value: str, context: str = '') None[source]
is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.PermissiveInts(min_value: Union[int, numpy.integer] = - 1000000000000000000, max_value: Union[int, numpy.integer] = 1000000000000000000)[source]

Bases: qcodes.utils.validators.Validator[Union[int, np.integer[Any]]]

Requires an integer or a float close to an integer optional parameters min_value and max_value enforce min_value <= value <= max_value. Note that you probably always want to use this with a set_parser that converts the float repr to an actual int.

validate(value: Union[float, int, numpy.floating, numpy.integer], context: str = '') None[source]

Validates if int or close to int (remainder to the rounded value is less than 1e-5) else raises error.

Parameters
  • value – Integer or close to integer.

  • context – Context for validation.

Raises

TypeError – If not an int or close to it.

inttypes

alias of Union[int, numpy.integer]

is_numeric = True
property valid_values: Tuple[qcodes.utils.validators.T, ...]
validtypes = (<class 'int'>, <class 'numpy.integer'>)
class qcodes.utils.validators.PermissiveMultiples(divisor: Union[float, int, numpy.floating, numpy.integer], precision: float = 1e-09)[source]

Bases: qcodes.utils.validators.Validator[Union[float, int, numpy.floating, numpy.integer]]

A validator that checks whether a value is an integer multiple of a fixed divisor (to within some precision). If both value and divisor are integers, the (exact) Multiples validator is used.

We also allow negative values, meaning that zero by construction is always a valid value.

Parameters
  • divisor – The number that the validated value should be an integer multiple of.

  • precision – The maximally allowed absolute error between the value and the nearest true multiple.

Raises

ValueError – If divisor is zero.

validate(value: Union[float, int, numpy.floating, numpy.integer], context: str = '') None[source]

Validate the given value. Note that this validator does not use context for anything.

Raises

ValueError – If value is not the multiple of divisor.

is_numeric = True
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Sequence(elt_validator: qcodes.utils.validators.Validator[Any] = <Anything>, length: Optional[int] = None, require_sorted: bool = False)[source]

Bases: qcodes.utils.validators.Validator[Sequence[Any]]

Validator for Sequences.

Parameters
  • elt_validator – Used to validate the individual elements of the Sequence.

  • length – Length of sequence.

  • require_sorted – True or False.

validate(value: Sequence[Any], context: str = '') None[source]

Validates if sequence else raise typeerror.

Parameters
  • value – A sequence.

  • context – Context for validation.

Raises
is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Strings(min_length: int = 0, max_length: int = 1000000000)[source]

Bases: qcodes.utils.validators.Validator[str]

Requires a string optional parameters min_length and max_length limit the allowed length to min_length <= len(value) <= max_length

Raises

TypeError – If min_length or max_length negative. Or max_length lower than min_length.

validate(value: str, context: str = '') None[source]

Validates if string else raises error.

Parameters
  • value – A string.

  • context – Context for validation.

Raises
  • TypeError – If not a string.

  • ValueError – If length is not between min_length and max_length.

is_numeric = False
property valid_values: Tuple[qcodes.utils.validators.T, ...]
class qcodes.utils.validators.Validator(*args, **kwds)[source]

Bases: Generic[qcodes.utils.validators.T]

Base class for all value validators each validator should implement:

__init__:

Here a private attribute, _valid_values, should be set. _valid_values must be a tuple of at least one valid value. If possible, it should include all valid values. The purpose of this attribute is to make it possible to find a valid value for a Parameter, given its validator.

validate:

Function of two args: value, context value is what you’re testing. context is a string identifying the caller better.

Raises an error (TypeError or ValueError) if the value fails.

is_numeric:

A boolean flag that marks if this a numeric type.

The base class implements,

valid_values:

A property exposing _valid_values, which is a tuple of examples of valid values. For very simple validators, like Bool or Enum, the tuple contains all valid values, but in general it just holds SOME valid values. These example values are intended to be useful when simulating instruments.

Alternatively you may override _valid_values and provide your own implementation of getting valid values.

is_numeric = False
validate(value: qcodes.utils.validators.T, context: str = '') None[source]
property valid_values: Tuple[qcodes.utils.validators.T, ...]
qcodes.utils.validators.range_str(min_val: Optional[Union[float, np.floating[Any], np.integer[Any]]], max_val: Optional[Union[float, np.floating[Any], np.integer[Any]]], name: str) str[source]

Utility to represent ranges in Validator repr’s.

qcodes.utils.validators.validate_all(*args: Tuple[qcodes.utils.validators.Validator[Any], Any], context: str = '') None[source]

Takes a list of (validator, value) couplets and tests whether they are all valid, raising ValueError otherwise.

Parameters

context – keyword-only arg with a string to include in the error message giving the user context for the error.