After running this code: enter image description here

I have an error:

KeyError Traceback (most recent call last) ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 3079 try: -> 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err:

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: (0, 0)

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last) in 3 penalty = num_assets # set parameter to scale the budget penalty term 4 ----> 5 qubitOp, offset = portfolio.get_operator(mu, sigma, q, budget, penalty) 6 print (qubitOp, offset)

~\anaconda3\lib\site-packages\qiskit\finance\applications\ising\portfolio.py in get_operator(mu, sigma, q, budget, penalty) 80 zp[j_] = True 81 pauli_list.append([2 * sigma_z[i_, j_], Pauli((zp, xp))]) ---> 82 offset += sigma_z[i_, i_] 83 84 return WeightedPauliOperator(paulis=pauli_list), offset

~\anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key) 3022 if self.columns.nlevels > 1: 3023 return self._getitem_multilevel(key) -> 3024 indexer = self.columns.get_loc(key) 3025 if is_integer(indexer): 3026 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: -> 3082 raise KeyError(key) from err 3083 3084 if tolerance is not None:

KeyError: (0, 0)

Anyone know how to fix it?

  • 1
    $\begingroup$ This looks like you are using older code using Aqua, seeing WeightedPauliOperator there, which is now deprecated. What version of Qiskit are you using. The latest 0.29 Qiskit release still does have Aqua in it but also newer finance/optimization logic. Please refer to this tutorial qiskit.org/documentation/finance/tutorials/… for using the latest released code and can you see whether you have the same error if you do things with the latest Qiskit release? $\endgroup$
    – Steve Wood
    Aug 12 at 18:04
  • $\begingroup$ Thank you @SteveWood. I upgraded the version, and everything works fine with RandomDataProvider. But when I want to use Wiki or Yahoo to get real historical data I have [nan nan nan nan nan] as the mean vector. Do you know how to overcome this? $\endgroup$
    – Milica G.
    Aug 13 at 8:13
  • $\begingroup$ You might like to check that there is indeed data on what you want for the dates you are supplying. Since it seems you have things working to some degere I am not sure if this notebook will be of any help - it shows the dataproviders and their use qiskit.org/documentation/finance/tutorials/11_time_series.html $\endgroup$
    – Steve Wood
    Aug 15 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.