QuantumVariable.encode(value, permit_dirtyness=False)[source]#

The encode method allows to quickly bring a QuantumVariable in a desired computational basis state.

A shorthand for this method is given by the [:] operator.

Note that the qubits to initialize have to be fresh (i.e. no operations performed on them).


A value supported by the encoder.

permit_dirtynessbool, optional

Surpresses the error message when calling encode on dirty qubits.



We create two quantum floats and encode the value 2.5. For one of them, we perform an x gate onto the corresponding qubits, resulting in an error.

>>> from qrisp import QuantumFloat, x
>>> qf_0 = QuantumFloat(3, -1, signed = False)
>>> qf_1 = QuantumFloat(3, -1, signed = False)
>>> x(qf_0)
>>> qf_0.encode(2.5)
Exception: Tried to initialize qubits which are not fresh anymore.
>>> qf_1[:] = 2.5
>>> print(qf_1)
{2.5: 1.0}