ApplyControlledOnInt is a Q# standard-library operation that invokes a gate on target qubits only when control qubits match an integer-encoded bit pattern. The supplied operation must define a controlled specialization. section 2.3.4
Use it by specifying:
- the integer control pattern, converted to bits using little-endian encoding; section 2.3.4
- the original operation, such as PrepOneQubit; section 2.3.4
- the control-qubit array and the operation's target and parameter arguments. section 2.3.4
Controlled-on-zero state preparation
ApplyControlledOnInt(0, PrepOneQubit, [qs[1]], (qs[0], a[0], a[2])) applies PrepOneQubit to qs[0] when qs[1] matches integer 0, implementing a controlled-on-zero branch. section 2.3.4
Listing 3.19 Q# code to implement an arbitrary CS unitary
import Std.Arrays.*;
operation ApplyArbitraryCSMatrix(
qs : Qubit[],
cs : (Double, Double)[] #1
) : Unit is Adj + Ctl {
for (k, (c, s)) in Enumerated(cs) {
let m = [[c, -s], [s, c]];
ApplyControlledOnInt(k, ApplyOneQubit,
Reversed(qs[1...]), ([qs[0]], m));
}
}
Inspect the arbitrary controlled-unitary implementation, which reverses the control-qubit array to reconcile little-endian integer patterns with Q#'s qubit-order convention. section 3.9.3
