def variance_experiment(n_shots):
dev = qml.device("default.qubit", wires = 1, shots = n_shots)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
return qml.expval(qml.PauliZ(wires=0))
variance = []
for _ in range(n_trials):
variance.append(circuit())
return np.var(variance)
def variance_scaling(n_shots):
estimated_variance = 0
dev = qml.device("default.qubit", wires = 1, shots = n_shots)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires = 0)
return qml.sample(qml.PauliZ(wires = 0))
estimated_variance = np.var(circuit())
return estimated_variance
shot_vals = [10, 20, 40, 100, 200, 400, 1000, 2000, 4000, 10000]
results_experiment = [variance_experiment(shots) for shots in shot_vals]
results_scaling = [variance_scaling(shots) for shots in shot_vals]
plot = plotter(shot_vals, results_experiment, results_scaling)
I’m getting an error: “Incoorect: Your variance fuction doesn’t look quite right”
I’m unable to understand what does the question mean by varience in expectation value when we run the experiment n_shot number of times
Hi @Mohammed_Bin_Ali_Maq , welcome to the Forum!
What the question is asking is not that you code the whole function again but instead that you look at the graph below and try to predict the shape of the function. Is the variance directly proportional to the number of shots? Or is it the opposite? Or something else?
I hope this helps you!
1 Like
Mayu
May 22, 2024, 1:31pm
3
I’m also confused about this part. I’m not sure what to write in the second function.
You are supposed to give a guess to the analytical math expression of the plotted result.
Hint: It is hyperbola
2 Likes
Mayu
May 23, 2024, 12:36am
6
I see!! Thanks! I’ll try it again!
Well done @Mayu , congratulations !
1 Like
Mayu
May 24, 2024, 10:01pm
9
@CatalinaAlbornoz , please could you help me with this exercise?
I did this:
def variance_experiment(n_shots):
n_trials = 100
dev = qml.device("default.qubit", wires = 1, shots = n_shots)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
return qml.expval(qml.PauliZ(wires=0))
variance = []
for _ in range(n_trials):
variance.append(circuit())
return np.var(variance)
Plus:
def variance_scaling(n_shots):
estimated_variance = 1.0 / (n_shots + 1.0)
return estimated_variance
My graphs are pretty close. However, I get this error: “Your variance function doesn’t look quite right”.
Please could you help me? I have no idea what I am doing wrong.
Thanks
Hi @Marcia_Hon ,
You’re right you’re pretty close! I’ve blurred the answer so that nobody accidentally gets a spoiler.
Are you sure that you need that “+ 1.0” in your variance scaling? What happens if you have 0 shots? Can you treat this as a special case?
I hope this helps you solve the codercise!
Thanks so much! I passed!
1 Like