2
$\begingroup$

Messed up circuit

I am new to Qiskit and was learning Quantum teleportation circuit on youtube. I unknowingly executed a cell while trying to modify the circuit draw style which added the unnecessary gates. How can I remove them?

circuit.h(1)
circuit.cnot(1, 2)
circuit.draw(output="mpl")

This was the cell which I executed again. I want to remove the gates after the second hadamard.

$\endgroup$
2
  • 2
    $\begingroup$ Just execute the cells again, starting from the first cell. And if you want to avoid this problem, make all the circuit construction code in one cell. $\endgroup$ Mar 4, 2023 at 12:22
  • $\begingroup$ @Egretta.Thula aha. That helps. Thank you. $\endgroup$
    – Vaishnav
    Mar 4, 2023 at 12:24

1 Answer 1

2
$\begingroup$

For your case, indeed, rerun the cell in the best solution.

Here, a general solution for situation when that is not possible. Say you have the following circuit and you would like to remove the first CNOT:

A circuit with X in 0, a barrier, H 1, CX 1 2, and CX 0, 1

In QuantumCircuit.data, there is a list of the instructions in the circuit. You can pretty-print-ish like this:

for index, instruction in enumerate(circuit.data):
    print(index, instruction.operation.name, [ circuit.find_bit(q).index for q in instruction.qubits])
0 x [0]
1 barrier [0, 1, 2]
2 h [1]
3 cx [1, 2]
4 cx [0, 1]

You can directly remove an instructions by removing the entry in the data structure:

del circuit.data[3]

Resulting in the following circuit:

A circuit with X in 0, a barrier, H 1, and CX 0, 1

$\endgroup$

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.