mqt.core.operations

Operations Module.

Module Contents

class ClassicControlledOperation(operation: Operation, control_register: tuple[int, int], expected_value: int = 1)

Bases: Operation

Classic controlled quantum operation.

This class is used to represent quantum operations that are controlled by the value of a classical register. The operation is only executed if the value of the classical register matches the expected value.

Parameters:
  • operation – The operation that is controlled.

  • control_register – The classical register that controls the operation.

  • expected_value – The expected value of the classical register.

property operation: Operation

The operation that is classically controlled.

property control_register: tuple[int, int]

The classical register that controls the operation.

The register is specified as a tuple of the start index and the length.

Examples

A register that starts at index 0 and has a length of 2 is specified as (0, 2).

property expected_value: int

The expected value of the classical register.

The operation is only executed if the value of the classical register matches the expected value. If the classical register is a single bit, the expected value is either 0 or 1. Otherwise, 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.

add_control(control: Control) None

Adds a control to the underlying operation.

Parameters:

control – The control to add.

clear_controls() None

Clears the controls of the underlying operation.

remove_control(control: Control) None

Removes a control from the underlying operation.

Parameters:

control – The control to remove.

invert() None

Inverts the underlying operation.

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

Bases: 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__(idx: int) Operation
__getitem__(idx: slice) list[Operation]

Get the operations in the given slice.

Parameters:

idx – The slice of the operations to get.

Returns:

The operations in the given slice.

Notes

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

append(op: Operation) None

Append an operation to the compound operation.

empty() bool

Check if the compound operation is empty.

add_control(control: Control) None

Add a control to the operation.

This will add the control to all operations in the compound operation. Additionally, the control is added to the compound operation itself to keep track of all controls that are applied to the compound operation.

Parameters:

control – The control to add.

clear_controls() None

Clear all controls of the operation.

This will clear all controls that have been tracked in the compound operation itself and will clear these controls of all operations that are part of the compound operation.

remove_control(control: Control) None

Remove a control from the operation.

This will remove the control from all operations in the compound operation. Additionally, the control is removed from the compound operation itself to keep track of all controls that are applied to the compound operation.

Parameters:

control – The control to remove.

invert() None

Invert the operation (in-place).

This will invert all operations in the compound operation and reverse the order of the operations. This only works if all operations in the compound operation are invertible and will throw an error otherwise.

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

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 (default is positive).

class Type(value: int)

The type of a control. It can be either positive or negative.

property name: str
property value: int
__members__: ClassVar[dict[Control, str]]
Neg: ClassVar[Control]

A negative control.

The operation that is controlled on this qubit is only executed if the qubit is in the \(|0\rangle\) state.

Pos: ClassVar[Control]

A positive control.

The operation that is controlled on this qubit is only executed if the qubit is in the \(|1\rangle\) state.

__eq__(other: object) bool

Return self==value.

__getstate__() int

Helper for pickle.

__hash__() int

Return hash(self).

__index__() int
__int__() int
__ne__(other: object) bool

Return self!=value.

__setstate__(state: int) None
qubit: int
type_: Type
__eq__(other: object) bool

Check if two controls are equal.

__ne__(other: object) bool

Check if two controls are not equal.

__hash__() int

Get the hash of the control.

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 registers that are associated with the operation.

add_control(control: Control) None

Adding controls to a non-unitary operation is not supported.

clear_controls() None

Cannot clear controls of a non-unitary operation.

remove_control(control: Control) None

Removing controls from a non-unitary operation is not supported.

invert() None

Non-unitary operations are, per definition, not invertible.

class Operation

Bases: abc.ABC

An abstract base class for operations that can be added to a QuantumComputation.

property name: str

The name of the operation.

property num_targets: int

The number of targets of the operation.

property num_controls: int

The number of controls of the operation.

type_: OpType

The type of the operation.

controls: set[Control]

The controls of the operation.

Note

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

targets: list[int]

The targets of the operation.

Note

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

parameter: list[float]

The parameters of the operation.

Note

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

abstract 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.

abstract clear_controls() None

Clear all controls of the operation.

abstract 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.

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.

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.

is_classic_controlled_operation() bool

Check if the operation is a ClassicControlledOperation.

Returns:

True if the operation is a ClassicControlledOperation, False otherwise.

is_compound_operation() bool

Check if the operation is a CompoundOperation.

Returns:

True if the operation is a CompoundOperation, False otherwise.

is_controlled() bool

Check if the operation is controlled.

Returns:

True if the operation is controlled, 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_standard_operation() bool

Check if the operation is a StandardOperation.

Returns:

True if the operation is a StandardOperation, False otherwise.

is_symbolic_operation() bool

Check if the operation is a SymbolicOperation.

Returns:

True if the operation is a SymbolicOperation, False otherwise.

is_unitary() bool

Check if the operation is unitary.

Returns:

True if the operation is unitary, False otherwise.

get_inverted() Operation

Get the inverse of the operation.

Returns:

The inverse of the operation.

abstract invert() None

Invert the operation (in-place).

__eq__(other: object) bool

Return self==value.

__hash__() int

Return hash(self).

__ne__(other: object) bool

Return self!=value.

class OpType(value: int)
class OpType(arg0: str)

An Enum-like class that represents the type of an operation.

property name: str
property value: int
__members__: ClassVar[dict[OpType, str]]
barrier: ClassVar[OpType]

A barrier operation. It is used to separate operations in the circuit.

classic_controlled: ClassVar[OpType]

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

compound: ClassVar[OpType]

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

dcx: ClassVar[OpType]

A DCX gate.

ecr: ClassVar[OpType]

An ECR gate.

gphase: ClassVar[OpType]

A global phase operation.

h: ClassVar[OpType]

A Hadamard gate.

i: ClassVar[OpType]

An identity operation.

iswap: ClassVar[OpType]

An iSWAP gate.

iswapdg: ClassVar[OpType]

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

measure: ClassVar[OpType]

A measurement operation.

none: ClassVar[OpType]

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

peres: ClassVar[OpType]

A Peres gate.

peresdg: ClassVar[OpType]

A :math:` ext{Peres}^dagger` gate.

p: ClassVar[OpType]

A phase gate.

reset: ClassVar[OpType]

A reset operation.

rx: ClassVar[OpType]

An \(R_x\) gate.

rxx: ClassVar[OpType]

An \(R_{xx}\) gate.

ry: ClassVar[OpType]

An \(R_y\) gate.

ryy: ClassVar[OpType]

An \(R_{yy}\) gate.

rz: ClassVar[OpType]

An \(R_z\) gate.

rzx: ClassVar[OpType]

An \(R_{zx}\) gate.

rzz: ClassVar[OpType]

An \(R_{zz}\) gate.

s: ClassVar[OpType]

An S gate.

sdg: ClassVar[OpType]

An \(S^\dagger\) gate.

swap: ClassVar[OpType]

A SWAP gate.

sx: ClassVar[OpType]

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

sxdg: ClassVar[OpType]

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

t: ClassVar[OpType]

A T gate.

tdg: ClassVar[OpType]

A \(T^\dagger\) gate.

teleportation: ClassVar[OpType]

A teleportation operation.

u2: ClassVar[OpType]

A U2 gate.

u: ClassVar[OpType]

A U gate.

v: ClassVar[OpType]

A V gate.

vdg: ClassVar[OpType]

A \(V^\dagger\) gate.

x: ClassVar[OpType]

An X gate.

xx_minus_yy: ClassVar[OpType]

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

xx_plus_yy: ClassVar[OpType]

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

y: ClassVar[OpType]

A Y gate.

z: ClassVar[OpType]

A Z gate.

__eq__(other: object) bool

Return self==value.

__getstate__() int

Helper for pickle.

__hash__() int

Return hash(self).

__index__() int
__int__() int
__ne__(other: object) bool

Return self!=value.

__setstate__(state: int) None
class StandardOperation
class StandardOperation(target: int, op_type: OpType, params: Sequence[float] | None = None)
class StandardOperation(targets: Sequence[int], op_type: OpType, params: Sequence[float] | None = None)
class StandardOperation(control: Control, target: int, op_type: OpType, params: Sequence[float] | None = None)
class StandardOperation(control: Control, targets: Sequence[int], op_type: OpType, params: Sequence[float] | None = None)
class StandardOperation(controls: set[Control], target: int, op_type: OpType, params: Sequence[float] | None = None)
class StandardOperation(controls: set[Control], targets: Sequence[int], op_type: OpType, params: Sequence[float] | None = None)
class StandardOperation(controls: set[Control], target0: int, target1: int, op_type: OpType, params: Sequence[float] | None = None)

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).

add_control(control: Control) None

Add a control to the operation.

StandardOperation supports arbitrarily many controls per operation.

Parameters:

control – The control 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.

invert() None

Invert the operation (in-place).

Since any StandardOperation is unitary, the inverse is simply the conjugate transpose of the operation’s matrix representation.

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

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(idx: int) Expression | float

Get the parameter at the given index.

Parameters:

idx – 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.