Skip to content

Commit

Permalink
Update state_preparation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Hirmay authored Jul 2, 2024
1 parent 58c71eb commit 7c88d84
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions qiskit/circuit/library/data_preparation/state_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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]
Expand All @@ -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

0 comments on commit 7c88d84

Please sign in to comment.