mimiqcircuits.operations.control

Control operation.

Functions

canonical_control(num_controls, inner_gate_type)

Decorator that registers a named subclass of Control.

register_control_decomposition(num_controls, ...)

Decorator that registers a custom decomposition for a controlled gate.

Classes

Control([num_controls, operation])

Gate controlled by one or more qubits.

class Control(num_controls=None, operation=None, *args, **kwargs)[source]

Bases: Gate

Gate controlled by one or more qubits.

Control(n, gate) applies gate to the target qubits only when all n control qubits are in state \(|1\rangle\). The control qubits come first in the target list, followed by the qubits of gate.

The constructor takes the number of controls and either a gate instance or a gate class followed by its constructor arguments, so Control(2, GateRX, 0.5) equals Control(2, GateRX(0.5)). A controlled Control is flattened: Control(1, Control(2, GateX())) is Control(3, GateX()).

When a named subclass is registered for the combination (see canonical_control()), the constructor returns an instance of that subclass: Control(1, GateX()) is a GateCX.

The control method of a gate is the usual way to build one, for example GateH().control(2).

Parameters:
  • num_controls (int) – Number of control qubits, at least 1.

  • operation (Type[Gate] | Gate) – The gate to control, or a gate class.

  • *args – Constructor arguments when operation is a class.

  • **kwargs – Constructor keyword arguments when operation is a class.

Raises:
  • TypeError – If operation is not a gate or gate class, or acts on classical bits or z-variables.

  • ValueError – If num_controls is less than 1.

Examples

>>> from mimiqcircuits import *
>>> # the generic form returns the named gate when one exists
>>> isinstance(Control(1, GateX()), GateCX)
True
>>> # a Toffoli-like gate with three controls: q[1:3] control, q[4] is the target
>>> c = Circuit()
>>> c.push(Control(3,GateX()),1,2,3,4)
5-qubit circuit with 1 instruction:
└── C₃X @ q[1:3], q[4]

>>> # the matrix is the identity except for the last block, which is X
>>> Control(2, GateX()).matrix()
[1.0, 0, 0, 0, 0, 0, 0, 0]
[0, 1.0, 0, 0, 0, 0, 0, 0]
[0, 0, 1.0, 0, 0, 0, 0, 0]
[0, 0, 0, 1.0, 0, 0, 0, 0]
[0, 0, 0, 0, 1.0, 0, 0, 0]
[0, 0, 0, 0, 0, 1.0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1.0]
[0, 0, 0, 0, 0, 0, 1.0, 0]
static __new__(cls, num_controls=None, operation=None, *args, **kwargs)[source]

Create a Control instance, returning canonical subclass if registered.

If a canonical subclass is registered for (num_controls, type(operation)), an instance of that subclass is returned instead of a plain Control.

Parameters:
  • num_controls (int) – Number of control qubits

  • operation (Type[Gate] | Gate) – Gate operation to control or gate class to instantiate

  • *args – Arguments to pass to operation constructor if a class is provided

  • **kwargs – Keyword arguments to pass to operation constructor

Returns:

Instance of Control or a registered canonical subclass

__init__(num_controls, operation, *args, **kwargs)[source]

Initialize a Control gate.

Parameters:
  • num_controls (int) – Number of control qubits

  • operation (Type[Gate] | Gate) – Gate operation to control or gate class to instantiate

  • *args – Arguments to pass to operation constructor if a class is provided

  • **kwargs – Keyword arguments to pass to operation constructor if a class is provided

Raises:
  • TypeError – If operation is not a Gate object or Gate class

  • ValueError – If num_controls is less than 1

property num_controls: int

Number of control qubits. Read-only.

property num_targets: int

Number of target qubits, that is the qubits of op.

property op: Gate

The controlled gate. Read-only.

inverse()[source]

Return the inverse, the same controls applied to the inverse gate.

Returns:

Control(num_controls, op.inverse()).

Return type:

Control

getparams()[source]

Return the parameters of the controlled gate.

Returns:

The value of op.getparams().

Return type:

list

get_operation()[source]

Return the controlled gate, op.

control(*args)[source]

Add more control qubits.

Parameters:

*args – The number of extra controls. When omitted, a lazy expression is returned that takes the number later.

Returns:

The same gate with num_controls + n controls, or a lazy expression when called without arguments.

Return type:

Control

Raises:

ValueError – If more than one argument is given.

Examples

>>> from mimiqcircuits import *
>>> Control(2, GateH()).control(1)
C₃H
power(*args)[source]

Raise the controlled gate to a power.

A controlled gate raised to p is the same controls applied to the gate raised to p.

Parameters:

*args – The exponent. When omitted, a lazy expression is returned that takes the exponent later.

Returns:

Control(num_controls, op.power(p)), or a lazy expression when called without arguments.

Return type:

Control

Raises:

ValueError – If more than one argument is given.

Examples

>>> from mimiqcircuits import *
>>> Control(2, GateX()).power(0.5)
C₂SX
parallel(*args)[source]

Return copies of this gate applied in parallel on separate qubits.

Parameters:

*args – The number of copies. When omitted, a lazy expression is returned that takes the number later.

Returns:

Parallel(n, self), or a lazy expression when called without arguments.

Return type:

Parallel

Raises:

ValueError – If more than one argument is given.

__pow__(power)[source]

Return self.power(power).

iswrapper()[source]

Return True: a Control wraps another gate.

__str__()[source]

Return C followed by the gate, with the number of controls as a subscript when there is more than one, for example C₂X.

evaluate(d)[source]

Substitute values for the symbols in the controlled gate.

Parameters:

d (Any) – Map from symbols (or their names) to values.

Returns:

The controlled gate with the symbols substituted, or self when it has no symbolic parameters.

Return type:

Control

gettypekey()[source]

Return the key used to look up registered decompositions.

Returns:

(Control, num_controls, op.gettypekey()).

Return type:

tuple

canonical_control(num_controls, inner_gate_type)[source]

Decorator that registers a named subclass of Control.

After registration, Control(num_controls, g) with g an instance of inner_gate_type builds an instance of the decorated class instead of a plain Control. This is how named gates such as GateCX (Control(1, GateX())) are defined, and it makes isinstance checks work whichever way the gate was built.

Registering a class for a pair that already has one replaces it for the rest of the session.

Parameters:
  • num_controls (int) – Number of control qubits.

  • inner_gate_type (Type) – Class of the target gate.

Returns:

The decorator. It registers the class and returns it unchanged.

Examples

>>> from mimiqcircuits import *
>>> # GateCX is registered for one control on GateX, so building
>>> # the generic form gives a GateCX
>>> isinstance(Control(1, GateX()), GateCX)
True