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
withstatement:with q_bool_3: qf += 2
>>> print(qf) {0: 0.5, 10: 0.5}
QuantumBools that are created directly after a
withstatement 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_qblNote that there is only a single QuantumBool listed in the βLive QuantumVariablesβ section, because the QuantumBool of the comparison
qf == 10(calledcond_env) has been uncomputed.
Methods#
Flips the QuantumBool's value. |