bits¶
Defines various classes describing classical or quantum bits.
Bits are identified by unique identifiers, which can by any hashable, comparable type.
Defines classical bits (Cbit) and logical operations on them as well as quantum bits (Qubit).
Supplies helper functions get_q and get_c for easily creating qubits and classical bits from
their identifiers.
- class parityos.bits.Identifier(*args, **kwargs)¶
Bases:
Hashable,SupportsLessThan,ProtocolA class representing hashable, comparable identifiers.
- class parityos.bits.IdentifierT_co¶
A covariant
TypeVarrepresenting anIdentifier.alias of TypeVar(‘IdentifierT_co’, bound=
Identifier, covariant=True)
- class parityos.bits.Bit¶
Bases:
Generic[IdentifierT_co],ABCInterface for quantum bits and classical bits.
All bits can be uniquely identified by their hashable, comparable
ididentifier.- STR_PREFIX: ClassVar[str]¶
Prefix to be added before
idin the string representation for discriminating differentBittypes. Subclasses can override thisClassVar.Example
>>> print(Cbit(3)) # 'c_3' >>> print(Qubit("a")) # 'q_a'
- abstract property id: IdentifierT_co¶
Hashable comparable identifier used as unique identifier for this bit.
- class parityos.bits.HasFrozenCBits¶
Bases:
ABCMixin interface for an object holding an immutable collection of classical bits.
- class parityos.bits.HasFrozenUnorderedCBits¶
Bases:
HasFrozenCBits,ABCMixin interface for an object holding an unordered set of classical bits.
- class parityos.bits.HasFrozenOrderedCBits¶
Bases:
HasFrozenCBits,ABCMixin interface for an object holding an ordered sequence of classical bits.
- class parityos.bits.Logical¶
Bases:
ABCMixin interface for logical operations on classical bits.
Supported logical operations:
NOT (
~)AND (
&)OR (
|)XOR (
^)
Examples
>>> a, b, c = Cbit(1), Cbit(2), Cbit(3) >>> (~a | b) ^ (a & b & ~c) >>> # CbitExpression(expr=(c_2 | ~c_1) ^ (c_1 & c_2 & ~c_3))
- class parityos.bits.Cbit(id: int)¶
-
A classical bit that can be used as output for measurements and classical control logic within a quantum circuit.
- STR_PREFIX: ClassVar[str] = 'c_'¶
Prefix to be added before
idin the string representation for discriminating differentBittypes. Subclasses can override thisClassVar.Example
>>> print(Cbit(3)) # 'c_3' >>> print(Qubit("a")) # 'q_a'
- classmethod from_str(str_representation: str) Self¶
Convert from a string representation to a classical bit.
Caution
str_representationmust start withSTR_PREFIXand the remaining part must be convertible to an integer.- Parameters:
str_representation – The string to be converted. Must start with
STR_PREFIXand continue with a string that can be converted to an integer.- Returns:
A classical bit from the input.
- Raises:
ParityOSException – If
str_representationdoes not start withSTR_PREFIX.ValueError – If the remaining part after
STR_PREFIXcan’t be converted to integer.
- class parityos.bits.CbitExpression(expr: Boolean)¶
Bases:
Logical,HasFrozenCBitsAn expression consisting of classical bits.
CbitExpressioninstances are usually the result of logical operations on elementaryCbitinstances.Examples
>>> a, b, c = Cbit(1), Cbit(2), Cbit(3) >>> (~a | b) ^ (a & b & ~c) >>> # CbitExpression(expr=(c_2 | ~c_1) ^ (c_1 & c_2 & ~c_3))
Note
While a
CbitExpressioncan be directly created from aBooleanexpression, the simpler and more natural way to create one is through boolean arithmetic onCbitinstances.
- parityos.bits.reduce_and(*args: Logical) CbitExpression¶
Join all input args with logical AND (
&).Example
>>> c0, c1, c2 = Cbit(0), Cbit(1), Cbit(2) >>> assert reduce_and(c0, c1, c2) == c0 & c1 & c2
- parityos.bits.reduce_or(*args: Logical) CbitExpression¶
Join all input args with logical OR (
|).Example
>>> c0, c1, c2 = Cbit(0), Cbit(1), Cbit(2) >>> assert reduce_or(c0, c1, c2) == c0 | c1 | c2
- parityos.bits.reduce_xor(*args: Logical) CbitExpression¶
Join all input args with logical XOR (
^).Example
>>> c0, c1, c2 = Cbit(0), Cbit(1), Cbit(2) >>> assert reduce_xor(c0, c1, c2) == c0 ^ c1 ^ c2
- class parityos.bits.Qubit(id: IdentifierT_co)¶
Bases:
Bit[IdentifierT_co]A quantum bit.
Used as arguments to
OperatororStateinstances to define the Hilbert space these objects act or are defined on.Note
A
Qubitis an abstract binary quantum variable with a unique identifier and is a priori not associated with any operator eigenbasis (e.g. Pauli Basis). Associations with operator eigenspaces is done in various classes containing qubits (cf. e.g.PauliBasisState).- STR_PREFIX: ClassVar[str] = 'q_'¶
Prefix to be added before
idin the string representation for discriminating differentBittypes. Subclasses can override thisClassVar.Example
>>> print(Cbit(3)) # 'c_3' >>> print(Qubit("a")) # 'q_a'
- id: IdentifierT_co¶
Hashable, comparable identifier for this quantum bit.
- class parityos.bits.QubitT_co¶
A covariant
TypeVarrepresenting aQubit.alias of TypeVar(‘QubitT_co’, bound=
Qubit, covariant=True)
- class parityos.bits.OtherQubitT¶
A
TypeVarrepresenting anotherQubit.alias of TypeVar(‘OtherQubitT’, bound=
Qubit)
- class parityos.bits.OtherQubitT_co¶
A covariant
TypeVarrepresenting anotherQubit.alias of TypeVar(‘OtherQubitT_co’, bound=
Qubit, covariant=True)
- class parityos.bits.HasFrozenQubits¶
Bases:
Generic[QubitT_co],ABCMixin interface for an object with qubits.
- class parityos.bits.HasFrozenUnorderedQubits¶
Bases:
HasFrozenQubits[QubitT_co],ABCMixin interface for an object with an unordered set of qubits.
- class parityos.bits.HasFrozenOrderedQubits¶
Bases:
HasFrozenQubits[QubitT_co],ABCMixin interface for an object with an ordered sequence of qubits.
- parityos.bits.get_q(x: RealT, y: RealT) Qubit[Point2D[RealT]]¶
- parityos.bits.get_q(coordinates: tuple[RealT, RealT]) Qubit[Point2D[RealT]]
- parityos.bits.get_q(x: RealT, y: RealT, z: RealT) Qubit[Point3D[RealT]]
- parityos.bits.get_q(coordinates: tuple[RealT, RealT, RealT]) Qubit[Point3D[RealT]]
- parityos.bits.get_q(id: IdentifierT_co) Qubit[IdentifierT_co]
Helper function for easily creating different kinds of qubits from various formats of their identifiers.
Examples
>>> assert get_q(1) == Qubit(1) >>> assert get_q("A") == Qubit("A") >>> assert get_q(0, 1) == Qubit(Point2D(0, 1)) >>> assert get_q((0, 1)) == Qubit(Point2D(0, 1)) >>> assert get_q(Point2D(0, 1)) == Qubit(Point2D(0, 1)) >>> assert get_q(0, 1, 2) == Qubit(Point3D(0, 1, 2)) >>> assert get_q((0, 1, 2)) == Qubit(Point3D(0, 1, 2)) >>> assert get_q(Point3D(0, 1, 2)) == Qubit(Point3D(0, 1, 2))