Q sharp and operation functors
A quantum programming language with operations and structured ways to request adjoint and controlled specializations.
Also called Q#
On this page
Definition and significance
Q# separates functions from quantum operations and supports adjoint and controlled specializations when an operation provides them. These language features express mathematical structure that algorithms repeatedly need. They do not imply that the resulting hardware implementation has negligible cost.
Mechanism and mathematical treatment
For a unitary U, the adjoint specialization implements U dagger. The controlled specialization applies U only in the selected control sector. For supported operations the two constructions commute: the controlled adjoint equals the adjoint of the controlled operation. A measurement or irreversible reset cannot be treated as an ordinary invertible unitary operation.
Fully worked reference example
Take U=R_Z(theta). Its adjoint is R_Z(-theta), and U dagger U=I exactly in the ideal model. For a control in |+> and target |1>, controlled-U produces (|0>+exp(i theta/2)|1>)/sqrt(2) on the control, retaining the target. Replacing U by a phase-equivalent gate can change this relative phase, so a compiler must respect controlled-context semantics.
Limitations and common misconceptions
A callable's supported specializations must be checked. A high-level adjoint construct does not reverse discarded measurement outcomes or undo environmental noise. Publish versioned code only after verifying the current language/toolchain.
Scientific relationships
An adjoint specialization can reverse a suitable unitary operation.
Adjoint order for a composite operation
Define U=R_Z(alpha)R_Y(beta), meaning that R_Y(beta) acts first. Its adjoint is U dagger=R_Y(-beta)R_Z(-alpha), so the inverse circuit first applies R_Z(-alpha) and then R_Y(-beta). The order reversal is part of the adjoint, not a formatting convention. Applying inverse angles in the original chronological order generally leaves a nontrivial commutator.
For alpha=pi/2 and beta=pi/3, U|0>=sqrt(3)exp(-i pi/4)|0>/2+exp(i pi/4)|1>/2. The first inverse step removes those Z phases, returning sqrt(3)|0>/2+|1>/2. The second inverse step R_Y(-pi/3) returns |0>. This complete state trace verifies the composite adjoint without relying on automatic language generation.
An operation specialization can provide this mathematical inverse only when its supported body and implementation satisfy the required contract. A subroutine that measures a qubit and discards its outcome does not have a unitary inverse on the visible registers. Naming an adjoint specialization cannot recreate information that the subroutine erased.
Controlled and adjoint constructions commute algebraically
For one control, C(U)=|0><0| tensor I+|1><1| tensor U. Taking its adjoint gives |0><0| tensor I+|1><1| tensor U dagger, which is C(U dagger). Thus controlled-adjoint and adjoint-controlled have the same mathematical operation. The official Q# functor documentation describes access to the corresponding specialization when it is provided.
For two operations U and V on the same target, C(UV)=C(U)C(V), with the usual rightmost-first multiplication. The identity follows because the control projectors are orthogonal and idempotent. It allows a controlled composite operation to be assembled from controlled constituents, but its resource cost can exceed that of a specialized whole-operation construction.
Multiple controls select the subspace in which every control is one under the standard positive-control convention. A desired bit pattern can be converted to that condition by X gates on the zero-valued control positions before and after the controlled operation. For pattern 010, apply X to the first and third controls, apply the all-one controlled operation, then undo those X gates. This restores the control labels while applying U only on the intended original pattern.
Compute, act and uncompute can reduce control overhead
Suppose a subroutine has the structure A dagger B A, with A a reversible preparation and B the central action. Its controlled version can often be implemented by applying A unconditionally, applying controlled-B, and then applying A dagger unconditionally. On the control-zero branch, the central action is identity and A dagger A cancels. On the control-one branch, the desired A dagger B A remains.
This identity can avoid controlling a complicated preparation and its inverse. It requires that the same A act on both branches and that the surrounding registers satisfy its reversible workspace contract. If B changes information needed to undo A in a way not included in the intended composite, or if A contains irreversible measurements, the reasoning does not apply as stated.
As a concrete one-qubit example, choose A=H and B=Z. The composite is HZH=X. A controlled-X can be implemented with H on the target, controlled-Z, and H on the target. The two Hadamards need no control. The identity is exact, including its phase, and illustrates the general conjugation pattern in the smallest possible setting.
Phase, types and execution scope
An uncontrolled operation U and exp(i chi)U produce the same density-matrix transformation on their target. Their controlled versions differ by a phase exp(i chi) on the control-one branch. Therefore a specialization used under coherent control must preserve the scalar phase specified by its operation semantics. A compiler transformation certified only up to global phase may need a correction before it is used in that context.
The controlled form also changes the callable's argument structure by adding control qubits. This is a programming interface distinction, while the equation for C(U) supplies the mathematical meaning. A type indicating support for adjoint or controlled use is a claim about available specializations, not a proof of low gate count, fault tolerance or physical implementation quality.
The executable NumPy reference below verifies the state trace, order-reversed adjoint, controlled blocks, and conjugation identity as explicit matrices. It supplies the actual numerical results under a pinned Python and NumPy contract. It does not instantiate a Q# compiler or generated specialization; those software mechanisms remain separate from the verified mathematical identities.
Executed reference: adjoint, control, and conjugation
For alpha = pi/2 and beta = pi/3, the forward state prints [0.61237244-0.61237244j, 0.35355339+0.35355339j]. Applying the constructed adjoint returns [1,0] within floating-point roundoff. The two residual norms compare U dagger U with identity and controlled-U dagger with controlled-U-dagger; both are below 10 to the minus 14. The last residual compares a target-H, controlled-Z, target-H conjugation with controlled-X and is also below that tolerance.
The block-diagonal control construction acts on a leftmost control and a rightmost target. Its inactive block remains identity, which is essential when testing controlled global phases. The code reverses multiplication order when forming the adjoint and checks the algebra on entire matrices, rather than a single sampled input. These locally executed calculations establish the semantics used to explain Q# functors. They do not assert a Q# toolchain version, generated specialization, or compiler execution.
import numpy as np
np.set_printoptions(precision=8, suppress=True)
I = np.eye(2, dtype=complex)
Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
Z = np.diag([1, -1])
H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
def rotation(P, angle):
return np.cos(angle / 2) * I - 1j * np.sin(angle / 2) * P
def controlled(U):
zeros = np.zeros((2, 2), dtype=complex)
return np.block([[I, zeros], [zeros, U]])
alpha, beta = np.pi / 2, np.pi / 3
U = rotation(Z, alpha) @ rotation(Y, beta)
adjoint = rotation(Y, -beta) @ rotation(Z, -alpha)
v = U @ np.array([1, 0])
CX = np.array([[1, 0, 0, 0], [0, 1, 0, 0],
[0, 0, 0, 1], [0, 0, 1, 0]])
conjugated = np.kron(I, H) @ controlled(Z) @ np.kron(I, H)
residuals = [np.linalg.norm(adjoint @ U - I),
np.linalg.norm(controlled(U).conj().T - controlled(adjoint)),
np.linalg.norm(conjugated - CX)]
print(v)
print(adjoint @ v)
print(np.array(residuals))
assert max(residuals) < 1e-14
Sources
Verification scope
Recorded source check ·
Official functor semantics readable despite authorization banner, 91 lines; adjoint and controlled specializations inspected.
Formal or reference-semantic source anchor only; no SDK execution, hardware run, or empirical advantage is asserted. Original worked examples are independent mathematical constructions.