3
$\begingroup$

I have QAOA Quantum Circuit.

   qubit_gate_idx={}
   dag = circuit_to_dag(qc)
   for qubit in dag.qubits:
     qubit_gate_idx[qubit] = 0
    
  for vertex in dag.topological_op_nodes():
    if len(vertex.qargs) == 0:
        continue  # Skip vertices with no qargs
    elif len(vertex.qargs) == 1:
        arg0 = vertex.qargs[0]

how to access the register name and index from qubits i.e. Qubit(QuantumRegister(5, 'q'), 0) in Qiskit SDK 1.0

$\endgroup$

2 Answers 2

2
$\begingroup$

To see everything that can be accessed in the Qubit class, look at the directory dir. From the directory, we see that the register and index are stored as private variables. Below is a code snippet showing how to access the desired information.

from qiskit.circuit import QuantumRegister, Qubit

qubit = Qubit(QuantumRegister(5, 'q'), 0)
print(dir(qubit))
print("Index:", qubit._index)
print("Register name:", qubit._register.name)

The output is

['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_hash', '_index', '_register', '_repr']
Index: 0
Register name: q
$\endgroup$
2
  • $\begingroup$ Thanks Sir. I have Question h to Add my own Noise like depolarising Error in Qiskit Sdk 1.0 and count it through qiskit_ibm_runtime $\endgroup$ Mar 6 at 3:16
  • $\begingroup$ Please start a new question about adding noise, since it is not the same topic as the current question. $\endgroup$ Mar 6 at 21:14
2
$\begingroup$

To get the location(s) of a qubit in the circuit you can use QuantumCircuit's find_bit method as follows:

qubit = vertex.qargs[0]

bit_locations = qc.find_bit(qubit)
print(bit_locations.registers[0][0].name)
print(bit_locations.registers[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.