Skip to content

Control

The control statement applies a unitary operation conditionally, depending on a quantum state. The unitary operation is specified as a nested statement block. The objects used in the statement block become entangled with the object used in the condition, so that any superposition in the state of the condition carries over to the operations in the statement block. The control statement could be viewed as the quantum equivalent of the classical if statement.

The condition can be specified in one of two forms: as a single quantum variable, and as a quantum logical expression.

Syntax

control ( ctrl-var ) { statements }

control ( ctrl-expression ) { statements }

@overload
def control(
    ctrl: Union[QBit, QArray[QBit]], operand: Union[QCallable, Callable[[], None]]
) -> None:
    pass


@overload
def control(ctrl: SymbolicExpr, operand: Union[QCallable, Callable[[], None]]) -> None:
    pass

Semantics

  • ctrl-var (in the first variant) is a quantum variable of type qbit or qbit[], and ctrl-expression (in the second variant) is a logical expression over a quantum variable.
  • The statement block is applied if all the qubits in ctrl are in state \(|1\rangle\) (in the
  • first variant), or if the ctrl-expression evaluates to true (in the second variant).
  • Currently, ctrl-expression is restricted to the form <var> == <classical-expression> where <var> is of type qbit or qnum, and <classical-expression> evaluates to a classical integer.
  • The same variable cannot occur both in the condition and inside the statement block. This restriction applies also to mutually exclusive slices of the same variable.
  • Quantum variables declared outside the control statement and used in its condition or inside its nested block must be initialized prior to it and remain initialized subsequently.

Examples

Example 1: Single-qubit control

In the following example, control statement applies function X on the variable target conditioned on a single qubit variable qb. Note that this is equivalent to using the built-in gate-level function CX.

qfunc main(output target: qbit) {
  qb: qbit;
  allocate<1>(qb);
  H(qb);
  allocate<1>(target);
  control (qb) {
    X(target);
  }
}
from classiq import H, Output, QBit, X, allocate, control, qfunc


@qfunc
def main(target: Output[QBit]) -> None:
    qb = QBit("qb")
    allocate(1, qb)
    H(qb)
    allocate(1, target)
    control(qb, lambda: X(target))

Synthesizing this model creates the quantum program shown below.

control_single_qubit.png

Example 2: Multi-qubit control

The next example shows how control can be similarly used with multi-qubit control variable. In this case target is rotated when the state of qba is the bit string 111.

qfunc main(output target: qbit) {
  qba: qbit[];
  allocate<3>(qba);
  hadamard_transform(qba);
  allocate<1>(target);
  control (qba) {
    RX<pi / 2>(target);
  }
}
from sympy import pi
from classiq import (
    Output,
    QBit,
    QArray,
    allocate,
    control,
    qfunc,
    hadamard_transform,
    RX,
)


@qfunc
def main(target: Output[QBit]) -> None:
    qba = QArray("qba")
    allocate(3, qba)
    hadamard_transform(qba)
    allocate(1, target)
    control(qba, lambda: RX(pi / 2, target))

Synthesizing this model creates the quantum program shown below.

control_multi_qubit.png

Example 3: Numeric equality condition

The following example demonstrates the use of control to rotate the state of a qubit by an angle determined by another quantum variable. In this case the condition compares the quantum variable with the repeat index.

qfunc switch_rx(x: qnum, target: qbit) {
  repeat (i: 4) {
    control (x == i) {
      RX<pi / (2 ** i)>(target);
    }
  }
}

qfunc main(output res: qbit) {
  allocate<1>(res);
  x: qnum;
  prepare_int<2>(x);
  switch_rx(x, res);
}
from classiq import (
    qfunc,
    QNum,
    allocate,
    control,
    QBit,
    repeat,
    RX,
    Output,
    prepare_int,
)
from classiq.qmod.symbolic import pi


@qfunc
def switch_rx(x: QNum, target: QBit) -> None:
    repeat(4, lambda i: control(x == i, lambda: RX(pi / 2**i, target)))


@qfunc
def main(res: Output[QBit]):
    allocate(1, res)
    x = QNum("x")
    prepare_int(2, x)
    switch_rx(x, res)

Synthesizing this model creates the following quantum program. Note how control is implemented as positive and negative controls in the respective numeric qubits.

control_value.png