0
$\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$
1
$\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.

| improve this answer | |
$\endgroup$
  • $\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$ – Yael Ben-Haim Sep 16 at 13:25
  • $\begingroup$ This requires to import the instruction: from qiskit.providers.aer.extensions.snapshot_statevector import * $\endgroup$ – Yael Ben-Haim Sep 16 at 13:27
  • $\begingroup$ Thank you, it is what I was looking for ! $\endgroup$ – user12910 Sep 18 at 6:25

Your Answer

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

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