4

Hi i know the question was already asked but my case is slightly different. I am trying to do the qiskit tutorial:

from qiskit import QuantumCircuit, assemble, Aer
from qiskit.visualization import plot_histogram, plot_bloch_vector
from math import sqrt, pi

qc = QuantumCircuit(1) # We are redefining qc initial_state = [0.+1.j/sqrt(2),1/sqrt(2)+0.j] qc.initialize(initial_state, 0) qc.draw()

qc.save_statevector() result = sim.run(assemble(qc)).result() state = result.get_statevector() print("Qubit State = " + str(state))

but i get this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [29], in <cell line: 10>()
      7 qc.initialize(initial_state, 0)
      8 qc.draw()
---> 10 qc.save_statevector()
     11 result = sim.run(assemble(qc)).result()
     12 state = result.get_statevector()

AttributeError: 'QuantumCircuit' object has no attribute 'save_statevector'

I know that this is verion dependen but pip show qiskit proofs that i use version 0.36 can somebody pls tell me what else i could try? thanks

Egretta.Thula
  • 9,972
  • 1
  • 11
  • 30
user21064
  • 43
  • 4

2 Answers2

4

As with so many programming/debugging questions, the accepted answer

Just import Aer as follows:

from qiskit.providers.aer import Aer

is already stale.

The most recent version of qiskit does not appear to even include an importable qiskit.providers.aer.

An alternative solution to the problem is to make sure that the line

sim = Aer.get_backend('aer_simulator')

Is called before the line

qc.save_statevector() 

See this discussion for more details about this strange behavior.

hft
  • 878
  • 3
  • 13
2

This issue happens because save_statevector is a Qiskit Aer instruction that is added to QuantumCircuit class when Aer is imported properly (see the details here)

Just import Aer as follows:

from qiskit.providers.aer import Aer

Another solution is to import save_statevector itself:

from qiskit.providers.aer.library import save_statevector
Egretta.Thula
  • 9,972
  • 1
  • 11
  • 30