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 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.
See also
- remove_control(control: Control) None ¶
Removes a control from the underlying operation.
- Parameters:
control – The control to remove.
See also
- 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 otherCompoundOperation
’s.- Parameters:
ops – The operations that are part of 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.
- 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.
- 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.
- 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.
- 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.
- class Operation¶
Bases:
abc.ABC
An abstract base class for operations that can be added to a
QuantumComputation
.- 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 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.
- class OpType(value: int)¶
- class OpType(arg0: str)
An Enum-like class that represents the type of an operation.
- 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.
See also
- none: ClassVar[OpType]¶
A placeholder operation. It is used to represent an operation that is not yet defined.
- 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.
- 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 itsOpType
, 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.