# Access individual qubits of a quantum circuit in qiskit by qubit name

I am very new to quantum computing and only recently started learning Qiskit, so the question may sound a bit silly.

Essentially, when I create a QuantumCircuit object, I want to be able to access an individual qubit of the quantum circuit by the name of the qubit. So far, I have looked at various sources online, but none of them helped me.

More concretely, given a QuantumCircuit object, say qc, I want to do the following:

q = qc['p0'] # Assume that p0 is a qubit name


Also, suppose I have to add a quantum gate to one or more qubits in the quantum circuit. Currently, I know how to do this using the qubit indices in the quantum circuit. But can I do this using the qubit names?

Change

qc.append(HGate(), [0])


to something like

qc.append(HGate(), ['p0']) # p0 is a qubit name


Are there any methods to achieve what I want? Any help would be highly appreciated!

There might be a more elegant solution, but this is what I came up with.

1. Define the quantum registers with custom names

from qiskit.circuit import QuantumRegister
q0 = QuantumRegister(1, 'Alice')
q1 = QuantumRegister(1, 'Bob')

2. Construct the circuit

from qiskit import QuantumCircuit

qc = QuantumCircuit(q0, q1)

3. Define a custom function to get the qubits by name

def get_qubit_by_name(qc, name):
for qubit in qc.qubits:
if qubit.register.name == name:
return qubit


Then you could do something like

q2 = get_qubit_by_name(qc, 'Alice')


However, q0 and q2 are two different Qubit objects (q1 != q2). Then you can call

qc.append(HGate(), [get_qubit_by_name(qc, 'Alice')])


Hope this helps!

• Thanks, but as far as I know, the register function has been deprecated as of Qiskit version 0.17 and will be removed soon (you can see it in the API documentation here). Another way I found was using find_bit, but that seems very convoluted. Any other elegant method? Commented Dec 5, 2023 at 9:50

I found an answer to my question, which builds from what @squareroottwo suggested in their response.

Basically, given a QuantumCircuit qc, a QuantumRegister qr containing one qubit, I can use the find_bit method of QuantumCircuit in this way:

def get_qubit_index(qc: QuantumCircuit, qr: QuantumRegister):
return qc.find_bit(qr[0]).index


If the QuantumRegister contains more than one qubit, then I can specify the index of the qubit in the QuantumRegister when I call the above function instead of hard-coding index 0.

If anyone has any other elegant answers, please post them as well!