1
$\begingroup$

I'm practising some code in Qiskit. However, I can't undo or get rid of previously applied gates.

If you take a look at the two screenshots I'm including. The more I run my code, even though I won't repeat the code, the more X gates show up.

How can I just have one even though I run the code multiple times? enter image description here

enter image description here

$\endgroup$
1
  • $\begingroup$ Just add the circuit definition (circuit = QuantumCircuit(3, 3)) to same Jupyter code cell $\endgroup$ Commented Jan 9, 2023 at 10:42

1 Answer 1

5
$\begingroup$

This is not related to Qiskit, but it's actually due to how jupyter notebooks work. In the cells you are showing, the circuit object gets updated at each execution by adding the $X$ gate to the first qubit. To do what you want, these two are the best options:

  1. move the circuit definition in the same cell
circuit = QuantumCircuit(3, 3)
circuit.x(0)
circuit.draw('mpl')
  1. clear the circuit in the same cell
circuit.clear()
circuit.x(0)
circuit.draw('mpl')
$\endgroup$
1
  • $\begingroup$ It makes sense. Thank you! $\endgroup$
    – AliceX
    Commented Jan 10, 2023 at 10:48

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.