Help me debug the code for Codercise 3.2a

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

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.

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

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

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)

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?

you can try and see! :face_with_monocle: