mimiqcircuits.operations.operation¶
Base Operation class.
Classes
Abstract base class for everything that can be pushed to a circuit. |
- class Operation[source]¶
Bases:
ABCAbstract base class for everything that can be pushed to a circuit.
An operation acts on a fixed number of qubits, classical bits and complex-valued z-variables (see
num_qubits,num_bitsandnum_zvars). Gates, measurements, resets, noise channels, classical operations and annotations are all operations. An operation only says what is applied; anInstructionpairs it with the targets it is applied to.Examples
>>> from mimiqcircuits import * >>> # operations know how many targets of each kind they need >>> op = Measure() >>> op.num_qubits, op.num_bits, op.num_zvars (1, 1, 0) >>> # pushing an operation to a circuit creates an instruction >>> c = Circuit() >>> c.push(op, 0, 0) 1-qubit, 1-bit circuit with 1 instruction: └── M @ q[0], c[0]
- classmethod allow_bit_aliasing()[source]¶
Whether an instruction may list the same classical bit more than once.
Falsefor most operations. Operations that read and write the same bit, such as the classical logic operations, returnTrue.
- classmethod allow_zvar_aliasing()[source]¶
Whether an instruction may list the same z-variable more than once.
Falsefor most operations. Arithmetic operations whose output may also be one of their inputs returnTrue.
- property num_qubits¶
Number of qubits the operation acts on.
Read-only: assigning to it raises
ValueError.
- property num_ancillas¶
Number of clean ancillas of the operation.
The last
num_ancillasqubit targets must be in|0>when the operation starts, and are returned in|0>. On those inputs the operation acts as the one it was built for, which only touches the other qubits.It is set on
GateDecl,BlockDecl,Blockand calls to them, and is0for every other operation.decompose()sets it when it wraps a decomposition that uses clean ancillas, and marks the ancillas of each call withAncillaAllocandAncillaFree.
- property num_qregs¶
Number of quantum registers (groups of qubit targets) the operation takes.
Read-only: assigning to it raises
ValueError.
- property num_bits¶
Number of classical bits the operation acts on.
Read-only: assigning to it raises
ValueError.
- property num_zvars¶
Number of complex-valued z-variables the operation acts on.
Read-only: assigning to it raises
ValueError.
- property num_cregs¶
Number of classical registers (groups of bit targets) the operation takes.
Read-only: assigning to it raises
ValueError.
- property qregsizes¶
Size of each quantum register, as a list with one entry per register.
Read-only: assigning to it raises
ValueError.
- property cregsizes¶
Size of each classical register, with one entry per register.
Empty or
Nonewhen the operation has no classical register. Read-only: assigning to it raisesValueError.
- property zregsizes¶
Size of each z-variable register, with one entry per register.
Empty or
Nonewhen the operation has no z-variable register. Read-only: assigning to it raisesValueError.
- property name¶
Short name of the operation type.
For a wrapper, and for a named gate defined through one, this is the name of the wrapper:
GateCX().nameis"Control".str(op)gives the name shown when printing a circuit. Read-only: assigning to it raisesValueError.
- property parnames¶
Names of the operation’s parameters, as a tuple of strings.
Read-only: assigning to it raises
ValueError.
- 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]
- is_symbolic()[source]¶
Whether any parameter contains a free symbol.
For wrappers such as
ControlorPower, the wrapped operation is checked too.- Returns:
Trueif at least one parameter is symbolic.- Return type:
Examples
>>> from mimiqcircuits import * >>> from symengine import Symbol >>> GateRX(Symbol("theta")).is_symbolic() True >>> GateRX(0.5).is_symbolic() False
- getparam(pn)[source]¶
Return the value of the parameter named
pn.- Parameters:
- Returns:
The parameter value, a number or a symbolic expression.
- Raises:
ValueError – If the operation has no parameter named
pn.
Examples
>>> from mimiqcircuits import * >>> GateRX(0.5).getparam("theta") 0.5
- copy()[source]¶
Return a shallow copy of the operation.
The copy shares its attributes (for example a wrapped operation) with the original. Use
deepcopy()for an independent copy.- Returns:
A new operation of the same type.
- Return type:
- deepcopy()[source]¶
Return a deep copy of the operation and all its attributes.
- Returns:
A new operation equal to the original that shares no mutable state with it.
- Return type:
- abstract iswrapper()[source]¶
Whether the operation wraps another operation.
Truefor composite operations such asControl,Power,Inverse,ParallelorIfStatement,Falseotherwise.- Returns:
bool
- 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:
- supports_canonical_rewrite()[source]¶
Whether
CanonicalRewritemay decompose this operation.Operations opt in: only those the canonical rewrite knows how to handle return
True.- Returns:
bool
- numparams()[source]¶
Return the number of parameters of the operation.
- Returns:
The length of
getparams().- 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]
- evaluate(d)[source]¶
Substitute values for the symbols in the operation’s parameters.
Operations without symbolic parameters may return themselves.
- isidentity()[source]¶
Whether the operation is known to act as the identity.
- Returns:
Trueonly when the operation is known to be the identity.Falsedoes not prove the opposite.- Return type:
- classmethod isunitary()[source]¶
Whether the operation acts unitarily on the quantum state.
- Returns:
Truefor gates, annotations,Barrier, and the operations that only read the state into a z-variable, such asExpectationValueorAmplitude.Falsefor everything else: measurements, resets, noise channels, classical operations, and containers such asBlockorIfStatement.- Return type:
- 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
- listvars()[source]¶
List the symbolic variables in the parameters of the operation.
Wrappers such as
Control,Inverse,Power,Parallel,Repeat,IfStatementandWhileStatementgive the variables of the operation they wrap, and aBlockthose of all its instructions.- Returns:
Each
symengine.Symbolonce, in no particular order. The list is empty when the operation has no symbolic parameters.- Return type:
Examples
>>> from mimiqcircuits import * >>> from symengine import Symbol >>> theta = Symbol("theta") >>> GateRX(theta).listvars() [theta] >>> Control(2, GateRY(2 * theta)).listvars() [theta] >>> GateH().listvars() []