0
$\begingroup$

The code below doesn't work. I am looking for a workaround. OK, I can save the state vector as numpy array, recreate the whole circuit and initialize it thanks to this array. It works, but any simpler idea would be welcome!

from qiskit.quantum_info import  Statevector
q=QuantumRegister(1)
c=ClassicalRegister(1)
qc=QuantumCircuit(q,c)

sv = Statevector(qc)
print(sv)

# Destroy the qubit
qc.measure(0,0) 

# Recreate the qubit
qc.reset(0)
print(qc)
sv = Statevector(qc)
print(sv) # => Error: 'Cannot apply instruction with classical bits: measure'
$\endgroup$

1 Answer 1

0
$\begingroup$

You can use save_statevector from Qiskit Aer as follows

from qiskit_aer import AerSimulator

simulator = AerSimulator()

# Create your circuit as usual
q=QuantumRegister(1)
c=ClassicalRegister(1)
qc=QuantumCircuit(q,c)

# Add save_statevector anywhere in the circuit to save the statevector at this point
qc.save_statevector('psi1')

qc.measure(0,0) 
qc.reset(0)

# You can add save_statevector more than once
qc.save_statevector('psi2')

# Simulate the circuit
job = simulator.run(qc)
 
# Extract statevectors from the result
psi1 = job.result().data()['psi1']
print(psi1)

psi2 = job.result().data()['psi2']
print(psi2)
$\endgroup$
1
  • $\begingroup$ Thank you for this quick answer. However, this is not really what I’m looking for. In addition, after qc.save_statevector('psi1') the command sv = Statevector(qc) generates an error. $\endgroup$ Dec 5, 2023 at 20:53

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.