Skip to main content
In addition to the quantum counterparts of Python types described in Section 4, Rhyme provides quantum types for unsigned integers, individual bits, and characters. Their basis values are ordinary Python integers or single-character strings, but each quantum object can hold a superposition of several such values. As in Section 4, the constructor arguments specify the values to include in the initial uniform superposition. If no values are given, the object starts at its type’s canonical zero value.

5.1 quint: unsigned quantum integers

A quint holds a superposition of non-negative integers.
size is a bit-width assertion, not a conversion or an instruction to truncate integers. For example, size=3 asserts that only unsigned integers representable using 3 bits (that is, in the range 0 to 7) may occur as basis values of this object. If the assertion is violated, the behavior is undefined. size=0 means unrestricted bit width. Useful class methods:
A positive size is required when calling all().

Fixed-width shortcuts

These functions are shortcuts for quint(..., size=n); they return quint objects, rather than distinct classes:

Binary helper functions

quint supports bit_pair(), bit_lr(), and bit_rot_angle() using the standard binary representation of unsigned integers. Bit index 0 is the least-significant bit. All three helpers behave as described in Section 3.4, including the handling of control bits where applicable.

5.2 qbit: a single quantum bit

A qbit holds a superposition of the integers 0 and 1. It is a specialized quint with a fixed bit width of one. Unlike qbool, which uses the Python values False and True, qbit uses integer basis values.
Because there are only two possible basis values, qbit provides convenient short forms for several operations:
For phase changes, either pass a function or give two angles, one for each basis value:
qbit also supports combine(u11, u12, u21, u22) without an explicit pair or left/right function. The following static helpers are provided if you want to supply the pair explicitly: For example, bit.interfere(qbit.pair01, qbit.lr0) is equivalent to bit.interfere().

Binary helper functions

As a subclass of quint, qbit inherits bit_pair(), bit_lr(), and bit_rot_angle(). Only bit index 0 is valid. The helpers pair 0 with 1, identify 0 as the “left” value, and select angle_0 or angle_1 according to the bit value. Since there is only one bit, no control bits can be specified.

5.3 qchar: quantum characters

A qchar holds a superposition of individual characters, each represented by a Python string of length one.
By default, Unicode characters are allowed. The assertion ascii_only=True restricts valid basis values to ASCII characters. If the assertion is violated, the behavior is undefined. Unlike qstr, qchar allows chr(0) as a basis value. This is its canonical zero value, whereas the canonical zero value of qstr is the empty string "". You can also request a uniform superposition over all valid characters:
Currently, qchar.all() supports only ASCII characters (ascii_only=True). It creates a uniform superposition over all 128 ASCII characters, including chr(0).

qchar7: ASCII quantum characters

qchar7 is a convenience function, equivalent to qchar(*c, ascii_only=True, name=name). The name qchar7 refers to the seven-bit ASCII character set.
It returns a qchar, not a distinct quantum class.

Binary helper functions

qchar supports bit_pair(), bit_lr(), and bit_rot_angle() only when ascii_only=True. Each character is represented using 7 bits, indexed from 0 (least significant) to 6 (most significant). The helpers operate on the ASCII code of the character and behave as described in Section 3.4, including the handling of control bits where applicable. For example:
Using these helpers with a non-ASCII qchar results in undefined behavior.

5.4 Which type should you use?

Choose the type according to the meaning of its basis values, not merely their binary representation. For example, use qbit for the integers 0 and 1, and qbool for the logical values False and True.

5.5 Common behavior

All three classes inherit the qobject methods: observe(), measure(), add_phase(), interfere(), flip(), rotate(), combine(), discard(), and the binary helpers. x.observe() returns a Python int for quint and qbit, and a str for qchar. It also collapses the quantum value.