mimiqcircuits.operations.control

Control operation.

Functions

canonical_control(num_controls, inner_gate_type)

Decorator to register a canonical Control subclass.

register_control_decomposition(num_controls, ...)

Decorator to register a decomposition function for a specific gate type.

Classes

Control([num_controls, operation])

Control operation that applies multi-control gates to a Circuit.

class mimiqcircuits.operations.control.Control(num_controls=None, operation=None, *args, **kwargs)[source]

Bases: Gate

Control operation that applies multi-control gates to a Circuit.

A Control is a special operation that wraps another gate with multiple control qubits.

When a canonical subclass is registered (e.g., GateCX for Control(1, GateX())), constructing Control with matching arguments will return an instance of that subclass. This enables isinstance() checks to work correctly:

Examples

>>> from mimiqcircuits import *
>>> isinstance(Control(1, GateX()), GateCX)
True
>>> c = Circuit()
>>> c.push(Control(3,GateX()),1,2,3,4)
5-qubit circuit with 1 instruction:
└── C₃X @ q[1:3], q[4]

>>> 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.

property num_targets: int

Number of target qubits.

property op: Gate

Target gate operation.

inverse()[source]

Return the inverse of this controlled operation.

Returns:

New Control operation with inverted target gate

Return type:

Control

getparams()[source]

Get parameters from the wrapped operation.

Returns:

Parameters of the wrapped operation

Return type:

Any

get_operation()[source]
control(*args)[source]

Create a new controlled operation with additional controls.

Parameters:

*args – Optional number of additional controls

Returns:

Lazy computation or new Control operation

Return type:

Union[lazy.LazyValue, Control]

Raises:

ValueError – If invalid number of arguments

power(*args)[source]

Create a new operation raised to a power.

Parameters:

*args – Optional power value

Returns:

Lazy computation or new Control operation

Return type:

Union[lazy.LazyValue, Control]

Raises:

ValueError – If invalid number of arguments

parallel(*args)[source]

Create parallel copies of this operation.

Parameters:

*args – Optional number of parallel copies

Returns:

Lazy computation or new Parallel operation

Return type:

Union[lazy.LazyValue, Parallel]

Raises:

ValueError – If invalid number of arguments

__pow__(power)[source]

Implement the power operator.

Parameters:

power (Any) – Power to raise the operation to

Returns:

New Control operation with powered target gate

Return type:

Control

iswrapper()[source]

Check if this operation is a wrapper.

Returns:

Always True for Control operations

Return type:

bool

__str__()[source]

Get string representation of the Control operation.

Returns:

String representation with subscript for number of controls

Return type:

str

evaluate(d)[source]

Evaluate the operation with given parameters.

Parameters:

d (Any) – Parameters for evaluation

Returns:

Evaluated operation with controls

Return type:

Any

gettypekey()[source]

Get a tuple that uniquely identifies this operation type.

Returns:

Type key for the operation

Return type:

Tuple

mimiqcircuits.operations.control.canonical_control(num_controls, inner_gate_type)[source]

Decorator to register a canonical Control subclass.

When Control(num_controls, inner_gate_type()) is called, it will return an instance of the decorated subclass instead.

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

  • inner_gate_type (Type) – Type of the inner gate

Returns:

Decorator that registers the class

Example

>>> from mimiqcircuits import *
>>> @canonical_control(1, GateX)
... class GateCX(Control):
...     pass
>>> isinstance(Control(1, GateX()), GateCX)
True