qml.ops.functions.assert_valid

assert_valid(op, **kwargs)[source]

Runs basic validation checks on an Operator to make sure it has been correctly defined.

Parameters:

op (.Operator) – an operator instance to validate

Keyword Arguments:
  • skip_deepcopy=False – If True, deepcopy tests are not run.

  • skip_pickle=False – If True, pickling tests are not run. Set to True when testing a locally defined operator, as pickle cannot handle local objects

  • skip_wire_mapping – If True, the operator will not be tested for wire mapping.

  • skip_differentiation – If True, differentiation tests are not run. Set to True when the operator is parametrized but not differentiable.

  • skip_new_decomp – If True, the operator will not be tested for its decomposition defined using the new system.

  • heuristic_resources – If True, the decomposition is not required to match exactly with the registered resource estimate.

Examples:

class MyOp(qml.operation.Operator):

    def __init__(self, data, wires):
        self.data = data
        super().__init__(wires=wires)

op = MyOp(qml.numpy.array(0.5), wires=0)
>>> assert_valid(op)
AssertionError: op.data must be a tuple
class MyOp(qml.operation.Operator):

    def __init__(self, wires):
        self.hyperparameters["unhashable_list"] = []
        super().__init__(wires=wires)

op = MyOp(wires = 0)
assert_valid(op)
ValueError: metadata output from _flatten must be hashable. This also applies to hyperparameters