mimiqcircuits.instruction¶
Instruction class and utilities.
Classes
|
An operation together with the qubits, bits and z-variables it acts on. |
- class Instruction(operation, qubits=None, bits=None, zvars=None)[source]¶
Bases:
objectAn operation together with the qubits, bits and z-variables it acts on.
Instructions are the elements of a
Circuit. They are usually created byCircuit.push(); build one directly to push it later or to insert it at a given position. An instruction cannot be modified after it is created.- Parameters:
operation (Operation) – The operation to apply.
qubits (tuple of int) – The target qubits, as many as
operation.num_qubits. Defaults to().bits (tuple of int) – The target classical bits, as many as
operation.num_bits. Defaults to().zvars (tuple of int) – The target z-variables, as many as
operation.num_zvars. Defaults to().
- Raises:
TypeError – If
operationis not anOperation, or a target group is not a tuple.ValueError – If a target group has the wrong length, an index is negative, or a qubit is repeated. Repeated bits or z-variables are refused too, unless the operation allows them.
Examples
>>> from mimiqcircuits import * >>> Instruction(GateX(), (0,)) X @ q[0] >>> # a measurement targets one qubit and one classical bit >>> Instruction(Measure(), (2,), (0,)) M @ q[2], c[0] >>> # consecutive targets are shown as a range >>> Instruction(Barrier(4), (0, 1, 2, 3)) Barrier @ q[0:3]
- property operation¶
The operation applied by the instruction (read-only).
- property qubits¶
The target qubits, as a tuple of int (read-only).
- property bits¶
The target classical bits, as a tuple of int (read-only).
- property zvars¶
The target z-variables, as a tuple of int (read-only).
- listvars()[source]¶
List the symbolic variables of the operation of the instruction.
See
Operation.listvars().
- inverse()[source]¶
Return the instruction applying the inverse operation on the same targets.
- Raises:
TypeError – If the operation has no inverse, such as a measurement.
NotImplementedError – If the operation is a Kraus channel.
Examples
>>> from mimiqcircuits import * >>> Instruction(GateS(), (1,)).inverse() S† @ q[1]
- copy()[source]¶
Return a shallow copy of the instruction.
The copy shares its operation with the original. Use
deepcopy()to copy the operation too.
- decompose()[source]¶
Return a circuit with one decomposition step of the operation.
The operation is replaced by the instructions of its own decomposition, placed on the instruction’s targets. Operations that have no simpler form, such as
GateUorGateCX, come back unchanged. UseCircuit.decompose()to decompose to a basis.- Returns:
The decomposed instruction.
- Return type:
Examples
>>> from mimiqcircuits import * >>> Instruction(GateCZ(), (0, 1)).decompose() 2-qubit circuit with 3 instructions: ├── H @ q[1] ├── CX @ q[0], q[1] └── H @ q[1]
- evaluate(d)[source]¶
Return the instruction with symbols replaced by values from
d.Symbols not in
dstay symbolic. When nothing changes, the instruction itself is returned.- Parameters:
d (dict) – Map from symbols to values.
- Returns:
The evaluated instruction.
- Return type:
Examples
>>> from mimiqcircuits import * >>> from symengine import Symbol >>> theta = Symbol("theta") >>> Instruction(GateRX(theta), (0,)).evaluate({theta: 0.5}) RX(0.5) @ q[0]
- matrix(nq=None)[source]¶
Return the matrix of the instruction on the full register of
nqqubits.The matrix of the operation is placed on the target qubits and extended with the identity on the other ones.
- Parameters:
nq (int) – The number of qubits of the register. Defaults to one more than the largest target qubit.
- Returns:
The
2**nqby2**nqmatrix, as a symengine matrix.- Raises:
ValueError – If
nqis not given and the instruction has no target qubits.
Examples
>>> from mimiqcircuits import * >>> # X on qubit 0 of a 2-qubit register: X ⊗ I >>> Instruction(GateX(), (0,)).matrix(2) [0, 0, 1.0, 0] [0, 0, 0, 1.0] [1.0, 0, 0, 0] [0, 1.0, 0, 0]