liveBook · Manning

Conjugation

As explained in Quantum Programming in Depth

Conjugation, or the Q# within-apply construct, performs a computation, applies an operation, and then automatically applies the computation's adjoint to uncompute intermediate state.

Conjugation, expressed by Q#’s within-apply construct, computes intermediate results, applies a final operation, and automatically applies the within block’s adjoint to uncompute those results. section 6.5.3

Use the construct to organize a reversible quantum computation:

  • Compute intermediate values inside within. section 6.5.3
  • Compute the desired result inside apply. section 6.5.3
  • Let Q# uncompute the intermediates by applying the within code’s adjoint. section 6.5.3
Listing 6.9 Q#: Evaluating a Boolean expression as a quantum operation
import Std.Arrays.*;

operation EvaluateClause(  #1
  x : Qubit[], 
  y : Qubit, 
  literals : (Int, Bool)[]
) : Unit is Adj + Ctl {
  let controlQubits = 
    Mapped((ind, _) -> x[ind], literals);  #2
                              
  let controlPattern = 
    Mapped((_, pos) -> pos, literals);  #3
  within {
    ApplyPauliFromBitString(PauliX, false,  #4
      controlPattern, controlQubits);
  } apply {
    MultiOr(controlQubits, y);  #5
  }                        #6
}

operation EvaluateExpression(  #7
  x : Qubit[], 
  y : Qubit, 
  expression : (Int, Bool)[][]
) : Unit is Adj + Ctl {
  let nClauses = Length(expression);
  use clauseResults = Qubit[nClauses];  #8
  within {
    for (clause, result) in 
      Zipped(expression, clauseResults) {  #9
      EvaluateClause(x, result, clause);
    }
  } apply {
    MultiAnd(clauseResults, y);  #10
  }               #11
}

EvaluateClause flips selected controls within, applies MultiOr, and leaves the enclosing EvaluateExpression operation to combine clause results with MultiAnd. section 6.5.3

N queens marking oracle

A marking oracle allocates qubits for column and row-pair constraints, evaluates them within, computes the oracle value in apply, and automatically uncomputes the constraint results. Tests check the oracle on search-space basis states. section 8.2.3

How Quantum Programming in Depth covers it

How deep it goes
Defines the three-stage control flow and demonstrates auxiliary-qubit allocation, but leaves adjoint gate construction implicit.
Examples
Demonstrates Boolean-expression evaluation and an N queens marking oracle with constraint-validation qubits.
What it assumes
Assumes familiarity with Q# operations, adjoints, allocated qubits, and quantum oracle testing.
What you can do after
Apply it via Q# to express compute-apply-uncompute logic without manually duplicating uncomputation code.
How it connects
Connects adjoints, uncomputation, auxiliary qubits, Boolean oracles, and Grover’s algorithm.

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.