qml.ops.functions.assert_valid

assert_valid(op, *, skip_deepcopy=False, skip_differentiation=False, skip_new_decomp=False, skip_pickle=False, skip_wire_mapping=False, skip_capture=False)[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_differentiation=False – If True, differentiation tests are not run.

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

  • 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.

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)
Traceback (most recent call last):
    ...
AssertionError: MyOp._unflatten must be able to reproduce the original operation from () and (Wires([0]), ()). You may need to override either the _unflatten or _flatten method.
For local testing, try type(op)._unflatten(*op._flatten())
class MyOp(qml.operation.Operator):

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

op = MyOp(wires = 0)
>>> assert_valid(op)
Traceback (most recent call last):
    ...
AssertionError: metadata output from _flatten must be hashable. Got metadata (Wires([0]), (('unhashable_list', []),))