4

The code that I follow is from this tutorial.
But there are some errors due to the version of qiskit on the Estimator primitive

from qiskit.primitives import Estimator
estimator = Estimator([qc], [H]) 
e_result = estimator([qc], [H], [theta])
print(e_result)

This is easy to solve when I fixed it by add estimator attribute like this one

from qiskit.primitives import Estimator
estimator = Estimator([qc], [H]) 
e_result = estimator.run([qc], [H], [theta])
print(e_result.result())`

But there is a problem I need your help with regarding the minimize by SPSA, I can't know how to variable the objective. I have tried to add an attribute run on the estimator but now it doesn't work

# define objective as expectation value of Hamiltonian with circuit ansatz

objective = lambda x: estimator.run([qc], [H], [x]).values[0]

instantiate optimizer

from qiskit.algorithms.optimizers import SPSA optimizer = SPSA(maxiter=500)

define initial values for our circuit parameters

x0 = np.random.rand(qc.num_parameters)

minimize the objective function

result = optimizer.minimize(objective, x0=x0)

store ground state parameters for later

ground_state_params = result.x

print the resulting ground state energy

print(result.fun)

Here is the error

    Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
d:\Book store\Learning document\QC\Code\qce22-qiskit-runtime-tutorial-main\docs\tutorials\Building algorithms with Qiskit Runtime.ipynb Cell 24 in <cell line: 5>()
      2 x0 = np.random.rand(qc.num_parameters)
      4 # minimize the objective function
----> 5 result = optimizer.minimize(objective, x0=x0)
      7 # store ground state parameters for later
      8 ground_state_params = result.x

File c:\Users\DELL\anaconda3\lib\site-packages\qiskit\algorithms\optimizers\spsa.py:512, in SPSA.minimize(self, fun, x0, jac, bounds) 502 def minimize( 503 self, 504 fun: Callable[[POINT], float], (...) 509 # ensure learning rate and perturbation are correctly set: either none or both 510 # this happens only here because for the calibration the loss function is required 511 if self.learning_rate is None and self.perturbation is None: --> 512 get_eta, get_eps = self.calibrate(fun, x0, max_evals_grouped=self._max_evals_grouped) 513 else: 514 get_eta, get_eps = _validate_pert_and_learningrate( 515 self.perturbation, self.learning_rate 516 )

File c:\Users\DELL\anaconda3\lib\site-packages\qiskit\algorithms\optimizers\spsa.py:333, in SPSA.calibrate(loss, initial_point, c, stability_constant, target_magnitude, alpha, gamma, modelspace, max_evals_grouped) 330 pert = bernoulli_perturbation(dim) ... d:\Book store\Learning document\QC\Code\qce22-qiskit-runtime-tutorial-main\docs\tutorials\Building algorithms with Qiskit Runtime.ipynb Cell 24 in <lambda>(x) 1 # define objective as expectation value of Hamiltonian with circuit ansatz ----> 3 objective = lambda x: estimator.run([qc], [H], [x]).values[0]

AttributeError: 'PrimitiveJob' object has no attribute 'values'

Please help me know if you have the answer, thanks

luciano
  • 5,763
  • 1
  • 12
  • 34

0 Answers0