AllowAtMostNCallsCA operation

Namespace: Microsoft.Quantum.Diagnostics

Package: Microsoft.Quantum.Standard

Between a call to this operation and its adjoint, asserts that a given operation is called at most a certain number of times.

Operation calls are considered, if they contain the the specified variant. For example, if op is X, Adjoint X or Controlled X are also counted, but if op is Controlled X, only Controlled X or Controlled Adjoint X are counted.

operation AllowAtMostNCallsCA<'TInput, 'TOutput> (nTimes : Int, op : ('TInput => 'TOutput), message : String) : Unit is Adj


nTimes : Int

The maximum number of times that op may be called.

op : 'TInput => 'TOutput

An operation whose calls are to be restricted.

message : String

A message to be displayed upon failure.

Output : Unit

Type Parameters




The following snippet will fail when executed on machines which support this diagnostic:

within {
    AllowAtMostNCallsCA(3, H, "Too many calls to H.");
} apply {
    use register = Qubit[4];
    // Fails since this calls H four times, rather than the
    // allowed maximum of three.
    ApplyToEach(H, register);

Another example illustrates how restricted calls are handled.

within {
    // Both tests will pass in this case
    AllowAtMostNCallsCA(1, Controlled H, "Too many calls to Controlled H.");
    AllowAtMostNCallsCA(2, H, "Too many calls to H or Controlled H.");
} apply {
    use (a, b) = (Qubit(), Qubit());
    Controlled H([a], b);
    ResetAll([a, b]);


This operation may be replaced by a no-op on targets which do not support it.