From 7c88d847259fdaf52eaec6c160d02b5c08ab9c17 Mon Sep 17 00:00:00 2001 From: Hirmay Sandesara <56473003+Hirmay@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:49:02 +0530 Subject: [PATCH] Update state_preparation.py --- .../data_preparation/state_preparation.py | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/qiskit/circuit/library/data_preparation/state_preparation.py b/qiskit/circuit/library/data_preparation/state_preparation.py index d4abcd257d22..d813c7bf3df4 100644 --- a/qiskit/circuit/library/data_preparation/state_preparation.py +++ b/qiskit/circuit/library/data_preparation/state_preparation.py @@ -275,18 +275,19 @@ def __init__( num_superpos_states (int): A positive integer M = num_superpos_states (> 1) representing the number of computational basis states with an amplitude of 1/sqrt(M) in the uniform superposition - state (:math:`\frac{1}{\sqrt{M}} \sum_{j=0}^{M-1} \ket{j}`, where - :math:`1< M <= 2^n`). Note that the remaining (2^n - M) computational basis + state (:math:`\frac{1}{\sqrt{M}} \sum_{j=0}^{M-1} |j\rangle`, where + :math:`1< M <= 2^n`). Note that the remaining (:math:`2^n - M`) computational basis states have zero amplitudes. Here M need not be an integer power of 2. num_qubits (int): - A positive integer representing the number of qubits used. + A positive integer representing the number of qubits used. If num_qubits is None + or is not specified, then num_qubits is set to ceil(log2(num_superpos_states)). Raises: ValueError: num_qubits must be an integer greater than or equal to log2(num_superpos_states). """ - if not (isinstance(num_superpos_states, int) and (num_superpos_states > 1)): + if not num_superpos_states > 1: raise ValueError("num_superpos_states must be a positive integer greater than 1.") if num_qubits is None: num_qubits = int(math.ceil(math.log2(num_superpos_states))) @@ -298,11 +299,7 @@ def __init__( super().__init__("USup", num_qubits, [num_superpos_states]) def _define(self): - """ - Defines the gate operation. - - """ - qreg = QuantumRegister(self._num_qubits, "q") + qc = QuantumCircuit(self._num_qubits) num_superpos_states = self.params[0] @@ -311,29 +308,29 @@ def _define(self): num_superpos_states & (num_superpos_states - 1) ) == 0: # if num_superpos_states is an integer power of 2 m = int(math.log2(num_superpos_states)) - qc.h(qreg[0:m]) + qc.h(range(m)) self.definition = qc return - n_value = [int(x) for x in list(np.binary_repr(num_superpos_states))][::-1] + n_value = [int(x) for x in reversed(np.binary_repr(num_superpos_states))] k = len(n_value) l_value = [index for (index, item) in enumerate(n_value) if item == 1] # Locations of '1's - qc.x(qreg[l_value[1:k]]) + qc.x(l_value[1:k]) m_current_value = 2 ** (l_value[0]) theta = -2 * np.arccos(np.sqrt(m_current_value / num_superpos_states)) if l_value[0] > 0: # if num_superpos_states is even - qc.h(qreg[0 : l_value[0]]) - qc.ry(theta, qreg[l_value[1]]) - qc.ch(qreg[l_value[1]], qreg[l_value[0] : l_value[1]], ctrl_state="0") + qc.h(range(l_value[0])) + qc.ry(theta, l_value[1]) + qc.ch(l_value[1], range(l_value[0],l_value[1]), ctrl_state="0") for m in range(1, len(l_value) - 1): theta = -2 * np.arccos( np.sqrt(2 ** l_value[m] / (num_superpos_states - m_current_value)) ) - qc.cry(theta, qreg[l_value[m]], qreg[l_value[m + 1]], ctrl_state="0") - qc.ch(qreg[l_value[m + 1]], qreg[l_value[m] : l_value[m + 1]], ctrl_state="0") - m_current_value = m_current_value + 2 ** (l_value[m]) + qc.cry(theta, l_value[m], l_value[m + 1], ctrl_state="0") + qc.ch(l_value[m + 1], range(l_value[m],l_value[m + 1]), ctrl_state="0") + m_current_value = m_current_value + 2 ** l_value[m] self.definition = qc