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
