Controlled

Function Controlled 

Source
pub fn Controlled(
    expr: UnitaryExpression,
    control_radices: Radices,
    control_levels: Option<Vec<Vec<usize>>>,
) -> UnitaryExpression
Expand description

An arbitrary controlled gate.

Given any gate, ControlledGate can add control qudits.

A controlled gate adds arbitrarily controls, and is generalized for qudit or even mixed-qudit representation.

A controlled gate has a circuit structure as follows:

    controls ----/----■----
                      |
                     .-.
    targets  ----/---|G|---
                     '-'

Where $G$ is the gate being controlled.

To calculate the unitary for a controlled gate, given the unitary of the gate being controlled, we can use the following equation:

$$U_{control} = P_i \otimes I + P_c \otimes G$$

Where $P_i$ is the projection matrix for the states that don’t activate the gate, $P_c$ is the projection matrix for the states that do activate the gate, $I$ is the identity matrix of dimension equal to the gate being controlled, and $G$ is the unitary matrix of the gate being controlled.

In the simple case of a normal qubit CNOT ($G = X$), $P_i$ and $P_c$ are defined as follows:

$$ P_i = \ket{0}\bra{0} P_c = \ket{1}\bra{1} $$

This is because the $\ket{0}$ state is the state that doesn’t activate the gate, and the $\ket{1}$ state is the state that does activate the gate.

We can also decide to invert this, and have the $\ket{0}$ state activate the gate, and the $\ket{1}$ state not activate the gate. This is equivalent to swapping $P_i$ and $P_c$, and usually drawn diagrammatically as follows:

    controls ----/----□----
                      |
                     .-.
    targets  ----/---|G|---
                     '-'

When we add more controls the projection matrices become more complex, but the basic idea stays the same: we have a projection matrix for the states that activate the gate, and a projection matrix for the states that don’t activate the gate. As in the case of a toffoli gate, the projection matrices are defined as follows:

$$ P_i = \ket{00}\bra{00} + \ket{01}\bra{01} + \ket{10}\bra{10} P_c = \ket{11}\bra{11} $$

This is because the $\ket{00}$, $\ket{01}$, and $\ket{10}$ states are the states that don’t activate the gate, and the $\ket{11}$ state is the state that does activate the gate.

With qudits, we have more states and as such, more complex projection matrices; however, the basic idea is the same. For example, a qutrit controlled-not gate that is activated by the $\ket{2}$ state and not activated by the $\ket{0}$ and $\ket{1}$ states is defined as follows:

$$ P_i = \ket{0}\bra{0} + \ket{1}\bra{1} P_c = \ket{2}\bra{2} $$

One interesting concept with qudits is that we can have multiple active control levels. For example, a qutrit controlled-not gate that is activated by the $\ket{1}$ and $\ket{2}$ states and not activated by the $\ket{0}$ state is defined similarly as follows:

$$ P_i = \ket{0}\bra{0} P_c = \ket{1}\bra{1} + \ket{2}\bra{2} $$

Note that we can always define $P_i$ simply from $P_c$:

$$P_i = I_p - P_c$$

Where $I_p$ is the identity matrix of dimension equal to the dimension of the control qudits. This leaves us with out final equation:

$$U_{control} = (I_p - P_c) \otimes I + P_c \otimes G$$

If, G is a unitary-valued function of real parameters, then the gradient of the controlled gate simply discards the constant half of the equation:

$$ \frac{\partial U_{control}}{\partial \theta} = P_c \otimes \frac{\partial G}{\partial \theta} $$

§Arguments

  • expr - The gate to control.

  • control_radixes - The number of levels for each control qudit.

  • control_levels - The levels of the control qudits that activate the gate. If more than one level is selected, the subspace spanned by the levels acts as a control subspace. If all levels are selected for a given qudit, the operation is equivalent to the original gate without controls.

§Panics

  • If control_radixes and control_levels have different lengths.

  • If control_levels contains an empty level.

  • If any level in control_levels is greater than or equal to the corresponding radix in control_radixes.

  • If any level in control_levels is not unique.