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:
Operation
Class 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:
Operation
Generic 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
qubits
is passed, it takes precedence overcontrol
andtarget
, and replaces those two attributes using the operations_num_control
and_num_target
class attributes.
- matrix[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedproperty
decorator differs from the regularproperty
decorator 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
self
is 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
mixedproperty
decorator differs from the regularproperty
decorator 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
self
is 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
mixedproperty
decorator differs from the regularproperty
decorator 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
self
is 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:
Operation
Class 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:
object
Class 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
_cond
attribute, conditioning the operation on one or more bits.
- decomposition[source]#
Decorator class for creating a property for a class/instance method.
Note that the
mixedproperty
decorator differs from the regularproperty
decorator 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
self
is 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
mixedproperty
decorator differs from the regularproperty
decorator 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
self
is 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:
Operation
Class 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
None
then variable values are used (if existent).inplace – Whether to evaluate the parameters on
self
or on a copy ofself
(returned).
- Returns:
Either
self
or 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
mixedproperty
decorator differs from the regularproperty
decorator 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
self
is 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
Operation
class.- 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:
Operation
CCX (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:
ControlledOperation
Controlled-Hadamard operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- to_qasm(mapping: Mapping | None = None) str [source]#
Converts the Controlled-Hadamard operation into an OpenQASM string.
- Parameters:
mapping – Optional mapping between qubits and their indices in the circuit.
- Returns:
OpenQASM string representation of the Controlled-Hadamard operation.
- Return type:
- class CRX(parameters: Variable | complex | Sequence[Variable | complex], control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ParametricControlledOperation
Controlled-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:
ParametricControlledOperation
Controlled-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:
ParametricControlledOperation
Controlled-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:
ParametricControlledOperation
Controlled-Rotation operation.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.
- to_qasm(mapping: Mapping | None = None) str [source]#
Converts the Controlled-Rotation operation into an OpenQASM string.
- Parameters:
mapping – Optional mapping between qubits and their indices in the circuit.
- Returns:
OpenQASM string representation of the Controlled-Rotation operation.
- Return type:
- class CSWAP(qubits: Qubit | Sequence[Qubit] | None = None)[source]#
Bases:
Operation
CSWAP (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:
ControlledOperation
Controlled-X (CNOT) operation.
- Parameters:
control – Qubit on which the target operation
X
is controlled.target – Qubit on which the target operation
X
is applied.
- class CY(control: Qubit | Sequence[Qubit] | None = None, target: Qubit | Sequence[Qubit] | None = None, **kwargs)[source]#
Bases:
ControlledOperation
Controlled-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:
ControlledOperation
Controlled-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:
Operation
Hadamard 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:
Operation
Identity 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:
ParametricOperation
Rotation-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:
ParametricOperation
Rotation-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:
ParametricOperation
Rotation-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:
ParametricOperation
Rotation 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:
Operation
S (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:
Operation
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, 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:
Operation
T 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:
Operation
Pauli 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:
Operation
Pauli Y operator.
- Parameters:
qubits – Qubits on which the operation should be applied. Only required when applying an operation within a circuit context.