A control pattern is a bit string, or equivalent integer encoding, that specifies the computational-basis values required on control qubits before a target operation is applied. section 2.3.4
Use control patterns to connect mathematical gate descriptions with implementation details:
- Represent the pattern as an integer in little-endian notation; for a controlled rotation, the binary representation of k identifies the n−1 control values. section 3.9.1
- Use 0 for a controlled-on-zero operation; Q# converts the integer into a bit string or accepts the bit string directly. section 2.3.4
- Reconcile register conventions explicitly: Q# stores the most significant bit first, while ApplyControlledOnInt interprets integers little-endian. section 3.9.3
Applying a patterned control in Q#
ApplyControlledOnInt(0, PrepOneQubit, [qs[1]], (qs[0], a[0], a[2])) applies PrepOneQubit when the control integer 0 matches the control qubit's basis value. For a general controlled unitary, iterating over k applies each target matrix under its corresponding control pattern. section 2.3.4 section 3.9.3
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 Q# implementation of an arbitrary controlled single-qubit unitary, including pattern iteration and qubit-order reversal. section 3.9.3
