9
$\begingroup$

I'm currently able to get state vector in Qiskit using state_vector backend using something like :

backend = Aer.get_backend('statevector_simulator')
job = execute(qc, backend=backend, shots=1, memory=True)
job_result = job.result()
print(job_result.get_statevector(qc))

But now I would like to get state vector using qasm_backend, I have seen in the Qiskit doc this sample code :

backend = QasmSimulator()
backend_options = {"method": "statevector"}

# Circuit execution
job = execute(qc, backend, backend_options=backend_options)

So I tried this backend implementation then trying to get state vector with :

job_result = job.result()
print(job_result.get_statevector(qc))

But it still gives me an error saying that get_statevector is not available, what is the right way to do that ?

Thank you.

$\endgroup$

2 Answers 2

7
$\begingroup$

If you're using the QasmSimulator class from Aer it does not generate a state vector result. The statevector method you passed in as a backend option is the simulation method/technique that Aer will use for simulating the circuit, but it is still the QASM simulator which is expected to return measurement counts not a statevector. You can see a description of the different simulation methods in the QasmSimulator Docs.

When you were calling Aer.get_backend('statevector_simulator') that returns a StateVectorSimulator backend object which will simulate the circuit and return the state vector at the end of the execution and return that in the result, which is why get_statevector works there. If you want to get the state vector while using the QasmSimulator you can use the SnapshotStatevector instruction in your circuit which will return the state vector at that point in the circuit. Although, if you do this the state vector won't be returned by get_statevector (that will still fail) it will be in the snapshots under Result.data.

$\endgroup$
3
  • $\begingroup$ To insert a snapshot instruction: circ.snapshot_statevector('snapshot_label') (this will add a snapshot whose label is "snapshot_label", you can assign any string as a label name). $\endgroup$ Commented Sep 16, 2020 at 13:25
  • $\begingroup$ This requires to import the instruction: from qiskit.providers.aer.extensions.snapshot_statevector import * $\endgroup$ Commented Sep 16, 2020 at 13:27
  • $\begingroup$ Thank you, it is what I was looking for ! $\endgroup$
    – user12910
    Commented Sep 18, 2020 at 6:25
2
$\begingroup$

If you are using Qiskit v0.9 or higher, then snapshot_statevector has been superseded by the save_statevector circuit method. So you can use your initial code but need to just save the statevector at some point:

from qiskit import QuantumCircuit, execute
from qiskit.providers.aer import QasmSimulator

qc = QuantumCircuit(2,2)
qc.h(0)
qc.cx(0,1)
qc.save_statevector()

backend = QasmSimulator()
backend_options = {'method': 'statevector'}
job = execute(qc, backend, backend_options=backend_options)
job_result = job.result()
print(job_result.get_statevector(qc))

If you need to save statevector at multiple places, then the result must be read from result.data(0)[], result.get_statevector won't work here:

from qiskit import QuantumCircuit, execute
from qiskit.providers.aer import QasmSimulator

qc = QuantumCircuit(2,2)
qc.h(0)
qc.save_statevector(label='v1')
qc.cx(0,1)
qc.save_statevector(label='v2')

backend = QasmSimulator()
backend_options = {'method': 'statevector'}
job = execute(qc, backend, backend_options=backend_options)
result = job.result()
sv1 = result.data(0)['v1']
sv2 = result.data(0)['v2']
print('Statevector 1', sv1)
print('Statevector 2', sv2)

There is also a very handy set of operations in qiskit.quantum_info, that allows to get and display the statevectors:

from qiskit import QuantumCircuit, execute
from qiskit.providers.aer import QasmSimulator
import qiskit.quantum_info as qi

qc = QuantumCircuit(2,2)
qc.h(0)
qc.cx(0,1)
stv1 = qi.Statevector.from_instruction(qc)

sim = QasmSimulator()
options = {'method': 'statevector'}
execute(qc, sim, backend_options=options)

stv1.draw('latex', prefix='Statevector1:')

This way you can get a nicely formatted result:

Statevector1: \begin{bmatrix} \tfrac{1}{\sqrt{2}} & 0 & 0 & \tfrac{1}{\sqrt{2}} \\ \end{bmatrix}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.