mqt.core.ir.operations

Module Contents

class OpType

Bases: enum.Enum

Enumeration of operation types.

none = 0

A placeholder operation.

It is used to represent an operation that is not yet defined.

gphase = 4

A global phase operation.

i = 10

An identity operation.

h = 16

A Hadamard gate.

x = 20

An X gate.

y = 24

A Y gate.

z = 30

A Z gate.

s = 34

An S gate.

sdg = 35

An \(S^\dagger\) gate.

t = 38

A T gate.

tdg = 39

A \(T^\dagger\) gate.

v = 40

A V gate.

vdg = 41

A \(V^\dagger\) gate.

u = 44

A U gate.

u2 = 48

A U2 gate.

p = 54

A phase gate.

sx = 56

A \(\sqrt{X}\) gate.

sxdg = 57

A \(\sqrt{X}^\dagger\) gate.

rx = 60

A \(R_x\) gate.

ry = 64

A \(R_y\) gate.

rz = 70

A \(R_z\) gate.

r = 164

An \(R\) gate.

swap = 72

A SWAP gate.

iswap = 76

A iSWAP gate.

iswapdg = 77

A \(i\text{SWAP}^\dagger\) gate.

peres = 80

A Peres gate.

peresdg = 81

A \(\text{Peres}^\dagger\) gate.

dcx = 84

A DCX gate.

ecr = 88

An ECR gate.

rxx = 92

A \(R_{xx}\) gate.

ryy = 96

A \(R_{yy}\) gate.

rzz = 102

A \(R_{zz}\) gate.

rzx = 104

A \(R_{zx}\) gate.

xx_minus_yy = 108

A \(R_{XX - YY}\) gate.

xx_plus_yy = 112

A \(R_{XX + YY}\) gate.

compound = 116

A compound operation.

It is used to group multiple operations into a single operation.

See also CompoundOperation

measure = 120

A measurement operation.

reset = 124

A reset operation.

barrier = 14

A barrier operation.

It is used to separate operations in the circuit.

if_else = 128

An if-else operation.

It is used to control the execution of an operation based on the value of a classical register.

class Control(qubit: int, type_: Control = ...)

A control is a pair of a qubit and a type. The type can be either positive or negative.

Parameters:
  • qubit – The qubit that is the control.

  • type – The type of the control.

class Type

Bases: enum.Enum

Enumeration of control types.

Pos = 1
Neg = 0
property qubit: int

The qubit that is the control.

property type_: Control

The type of the control.

__eq__(arg: object, /) bool
__ne__(arg: object, /) bool
__hash__() int
class Operation
property name: str

The name of the operation.

property type_: OpType

The type of the operation.

property targets: list[int]

The targets of the operation.

Note

The notion of a target might not make sense for all types of operations.

property num_targets: int

The number of targets of the operation.

property controls: set[Control]

The controls of the operation.

Note

The notion of a control might not make sense for all types of operations.

property num_controls: int

The number of controls of the operation.

add_control(control: Control) None

Add a control to the operation.

Parameters:

control – The control to add.

add_controls(controls: Set[Control]) None

Add multiple controls to the operation.

Parameters:

controls – The controls to add.

clear_controls() None

Clear all controls of the operation.

remove_control(control: Control) None

Remove a control from the operation.

Parameters:

control – The control to remove.

remove_controls(controls: Set[Control]) None

Remove multiple controls from the operation.

Parameters:

controls – The controls to remove.

get_used_qubits() set[int]

Get the qubits that are used by the operation.

Returns:

The set of qubits that are used by the operation.

acts_on(qubit: int) bool

Check if the operation acts on a specific qubit.

Parameters:

qubit – The qubit to check.

Returns:

True if the operation acts on the qubit, False otherwise.

property parameter: list[float]

The parameters of the operation.

Note

The notion of a parameter might not make sense for all types of operations.

is_unitary() bool

Check if the operation is unitary.

Returns:

True if the operation is unitary, False otherwise.

is_standard_operation() bool

Check if the operation is a StandardOperation.

Returns:

True if the operation is a StandardOperation, False otherwise.

is_compound_operation() bool

Check if the operation is a CompoundOperation.

Returns:

True if the operation is a CompoundOperation, False otherwise.

is_non_unitary_operation() bool

Check if the operation is a NonUnitaryOperation.

Returns:

True if the operation is a NonUnitaryOperation, False otherwise.

is_if_else_operation() bool

Check if the operation is a IfElseOperation.

Returns:

True if the operation is a IfElseOperation, False otherwise.

is_symbolic_operation() bool

Check if the operation is a SymbolicOperation.

Returns:

True if the operation is a SymbolicOperation, False otherwise.

is_controlled() bool

Check if the operation is controlled.

Returns:

True if the operation is controlled, False otherwise.

get_inverted() Operation

Get the inverse of the operation.

Returns:

The inverse of the operation.

invert() None

Invert the operation (in-place).

__eq__(arg: object, /) bool
__ne__(arg: object, /) bool
__hash__() int
class StandardOperation
class StandardOperation(target: int, op_type: OpType, params: Sequence[float] = ...)
class StandardOperation(targets: Sequence[int], op_type: OpType, params: Sequence[float] = ...)
class StandardOperation(control: Control, target: int, op_type: OpType, params: Sequence[float] = ...)
class StandardOperation(control: Control, targets: Sequence[int], op_type: OpType, params: Sequence[float] = ...)
class StandardOperation(controls: Set[Control], target: int, op_type: OpType, params: Sequence[float] = ...)
class StandardOperation(controls: Set[Control], targets: Sequence[int], op_type: OpType, params: Sequence[float] = ...)
class StandardOperation(controls: Set[Control], target0: int, target1: int, op_type: OpType, params: Sequence[float] = ...)

Bases: Operation

Standard quantum operation.

This class is used to represent all standard quantum operations, i.e., operations that are unitary. This includes all possible quantum gates. Such Operations are defined by their OpType, the qubits (controls and targets) they act on, and their parameters.

Parameters:
  • control – The control qubit(s) of the operation (if any).

  • target – The target qubit(s) of the operation.

  • op_type – The type of the operation.

  • params – The parameters of the operation (if any).

class CompoundOperation
class CompoundOperation(ops: Sequence[Operation])

Bases: Operation, collections.abc.MutableSequence[Operation]

Compound quantum operation.

This class is used to aggregate and group multiple operations into a single object. This is useful for optimizations and for representing complex quantum functionality. A CompoundOperation can contain any number of operations, including other CompoundOperation’s.

Parameters:

ops – The operations that are part of the compound operation.

__len__() int

The number of operations in the compound operation.

__getitem__(index: int) Operation
__getitem__(index: slice) list[Operation]

Get the operations in the given slice.

Note

This gives direct access to the operations in the compound operation.

Parameters:

index – The slice of the operations to get.

Returns:

The operations in the given slice.

__setitem__(index: int, value: Operation) None
__setitem__(index: slice, value: Iterable[Operation]) None

Set the operations in the given slice.

Parameters:
  • index – The slice of operations to set.

  • value – The operations to set in the given slice.

__delitem__(index: int) None
__delitem__(index: slice) None

Delete the operations in the given slice.

Parameters:

index – The slice of operations to delete.

append(value: Operation) None

Append an operation to the compound operation.

insert(index: int, value: Operation) None

Insert an operation at the given index.

Parameters:
  • index – The index to insert the operation at.

  • value – The operation to insert.

empty() bool

Check if the compound operation is empty.

clear() None

Clear all operations in the compound operation.

class NonUnitaryOperation(targets: Sequence[int], classics: Sequence[int])
class NonUnitaryOperation(target: int, classic: int)
class NonUnitaryOperation(targets: Sequence[int], op_type: OpType = ...)

Bases: Operation

Non-unitary operation.

This class is used to represent all non-unitary operations, i.e., operations that are not reversible. This includes measurements and resets.

Parameters:
  • targets – The target qubit(s) of the operation.

  • classics – The classical bit(s) that are associated with the operation (only relevant for measurements).

  • op_type – The type of the operation.

property classics: list[int]

The classical bits that are associated with the operation.

class SymbolicOperation
class SymbolicOperation(target: int, op_type: OpType, params: Sequence[Expression | float] = ...)
class SymbolicOperation(targets: Sequence[int], op_type: OpType, params: Sequence[Expression | float] = ...)
class SymbolicOperation(control: Control, target: int, op_type: OpType, params: Sequence[Expression | float] = ...)
class SymbolicOperation(control: Control, targets: Sequence[int], op_type: OpType, params: Sequence[Expression | float] = ...)
class SymbolicOperation(controls: Set[Control], target: int, op_type: OpType, params: Sequence[Expression | float] = ...)
class SymbolicOperation(controls: Set[Control], targets: Sequence[int], op_type: OpType, params: Sequence[Expression | float] = ...)
class SymbolicOperation(controls: Set[Control], target0: int, target1: int, op_type: OpType, params: Sequence[Expression | float] = ...)

Bases: StandardOperation

Symbolic quantum operation.

This class is used to represent quantum operations that are not yet fully defined. This can be useful for representing operations that depend on parameters that are not yet known. A SymbolicOperation is defined by its OpType, the qubits (controls and targets) it acts on, and its parameters. The parameters can be either fixed values or symbolic expressions.

Parameters:
  • controls – The control qubit(s) of the operation (if any).

  • targets – The target qubit(s) of the operation.

  • op_type – The type of the operation.

  • params – The parameters of the operation (if any).

get_parameter(index: int) Expression | float

Get the parameter at the given index.

Parameters:

index – The index of the parameter to get.

Returns:

The parameter at the given index.

get_parameters() list[Expression | float]

Get all parameters of the operation.

Returns:

The parameters of the operation.

get_instantiated_operation(assignment: Mapping[Variable, float]) StandardOperation

Get the instantiated operation.

Parameters:

assignment – The assignment of the symbolic parameters.

Returns:

The instantiated operation.

instantiate(assignment: Mapping[Variable, float]) None

Instantiate the operation (in-place).

Parameters:

assignment – The assignment of the symbolic parameters.

class ComparisonKind

Bases: enum.Enum

Enumeration of comparison types for classic-controlled operations.

eq = 0

Equality comparison.

neq = 1

Inequality comparison.

lt = 2

Less-than comparison.

leq = 3

Less-than-or-equal comparison.

gt = 4

Greater-than comparison.

geq = 5

Greater-than-or-equal comparison.

class IfElseOperation(then_operation: Operation, else_operation: Operation | None, control_register: ClassicalRegister, expected_value: int = 1, comparison_kind: ComparisonKind = ...)
class IfElseOperation(then_operation: Operation, else_operation: Operation | None, control_bit: int, expected_value: bool = True, comparison_kind: ComparisonKind = ...)

Bases: Operation

If-else quantum operation.

This class is used to represent an if-else operation. The then operation is executed if the value of the classical register matches the expected value. Otherwise, the else operation is executed.

Parameters:
  • then_operation – The operation that is executed if the condition is met.

  • else_operation – The operation that is executed if the condition is not met.

  • control_register – The classical register that controls the operation.

  • expected_value – The expected value of the classical register.

  • comparison_kind – The kind of comparison (default is equality).

property then_operation: Operation

The operation that is executed if the condition is met.

property else_operation: Operation | None

The operation that is executed if the condition is not met.

property control_register: ClassicalRegister | None

The classical register that controls the operation.

property control_bit: int | None

The classical bit that controls the operation.

property expected_value_register: int

The expected value of the classical register.

The then-operation is executed if the value of the classical register matches the expected value based on the kind of comparison. The expected value is an integer that is interpreted as a binary number, where the least significant bit is at the start index of the classical register.

property expected_value_bit: bool

The expected value of the classical bit.

The then-operation is executed if the value of the classical bit matches the expected value based on the kind of comparison.

property comparison_kind: ComparisonKind

The kind of comparison.

The then-operation is executed if the value of the control matches the expected value based on the kind of comparison.