I’ve completed this section, and I’d like to play around with various number lists. However, I don’t understand how to create the final bar graph. What should I write as arguments for plt.bar?
Hi @Mayu
The plot is created for you in the backend. If it’s helpful to you, here is the code. It’s made with the altair
library.
import pennylane as qml
import pennylane.numpy as np
import pandas as pd
import altair as alt
numbers = [1,3,4,7,13]
total_sum = sum(numbers)
# Number of qubits needed (each number is represented by a qubit)
n_wires = len(numbers)
# Define the mixer Hamiltonian
mixer_h = qml.Hamiltonian([1.0] * n_wires, [qml.PauliX(i) for i in range(n_wires)])
dev = qml.device('default.qubit', wires=n_wires)
cost_h=build_cost_number_partition(numbers)
probs_qaoa,best_params=QAOA_number_partition(params, p,step_size,max_steps,cost_h)
output = np.real(probs_qaoa)
output_from_params=np.real(probability_circuit(best_params,p,cost_h))
# Add the plot
source = pd.DataFrame(
{"Basis state": [np.binary_repr(j, n_wires) for j in range(2**n_wires)], "Probability": output.tolist()}
)
chart = alt.Chart(source).mark_bar().encode(x="Basis state", y="Probability").properties(
width=500,
height=400
)
Let me know if this is what you wanted, or if there’s anything else I can provide!
Cheers,
Alvaro
@Alvaro_Ballon Thank you for the code! That’s exactly what I was looking for! It works! But my output looks weird. I tried the same numbers, [1, 3, 4, 7, 13]. I think I might have made a mistake in either the cost_function or the probability_function.
@Mayu that’s right.
These are functions you should have coded in V.3.1.a and V.3.1.b. If your graph is not correct it means that either these functions or the ones you coded in V.3.3 are not correct. Keep working on them!
@CatalinaAlbornoz Oops! I’ll check them again.
Congratulations @Mayu !
1 Like
Thanks, @CatalinaAlbornoz !