qrisp.QuantumVariable.init_state#

QuantumVariable.init_state(state_dic)[source]#

The init_state method allows the initialization of arbitrary quantum states. It recieves a dictionary of the type

{value : complex number}

and initializes the normalized state. Amplitudes not specified are assumed to be zero.

Note that the state initialization algorithm requires it’s qubits to be in state \(\ket{0}\).

A shorthand for this method is the [:] operator, when handed the corresponding dictionary

Parameters:
state_dicdict

Dictionary describing the wave function to be initialized.

Raises:
Exception

Tried to initialize qubits which are not fresh anymore.

Examples

We create a QuantumFloat and encode the state

\[\ket{\psi} = \sqrt{\frac{1}{3}} \ket{0.5} + i\sqrt{\frac{2}{3}} \ket{2}\]
>>> from qrisp import QuantumFloat
>>> qf = QuantumFloat(3, -1)

We can now use either

>>> qf.init_state({0.5: (1/3)**0.5, 2.0 : 1j*(2/3)**0.5})

or:

>>> qf[:] = {0.5: (1/3)**0.5, 2.0 : 1j*(2/3)**0.5}

To acquire the expected result

>>> print(qf)
{2.0: 0.6667, 0.5: 0.3333}