qrisp.QuantumArray.encode#
- QuantumArray.encode(encoding_array)[source]#
The encode method allows to quickly bring a QuantumArray in a desired computational basis state. For this, it performs a circuit, bringing fresh qubits into the integer state specified by the encoder.
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).
- Parameters:
- value
A value supported by the encoder.
Examples
We create a QuantumArray and encode the identity matrix.
>>> from qrisp import QuantumArray, QuantumFloat >>> qtype = QuantumFloat(5) >>> q_array = QuantumArray(qtype, (4,4)) >>> q_array.encode(np.eye(4)) >>> print(q_array) {OutcomeArray([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]): 1.0}
Using the slice operator we can also encode on slices of QuantumArrays
>>> q_array = QuantumArray(qtype, (4,4)) >>> q_array[:,:2] = np.ones((4,2)) >>> print(q_array) {OutcomeArray([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]): 1.0}