Introduction
Controlled operations are one of the main ways quantum programs apply operations depending on the state of quantum variables. In classical programming, anif statement chooses which instructions run based on a classical Boolean value. In quantum programming, the analogous idea is more subtle: the
condition may itself be quantum, meaning it may be in superposition.
In Qmod, controlled quantum behavior is expressed using the control statement. The statement applies a unitary operation conditionally,
depending on a quantum state, and may optionally apply a different operation if the condition does not hold. The condition can be given
either as a single qubit or qubit array, or as a quantum Boolean expression over quantum variables. When the control variable is in
superposition, the controlled operation entangles the objects used in the controlled block with the condition.
In this guide, we cover:
- Controlled operations: applying quantum operations only on states that satisfy a quantum condition.
- Else blocks: applying one operation when the condition is true and another when it is false.
- Operations controlled by expressions: using Boolean expressions over quantum variables as control conditions.
Controlled operations
A controlled operation applies a quantum operation subject to a condition satisfied by the quantum state. The simplest case is control by a single qubit. If the control qubit is in state , the operation is applied to the target. If the control qubit is in state , the operation is not applied. In Qmod, this is written withcontrol:
In the Python SDK, the controlled statement block is passed as a Python callable, commonly written using
lambda.- Allocates a control qubit
ctrland a target qubittarget. - Applies the Hadamard gate, , to
ctrl, creating the superposition . - Applies the Pauli-Y gate to
targetconditioned onctrl == 1. - The resulting state is where the first qubit is
ctrland the second istarget.
The
control statement applies quantum operations coherently. It should not be interpreted as measuring the condition and then choosing a branch.Multi-qubit control
The control variable can also be a quantum array. In that case, the controlled block is applied only when all qubits in the control array are in state .- Allocates a 3-qubit control array.
- Puts the control array into a uniform superposition over all 3-bit strings.
- Applies a Quantum Fourier Transform to
targetonly on the basis statectrl == 111.
ctrl == 111.
Else blocks
A control statement may include anelse_block. The main block is applied for states where the condition is true, and the else block is applied for states where the condition is false.
When the condition is a qubit array, the condition holds only when all qubits in the control array are in state ; otherwise, the else block applies.
- Allocates
ctrlandtarget. - Puts
ctrlin superposition. - Applies the Pauli-X gate to
targeton the branch wherectrl == 1. - Applies Hadamard gate (H) to
targeton the branch wherectrl == 0.
ctrl is in superposition, both branches are applied coherently to the corresponding parts
of the quantum state.
Example: choosing between two rotations
The next example uses an else block to apply one rotation when a control qubit is 1 and a different rotation when it is 0.
This program creates a state in which the target qubit is rotated differently depending on the value of
ctrl. Since ctrl is in superposition,
the two rotations are applied to different components of the full quantum state.
A
control statement, like other control-flow statements such as if_, repeat, foreach, and power, must preserve the initialization status
of variables declared outside its blocks. A variable declared outside the controlled block may be allocated inside the block only if it is also
released within that same block. Similarly, variables declared inside a controlled block must be uninitialized by the end of that block.Operations controlled by expressions
In Qmod, a control condition may also be a quantum logical expression over quantum variables. Expressions over quantum variables evaluate coherently over superpositions, producing correlated quantum values rather than a single classical value. Qmod supports arithmetic operators such as +, -, *, **, relational operators such as==, !=, <, <=, >, >=, and logical operators such as
logical_and/&, logical_or/|, and logical_not/ ~ for Boolean expressions.
Example: Uniformly controlled rotations
The following example applies a sequence of multi-controlled rotations- Allocates
xas a 2-qubit quantum number. - Places
xin a superposition over the values . - Applies a RX rotation with angle dependent on the numeric value of x.
The target qubit becomes entangled with
x, because only different rotations occur depending on the values of x.
Example: Arithmetic condition
Control expressions can involve arithmetic over multiple quantum variables. This is useful in search, optimization, and oracle construction, where a state should be marked or modified only if it satisfies a predicate.- Allocates two 2-qubit quantum numbers,
xandy. - Places both in uniform superposition.
- Flips
targetonly for basis states satisfyingx + y >= 5
After the controlled operation, the target qubit is entangled with the predicate
x + y >= 5. If target is later measured as 1,
the variables x and y collapse to the subspace of satisfying assignments.
This style is useful for expressing predicates directly in terms of problem variables, without manually decomposing the condition into lower-level gates.
Example: Controlled phase
A common use of controlled operations is to apply a phase only to selected states. This is central in algorithms such as Grover search, phase oracles, QAOA, and Hamiltonian-inspired constructions. Thephase statement applies a fixed or state-dependent phase shift to the quantum state. For example, phase(x2, pi / 4)
applies a phase proportional to the value of x2. When x is initialized over the values 0, 1, 2, and 3, the phase statement rotates each
basis state according to the expression value.
A controlled phase applies such a phase only under a control condition.
- Allocates two qubits.
- Places them in a uniform superposition.
- Applies a fixed phase of only when
q[0] == 1.
It is possible to apply
phase under both single-qubit and multi-qubit controls; the resulting phases accumulate on states satisfying
the respective control conditions.
Example: Phase oracle from an expression
A controlled phase can be used to mark states satisfying a Boolean expression. The following example treatsv as a selection register
over the list S. Each basis state of v represents a subset of the elements of S: if v[i] == 1, then S[i] is selected. The
controlled phase marks states that select exactly three elements whose sum is at most 16.
- Prepares
vin uniform superposition. - Applies a relative phase of to states representing three selected elements of
Swhose sum is at most 16. - Leaves all other states unchanged.
The control statement and runtime if_
Qmod provides two distinct mechanisms for conditional execution: the control statement for quantum conditions, and the if_ statement for classical conditions.
Use
control when the condition depends on quantum variables such as qubits, quantum arrays, or expressions over quantum numbers. Use if_ when the condition is a
classical Qmod expression — whether symbolic or a runtime value such as a mid-circuit measurement result. Use a Python if when the condition is a concrete Python value
known at model construction time, including Python-type parameters in generative functions.