TheAlgorithms/Python · error · TypeError
number of qubits must be a integer.
Error message
number of qubits must be a integer.
What it means
Raised by quantum_fourier_transform() in quantum/q_fourier_transform.py when number_of_qubits is a str. The guard exists because string input (e.g. "3") would otherwise fail later comparisons with a confusing TypeError; only the str type is singled out here, other non-numeric types fall through to the <= comparison and raise Python's own TypeError.
Source
Thrown at quantum/q_fourier_transform.py:59
>>> quantum_fourier_transform(-1)
Traceback (most recent call last):
...
ValueError: number of qubits must be > 0.
>>> quantum_fourier_transform('a')
Traceback (most recent call last):
...
TypeError: number of qubits must be a integer.
>>> quantum_fourier_transform(100)
Traceback (most recent call last):
...
ValueError: number of qubits too large to simulate(>10).
>>> quantum_fourier_transform(0.5)
Traceback (most recent call last):
...
ValueError: number of qubits must be exact integer.
"""
if isinstance(number_of_qubits, str):
raise TypeError("number of qubits must be a integer.")
if number_of_qubits <= 0:
raise ValueError("number of qubits must be > 0.")
if math.floor(number_of_qubits) != number_of_qubits:
raise ValueError("number of qubits must be exact integer.")
if number_of_qubits > 10:
raise ValueError("number of qubits too large to simulate(>10).")
qr = QuantumRegister(number_of_qubits, "qr")
cr = ClassicalRegister(number_of_qubits, "cr")
quantum_circuit = QuantumCircuit(qr, cr)
counter = number_of_qubits
for i in range(counter):
quantum_circuit.h(number_of_qubits - i - 1)
counter -= 1
for j in range(counter):View on GitHub (pinned to f5988cc097)
Solutions
- Pass a numeric value: quantum_fourier_transform(3).
- Convert string inputs before calling: int(num_qubits_str) (with its own error handling).
- Use argparse type=int or pydantic/FastAPI int annotations to coerce at the boundary.
Example fix
# before
nq = request.args.get('qubits') # "3"
quantum_fourier_transform(nq) # TypeError
# after
nq = int(request.args.get('qubits'))
quantum_fourier_transform(nq) Defensive patterns
Strategy: type-guard
Validate before calling
if isinstance(number_of_qubits, str):
number_of_qubits = int(number_of_qubits) # let ValueError propagate on bad text
quantum_fourier_transform(number_of_qubits) Type guard
def is_numeric_qubit_count(value) -> bool:
return not isinstance(value, str) and isinstance(value, (int, float)) Try / catch
try:
qc = quantum_fourier_transform(nq)
except TypeError as e:
if "must be a integer" in str(e):
qc = quantum_fourier_transform(int(nq))
else:
raise Prevention
- Coerce string inputs (CLI, HTTP params) to int at the boundary.
- Use argparse type=int or typed schema validation for qubit counts.
- Only str is special-cased; other bad types raise Python's own comparison TypeError.
When it happens
Trigger: quantum_fourier_transform("3"), quantum_fourier_transform("ten"). Any value arriving as text from CLI args, config files, or HTTP query parameters without conversion.
Common situations: argparse without type=int; YAML/JSON config values quoted as strings; REST endpoints passing query params as strings into the function.
Related errors
- Parameter number must be int
- Parameters chain_length and number_limit must be int
- degrees must be a numeric value between 0 and 360.
- length must be a positive numeric value.
- Expected a matrix, got int/list instead
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/80387b9d4a39602c.
Report an issue: GitHub.