Explanation for Codebook Exercise I.10.4

  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!