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')
qc.save_statevector()
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
backend=Aer.get_backend('statevector_simulator')
result=execute(qc,backend).result()
out_state=result.get_statevector()
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.