mimiqcircuits.operations.power¶
Power operation.
Functions
|
Decorator that registers a named subclass of |
|
Register a display name for a gate raised to a given exponent. |
|
Decorator that registers a custom decomposition for a gate power. |
Classes
|
Gate raised to a real power. |
- class Power(operation=None, exponent=None, *args, **kwargs)[source]¶
Bases:
GateGate raised to a real power.
Power(gate, p)is the unitary \(U^p\), where \(U\) is the matrix ofgate. For non-integerpthe principal matrix power is used.The constructor takes the gate (or a gate class followed by its constructor arguments) and the exponent. The exponent must be a real number: a float that is a simple fraction (denominator up to 1000) is stored as a
Fractionorint, soPower(g, 0.5)andPower(g, Fraction(1, 2))are the same operation. A power of aPoweris folded into one:Power(Power(g, a), b)isPower(g, a * b).When a named subclass is registered for the combination (see
canonical_power()), the constructor returns an instance of that subclass:Power(GateZ(), 0.5)is aGateS. A folded power stays a plainPower, even when a subclass is registered for the folded exponent.The
powermethod of a gate, or the**operator, is the usual way to build one; it may simplify the result further.- Parameters:
- Raises:
ValueError – If
operationis not a gate or gate class.TypeError – If
exponentis not a real number, for example a symbol or a complex number.
Examples
>>> from mimiqcircuits import * >>> # the generic form returns the named gate when one exists >>> isinstance(Power(GateZ(), 0.5), GateS) True >>> c = Circuit() >>> # the square root of X prints with its own name, SX >>> c.push(Power(GateX(), 1/2), 1) 2-qubit circuit with 1 instruction: └── SX @ q[1] >>> c.push(Power(GateX(), 5), 1) 2-qubit circuit with 2 instructions: ├── SX @ q[1] └── X**5 @ q[1] >>> # an integer power decomposes into repeated applications >>> Power(GateX(), 3).decompose() 1-qubit circuit with 3 instructions: ├── X @ q[0] ├── X @ q[0] └── X @ q[0]
- static __new__(cls, operation=None, exponent=None, *args, **kwargs)[source]¶
Create a Power instance, returning canonical subclass if registered.
If a canonical subclass is registered for (type(operation), exponent), an instance of that subclass is returned instead of a plain Power.
- Parameters:
- Returns:
Instance of Power or a registered canonical subclass
- property op¶
The gate being raised to a power. Read-only.
- property exponent¶
The exponent, normalized to an
intorFractionwhen possible. Read-only.
- get_operation()[source]¶
Return the operation that this one wraps, or the operation itself.
Wrappers such as
ControlorPowerreturn the operation they wrap; other operations return themselves.- Returns:
Operation
- power(*args)[source]¶
Raise to a further power, multiplying the exponents.
- Parameters:
*args – The exponent. When omitted, a lazy expression is returned that takes the exponent later.
- Returns:
op.power(exponent * p), or a lazy expression when called without arguments.- Return type:
- Raises:
ValueError – If more than one argument is given.
Examples
>>> from mimiqcircuits import * >>> # (X^(1/2))^2 is X >>> Power(GateX(), 0.5).power(2) X
- control(*args)[source]¶
Return the gate controlled by
num_controlsqubits.Gates with a named controlled form return it directly (for example
GateX().control(1)isGateCX()); otherwise the gate is wrapped inControl. The controls are the first qubits.- Parameters:
*args – The number of controls. Without it, return a
LazyExprthat takes the number later.- Raises:
ValueError – If more than one argument is given.
- parallel(*args)[source]¶
Return a
Parallelapplying the gate to separate qubit groups.- Parameters:
*args – The number of repetitions. Without it, return a
LazyExprthat takes the number later.- Raises:
ValueError – If more than one argument is given.
- getparams()[source]¶
Return the values of the operation’s parameters.
- Returns:
One value per name in
parnames, in the same order. Values may be numbers or symbolic expressions.- Return type:
Examples
>>> from mimiqcircuits import * >>> # GateU has a fourth parameter, the global phase, defaulting to 0 >>> g = GateU(0.1, 0.2, 0.3) >>> g.parnames ('theta', 'phi', 'lmbda', 'gamma') >>> g.getparams() [0.1, 0.2, 0.3, 0.0]
- isopalias()[source]¶
Whether the operation is a named alias of a wrapper.
Some named gates are defined as a wrapper around another operation, for example
GateSasGateZto the power 1/2. Such gates returnTrue. Not every such gate reports it:GateSDGandGateCXreturnFalse.- Returns:
Truefor a named alias of a wrapper that reports it,Falseotherwise.- Return type:
- decompose()[source]¶
Decompose the operation one level into simpler operations.
The result is a new circuit acting on qubits
0tonum_qubits - 1(and likewise for bits and z-variables). Operations without a decomposition return a circuit holding only themselves.- Returns:
The decomposed operation.
- Return type:
Examples
>>> from mimiqcircuits import * >>> # GateSWAP decomposes into three CX gates >>> GateSWAP().decompose() 2-qubit circuit with 3 instructions: ├── CX @ q[0], q[1] ├── CX @ q[1], q[0] └── CX @ q[0], q[1]
- canonical_power(inner_gate_type, exponent)[source]¶
Decorator that registers a named subclass of
Power.After registration,
Power(g, exponent)withgan instance ofinner_gate_typebuilds an instance of the decorated class instead of a plainPower. This is how named gates such asGateS(Power(GateZ(), 1/2)) are defined. The exponent is compared after normalization, so0.5andFraction(1, 2)match the same entry.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 * >>> # GateS is registered for GateZ to the power 1/2 >>> isinstance(Power(GateZ(), 0.5), GateS) True