# Help me debug the code for Codercise 3.2a

**URL:** <https://discuss.pennylane.ai/t/help-me-debug-the-code-for-codercise-3-2a/8912>\
**Category:** Codebook\
**Created:** [August 15, 2025, 2:54am UTC](https://discuss.pennylane.ai/t/help-me-debug-the-code-for-codercise-3-2a/8912 "2025-08-15T02:54:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wen\_Yi\_Hing](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/wen_yi_hing/32/3978_2.png) [@Wen\_Yi\_Hing](https://discuss.pennylane.ai/u/Wen_Yi_Hing)\
**Post date:** [August 15, 2025, 2:54am UTC](https://discuss.pennylane.ai/t/help-me-debug-the-code-for-codercise-3-2a/8912/1 "2025-08-15T02:54:09Z")

</div>

coeffs = [1/2, 1/8, 1/4]  
obs = [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliZ(0), qml.PauliZ(1)]  
H = qml.Hamiltonian(coeffs, obs)# Create the Hamiltonian here

control\_wires = [2, 3]  
estimation\_wires = [4, 5, 6, 7, 8 ,9]

dev = qml.device(“default.qubit”)

@qml.qnode(dev)  
def prep\_sel\_prep\_qpe(state):  
“”"  
Applies QPE to the PrepSelPrep encoding for the Hamiltonian H with some initial ground state

```auto
Args:
- state (list(float)): The initial state ground state candidate
Returns:
- np.ndarray(float): The output probabilities
"""

####################
###YOUR CODE HERE###
####################
qml.StatePrep(state, wires=[0, 1])

# 2. Apply Hadamards to the estimation wires to create a superposition.
for wire in estimation_wires:
    qml.Hadamard(wires=wire)

# 3. Apply the controlled unitary operations C-(U^(2^k)).
# This requires repeating the application of the controlled unitary.

for k in range(len(estimation_wires)):
    control_qubit = estimation_wires[k]
    # U^(2^k) is implemented by applying U, 2^k times.
    for _ in range(2**k):
        qml.ctrl(qml.PrepSelPrep(H, control_wires), control=control_qubit)

# 4. Apply the inverse Quantum Fourier Transform (this was the main bug).
qml.QFT(wires=estimation_wires)
return qml.probs(wires=estimation_wires)

```

# We know the matrix is diagonal, so the set of eigenstates is the computational basis

results = [prep\_sel\_prep\_qpe(state) for state in np.eye(4)]  
lambda\_ = sum([abs(coeff) for coeff in H.terms()[0]])  
eigenvalues = [lambda\_ \* np.cos(2 \* np.pi \* np.argmax(result) / 2 \*\* (len(estimation\_wires))) for result in results]

# Print the eigenvalues

print("E = ", eigenvalues)

Incorrect: your PREPSELPREP-QPE circuit isn’t quite right yet!, Can someone help me debug what is going wrong with my code.

---

<div class="post-metadata">

**Author:** ![Wen\_Yi\_Hing](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/wen_yi_hing/32/3978_2.png) [@Wen\_Yi\_Hing](https://discuss.pennylane.ai/u/Wen_Yi_Hing)\
**Post date:** [August 15, 2025, 5:03am UTC](https://discuss.pennylane.ai/t/help-me-debug-the-code-for-codercise-3-2a/8912/2 "2025-08-15T05:03:49Z")

</div>

Codercise 3.2b is almost the same and I also stuck with this codercise too. Please debug my code for Codercise 3.2b also.

H = qml.Hamiltonian([1/2, 1/8, 1/4], [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliZ(0), qml.PauliZ(1)])# Create the Hamiltonian here

control\_wires = [2, 3]  
estimation\_wires = [4, 5, 6, 7, 8 ,9]

dev = qml.device(“default.qubit”)

@qml.qnode(dev)  
def prep\_sel\_prep\_qpe(state):  
“”"  
Applies QPE to the Qubitization for the Hamiltonian H with some initial ground state

```auto
Args:
- state (list(float)): The initial state ground state candidate
Returns:
- np.ndarray(float): The output probabilities
"""

####################
###YOUR CODE HERE###
####################
qml.StatePrep(state, wires=[0,1])
for wire in estimation_wires:
    qml.Hadamard(wires=wire)

```

# Define the Qubitization unitary operator

```auto
U = qml.Qubitization(H, control_wires)
qml.QFT(wires=estimation_wires)
return qml.probs(wires=estimation_wires)

```

# We know the matrix is diagonal, so the set of eigenstates is the computational basis

results = [prep\_sel\_prep\_qpe(state) for state in np.eye(4)]  
lambda\_ = sum([abs(coeff) for coeff in H.terms()[0]])  
eigenvalues = [lambda\_ \* np.cos(2 \* np.pi \* np.argmax(result) / 2 \*\* (len(estimation\_wires))) for result in results]

# Print the eigenvalues

print("E = ", eigenvalues)

---

<div class="post-metadata">

**Author:** ![Wen\_Yi\_Hing](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/wen_yi_hing/32/3978_2.png) [@Wen\_Yi\_Hing](https://discuss.pennylane.ai/u/Wen_Yi_Hing)\
**Post date:** [August 15, 2025, 6:35am UTC](https://discuss.pennylane.ai/t/help-me-debug-the-code-for-codercise-3-2a/8912/3 "2025-08-15T06:35:29Z")

</div>

Never mind, I have solved Codercise 3.2a, I just use the loop:

num\_est = len(estimation\_wires)

for k, est\_wire in enumerate(estimation\_wires):  
repetitions = 2 \*\* (num\_est - k - 1)  
for \_ in range(repetitions):  
qml.ctrl(qml.PrepSelPrep, control=est\_wire)(  
H,control\_wires)

So I just need to replace qml.PrepSelPrep with qml.Qubitization for Codercize 3.2b?

---

<div class="post-metadata">

**Author:** ![daniela.murcillo](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/daniela.murcillo/32/3612_2.png) [@daniela.murcillo](https://discuss.pennylane.ai/u/daniela.murcillo)\
**Post date:** [August 15, 2025, 2:09pm UTC](https://discuss.pennylane.ai/t/help-me-debug-the-code-for-codercise-3-2a/8912/4 "2025-08-15T14:09:12Z")

</div>

you can try and see! 🧐
