mimiqcircuits.operations.control¶
Control operation.
Functions
|
Decorator that registers a named subclass of |
|
Decorator that registers a custom decomposition for a controlled gate. |
Classes
|
Gate controlled by one or more qubits. |
- class Control(num_controls=None, operation=None, *args, **kwargs)[source]¶
Bases:
GateGate controlled by one or more qubits.
Control(n, gate)appliesgateto the target qubits only when allncontrol qubits are in state \(|1\rangle\). The control qubits come first in the target list, followed by the qubits ofgate.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)equalsControl(2, GateRX(0.5)). A controlledControlis flattened:Control(1, Control(2, GateX()))isControl(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 aGateCX.The
controlmethod of a gate is the usual way to build one, for exampleGateH().control(2).- Parameters:
- Raises:
TypeError – If
operationis not a gate or gate class, or acts on classical bits or z-variables.ValueError – If
num_controlsis 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:
- Returns:
Instance of Control or a registered canonical subclass
- __init__(num_controls, operation, *args, **kwargs)[source]¶
Initialize a Control gate.
- Parameters:
- Raises:
TypeError – If operation is not a Gate object or Gate class
ValueError – If num_controls is less than 1
- inverse()[source]¶
Return the inverse, the same controls applied to the inverse gate.
- Returns:
Control(num_controls, op.inverse()).- Return type:
- getparams()[source]¶
Return the parameters of the controlled gate.
- Returns:
The value of
op.getparams().- Return type:
- 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 + ncontrols, or a lazy expression when called without arguments.- Return type:
- 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
pis the same controls applied to the gate raised top.- 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:
- 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:
- Raises:
ValueError – If more than one argument is given.
- __str__()[source]¶
Return
Cfollowed by the gate, with the number of controls as a subscript when there is more than one, for exampleC₂X.
- canonical_control(num_controls, inner_gate_type)[source]¶
Decorator that registers a named subclass of
Control.After registration,
Control(num_controls, g)withgan instance ofinner_gate_typebuilds an instance of the decorated class instead of a plainControl. This is how named gates such asGateCX(Control(1, GateX())) are defined, and it makesisinstancechecks 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:
- 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