Bits

ParityOS supports both quantum and classical binary variables under a common interface Bit. The Bit instances just represent a variable and hold no information about its value. They are hashable and comparable objects that can be uniquely identified by their hashable and comparable id property.

Qubits

ParityOS offers a simple Qubit class that users can easily extend through subclassing to define their own more complex qubit types. Qubits are subclasses of Bit.

They can take any hashable and sortable object as identifier (Qubit.id) through which they can be uniquely identified together with their type. Two distinct Qubit instances of the same type and with the same identifier are considered the same qubit.

Qubits come with a default prefix "q_" before their id when stringifying them.

The get_q function eases qubit creation from various formats of input identifiers.

from parityos.bits import Bit, Qubit, get_q
from parityos.utils.point import Point2D

q0 = Qubit(0)
assert q0.id == 0
assert str(q0) == "q_0"
assert get_q(0) == q0
assert isinstance(q0, Qubit)
assert isinstance(q0, Bit)

qa = Qubit("a")
assert qa.id == "a"
assert str(qa) == "q_a"
assert get_q("a") == qa
assert isinstance(qa, Qubit)
assert isinstance(qa, Bit)

q1_1 = Qubit(Point2D(1, 1))
assert q1_1.id == Point2D(1, 1)
assert str(q1_1) == "q_(1, 1)"
assert get_q(1, 1) == q1_1
assert get_q((1, 1)) == q1_1
assert get_q(Point2D(1, 1)) == q1_1
assert isinstance(q1_1, Qubit)
assert isinstance(q1_1, Bit)

assert sorted([q1_1, get_q(2), qa, get_q("B"), q0]) == [q1_1, q0, get_q(2), get_q("B"), qa]

Classical Bits

Classical bits are represented by the Cbit with an integer id identifier. Similar to a Qubit, a Cbit is the stringified by prepending "c_" to its identifier.

Cbits also support boolean logic for constructing boolean expressions (CbitExpression). These can be used as conditions e.g. for ConditionalOperators.

from parityos.bits import Bit, Cbit, CbitExpression

c0, c1, c2 = Cbit(0), Cbit(1), Cbit(2)

assert c0.id == 0
assert str(c0) == "c_0"
assert isinstance(c0, Bit)

expression = c0 ^ (c1 | ~c2)
assert isinstance(expression, CbitExpression)
assert str(expression) == "c_0 ^ (c_1 | ~c_2)"