QuantumBool#

class QuantumBool(qs=None, name=None)[source]#

QuantumBools are the quantum type, which represents boolean truth values. They are the return type of comparison operators like the equality ==.

Apart from their behavior as a QuantumVariable, they can also be treated like ControlEnvironments.

Examples

We create a QuantumBool and set it to uniform superposition

>>> from qrisp import QuantumBool, h
>>> q_bool_0 = QuantumBool()
>>> h(q_bool_0)
>>> print(q_bool_0)
{False: 0.5, True: 0.5}

We create a second QuantumBool and evaluate some logical functions

>>> q_bool_1 = QuantumBool()
>>> print(q_bool_1 | q_bool_0)
{False: 0.5, True: 0.5}
>>> print(q_bool_1 & q_bool_0)
{False: 1.0}

QuantumBools are the results of comparisons:

>>> from qrisp import QuantumFloat, QuantumChar
>>> q_ch = QuantumChar()
>>> q_ch[:] = {"g" : 1, "l" : -1}
>>> q_bool_2 = (q_ch == "g")
>>> q_bool_2.qs.statevector()
sqrt(2)*(|g>*|True> - |l>*|False>)/2

For QuantumFloats, numeric comparison is also possible:

>>> qf = QuantumFloat(4)
>>> h(qf[3])
>>> print(qf)
{0: 0.5, 8: 0.5}
>>> q_bool_3 = (qf >=  4)
>>> print(q_bool_3)
{False: 0.5, True: 0.5}

To use a QuantumBool as a ControlEnvironment, we simply put it in a with statement:

with q_bool_3:
    qf += 2
>>> print(qf)
{0: 0.5, 10: 0.5}

QuantumBools that are created directly after a with statement are uncomputed automatically:

with qf == 10:
    q_bool_3.flip()
>>> print(qf.qs)
QuantumCircuit:
--------------
                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        qf.0: ──────0           β”œβ”€β”€β”€β”€β”€β”€0          β”œβ”€β”€o─────────o──
                   β”‚            β”‚     β”‚           β”‚  β”‚         β”‚
        qf.1: ──────1           β”œβ”€β”€β”€β”€β”€β”€1          β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β– β”€β”€
                   β”‚            β”‚     β”‚  __iadd__ β”‚  β”‚         β”‚
        qf.2: ──────2           β”œβ”€β”€β”€β”€β”€β”€2          β”œβ”€β”€o─────────o──
              β”Œβ”€β”€β”€β”β”‚  less_than β”‚     β”‚           β”‚  β”‚         β”‚
        qf.3: ─ H β”œβ”€3           β”œβ”€β”€β”€β”€β”€β”€3          β”œβ”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”€β– β”€β”€
              β””β”€β”€β”€β”˜β”‚            β”‚β”Œβ”€β”€β”€β”β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β”‚  β”Œβ”€β”€β”€β”  β”‚
    lt_qbl.0: ──────4           β”œβ”€ X β”œβ”€β”€β”€β”€β”€β”€β– β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€ X β”œβ”€β”€β”Όβ”€β”€
                   β”‚            β”‚β””β”€β”€β”€β”˜               β”‚  β””β”€β”¬β”€β”˜  β”‚
lt_ancilla.0: ──────5           β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β”Œβ”€β”΄β”€β”  β”‚  β”Œβ”€β”΄β”€β”
  cond_env.0: ────────────────────────────────────── X β”œβ”€β”€β– β”€β”€β”€ X β”œ
                                                   β””β”€β”€β”€β”˜     β””β”€β”€β”€β”˜
Live QuantumVariables:
---------------------
QuantumFloat qf
QuantumBool lt_qbl

Note that there is only a single QuantumBool listed in the β€œLive QuantumVariables” section, because the QuantumBool of the comparison qf == 10 (called cond_env) has been uncomputed.

Methods#

QuantumBool.flip()

Flips the QuantumBool's value.