mimiqcircuits.instruction

Instruction class and utilities.

Classes

Instruction(operation[, qubits, bits, zvars])

An operation together with the qubits, bits and z-variables it acts on.

class Instruction(operation, qubits=None, bits=None, zvars=None)[source]

Bases: object

An operation together with the qubits, bits and z-variables it acts on.

Instructions are the elements of a Circuit. They are usually created by Circuit.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 operation is not an Operation, 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).

get_operation()[source]

Return the operation of the instruction. Same as operation.

get_qubits()[source]

Return the target qubits. Same as qubits.

get_bits()[source]

Return the target classical bits. Same as bits.

get_zvars()[source]

Return the target z-variables. Same as zvars.

num_qubits()[source]

Return the number of target qubits.

num_bits()[source]

Return the number of target classical bits.

num_zvars()[source]

Return the number of target z-variables.

getparams()[source]

Return the parameters of the operation, as a list.

See Operation.getparams().

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:

Examples

>>> from mimiqcircuits import *
>>> Instruction(GateS(), (1,)).inverse()
S† @ q[1]
isunitary()[source]

Return True if the operation of the instruction is unitary.

copy()[source]

Return a shallow copy of the instruction.

The copy shares its operation with the original. Use deepcopy() to copy the operation too.

deepcopy()[source]

Return a deep copy of the instruction, operation included.

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 GateU or GateCX, come back unchanged. Use Circuit.decompose() to decompose to a basis.

Returns:

The decomposed instruction.

Return type:

Circuit

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 d stay symbolic. When nothing changes, the instruction itself is returned.

Parameters:

d (dict) – Map from symbols to values.

Returns:

The evaluated instruction.

Return type:

Instruction

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

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**nq by 2**nq matrix, as a symengine matrix.

Raises:

ValueError – If nq is 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]