Allocate a register of qubits of size `qubit_count` and return a handle to them as a :class:`QuakeValue`. Args: initializer (Union[`int`,`QuakeValue`, `list[T]`): The number of qubits to allocate or a concrete state to allocate and initialize
(self, initializer=None)
| 767 | return cc.CastOp(stateTy, valRef) |
| 768 | |
| 769 | def qalloc(self, initializer=None): |
| 770 | """ |
| 771 | Allocate a register of qubits of size `qubit_count` and return a handle |
| 772 | to them as a :class:`QuakeValue`. |
| 773 | |
| 774 | Args: |
| 775 | initializer (Union[`int`,`QuakeValue`, `list[T]`): The number of |
| 776 | qubits to allocate or a concrete state to allocate and initialize |
| 777 | the qubits. |
| 778 | Returns: |
| 779 | :class:`QuakeValue`: A handle to the allocated qubits in the MLIR. |
| 780 | |
| 781 | ```python |
| 782 | # Example: |
| 783 | kernel = cudaq.make_kernel() |
| 784 | qubits = kernel.qalloc(10) |
| 785 | ``` |
| 786 | """ |
| 787 | with self.ctx, self.insertPoint, self.loc: |
| 788 | # If the initializer is an integer, create `veq<N>` |
| 789 | if isinstance(initializer, int): |
| 790 | veqTy = quake.VeqType.get(initializer) |
| 791 | return self.__createQuakeValue(quake.AllocaOp(veqTy).result) |
| 792 | |
| 793 | if isinstance(initializer, list): |
| 794 | initializer = np.array(initializer, dtype=type(initializer[0])) |
| 795 | |
| 796 | if isinstance(initializer, np.ndarray): |
| 797 | if len(initializer.shape) != 1: |
| 798 | raise RuntimeError("invalid initializer for qalloc " |
| 799 | "(np.ndarray must be 1D, vector-like)") |
| 800 | |
| 801 | if initializer.dtype not in [ |
| 802 | complex, np.complex128, np.complex64, float, np.float64, |
| 803 | np.float32, int |
| 804 | ]: |
| 805 | raise RuntimeError("invalid initializer for qalloc (must " |
| 806 | "be int, float, or complex dtype)") |
| 807 | |
| 808 | # Get current simulation precision and convert the initializer |
| 809 | # if needed. |
| 810 | simulationPrecision = self.simulationPrecision() |
| 811 | if simulationPrecision == cudaq_runtime.SimulationPrecision.fp64: |
| 812 | if initializer.dtype not in [complex, np.complex128]: |
| 813 | initializer = initializer.astype(dtype=np.complex128) |
| 814 | |
| 815 | if simulationPrecision == cudaq_runtime.SimulationPrecision.fp32: |
| 816 | if initializer.dtype != np.complex64: |
| 817 | initializer = initializer.astype(dtype=np.complex64) |
| 818 | |
| 819 | if initializer.dtype == np.complex64: |
| 820 | fltTy = self.getFloatType(width=32) |
| 821 | eleTy = ComplexType.get(fltTy) |
| 822 | else: |
| 823 | assert initializer.dtype == np.complex128 |
| 824 | fltTy = self.getFloatType(width=64) |
| 825 | eleTy = ComplexType.get(fltTy) |
| 826 |