0
$\begingroup$

I am looking for a method to remove all operations for a given qubit in a Qiskit circuit. Here, I am looking to remove potentially many gate operations that are operating on that qubit line. Ideally I would want to remove all operations on that qubit line, including delay gates and measurement operations. I can write code to do this using string parsing in OpenQASM, but this does not work for circuits which are defined in Qiskit and not OpenQASM (for example, scheduled circuits which have delay gates), which is why I am wondering if there is a general method built into Qiskit for this operation.

My question is similar to existing questions, such as this one, but I could not find this exact question posed elsewhere.

$\endgroup$

1 Answer 1

1
$\begingroup$

One way to achieve that is by manipulating the list of instructions in the circuit directly as follows.

Let's first create a sample circuit that contains measurements and delays:

from qiskit.circuit.random import random_circuit

circ = random_circuit(4, 10)
circ.delay(1)
circ.measure_all()

enter image description here

Now, remove all instructions for one of the qubits

qubit_index = 2
qubit = circ.qbit_argument_conversion(qubit_index)[0]

for idx, _instruction in reversed(list(enumerate(circ.data))):
    for _qubit in _instruction.qubits:
        if _qubit == qubit:
            del circ.data[idx]

The result:

enter image description here

$\endgroup$
1
  • $\begingroup$ Exactly what I needed, thank you! $\endgroup$ May 11 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.