# qrisp.QuantumVariable.init_from#

QuantumVariable.init_from(other)[source]#

Method to initiate a QuantumVariable based on the state of another. This method does NOT copy the state. Much rather it performs the operation

$U_{\text{init_from}} \left( \sum_{x \in \text{labels}} a_x \ket{x} \right) \ket{0} = \sum_{x \in \text{labels}} a_x \ket{x} \ket{x}$

This is different from a state copying operation:

$U_{\text{copy}} \left( \sum_{x \in \text{labels}} a_x \ket{x} \right) \ket{0} = \left( \sum_{x \in \text{labels}} a_x \ket{x} \right) \left( \sum_{x \in \text{labels}} a_x \ket{x} \right)$

A shorthand for initiating this way is the [:] operator.

Parameters
otherQuantumVariable

The QuantumVariable from which to initiate.

Raises
Exception

Tried to initialize qubits which are not fresh anymore.

Examples

We create a QuantumFloat, and bring it into superposition.

>>> from qrisp import QuantumFloat, h, multi_measurement
>>> qf_a = QuantumFloat(8)
>>> qf_a[:] = 6
>>> h(qf_a[0])
>>> print(qf_a)
{6: 0.5, 7: 0.5}


We now duplicate and initiate the duplicate

>>> qf_b = qf_a.duplicate()
>>> print(qf_b)
{0: 1.0}
>>> qf_b.init_from(qf_a)
>>> print(multi_measurement([qf_a, qf_b]))
{(6, 6): 0.5, (7, 7): 0.5}


The slicing operator achieves the same:

>>> qf_c = qf_a.duplicate()
>>> qf_c[:] = qf_b
>>> print(multi_measurement([qf_a, qf_b, qf_c]))
{(6, 6, 6): 0.5, (7, 7, 7): 0.5}