I am programming in IBM tools like Qiskit and ProjectQ but have not found any examples/tutorials to debug a quantum program. I am interested in knowing the qbits value in-between the circuit within simulator (might be difficult for real quantum processors).

I couldn't find any debug tag to make question more relevant.

  • $\begingroup$ You can place your circuit to IBM composer and use Inspect to run gate after gate. There is however restriction on number of qubits you can see visualizations for. $\endgroup$ Aug 12, 2022 at 5:56

1 Answer 1


When using a simulator, indeed it is possible to keep track on several things that evolves with the circuit. I find that keeping a track of the quantum statevector of the system and the unitary operator of the circuit are the most important ones. Here is an example of a simple code that tracks the statevector and unitary operator of a simple circuit:

from qiskit import QuantumCircuit
from qiskit.visualization import array_to_latex
from qiskit.quantum_info import Operator, Statevector

qc = QuantumCircuit(2)

u1 = Operator(qc)
psi1 = Statevector(qc)
qc.barrier() # |psi1>

u2 = Operator(qc)
psi2 = Statevector(qc)
qc.barrier() # |psi2>

u3 = Operator(qc)
psi3 = Statevector(qc)
qc.barrier() # |psi3>

sim = Aer.get_backend('aer_simulator')
simJob = sim.run(qc)
results = simJob.result()
data = results.data()

display(array_to_latex(u1, prefix = "U_1 = "))
display(array_to_latex(psi1, prefix = "|psi1> = "))
display(array_to_latex(u2, prefix = "U_2 = "))
display(array_to_latex(psi2, prefix = "|psi2> = "))
display(array_to_latex(u3, prefix = "U_3 = "))
display(array_to_latex(psi3, prefix = "|psi3> = "))

And the output:

enter image description here

Each dashed line in the circuit drawn is where we took the samples of the quantum statevector and unitary operator of the system. There are few methods to implement the code above, I think that using the Operator and Statvector classes is the most convenient way.

  • $\begingroup$ You might like to check this out too in respect of saving state quantumcomputing.stackexchange.com/questions/20894/… $\endgroup$
    – Steve Wood
    Aug 11, 2022 at 15:31
  • $\begingroup$ save_statevector() is indeed a decent option, but then you can't use save_unitary() in the same circuit. That's why I prefer working with the Statevector and Operator classes instead. $\endgroup$
    – Ohad
    Aug 11, 2022 at 16:18

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.