0
$\begingroup$

I am learning Qiskit, and I have a specific application in mind where all of the qubits in my QuantumCircit have natural labels (say, by letters of the alphabet). I can initialize this with the code

qc = QuantumCircuit(0)

for label in keys:
    qc.add_register(QuantumRegister(1,label))

This works and displays very nicely, but immediately gives me problems. For instance, something like

qc.x(qubit="label")

gives me the error

CircuitError: "Invalid bit index: 'label' of type '<class 'str'>'"

Is there any way around this? Can I choose the labels I desire for my QuantumCircut, or must I stick with the default?

$\endgroup$

1 Answer 1

3
$\begingroup$

You can use your method to initialize the qubits, but you need to remember their indices to apply gates. One way to do this is to create a dictionary mapping each label to its corresponding index and then use it when applying quantum gates to your circuit. The code would look something like this:

from qiskit import QuantumCircuit, QuantumRegister 

labels = ['qub1', 'qub2', 'qub3', 'ancilla']
index = {k: v for v, k in enumerate(labels)} # map the labels to their indices

qc = QuantumCircuit()
for label in labels:
    qc.add_register(QuantumRegister(1, label))

x_gates = ['qub1', 'qub3', 'ancilla']

for register in x_gates:
    qc.x(index[register])
    
qc.draw()

The circuit will then look like this:

enter image description here

Be careful though, because not all register names are valid according to the OpenQASM specification!

$\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.