liveBook · Manning

ApplyControlledOnInt

As explained in Quantum Programming in Depth

ApplyControlledOnInt is a Q# standard-library operation that applies a gate when control qubits match an integer-encoded bit pattern.

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

How Quantum Programming in Depth covers it

How deep it goes
Explains invocation and endian-order handling, but assumes the original operation already provides a controlled specialization.
Examples
Uses controlled-on-zero PrepOneQubit preparation and an arbitrary controlled-unitary loop indexed by integer patterns.
What it assumes
Assumes Q# operations, control qubits, and controlled specializations; demonstrates neither those fundamentals nor operation construction from zero.
What you can do after
Apply it via Q# to select operation branches using integer-encoded control states.
How it connects
Connects integer control patterns with ApplyControlledOnBitString, controlled specializations, state preparation, and controlled unitaries.

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.