Remove Inactive Qubits from Qiskit Circuit

Is there any way to remove unused/inactive qubits from my Qiskit circuit? For example, I have the register $$x0\_float$$ who has an unused qubit $$x0\_{float}_0$$ (see image),

For context, my code is encoding binary numbers and in my program I rely on the qubit index (the subscript beside the register name) to figure out what gates it needs to apply later on. For some binary numbers, (like bin(0.25) = .01) I only need to apply the X gate to the second qubit in the register.

These inactive qubits are preventing me from executing the circuit on various IBMQ backends due to resource constraints despite technically having the right number of active qubits.

I've already scoured the Qiskit manual for any methods to remove qubits but I have yet to come across anything useful. I was wondering if there is a work-around method that one of you have used in your own work.

Here is a function to remove the idle wires. However, it has the drawback that you will lose the registers info:

from qiskit.converters import circuit_to_dag, dag_to_circuit
from collections import OrderedDict

def remove_idle_qwires(circ):
dag = circuit_to_dag(circ)

idle_wires = list(dag.idle_wires())
for w in idle_wires:
dag._remove_idle_wire(w)
dag.qubits.remove(w)

dag.qregs = OrderedDict()

return dag_to_circuit(dag)


Now, let's create a multi-register circuit with some idle wires and use it to check the solution:

q = QuantumRegister(2, 'q')
a = QuantumRegister(2, 'a')
circ = QuantumCircuit(q, a)
circ.h(q[0])
circ.cx(q[0], a[0])
circ.draw('mpl')


circ2 = remove_idle_qwires(circ)
circ2.draw('mpl')


• Amazing, this worked! However, I can't seem to run the filtered circuit immediately on the backend. I get this error, Unable to retrieve result for job 624106760af65d4791d943aa. Job has failed: QObj does not conform to the schema. Error code: 1101.' However, I found a workaround where I push it to a QASM file and then read it into a new circuit and run that new circuit. Any suggestions? Commented Mar 28, 2022 at 0:59
• @user19571, I didn't face this issue when I ran my toy example. Could you provide more details about your circuit? Commented Mar 28, 2022 at 8:31
• This solution works like magic. Thanks. Commented Jun 1 at 18:06

The answers given here are not ideal, simply because the qiskit developers themselves have given the answer: https://www.youtube.com/watch?v=lOTw8d9tIvc

The code is recreated here:

def count_gates(qc: QuantumCircuit):
gate_count = { qubit: 0 for qubit in qc.qubits }
for gate in qc.data:
for qubit in gate.qubits:
gate_count[qubit] += 1
return gate_count

def remove_idle_wires(qc: QuantumCircuit):
qc_out = qc.copy()
gate_count = count_gates(qc_out)
for qubit, count in gate_count.items():
if count == 0:
qc_out.qubits.remove(qubit)
return qc_out


Here is one convoluted way that I have done this. It was a one time thing I never care to find a better alternative. I modified the OPENQASM script and built the new circuit from there.

For example, if you have the following circuit:

qc = QuantumCircuit(4)
qc.h([0,1,2])
print(qc)

┌───┐
q_0: ┤ H ├
├───┤
q_1: ┤ H ├
├───┤
q_2: ┤ H ├
└───┘
q_3: ─────



then you can generate the OPENQASM code and save it into a file as

qc.qasm(filename = 'qasm_str')
qasm_str = open('qasm_str')
print(qasm_str )

OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
h q[0];
h q[1];
h q[2];


Now you can modify the qreq value depending on yourqasm_str. For this case, you can just do something like:

qasm_str = qasm_str.replace('4', '3')

Then you can recreate a new circuit using this new OPENQASM code as:

circuit = QuantumCircuit.from_qasm_str(qasm_str)
print(circuit)

┌───┐
q_0: ┤ H ├
├───┤
q_1: ┤ H ├
├───┤
q_2: ┤ H ├
└───┘


There is probably a better method though. This was just a quick dirty method for a one time thing. Furthermore, you might want to design your workflow so that you don't have to do this (deleting qubit) to start with.

Using the qBraid-SDK:

from qiskit import QuantumCircuit
from qbraid import circuit_wrapper

circuit = QiskitCircuit(...)
qprogram = circuit_wrapper(circuit)
qprogram.remove_idle_qubits()

circuit = qprogram.program


Behind the scenes, uses a similar implementation to https://quantumcomputing.stackexchange.com/a/25679/13991

Note: This method will work not just for Qiskit circuits, but also any other qBraid supported quantum program type: Qiskit, Cirq, Amazon Braket, OpenQASM 2, OpenQASM 3, PyQuil, PyTKET.