liveBook · Manning

Control pattern

As explained in Quantum Programming in Depth

A control pattern is a bit string or equivalent encoding specifying the basis values required on control qubits.

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

How Quantum Programming in Depth covers it

How deep it goes
Explains binary encoding, endian reconciliation, and matrix behavior, then demonstrates implementation in Q# without developing broader control-flow theory.
Examples
Uses controlled rotations, integer pattern zero, two-qubit state preparation, arbitrary controlled unitaries, and Q# state dumps.
What it assumes
Assumes quantum basis states, controlled gates, matrix indices, Q# operations, and little-endian and big-endian notation.
What you can do after
Apply it via Q# operations that trigger target gates for selected control bit strings or integer encodings.
How it connects
Connects controlled gates, controlled-on-zero operations, little-endian encoding, big-endian notation, and quantum state preparation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quantum Programming in Depth. The section links point to the parts of the book it draws on.