qrisp.QuantumArray.reshape#
- QuantumArray.reshape(*args)[source]#
Adjusts the shape of the QuantumArray with similar semantics as numpy.ndarray.reshape.
Note
This method never allocates additional qubits and instead returns a “view”,
- Parameters:
- shapetuple
The target shape.
- Returns:
- resQuantumArray
The reshaped QuantumArray.
Examples
We create a 1-dimensional QuantumArray with \(2**n\) entries and reshape it into a n dimensional QuantumArray with 2 entries per dimension.
from qrisp import QuantumArray, QuantumFloat import numpy as np n = 3 qtype = QuantumFloat(n) qa = QuantumArray(qtype = qtype, shape = 2**n) qa[:] = np.arange(2**n) print(qa) # Yields: # {OutcomeArray([0, 1, 2, 3, 4, 5, 6, 7]): 1.0}
We can now reshape:
reshaped_qa = qa.reshape(tuple(n*[2])) print(reshaped_qa) # Yields: # {OutcomeArray([[[0, 1], # [2, 3]], # [[4, 5], # [6, 7]]]): 1.0}