Operations#
Quantum operations and base operations for creating new operation classes.
Base Module#
Set of base operation classes subclassed by all quantum operations.
Contains all base classes for general quantum operations, including parametric operations,
controlled operations and measurements. Also provides the utility function create_operation()
for transforming circuits into operations.
- class Barrier(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationClass representing a barrier operation.
- Parameters:
qubits – Qubits on which the barrier operation should be applied. Only required when applying a barrier operation within a circuit context.
- class ControlledOperation(control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
OperationGeneric controlled operation.
- Parameters:
control – Qubit(s) on which the target operation is controlled.
target – Qubit(s) on which the target operation is applied.
- Keyword Arguments:
qubits – All qubits on which the operation is applied. If
qubitsis passed, it takes precedence overcontrolandtarget, and replaces those two attributes using the operations_num_controland_num_targetclass attributes.
- matrix[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedpropertydecorator differs from the regularpropertydecorator by supporting access to both the class (cls) and the instance (self). The latter is only available when calling the method on an instance of the class. The signature parameters are positional and assume that the first parameter is the class and the second parameter (optional) is the instance.This property can optionally also accept parameters (either positional or keyword) when initialized. Allowed parameters are listed under ‘Args’ below.
- Parameters:
self_required – Whether
selfis required for the property to be able to return the requested property, i.e., the function will only work when called on an instance and not on the class.
Example
class MixedpropertyExample: _a = "a_class" _b = "b_class" def __init__(self): self._b = "b_instance" self._c = "c_instance" # can be called on both instance and class and will # return the same result regardless @mixedproperty def a(cls): return cls._a # can be called on both instance and class and will # return different results (e.g., different defaults) @mixedproperty def b(cls, self): if self: return self._b return cls._b # can be called on both instance and class and will # return 'None' if called on class @mixedproperty(self_required=True) def c(cls, self): return self._c
- num_qubits[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedpropertydecorator differs from the regularpropertydecorator by supporting access to both the class (cls) and the instance (self). The latter is only available when calling the method on an instance of the class. The signature parameters are positional and assume that the first parameter is the class and the second parameter (optional) is the instance.This property can optionally also accept parameters (either positional or keyword) when initialized. Allowed parameters are listed under ‘Args’ below.
- Parameters:
self_required – Whether
selfis required for the property to be able to return the requested property, i.e., the function will only work when called on an instance and not on the class.
Example
class MixedpropertyExample: _a = "a_class" _b = "b_class" def __init__(self): self._b = "b_instance" self._c = "c_instance" # can be called on both instance and class and will # return the same result regardless @mixedproperty def a(cls): return cls._a # can be called on both instance and class and will # return different results (e.g., different defaults) @mixedproperty def b(cls, self): if self: return self._b return cls._b # can be called on both instance and class and will # return 'None' if called on class @mixedproperty(self_required=True) def c(cls, self): return self._c
- target_operation[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedpropertydecorator differs from the regularpropertydecorator by supporting access to both the class (cls) and the instance (self). The latter is only available when calling the method on an instance of the class. The signature parameters are positional and assume that the first parameter is the class and the second parameter (optional) is the instance.This property can optionally also accept parameters (either positional or keyword) when initialized. Allowed parameters are listed under ‘Args’ below.
- Parameters:
self_required – Whether
selfis required for the property to be able to return the requested property, i.e., the function will only work when called on an instance and not on the class.
Example
class MixedpropertyExample: _a = "a_class" _b = "b_class" def __init__(self): self._b = "b_instance" self._c = "c_instance" # can be called on both instance and class and will # return the same result regardless @mixedproperty def a(cls): return cls._a # can be called on both instance and class and will # return different results (e.g., different defaults) @mixedproperty def b(cls, self): if self: return self._b return cls._b # can be called on both instance and class and will # return 'None' if called on class @mixedproperty(self_required=True) def c(cls, self): return self._c
- class Measurement(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationClass representing a measurement.
- Parameters:
qubits – The qubits which should be measured. Only required when applying an measurement within a circuit context.
- expval(qubits: int | None = None, num_samples: int = 1000) list[float][source]#
Calculate the expectation value of a measurement.
- sample(qubits: Sequence[int] | None = None, num_samples: int = 1, as_bitstring: bool = False) list[int | str][source]#
Sample the measured state.
- Parameters:
qubits – The qubits to sample. If not given, all measured qubits are sampled.
num_samples – The number of samples to to return.
as_bitstring – Whether to return the samples as bitstrings or as lists of integers (default).
- Returns:
The measurement samples for each qubit.
- Return type:
- class Operation(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
objectClass representing a classical or quantum operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- conditional(bits: Bit | Sequence[Bit]) Operation[source]#
Sets the
_condattribute, conditioning the operation on one or more bits.
- decomposition[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedpropertydecorator differs from the regularpropertydecorator by supporting access to both the class (cls) and the instance (self). The latter is only available when calling the method on an instance of the class. The signature parameters are positional and assume that the first parameter is the class and the second parameter (optional) is the instance.This property can optionally also accept parameters (either positional or keyword) when initialized. Allowed parameters are listed under ‘Args’ below.
- Parameters:
self_required – Whether
selfis required for the property to be able to return the requested property, i.e., the function will only work when called on an instance and not on the class.
Example
class MixedpropertyExample: _a = "a_class" _b = "b_class" def __init__(self): self._b = "b_instance" self._c = "c_instance" # can be called on both instance and class and will # return the same result regardless @mixedproperty def a(cls): return cls._a # can be called on both instance and class and will # return different results (e.g., different defaults) @mixedproperty def b(cls, self): if self: return self._b return cls._b # can be called on both instance and class and will # return 'None' if called on class @mixedproperty(self_required=True) def c(cls, self): return self._c
- property is_blocked: bool[source]#
Whether the operation is blocked and should not be executed.
Note that this affects the operation only at runtime (e.g., in the simulator) and does not block the operation from being appended to the circuit. This happend when at least one conditional bit is False.
- num_qubits[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedpropertydecorator differs from the regularpropertydecorator by supporting access to both the class (cls) and the instance (self). The latter is only available when calling the method on an instance of the class. The signature parameters are positional and assume that the first parameter is the class and the second parameter (optional) is the instance.This property can optionally also accept parameters (either positional or keyword) when initialized. Allowed parameters are listed under ‘Args’ below.
- Parameters:
self_required – Whether
selfis required for the property to be able to return the requested property, i.e., the function will only work when called on an instance and not on the class.
Example
class MixedpropertyExample: _a = "a_class" _b = "b_class" def __init__(self): self._b = "b_instance" self._c = "c_instance" # can be called on both instance and class and will # return the same result regardless @mixedproperty def a(cls): return cls._a # can be called on both instance and class and will # return different results (e.g., different defaults) @mixedproperty def b(cls, self): if self: return self._b return cls._b # can be called on both instance and class and will # return 'None' if called on class @mixedproperty(self_required=True) def c(cls, self): return self._c
- to_qasm(*args, **kwargs)[source]#
Converts the operation into an OpenQASM 2.0 string.
Must be implemented by subclass to support OpenQASM 2.0 transpilation.
- Raises:
NotImplementedError – If called on a class which hasn’t implemented this method.
- class ParametricOperation(parameters: Variable | complex | Sequence[Variable | complex], qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationClass for creating parametric operations.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
parameters – Parameters for the operation. Required when constructing the matrix representation of the operation.
- eval(parameters: Sequence[complex] | None = None, inplace: bool = False) ParametricOperation[source]#
Evaluate operation with explicit parameters.
- Parameters:
parameters – Parameters to replace operation variables with. Overrides potential variable values. If
Nonethen variable values are used (if existent).inplace – Whether to evaluate the parameters on
selfor on a copy ofself(returned).
- Returns:
Either
selfor a copy ofself.- Return type:
- Raises:
ValueError – If no parameters are passed and if variable has no set value.
- num_parameters[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedpropertydecorator differs from the regularpropertydecorator by supporting access to both the class (cls) and the instance (self). The latter is only available when calling the method on an instance of the class. The signature parameters are positional and assume that the first parameter is the class and the second parameter (optional) is the instance.This property can optionally also accept parameters (either positional or keyword) when initialized. Allowed parameters are listed under ‘Args’ below.
- Parameters:
self_required – Whether
selfis required for the property to be able to return the requested property, i.e., the function will only work when called on an instance and not on the class.
Example
class MixedpropertyExample: _a = "a_class" _b = "b_class" def __init__(self): self._b = "b_instance" self._c = "c_instance" # can be called on both instance and class and will # return the same result regardless @mixedproperty def a(cls): return cls._a # can be called on both instance and class and will # return different results (e.g., different defaults) @mixedproperty def b(cls, self): if self: return self._b return cls._b # can be called on both instance and class and will # return 'None' if called on class @mixedproperty(self_required=True) def c(cls, self): return self._c
- create_operation(circuit: ParametricCircuit, name: str | None = None) Type[CustomParametricOperation][source]#
- create_operation(circuit: Circuit, name: str | None = None) Type[CustomOperation]
Create an operation from a circuit object.
Takes the circuit operations and creates a new custom operation class inheriting either directly or indirectly from
Operation. The custrom operation will automatically implement the matrix property which constructs and stores the matrix represenation.- Parameters:
circuit – Circuit object out of which to create an operation.
name – Name for the new operation. Usually the class name of the operation. Defaults to
"CustomOperation"if no other name is given.
- Returns:
Class inheriting from the
Operationclass.- Return type:
Operations Module#
Quantum operations that can be applied in a circuit.
Contains all supported quantum operations along with aliases for operations that may be known by
different names; e.g., Fredkin is an alias for CSWAP, and Toffoli and
CCNOT are aliases for CCX.
- class CCX(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationCCX (Toffoli) operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- matrix = array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])[source]#
- class CHadamard(control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ControlledOperationControlled-Hadamard operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class CRX(parameters: Variable | complex | Sequence[Variable | complex], control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ParametricControlledOperationControlled-RX operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class CRY(parameters: Variable | complex | Sequence[Variable | complex], control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ParametricControlledOperationControlled-RY operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class CRZ(parameters: Variable | complex | Sequence[Variable | complex], control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ParametricControlledOperationControlled-RZ operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class CRotation(parameters: Variable | complex | Sequence[Variable | complex], control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ParametricControlledOperationControlled-Rotation operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class CSWAP(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationCSWAP (controlled SWAP) operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- matrix = array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])[source]#
- to_qasm(mapping: Mapping | None = None) str[source]#
Converts the CSWAP operation into an OpenQASM string.
Note, the CSWAP operation must be defined by decomposing into existing gates, e.g., using CNOT and Toffoli gates as follows:
gate cswap c, a, b { cx b, a; ccx c, a, b; cx b, a; }
- Parameters:
mapping – Optional mapping between qubits and their indices in the circuit.
- Returns:
OpenQASM string representation of the SWAP operation.
- Return type:
- class CX(control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ControlledOperationControlled-X (CNOT) operation.
- Parameters:
control – Qubit on which the target operation
Xis controlled.target – Qubit on which the target operation
Xis applied.
- class CY(control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ControlledOperationControlled-Y operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class CZ(control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ControlledOperationControlled-Z operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class Hadamard(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationHadamard operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class Identity(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationIdentity operator.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class RX(parameters: Variable | complex | Sequence[Variable | complex], qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
ParametricOperationRotation-X operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
parameters – Parameters for the operation. Required when constructing the matrix representation of the operation.
- class RY(parameters: Variable | complex | Sequence[Variable | complex], qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
ParametricOperationRotation-Y operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
parameters – Parameters for the operation. Required when constructing the matrix representation of the operation.
- class RZ(parameters: Variable | complex | Sequence[Variable | complex], qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
ParametricOperationRotation-Z operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
theta – Parameter for the operation. Required when constructing the matrix representation of the operation.
- class Rotation(parameters: Variable | complex | Sequence[Variable | complex], qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
ParametricOperationRotation operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
parameters – Parameters for the operation. Required when constructing the matrix representation of the operation.
- to_qasm(mapping: Mapping | None = None) str[source]#
Converts the Rotation operation into an OpenQASM string.
Note, the Rotation operation must be defined by decomposing into existing gates, e.g., using RY and RZ gates as follows:
gate rot(beta, gamma, delta) { rz(beta) q[0]; ry(gamma) q[0]; rz(delta) q[0]; }
- Parameters:
mapping – Optional mapping between qubits and their indices in the circuit.
- Returns:
OpenQASM string representation of the Rotation operation.
- Return type:
- class S(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationS (Phase) operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class SWAP(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationSWAP operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- matrix = array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])[source]#
- to_qasm(mapping: Mapping | None = None) str[source]#
Converts the SWAP operation into an OpenQASM string.
Note, the SWAP operation must be defined by decomposing into existing gates, e.g., using CNOT gates as follows:
gate swap a, b { cx b, a; cx a, b; cx b, a; }
- Parameters:
mapping – Optional mapping between qubits and their indices in the circuit.
- Returns:
OpenQASM string representation of the SWAP operation.
- Return type:
- class T(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationT operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class X(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationPauli X operator.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- class Y(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
OperationPauli Y operator.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.