TransWikia.com

How to get state vector from QasmSimulator in Qiskit

Quantum Computing Asked on October 8, 2020

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.

One Answer

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.

Correct answer by Matthew Treinish on October 8, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP