1
$\begingroup$

How to find number of Qubits ?

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

bob   = QuantumRegister(8,'b')
alice = ClassicalRegister(2,'a')
eve   = QuantumRegister(4,'e')
qc = QuantumCircuit(bob, alice, eve)

I know of syntax QuantumCircuit(2, 2) ie. 2 parameter but not sure about 3 parameter.

$\endgroup$
5
  • $\begingroup$ It's IBM qiskit and language is python $\endgroup$ Commented Aug 14, 2023 at 13:04
  • $\begingroup$ Is your question how to extract the number of qubits from an instance of QuantumCircuit? Theres a QuantumCircuit.num_qubits attribute for this. $\endgroup$
    – Callum
    Commented Aug 14, 2023 at 13:37
  • $\begingroup$ Yeah. But how to extract without using QuantumCircuit.num_qubits attribute ? $\endgroup$ Commented Aug 14, 2023 at 13:47
  • 1
    $\begingroup$ So in your code why can't you just do print(qc.num_qubits) to get the number of qubits? $\endgroup$
    – Callum
    Commented Aug 14, 2023 at 15:56
  • $\begingroup$ Because I wanted to understand the code behavior and how it works in background. $\endgroup$ Commented Aug 16, 2023 at 9:52

1 Answer 1

1
$\begingroup$

TL;DR: With QuantumCircuit.num_qubits:

>>> print(qc.num_qubits)
12

A list of them, via QuantumCircuit.qubits:

>>> print(qc.qubits)
[Qubit(QuantumRegister(8, 'b'), 0),
 Qubit(QuantumRegister(8, 'b'), 1),
 Qubit(QuantumRegister(8, 'b'), 2),
 Qubit(QuantumRegister(8, 'b'), 3),
 Qubit(QuantumRegister(8, 'b'), 4),
 Qubit(QuantumRegister(8, 'b'), 5),
 Qubit(QuantumRegister(8, 'b'), 6),
 Qubit(QuantumRegister(8, 'b'), 7),
 Qubit(QuantumRegister(4, 'e'), 0),
 Qubit(QuantumRegister(4, 'e'), 1),
 Qubit(QuantumRegister(4, 'e'), 2),
 Qubit(QuantumRegister(4, 'e'), 3)]

The QuantumCircuit constructor documentation can be found here:

regs (list(Register) or list(int) or list(list(Bit))) – The registers to be included in the circuit.

This means, that the registers bob, alice, and eve are included. Their length is 8, 2, and 4 respectably. However, alice is a classical register, which does not extend the amount of qubit. So, $8+4=12$.

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