qrisp.QuantumArray.init_from#
- QuantumArray.init_from(other)[source]#
Performs the
init_from
method on all containing QuantumVariables based on their equivalent in the QuantumArray other. Note that this method does not copy the quantum state. For more details check the documentation of the init_from method of QuantumVariable.A shorthand for this method is the slicing operator
[:]
.- Parameters:
- otherQuantumArray
The QuantumArray from which to initiate.
- Raises:
- Exception
Tried to initialize from array of invalied shape or qtype.
Examples
We create a QuantumArray and bring it into superposition
>>> import numpy as np >>> from qrisp import QuantumArray, QuantumFloat, h, multi_measurement >>> qtype = QuantumFloat(5) >>> q_array_a = QuantumArray(qtype) >>> q_array_a[:] = np.eye(3) >>> h(q_array_a[0,0][0]) >>> print(q_array_a) {OutcomeArray([[0, 0, 0], [0, 1, 0], [0, 0, 1]]): 0.5, OutcomeArray([[1, 0, 0], [0, 1, 0], [0, 0, 1]]): 0.5}
We now duplicate this array and initiate
>>> q_array_b = q_array_a.duplicate() >>> q_array_b.init_from(q_array_a) >>> print(multi_measurement([q_array_a, q_array_b])) {(OutcomeArray([[0, 0, 0], [0, 1, 0], [0, 0, 1]]), OutcomeArray([[0, 0, 0], [0, 1, 0], [0, 0, 1]])): 0.5, (OutcomeArray([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), OutcomeArray([[1, 0, 0], [0, 1, 0], [0, 0, 1]])): 0.5}
We can achieve the same with the slicing operator
>>> q_array_c = QuantumArray(qtype, shape = (3, 3)) >>> q_array_c[1,:] = q_array_a[0,:] >>> print(multi_measurement([q_array_b, q_array_c])) {(OutcomeArray([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), OutcomeArray([[0, 0, 0], [0, 0, 0], [0, 0, 0]])): 0.5, (OutcomeArray([[0, 0, 0], [1, 0, 0], [0, 0, 0]]), OutcomeArray([[0, 0, 0], [1, 0, 0], [0, 0, 0]])): 0.5}