qp.testing.assert_valid_decomp_rule

assert_valid_decomp_rule(op, rule, check_matrix=True)[source]

Check that a decomposition rule is consistent with the operator it decomposes.

The checks are skipped if the rule is not applicable to op. Otherwise, the gate counts declared by the rule’s resource function must match the operations the rule produces, and the matrix of the decomposition must match the matrix of op.

Parameters:
  • op (Operator) – the operator to decompose

  • rule (DecompositionRule) – a decomposition rule for op

  • check_matrix (bool) – If True, the matrix of the decomposition is compared with the matrix of op. Defaults to True.

Raises:

AssertionError – if the rule is not consistent with op

See also

assert_decomposable(), which runs this check for every rule registered for an operator and requires at least one of them to be applicable.

Example

def cnot_to_cz(wires):
    qp.H(wires[1])
    qp.CZ(wires)
    qp.H(wires[1])

good_rule = qp.register_resources({qp.H: 2, qp.CZ: 1})(cnot_to_cz)
bad_rule = qp.register_resources({qp.H: 1, qp.CZ: 1})(cnot_to_cz)
>>> qp.testing.assert_valid_decomp_rule(qp.CNOT([0, 1]), good_rule)
>>> qp.testing.assert_valid_decomp_rule(qp.CNOT([0, 1]), bad_rule)
Traceback (most recent call last):
    ...
AssertionError: The actual gate counts do not match the resource estimate:
Operator : Actual  !=  Resource Estimate
Hadamard :      2  !=  1