I'm building a mini tutorial to introduce the construction of composite systems using qiskit. While doing so I am demonstrating multiple ways to construct and view the statevector, but I have come across an error which I'm hoping to circumvent.

In the initial cells I have created and displayed a simple two qubit circuit, qc, with Hadamard gates on each qubit. I then have the following two cells to show two different ways to simulate and display the resulting statevector.

svsim = Aer.get_backend('aer_simulator')
qobj = assemble(qc)
final_state = svsim.run(qobj).result().get_statevector()

#Now to display it
from qiskit.visualization import array_to_latex
array_to_latex(final_state, prefix="|\psi^{AB} \\rangle = ")

The second version is

from qiskit import execute
array_to_latex(out_state, prefix="|\psi^{AB} \\rangle = ")

Individually they both work to display an identical statevector, but running them in sequence gives the error: Failed to load qobj: Duplicate key "statevector" in save instruction.. My understanding is that even though my second one does not explicitly use save_statevector(), it still saves it as a part of the source coding. As a result I then have two statevectors saved and I need to somehow specify which one I am wanting to 'get'?

I've tried specifying an experiment in my second get_statevector() by using experiment= as a parameter, but nothing I've set it equal to has worked. Any ideas?

Note: I do know that if I simply switch the order of these it will work as the save_statevector() will update to just the one that has been saved preventing the error, but I'm hoping there might be another way to do this either by applying another save_statevector() in the second input cell, or perhaps by somehow specifying the experiment = accurately.


1 Answer 1


When you call save_statevector() method to add SaveStatevector instruction to your circuit, you can specify a label. After circuit execution, the Statevector will be saved to Result.data dictionary where the key will be that label. If you don't specify a label, statevector will be used by default.

And when you simulate your circuit using statevector_simulator, the result will be saved, similarly, to Result.data dictionary. The key this time also will be statevector.

Clearly, this will cause an issue if you add SaveStatevector instruction to your circuit then use statevector_simulator to execute it. A simple workaround is to call save_statevector() with a label:

qc.save_statevector(label = 'sv')

then retrieve the value using this label:

final_state = svsim.run(qobj).result().data()['sv']
  • $\begingroup$ This looks like exactly what I needed! Thank you! I should have a chance to try it out later today! $\endgroup$
    – PGibbon
    Sep 15, 2022 at 12:18
  • $\begingroup$ Will using the save_statevector() with labels like this then allow me to save multiple statevectors along a single circuit? $\endgroup$
    – PGibbon
    Sep 15, 2022 at 13:27
  • $\begingroup$ Yes! See this answer for some examples. $\endgroup$ Sep 15, 2022 at 19:47

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.